discrete_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 discrete velocity commands ##
8 ## (by default, the robot moves for 1 second only) ##
9 #######################################################
10 
11 # configuration :
12 MAX_SPEED = 0.3
13 MAX_ROTATIONAL_SPEED = 0.3
14 MOVE_TIME = 1
15 
16 #######################################################
17 ## DO NOT CHANGE ANYTHING AFTER THIS LINE! ##
18 #######################################################
19 
20 import roslib
21 import rospy
22 import time
23 import tty, termios, sys
24 from geometry_msgs.msg import Twist
25 rospy.init_node('discrete_keyboard_teleop')
26 roslib.load_manifest('pioneer_teleop')
27 
28 # define constants :
29 KEY_UP = 65
30 KEY_DOWN = 66
31 KEY_RIGHT = 67
32 KEY_LEFT = 68
33 KEY_PLUS = 43
34 KEY_MINUS = 45
35 KEY_Q = 81
36 KEY_Q2 = 113
37 
38 # define variables :
39 speed = 0.1
40 rotationalSpeed = 0.0
41 keyPress = 0
42 linearDirection = 0
43 rotationalDirection = 0
44 
45 #init :
46 pub = rospy.Publisher('cmd_vel', Twist, queue_size=10)
47 twist = Twist()
48 
49 while(keyPress != KEY_Q) and (keyPress != KEY_Q2):
50 
51  print " - Wainting for key press \n"
52  print " > Arrows = move robot \n"
53  print " > +/- = change speed \n"
54  print " > q = quit \n"
55 
56  # get char :
57  keyPress = " "
58  fd = sys.stdin.fileno()
59  old_settings = termios.tcgetattr(fd)
60  try:
61  tty.setraw(sys.stdin.fileno())
62  ch = sys.stdin.read(1)
63  finally:
64  termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
65  keyPress = ord(ch)
66 
67  # test command :
68  drive = 0
69  if (keyPress == KEY_UP):
70  linearDirection = 1
71  drive = 1
72  print " >> Move forward at speed " + str(speed) + " for " + str(MOVE_TIME) + " sec\n"
73  elif (keyPress == KEY_DOWN):
74  linearDirection = -1
75  drive = 1
76  print " >> Move backward at speed " + str(speed) + " for " + str(MOVE_TIME) + " sec\n"
77  elif (keyPress == KEY_LEFT):
78  rotationalDirection = 1
79  drive = 1
80  print " >> Turn left at speed " + str(speed) + " for " + str(MOVE_TIME) + " sec\n"
81  elif (keyPress == KEY_RIGHT):
82  rotationalDirection = -1
83  drive = 1
84  print " >> Turn Right at speed " + str(speed) + " for " + str(MOVE_TIME) + " sec\n"
85  elif (keyPress == KEY_PLUS) and (speed < MAX_SPEED):
86  speed += 0.1
87  print " >> Set speed to " + str(speed) + "\n"
88  elif (keyPress == KEY_PLUS):
89  print " >> Maximum speed value already reached : " + str(MAX_SPEED)
90  elif (keyPress == KEY_MINUS) and (speed > 0.1):
91  speed -= 0.1
92  print " >> Set speed to " + str(speed) + "\n"
93  elif (keyPress == KEY_MINUS):
94  print " >> Minimum speed value already reached : 0.1"
95 
96  if (drive):
97  if (speed > MAX_ROTATIONAL_SPEED):
98  rotationalSpeed = MAX_ROTATIONAL_SPEED
99  else:
100  rotationalSpeed = speed
101 
102  # send command :
103  twist.linear.x = linearDirection * speed
104  twist.angular.z = rotationalDirection * rotationalSpeed
105  pub.publish(twist)
106  t1 = time.time()
107  t2 = t1
108  while(t2-t1 < MOVE_TIME):
109  pub.publish(twist)
110  t2 = time.time()
111 
112  twist.linear.x = 0
113  twist.angular.z = 0
114  pub.publish(twist)
115 
116  # reinit :
117  rotationalSpeed = 0.0
118  linearDirection = 0
119  rotationalDirection = 0
120 
121 print " >> Quit"
122 exit()


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