keyboard_teleop.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 #######################################################
3 ## bendahmane.amine@gmail.com ##
4 ## last update : Jan 31st, 2016 ##
5 #######################################################
6 ## Description: This program transforms the inputs ##
7 ## from the keyboard to velocity commands. ##
8 #######################################################
9 
10 # configuration :
11 MAX_SPEED = 0.4
12 MAX_ROTATIONAL_SPEED = 0.3
13 
14 #######################################################
15 ## DO NOT CHANGE ANYTHING AFTER THIS LINE! ##
16 #######################################################
17 
18 
19 import roslib
20 import rospy
21 import time
22 import tty, termios, sys
23 from geometry_msgs.msg import Twist
24 rospy.init_node('keyboard_teleop')
25 roslib.load_manifest('pioneer_teleop')
26 
27 # define constants :
28 KEY_UP = 65
29 KEY_DOWN = 66
30 KEY_RIGHT = 67
31 KEY_LEFT = 68
32 KEY_Q = 81
33 KEY_Q2 = 113
34 KEY_S = 83
35 KEY_S2 = 115
36 MOVE_TIME = 0.01
37 
38 # define variables :
39 speed = 0.0
40 rotationalSpeed = 0.0
41 keyPress = 0
42 linearDirection = 0
43 rotationalDirection = 0
44 linearSpeed = 0
45 rotationalSpeed = 0
46 
47 # init :
48 pub = rospy.Publisher('cmd_vel', Twist, queue_size=10)
49 twist = Twist()
50 
51 while(keyPress != KEY_Q) and (keyPress != KEY_Q2):
52 
53  print " - Wainting for key press \n"
54  print " > Arrows = move robot \n"
55  print " > s = stop \n"
56  print " > q = quit \n"
57 
58  # get char :
59  keyPress = " "
60  fd = sys.stdin.fileno()
61  old_settings = termios.tcgetattr(fd)
62  try:
63  tty.setraw(sys.stdin.fileno())
64  ch = sys.stdin.read(1)
65  finally:
66  termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
67  keyPress = ord(ch)
68 
69  # test command :
70  drive = 0
71  if (keyPress == KEY_UP):
72  linearDirection = 1
73  print " >> Inscrement linear speed\n"
74  elif (keyPress == KEY_DOWN):
75  linearDirection = -1
76  print " >> Decrement linear speed\n"
77  elif (keyPress == KEY_LEFT):
78  rotationalDirection = 1
79  print " >> Inscrement rotational speed\n"
80  elif (keyPress == KEY_RIGHT):
81  rotationalDirection = -1
82  print " >> Decrement rotational speed\n"
83  elif (keyPress == KEY_S) or (keyPress == KEY_S2):
84  linearSpeed = 0.0
85  rotationalSpeed = 0.0
86  linearDirection = 0.0
87  rotationalDirection = 0.0
88  print " >> Stop robot\n"
89 
90  newLinearSpeed = linearSpeed + linearDirection * 0.1
91  newRotationalSpeed = rotationalSpeed + rotationalDirection * 0.1
92 
93  if (newLinearSpeed > MAX_SPEED) or (newLinearSpeed < MAX_SPEED*(-1)):
94  print " Warning ! Maximum linar speed already reached\n"
95  else:
96  linearSpeed = newLinearSpeed
97 
98  if (newRotationalSpeed > MAX_SPEED) or (newRotationalSpeed < MAX_SPEED*(-1)):
99  print " Warning ! Maximum rotational speed already reached\n"
100  else:
101  rotationalSpeed = newRotationalSpeed
102 
103  print " Speed = (linar: "+ str(linearSpeed) +", rotational: "+ str(rotationalSpeed) +")\n"
104 
105  # send command :
106  twist = Twist()
107  twist.linear.x = linearSpeed
108  twist.angular.z = rotationalSpeed
109  t1 = time.time()
110  t2 = t1
111  while(t2-t1 < MOVE_TIME):
112  pub.publish(twist)
113  t2 = time.time()
114 
115  # reinit :
116  linearDirection = 0
117  rotationalDirection = 0
118 
119 twist.linear.x = 0
120 twist.angular.z = 0
121 pub.publish(twist)
122 print " >> Quit"
123 exit()


pioneer_teleop
Author(s): Amine BENDAHMANE
autogenerated on Mon Jun 10 2019 14:16:37