Go to the documentation of this file.00001
00002
00003 import rospy, oculusprimesocket, math, re, thread, time
00004 from geometry_msgs.msg import Twist
00005
00006 lastlinear = 0
00007 lastangular = 0
00008 lastmove = 0
00009 smoothingdelay = 0.3
00010 twistid = 0
00011
00012
00013 def twistCallback(data):
00014 global twistid
00015
00016 linear = data.linear.x
00017 angular = data.angular.z
00018
00019 twistid = rospy.Time.now()
00020 thread.start_new_thread(waitifnecessary, (linear, angular, twistid) )
00021
00022
00023
00024 def waitifnecessary(linear, angular, threadid):
00025
00026 if linear == 0 and angular == 0:
00027 move(linear, angular)
00028 return
00029
00030 while twistid == threadid and smoothingdelay - (rospy.Time.now()-lastmove).to_sec() > 0:
00031 rospy.sleep(0.01)
00032
00033 if twistid == threadid:
00034 move(linear, angular)
00035
00036
00037 def move(linear, angular):
00038 global lastlinear, lastangular, lastmove
00039
00040 lastmove = rospy.Time.now()
00041
00042
00043 if linear == lastlinear and angular == lastangular and linear == 0 and angular == 0:
00044 return
00045
00046 lastlinear = linear
00047 lastangular = angular
00048
00049 cmd = None
00050
00051 d = "0.3"
00052 a = "25"
00053 arcmult = 3
00054
00055 if linear == 0 and angular == 0:
00056 cmd = "move stop"
00057
00058 elif linear > 0 and angular == 0:
00059 cmd = "forward "+d
00060
00061 elif linear < 0 and angular == 0:
00062 cmd = "backward "+d
00063
00064 elif linear == 0 and angular > 0:
00065 cmd = "left "+a
00066
00067 elif linear == 0 and angular < 0:
00068 cmd = "right "+a
00069
00070 elif linear > 0 and not angular == 0:
00071 angle = str(int(math.degrees(angular))/arcmult)
00072 cmd = "arcmove "+d+" "+angle
00073
00074 elif linear < 0 and not angular == 0:
00075 angle = str(int(math.degrees(angular))/arcmult)
00076 cmd = "arcmove -"+d+" "+angle
00077
00078 if not cmd == None:
00079 oculusprimesocket.sendString(cmd)
00080
00081
00082
00083 def cleanup():
00084 oculusprimesocket.sendString("odometrystop")
00085 oculusprimesocket.sendString("log cmd_vel_listener.py disconnecting")
00086
00087
00088
00089
00090
00091 rospy.init_node('cmd_vel_listener', anonymous=False)
00092 rospy.on_shutdown(cleanup)
00093 lastmove = rospy.Time.now()
00094
00095
00096 oculusprimesocket.connect()
00097 oculusprimesocket.sendString("log cmd_vel_listener.py connected")
00098 oculusprimesocket.sendString("odometrystart")
00099
00100
00101 rospy.Subscriber("cmd_vel", Twist, twistCallback)
00102
00103 rospy.spin()