youbot_keyboard_teleop.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # Initial code created by Graylin Trevor Jay (tjay@cs.brown.edu) an published under Crative Commens Attribution license.
00003 # addition for signal interrupt by Koen Buys
00004 
00005 #import youbot_driver_ros_interface
00006 #import roslib; roslib.load_manifest('youbot_oodl')
00007 import rospy
00008 
00009 from geometry_msgs.msg import Twist
00010 
00011 import sys, select, termios, tty, signal
00012 
00013 msg = """
00014 Reading from the keyboard  and Publishing to Twist!
00015 ---------------------------
00016 Moving around:
00017    u    i    o
00018    j    k    l
00019    m    ,    .
00020 
00021 q/z : increase/decrease max speeds by 10%
00022 w/x : increase/decrease only linear speed by 10%
00023 e/c : increase/decrease only angular speed by 10%
00024 anything else : stop
00025 
00026 CTRL-C to quit
00027 """
00028 
00029 moveBindings = {
00030 #                    x,y,tetha ratio
00031                 'i':(1,0,0),    # forwards
00032                 'o':(1,0,-1),   # forwards + rotation right
00033                 'j':(0,1,0),    # left
00034                 'l':(0,-1,0),   # right
00035                 'u':(1,0,1),    # forwards + rotation left
00036                 ',':(-1,0,0),   # backward
00037                 '.':(0,0,-1),   # turn right on spot
00038                 'm':(0,0,1),    # turn left on spot
00039                }
00040 
00041 speedBindings={
00042                 'q':(1.1,1.1),
00043                 'z':(.9,.9),
00044                 'w':(1.1,1),
00045                 'x':(.9,1),
00046                 'e':(1,1.1),
00047                 'c':(1,.9),
00048               }
00049 
00050 class TimeoutException(Exception): 
00051     pass 
00052 
00053 def getKey():
00054     def timeout_handler(signum, frame):
00055         raise TimeoutException()
00056     
00057     old_handler = signal.signal(signal.SIGALRM, timeout_handler)
00058     signal.alarm(1) #this is the watchdog timing
00059     tty.setraw(sys.stdin.fileno())
00060     select.select([sys.stdin], [], [], 0)
00061     try:
00062        key = sys.stdin.read(1)
00063        #print "Read key"
00064     except TimeoutException:
00065        #print "Timeout"
00066        return "-"
00067     finally:
00068        signal.signal(signal.SIGALRM, old_handler)
00069 
00070     signal.alarm(0)
00071     termios.tcsetattr(sys.stdin, termios.TCSADRAIN, settings)
00072     return key
00073 
00074 speed = 0.1
00075 turn = 0.1
00076 
00077 def vels(speed,turn):
00078         return "currently:\tspeed %s\tturn %s " % (speed,turn)
00079 
00080 if __name__=="__main__":
00081         settings = termios.tcgetattr(sys.stdin)
00082         
00083         pub = rospy.Publisher('cmd_vel', Twist)
00084         rospy.init_node('teleop_twist_keyboard')
00085 
00086         x = 0
00087         y = 0
00088         th = 0
00089         status = 0
00090 
00091         try:
00092                 print msg
00093                 print vels(speed,turn)
00094                 while(1):
00095                         key = getKey()
00096                         if key in moveBindings.keys():
00097                                 x = moveBindings[key][0]
00098                                 y = moveBindings[key][1]
00099                                 th = moveBindings[key][2]
00100                         elif key in speedBindings.keys():
00101                                 speed = speed * speedBindings[key][0]
00102                                 turn = turn * speedBindings[key][1]
00103 
00104                                 print vels(speed,turn)
00105                                 if (status == 14):
00106                                         print msg
00107                                 status = (status + 1) % 15
00108                         else:
00109                                 x = 0
00110                                 y = 0
00111                                 th = 0
00112                                 if (key == '\x03'):
00113                                         break
00114 
00115                         twist = Twist()
00116                         twist.linear.x = x*speed 
00117                         twist.linear.y = y*speed 
00118                         twist.linear.z = 0
00119 
00120                         twist.angular.x = 0 
00121                         twist.angular.y = 0
00122                         twist.angular.z = th*turn
00123                         pub.publish(twist)
00124 
00125         except:
00126                 print e
00127 
00128         finally:
00129                 twist = Twist()
00130                 twist.linear.x = 0; twist.linear.y = 0; twist.linear.z = 0
00131                 twist.angular.x = 0; twist.angular.y = 0; twist.angular.z = 0
00132                 pub.publish(twist)
00133 
00134                 termios.tcsetattr(sys.stdin, termios.TCSADRAIN, settings)
00135 
00136 


youbot_driver_ros_interface
Author(s): Sebastian Blumenthal
autogenerated on Thu Jun 6 2019 20:43:35