commandline_teleop.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 ####################################################### 
00003 ##             bendahmane.amine@gmail.com            ##
00004 ##            last update : Jan 31st, 2016           ##
00005 #######################################################
00006 ## Description : This program take transforms simple ##
00007 ##  command line arguments (forward, backward, left, ## 
00008 ##        right) to velocity commands. (by default, the  ##
00009 ##          robot moves for 1.5 seconds only)            ##
00010 #######################################################
00011 
00012 # configuration :
00013 MAX_SPEED = 0.4
00014 DEFAULT_MOVE_TIME = 1.5
00015 
00016 #######################################################
00017 ##      DO NOT CHANGE ANYTHING AFTER THIS LINE!      ##
00018 #######################################################
00019 
00020 import rospy
00021 import roslib; roslib.load_manifest('pioneer_teleop')
00022 import sys
00023 import time
00024 from geometry_msgs.msg import Twist
00025 
00026 rospy.init_node('commandline_teleop')
00027 
00028 # read arguments :
00029 direction = sys.argv[1]
00030 if (direction != "forward") and (direction != "backward") and (direction != "right") and (direction != "left"):
00031         print " - Error : incorrect direction " + direction
00032         exit()
00033 
00034 
00035 if (len(sys.argv) > 2):
00036         speed = float(sys.argv[2])
00037 else:
00038         speed = 0
00039 
00040 msg = ''
00041 if (speed <= 0):
00042         speed = 0.2
00043         msg = " (default speed)"
00044 elif (speed > MAX_SPEED):
00045         speed = MAX_SPEED
00046         msg = " (max speed)"
00047 
00048 
00049 if (len(sys.argv) > 3):
00050         moveTime = float(sys.argv[3])
00051 else:
00052         moveTime = 0.0
00053 if (moveTime <= 0):
00054         moveTime = DEFAULT_MOVE_TIME
00055 
00056 if (direction == "forward") or (direction == "backward"):
00057         print "  >> Move " + str(direction) + " at speed " + str(speed) + msg +" for " + str(moveTime) + " sec\n"
00058 else:
00059         print "  >> Turn " + str(direction) + " at speed " + str(speed) + msg +" for " + str(moveTime) + " sec\n"
00060 
00061 # define variables :
00062 linearDirection = 0
00063 rotationalDirection = 0
00064 
00065 #init :
00066 pub = rospy.Publisher('cmd_vel', Twist, queue_size=10)
00067 twist = Twist()
00068 
00069 # get command :
00070 if (direction == "forward"):
00071                 linearDirection = 1
00072 elif (direction == "backward"):
00073                 linearDirection = -1
00074 elif (direction == "left"):
00075                 rotationalDirection = 1
00076 elif (direction == "right"):
00077                 rotationalDirection = -1
00078 
00079 # send command :
00080 
00081 twist.linear.x = linearDirection * speed
00082 twist.angular.z = rotationalDirection * speed
00083 t1 = time.time()
00084 t2 = t1
00085 #print str(t1)
00086 while(t2-t1 < moveTime):
00087         pub.publish(twist)
00088         t2 = time.time()
00089         #print str(t2-t1)
00090 
00091 twist.linear.x = 0
00092 twist.angular.z = 0
00093 pub.publish(twist)
00094 
00095 exit()


pioneer_teleop
Author(s): Amine BENDAHMANE
autogenerated on Sat Aug 13 2016 04:11:18