;
学习任务2:键盘控制
1.代码讲解
2.实际操作
3.视频演示;
1代码讲解
?键盘控制程序使用的是raceworld1场景,由raceworld1.launch文件启动。找到/root/aliyun_demo/src/raceworld/launch文件夹中的raceworld1.launch文件并打开。
?在launch文件内加载了多个控制器,其中left_rear_wheel_velocity_controller,right_rear_wheel_velocity_controller,left_front_wheel_velocity_controller,right_front_wheel_velocity_controller,left_steering_hinge_position_controller和right_steering_hinge_position_controller接收ROS消息,分别控制了左右后轮速度,左右前轮速度和转向角度。
nodename=controller_managerpkg=controller_managertype=spawner;
?在加载控制器代码的下面,调用了一个名为servo_comannds1.py的代码文件。
nodepkg=raceworldtype=servo_commands1.pyname=servo_commandsoutput=screen
?打开/root/aliyun_demo/src/raceworld/scripts文件夹中的servo_comannds1.py文件我们可以看到,代码中新建了一个名为servo_comannds的节点,订阅并接收/deepracer1/ackermann_cmd_mux/output消息。
defservo_commands():
globalflag_move
rospy.init_node(servo_commands,anonymous=True)
rospy.Subscriber(/deepracer1/ackermann_cmd_mux/output,AckermannDriveStamped,set_thro
ttle_steer)
rospy.spin();
?在接收到AckermannDriveStamped类型消息后经过适当的转换,将速度和转向信息发布给之前所述六个控制器。
defset_throttle_steer(data):
globalflag_move
pub_vel_left_rear_wheel=rospy.Publisher(/deepracer1/left_rear_wheel_velocity_controller/command,Float64,queue_size=1)
pub_vel_right_rear_wheel=rospy.Publisher(/deepracer1/right_rear_wheel_velocity_controller/command,Float64,queue_size=1)
pub_vel_left_front_wheel=rospy.Publisher(/deepracer1/left_front_wheel_velocity_controller/command,Float64,queue_size=1)
pub_vel_right_front_wheel=rospy.Publisher(/deepracer1/right_front_wheel_velocity_controller/command,Float64,queue_size=1)
pub_pos_left_steering_hinge=rospy.Publisher(/deepracer1/left_steering_hinge_position_controller/command,Float64,queue_size=1)
pub_pos_right_steering_hinge=rospy.Publisher(/deepracer1/right_steering_hinge_position_controller/command,Float64,queue_size=1)
throttle=data.drive.speed*28
print(Throttle:,throttle)
steer=data.drive.steering_angle
print(Steer:,steer)
pub_vel