test_joy.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 import roslib
4 import rospy
5 
6 import pygame
7 import pygame.joystick
8 
9 import sys
10 
11 from sensor_msgs.msg import *
12 
13 def main():
14  node_name = "simple_joy"
15  if len(sys.argv) > 1:
16  node_name = sys.argv[1]
17 
18  rospy.init_node(node_name)
19  pub = rospy.Publisher("/" + node_name + "/joy", Joy, latch=True)
20 
21  pygame.init()
22  pygame.joystick.init()
23  devices = pygame.joystick.get_count()
24  if devices < 1:
25  rospy.logerr("No Joystick devices detected")
26  exit(-1)
27  rospy.loginfo("Found %d Joystick devices" % devices)
28 
29  joy_name = None
30  input_dev = 0
31  if len(sys.argv) > 2:
32  try:
33  input_dev = int(sys.argv[2])
34  except ValueError:
35  joy_name = sys.argv[2]
36  else:
37  rospy.loginfo("no input device supplied. will try to use default device.")
38  input_dev = 0
39  rospy.loginfo("Using input device %d" % input_dev)
40 
41  controller = None
42  if joy_name == None:
43  controller = pygame.joystick.Joystick(input_dev)
44  else:
45  for i in range(pygame.joystick.get_count()):
46  if joy_name in pygame.joystick.Joystick(i).get_name():
47  controller = pygame.joystick.Joystick(i)
48 
49  if controller == None:
50  rospy.logerr("No Joystick controller generated")
51  exit(-1)
52 
53  controller.init()
54  axes = controller.get_numaxes()
55  buttons = controller.get_numbuttons()
56  hats = controller.get_numhats()
57 
58  rospy.loginfo("Opened it")
59 
60  m = Joy()
61  m.axes = [ 0 ] * axes
62  m.buttons = [ 0 ] * buttons
63 
64  p = False
65  done = False
66 
67  while not rospy.is_shutdown() and done==False:
68  m.header.stamp = rospy.Time.now()
69 
70  for event in pygame.event.get():
71  if event.type == pygame.QUIT:
72  done=True
73  if event.type == pygame.JOYBUTTONDOWN:
74  done=False
75  if event.type == pygame.JOYBUTTONUP:
76  done=False
77 
78  for i in range( axes ):
79  axis = controller.get_axis( i )
80  m.axes[i] = axis
81 
82  for i in range( buttons ):
83  button = controller.get_button( i )
84  m.buttons[i] = button
85 
86  for i in range( hats ):
87  hat = controller.get_hat( i )
88 
89  pub.publish(m)
90  rospy.sleep(0.01) # 100hz max
91 
92 if __name__ == '__main__':
93  try:
94  main()
95  except rospy.ROSInterruptException:
96  pass
def main()
Definition: test_joy.py:13


jsk_teleop_joy
Author(s): Ryohei Ueda
autogenerated on Fri May 14 2021 02:52:11