Advertisement
WadeRollins2710

Turtlebot 3 joystick control

Jan 1st, 2020
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.33 KB | None | 0 0
  1. import pyglet
  2. from pyglet.gl import *
  3. import rospy
  4. from geometry_msgs.msg import Twist
  5. import sys
  6.  
  7. LINEAR_X_STEP = 0.05
  8. ANGULAR_Z_STEP = 0.1
  9.  
  10. joysticks = pyglet.input.get_joysticks()
  11. assert joysticks, 'No joystick device is connected'
  12. joystick = joysticks[0]
  13. joystick.open()
  14. window = pyglet.window.Window()
  15.  
  16. linear_x = 0.0
  17. angular_z = 0.0
  18. rospy.init_node("controller", anonymous=True)
  19. publisher = rospy.Publisher("cmd_vel", Twist, queue_size = 100)
  20. message = Twist()
  21. message.linear.x = message.linear.y = message.linear.z = 0
  22. message.angular.x = message.angular.y = message.angular.z = 0
  23.  
  24. def axes(x, y, z, angle):
  25.     glClear(GL_COLOR_BUFFER_BIT)
  26.     glColor3f(1, 0, 0)
  27.     glLoadIdentity()
  28.     glTranslatef(x, y, 0)
  29.     glScalef(1 + z, 1 + z, 1 + z)
  30.     glRotatef(-angle, 0, 0, 1)
  31.     glBegin(GL_TRIANGLES)
  32.     glVertex2f(-10, 0)
  33.     glVertex2f(0, 13)
  34.     glVertex2f(10, 0)
  35.     glEnd()
  36.  
  37. def buttons(x, y):
  38.     global linear_x, angular_z
  39.     glLoadIdentity()
  40.     x = 10
  41.     y = 10
  42.     glPointSize(5)
  43.     glBegin(GL_POINTS)
  44.     buttons = joystick.buttons
  45.  
  46.     if buttons[2]:
  47.         linear_x = 0.0
  48.         angular_z = 0.0
  49.  
  50.     if buttons  [3]:
  51.         linear_x = 0.0
  52.         angular_z = 0.0
  53.         sys.exit()
  54.        
  55.     for button in buttons:
  56.         if button:
  57.             glVertex2f(x, y)
  58.         x += 20
  59.     glEnd()
  60.  
  61. def control_robot(x, y):
  62.     global linear_x, angular_z
  63.     if y == 1.0:
  64.         linear_x += LINEAR_X_STEP
  65.     elif y == -1.0:
  66.         linear_x -= LINEAR_X_STEP
  67.     elif x == -1.0:
  68.         angular_z += ANGULAR_Z_STEP
  69.     elif x == 1.0:
  70.         angular_z -= ANGULAR_Z_STEP
  71.  
  72. def hat():
  73.     glColor3f(0, 0, 1)
  74.     x = window.width / 2
  75.     y = window.height / 2
  76.     glBegin(GL_POINTS)
  77.  
  78.     control_robot(joystick.hat_x, joystick.hat_y)
  79.  
  80.     glVertex2f(x + joystick.hat_x * 50, y + joystick.hat_y * 50)
  81.     glEnd()
  82.  
  83. @window.event
  84. def on_draw():
  85.     x = (0.8*joystick.x + 1) * window.width / 2
  86.     y = (-0.8*joystick.y + 1) * window.height / 2
  87.     z = joystick.z
  88.     angle = joystick.rz * 180
  89.  
  90.     axes(x, y, z, angle)
  91.    
  92.     buttons(x, y)
  93.  
  94.     hat()
  95.  
  96.     message.linear.x = linear_x
  97.     message.angular.z = angular_z
  98.     print("Linear: ", linear_x, "Angular: ", angular_z)
  99.     publisher.publish(message)
  100.  
  101. pyglet.clock.schedule(lambda dt: None)
  102. pyglet.app.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement