[text] key2

Viewer

  1. #!/usr/bin/env python3
  2. import rospy
  3. from std_msgs.msg import String
  4. from geometry_msgs.msg import Twist
  5. key_mapping = { 'w': [ 0, 1], 'x': [0, -1],
  6.                 'a': [-1, 0], 'd': [1, 0],
  7.                 's': [ 0, 0] }
  8. def keys_cb(msg, twist_pub):
  9.     if len(msg.data) == 0 or not msg.data[0] in key_mapping:
  10.         return # unknown key
  11.     vels = key_mapping[msg.data[0]]
  12.     t = Twist()
  13.     t.angular.z = vels[0]
  14.     t.linear.x = vels[1]
  15.     twist_pub.publish(t)
  16. if __name__ == '__main__':
  17.     rospy.init_node('keys_to_twist')
  18.     twist_pub = rospy.Publisher('cmd_vel', Twist, queue_size=1)
  19.     rospy.Subscriber('keys', String, keys_cb, twist_pub)
  20.     rospy.spin()

Editor

You can edit this paste and save as new:


File Description
  • key2
  • Paste Code
  • 17 Apr-2024
  • 689 Bytes
You can Share it: