对于移动机器人,键盘的控制往往满足不了我们的需求,以前看好多电影里边都是用一个摇杆来控制机器人的,简直帅爆了,正好我这里有一个操作杆,那就来尝试感受一下。
操作杆(joystick)控制会更加有操作感,ROS中的很多机器人也带有操作杆的相关代码,只需要简单的移植即可。我们使用的是赛钛客(saitek)的一款操作杆,如下图所示:
使用的移植代码是clearpath_husky机器人中的python代码。
参考链接: http://www.ros.org/wiki/joy
ls /dev/input/
sudo jstest /dev/input/js0
rosrun joy joy_node
rostopic echo joy
import roslib; roslib.load_manifest('smartcar_teleop') import rospy from sensor_msgs.msg import Joy from geometry_msgs.msg import Twist from std_msgs.msg import String class Teleop: def __init__(self): rospy.init_node('smartcar_teleop_joy') self.turn_scale = rospy.get_param('~turn_scale') self.drive_scale = rospy.get_param('~drive_scale') self.deadman_button = rospy.get_param('~deadman_button', 0) self.cmd = None cmd_pub = rospy.Publisher('cmd_vel', Twist) announce_pub = rospy.Publisher('/smartcar/announce/teleops', String, latch=True) announce_pub.publish(rospy.get_namespace()); rospy.Subscriber("joy", Joy, self.callback) rate = rospy.Rate(rospy.get_param('~hz', 20)) while not rospy.is_shutdown(): rate.sleep() if self.cmd: cmd_pub.publish(self.cmd) def callback(self, data): """ Receive joystick data, formulate Twist message. """ cmd = Twist() cmd.linear.x = data.axes[1] * self.drive_scale cmd.angular.z = data.axes[0] * self.turn_scale if data.buttons[self.deadman_button] == 1: self.cmd = cmd else: self.cmd = None if __name__ == "__main__": Teleop()
<launch> <arg name="drive_speed" default="1.0" /> <arg name="turn_speed" default="1.0" /> <arg name="joy_dev" default="/dev/input/js0" /> <arg name="cmd_topic" default="cmd_vel" /> <node pkg="joy" type="joy_node" name="joy_node"> <param name="dev" value="$(arg joy_dev)" /> <param name="deadzone" value="0.3" /> </node> <node pkg="smartcar_teleop" type="teleop_joy.py" name="smartcar_teleop"> <param name="turn_scale" value="$(arg turn_speed)" /> <param name="drive_scale" value="$(arg drive_speed)" /> <remap from="cmd_vel" to="$(arg cmd_topic)" /> </node> </launch>
roslaunch smartcar_display.rviz.launch roslaunch smartcar_teleop teleop_joy.launch
rostopic echo joy
----------------------------------------------------------------
欢迎大家转载我的文章。
转载请注明:转自古-月
http://blog.csdn.net/hcx25909
欢迎继续关注我的博客