voice_teleop.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 import rospy
00004 from geometry_msgs.msg import Twist
00005 from std_msgs.msg import String
00006 
00007 class RobotVoiceTeleop:
00008     #define the constructor of the class
00009     def  __init__(self):
00010         #initialize the ROS node with a name voice_teleop
00011         rospy.init_node('voice_teleop')
00012         
00013         # Publish the Twist message to the cmd_vel topic
00014         self.cmd_vel_pub = rospy.Publisher('/cmd_vel', Twist, queue_size=5)
00015         
00016         # Subscribe to the /recognizer/output topic to receive voice commands.
00017         rospy.Subscriber('/recognizer/output', String, self.voice_command_callback)
00018         
00019         #create a Rate object to sleep the process at 5 Hz
00020         rate = rospy.Rate(5)
00021         
00022         # Initialize the Twist message we will publish.
00023         self.cmd_vel = Twist()
00024         #make sure to make the robot stop by default
00025         self.cmd_vel.linear.x=0;
00026         self.cmd_vel.angular.z=0;
00027         
00028         
00029         
00030         # A mapping from keywords or phrases to commands
00031         #we consider the following simple commands, which you can extend on your own
00032         self.commands =             ['stop',
00033                                     'forward',
00034                                     'backward',
00035                                     'turn left',
00036                                     'turn right',
00037                                     ]
00038         rospy.loginfo("Ready to receive voice commands")
00039         # We have to keep publishing the cmd_vel message if we want the robot to keep moving.
00040         while not rospy.is_shutdown():
00041             self.cmd_vel_pub.publish(self.cmd_vel)
00042             rate.sleep()
00043 
00044 
00045     def voice_command_callback(self, msg):
00046         # Get the motion command from the recognized phrase
00047         command = msg.data
00048         if (command in self.commands):
00049             if command == 'forward':
00050                 self.cmd_vel.linear.x = 0.2
00051                 self.cmd_vel.angular.z = 0.0
00052             elif command == 'backward':
00053                 self.cmd_vel.linear.x = -0.2
00054                 self.cmd_vel.angular.z = 0.0
00055             elif command == 'turn left':
00056                 self.cmd_vel.linear.x = 0.0
00057                 self.cmd_vel.angular.z = 0.5
00058             elif command == 'turn right':
00059                 self.cmd_vel.linear.x = 0.0
00060                 self.cmd_vel.angular.z = -0.5
00061             elif command == 'stop':
00062                 self.cmd_vel.linear.x = 0.0
00063                 self.cmd_vel.angular.z = 0.0
00064 
00065         else: #command not found
00066             #print 'command not found: '+command
00067             self.cmd_vel.linear.x = 0.0
00068             self.cmd_vel.angular.z = 0.0
00069         print ("linear speed : " + str(self.cmd_vel.linear.x))
00070         print ("angular speed: " + str(self.cmd_vel.angular.z))
00071 
00072 
00073 
00074 if __name__=="__main__":
00075     try:
00076       RobotVoiceTeleop()
00077       rospy.spin()
00078     except rospy.ROSInterruptException:
00079       rospy.loginfo("Voice navigation terminated.")
00080 


gapter
Author(s):
autogenerated on Thu Jun 6 2019 22:05:13