commandline_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 take transforms simple ##
7 ## command line arguments (forward, backward, left, ##
8 ## right) to velocity commands. (by default, the ##
9 ## robot moves for 1.5 seconds only) ##
10 #######################################################
11 
12 # configuration :
13 MAX_SPEED = 0.4
14 DEFAULT_MOVE_TIME = 1.5
15 
16 #######################################################
17 ## DO NOT CHANGE ANYTHING AFTER THIS LINE! ##
18 #######################################################
19 
20 import rospy
21 import roslib; roslib.load_manifest('pioneer_teleop')
22 import sys
23 import time
24 from geometry_msgs.msg import Twist
25 
26 rospy.init_node('commandline_teleop')
27 
28 # read arguments :
29 direction = sys.argv[1]
30 if (direction != "forward") and (direction != "backward") and (direction != "right") and (direction != "left"):
31  print " - Error : incorrect direction " + direction
32  exit()
33 
34 
35 if (len(sys.argv) > 2):
36  speed = float(sys.argv[2])
37 else:
38  speed = 0
39 
40 msg = ''
41 if (speed <= 0):
42  speed = 0.2
43  msg = " (default speed)"
44 elif (speed > MAX_SPEED):
45  speed = MAX_SPEED
46  msg = " (max speed)"
47 
48 
49 if (len(sys.argv) > 3):
50  moveTime = float(sys.argv[3])
51 else:
52  moveTime = 0.0
53 if (moveTime <= 0):
54  moveTime = DEFAULT_MOVE_TIME
55 
56 if (direction == "forward") or (direction == "backward"):
57  print " >> Move " + str(direction) + " at speed " + str(speed) + msg +" for " + str(moveTime) + " sec\n"
58 else:
59  print " >> Turn " + str(direction) + " at speed " + str(speed) + msg +" for " + str(moveTime) + " sec\n"
60 
61 # define variables :
62 linearDirection = 0
63 rotationalDirection = 0
64 
65 #init :
66 pub = rospy.Publisher('cmd_vel', Twist, queue_size=10)
67 twist = Twist()
68 
69 # get command :
70 if (direction == "forward"):
71  linearDirection = 1
72 elif (direction == "backward"):
73  linearDirection = -1
74 elif (direction == "left"):
75  rotationalDirection = 1
76 elif (direction == "right"):
77  rotationalDirection = -1
78 
79 # send command :
80 
81 twist.linear.x = linearDirection * speed
82 twist.angular.z = rotationalDirection * speed
83 t1 = time.time()
84 t2 = t1
85 #print str(t1)
86 while(t2-t1 < moveTime):
87  pub.publish(twist)
88  t2 = time.time()
89  #print str(t2-t1)
90 
91 twist.linear.x = 0
92 twist.angular.z = 0
93 pub.publish(twist)
94 
95 exit()


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