Go to the documentation of this file.00001
00002 __author__ = 'tom1231'
00003 import pygame
00004 from pygame.locals import *
00005 import rospy
00006 import sys
00007 from threading import Thread, RLock
00008 from geometry_msgs.msg import Twist
00009
00010 LEFT_RIGHT_NUM = int(sys.argv[6])
00011 UP_DOWN_NUM = int(sys.argv[7])
00012
00013 BOOST_SPEED_BUTTON = int(sys.argv[8])
00014 ENABLE_BUTTON = int(sys.argv[9])
00015 JOY_NUM = int(sys.argv[10])
00016
00017 REV_UP_DOWN = int(sys.argv[11])
00018 REV_LEFT_RIGHT = int(sys.argv[12])
00019
00020 class Program:
00021 def __init__(self):
00022
00023 self._axes = [0.0, 0.0]
00024 self._enable = False
00025 self._maxSpeedLinear = float(sys.argv[1])
00026 self._maxSpeedAngular = float(sys.argv[5])
00027 self._scale = float(sys.argv[2])
00028 self._factor = 1
00029 self._lock = RLock()
00030
00031 rospy.init_node('ric_joyTeleop')
00032 self._pub = rospy.Publisher('%s' % sys.argv[3], Twist, queue_size=1)
00033
00034 rospy.loginfo("listen to %s" % self._pub.name)
00035
00036 Thread(target=self.listenToJoystick, args=()).start()
00037
00038 pubHz = int(sys.argv[4])
00039
00040 rate = rospy.Rate(pubHz)
00041 try:
00042 while not rospy.is_shutdown():
00043 if self.isEnable():
00044 msg = Twist()
00045
00046 msg.linear.x = self.getUpAndDownAxes() * REV_UP_DOWN
00047 msg.angular.z = self.getLeftAndRightAxes() * REV_LEFT_RIGHT
00048
00049 self._pub.publish(msg)
00050 rate.sleep()
00051
00052 except KeyboardInterrupt: pass
00053 finally:
00054 pygame.event.post(pygame.event.Event(QUIT))
00055
00056 def setLeftAndRightAxes(self, newAxis):
00057 with self._lock:
00058 self._axes[0] = newAxis
00059
00060 def setUpAndDownAxes(self, newAxis):
00061 with self._lock:
00062 self._axes[1] = newAxis
00063
00064 def getLeftAndRightAxes(self):
00065 with self._lock:
00066 return self._axes[LEFT_RIGHT_NUM] * self.getMaxSpeedAngular() * self.getFactor()
00067
00068 def getUpAndDownAxes(self):
00069 with self._lock:
00070 return self._axes[UP_DOWN_NUM] * self.getMaxSpeedLinear() * self.getFactor()
00071
00072 def setEnable(self, newVal):
00073 with self._lock:
00074 self._enable = newVal
00075
00076 def isEnable(self):
00077 with self._lock:
00078 return self._enable
00079
00080 def setFactor(self, val):
00081 with self._lock:
00082 self._factor = val
00083
00084 def getFactor(self):
00085 with self._lock:
00086 return self._factor
00087
00088 def getMaxSpeedLinear(self):
00089 with self._lock:
00090 return self._maxSpeedLinear
00091
00092 def getMaxSpeedAngular(self):
00093 with self._lock:
00094 return self._maxSpeedAngular
00095
00096 def listenToJoystick(self):
00097 pygame.init()
00098 pygame.joystick.init()
00099 try:
00100 joystick = pygame.joystick.Joystick(JOY_NUM)
00101 joystick.init()
00102 quitLoop = False
00103
00104 while not quitLoop:
00105 event = pygame.event.wait()
00106 if event.type == QUIT:
00107 quitLoop = True
00108
00109 elif event.type == JOYAXISMOTION:
00110 if self.isEnable():
00111
00112 data = event.dict
00113 if data['axis'] == LEFT_RIGHT_NUM:
00114 self.setLeftAndRightAxes(data['value'])
00115 elif data['axis'] == UP_DOWN_NUM:
00116 self.setUpAndDownAxes(data['value'])
00117
00118 elif event.type == JOYBUTTONDOWN:
00119 if event.dict['button'] == BOOST_SPEED_BUTTON:
00120 self.setFactor(self._scale)
00121 elif event.dict['button'] == ENABLE_BUTTON:
00122 self.setEnable(True)
00123
00124 elif event.type == JOYBUTTONUP:
00125 if event.dict['button'] == BOOST_SPEED_BUTTON:
00126 self.setFactor(1.0)
00127 elif event.dict['button'] == ENABLE_BUTTON:
00128 self.setEnable(False)
00129 self.setLeftAndRightAxes(0.0)
00130 self.setUpAndDownAxes(0.0)
00131
00132 msg = Twist()
00133
00134 msg.linear.x = self.getUpAndDownAxes() * REV_UP_DOWN
00135 msg.angular.z = self.getLeftAndRightAxes() * REV_LEFT_RIGHT
00136
00137 self._pub.publish(msg)
00138 except:
00139 rospy.logerr("Joystick [%d] not found" % JOY_NUM)
00140
00141
00142 if __name__ == '__main__':
00143 Program()