00001
00002
00003
00004
00005 import roslib; roslib.load_manifest('mrl_robots_teleop')
00006 import rospy
00007
00008 from geometry_msgs.msg import Twist
00009
00010 import sys, select, termios, tty
00011
00012 msg = """
00013 Reading from the keyboard and Publishing to Twist!
00014 ---------------------------
00015 Moving around:
00016
00017 U I O
00018 J 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 'i':(1,0),
00031 'o':(1,-1),
00032 'j':(0,1),
00033 'l':(0,-1),
00034 'u':(1,1),
00035 ',':(-1,0),
00036 '.':(-1,1),
00037 'm':(-1,-1),
00038 }
00039
00040 speedBindings={
00041 'q':(1.1, 1.1),
00042 'z':(0.9, 0.9),
00043 'w':(1.1, 1),
00044 'x':(0.9, 1),
00045 'e':(1, 1.1),
00046 'c':(1, 0.9),
00047 }
00048
00049 def getKey():
00050 tty.setraw(sys.stdin.fileno())
00051 select.select([sys.stdin], [], [], 0)
00052 key = sys.stdin.read(1)
00053 termios.tcsetattr(sys.stdin, termios.TCSADRAIN, settings)
00054 return key
00055
00056 speed = .25
00057 turn = 1
00058
00059 def vels(speed,turn):
00060 return "currently:\tspeed %s\tturn %s " % (speed,turn)
00061
00062 if __name__=="__main__":
00063 settings = termios.tcgetattr(sys.stdin)
00064
00065 pub = rospy.Publisher('cmd_vel', Twist)
00066 rospy.init_node('keyboard_teleop')
00067
00068 x = 0
00069 th = 0
00070 status = 0
00071
00072 try:
00073 print msg
00074 print vels(speed,turn)
00075 while(1):
00076 key = getKey()
00077 if key in moveBindings.keys():
00078 x = moveBindings[key][0]
00079 th = moveBindings[key][1]
00080 elif key in speedBindings.keys():
00081 speed = speed * speedBindings[key][0]
00082 turn = turn * speedBindings[key][1]
00083
00084 print vels(speed,turn)
00085 if (status == 14):
00086 print msg
00087 status = (status + 1) % 15
00088 else:
00089 x = 0
00090 th = 0
00091 if (key == '\x03'):
00092 break
00093
00094 twist = Twist()
00095 twist.linear.x = x*speed; twist.linear.y = 0; twist.linear.z = 0
00096 twist.angular.x = 0; twist.angular.y = 0; twist.angular.z = th*turn
00097 pub.publish(twist)
00098
00099 except:
00100 print e
00101
00102 finally:
00103 twist = Twist()
00104 twist.linear.x = 0; twist.linear.y = 0; twist.linear.z = 0
00105 twist.angular.x = 0; twist.angular.y = 0; twist.angular.z = 0
00106 pub.publish(twist)
00107
00108 termios.tcsetattr(sys.stdin, termios.TCSADRAIN, settings)
00109
00110