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


youbot_teleop
Author(s): Russell Toris , Graylin Trevor Jay
autogenerated on Mon Oct 6 2014 09:08:50