Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pyglet
- from pyglet.gl import *
- import rospy
- from geometry_msgs.msg import Twist
- import sys
- LINEAR_X_STEP = 0.05
- ANGULAR_Z_STEP = 0.1
- joysticks = pyglet.input.get_joysticks()
- assert joysticks, 'No joystick device is connected'
- joystick = joysticks[0]
- joystick.open()
- window = pyglet.window.Window()
- linear_x = 0.0
- angular_z = 0.0
- rospy.init_node("controller", anonymous=True)
- publisher = rospy.Publisher("cmd_vel", Twist, queue_size = 100)
- message = Twist()
- message.linear.x = message.linear.y = message.linear.z = 0
- message.angular.x = message.angular.y = message.angular.z = 0
- def axes(x, y, z, angle):
- glClear(GL_COLOR_BUFFER_BIT)
- glColor3f(1, 0, 0)
- glLoadIdentity()
- glTranslatef(x, y, 0)
- glScalef(1 + z, 1 + z, 1 + z)
- glRotatef(-angle, 0, 0, 1)
- glBegin(GL_TRIANGLES)
- glVertex2f(-10, 0)
- glVertex2f(0, 13)
- glVertex2f(10, 0)
- glEnd()
- def buttons(x, y):
- global linear_x, angular_z
- glLoadIdentity()
- x = 10
- y = 10
- glPointSize(5)
- glBegin(GL_POINTS)
- buttons = joystick.buttons
- if buttons[2]:
- linear_x = 0.0
- angular_z = 0.0
- if buttons [3]:
- linear_x = 0.0
- angular_z = 0.0
- sys.exit()
- for button in buttons:
- if button:
- glVertex2f(x, y)
- x += 20
- glEnd()
- def control_robot(x, y):
- global linear_x, angular_z
- if y == 1.0:
- linear_x += LINEAR_X_STEP
- elif y == -1.0:
- linear_x -= LINEAR_X_STEP
- elif x == -1.0:
- angular_z += ANGULAR_Z_STEP
- elif x == 1.0:
- angular_z -= ANGULAR_Z_STEP
- def hat():
- glColor3f(0, 0, 1)
- x = window.width / 2
- y = window.height / 2
- glBegin(GL_POINTS)
- control_robot(joystick.hat_x, joystick.hat_y)
- glVertex2f(x + joystick.hat_x * 50, y + joystick.hat_y * 50)
- glEnd()
- @window.event
- def on_draw():
- x = (0.8*joystick.x + 1) * window.width / 2
- y = (-0.8*joystick.y + 1) * window.height / 2
- z = joystick.z
- angle = joystick.rz * 180
- axes(x, y, z, angle)
- buttons(x, y)
- hat()
- message.linear.x = linear_x
- message.angular.z = angular_z
- print("Linear: ", linear_x, "Angular: ", angular_z)
- publisher.publish(message)
- pyglet.clock.schedule(lambda dt: None)
- pyglet.app.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement