ROS speech recognition control robot (2)
Article directory
Create Ros package
In the previous section, the voice to text is implemented. The content of this section is to convert the text converted from voice to instruction, which is used to control the famous little turbo in ROS
catkin_create_pkg baidu_asr roscpp rospy std_msgs
control logic
There are two nodes in the figure. To control the turtle, create two nodes in the baidu ASR package:
The first Node is to accept the text recognized by Baidu voice and publish it with the Topic of / recognizer/output in the format of std_msgs/String
The second Node subscribes to the message of / recognizer/output and interprets the message, such as what kind of instructions are corresponding to forward, backward, left and right, and publishes it as the Topic of CMD level. The message format is Twist()
The third Node is turnlesim, which subscribes to the topic of CMD vel and converts it into the action instruction of tortoise
Control the little turtle by writing instructions
The third node is out of the box, so start with the second.
Code can participate https://github.com/cmusphinx/ros-pocketsphinx
#!/usr/bin/env python """ voice_cmd_vel.py is a simple demo of speech recognition. You can control a mobile base using commands found in the corpus file. """ import rospy from geometry_msgs.msg import Twist from std_msgs.msg import String class voice_cmd_vel: def __init__(self): rospy.on_shutdown(self.cleanup) self.speed = 0.2 self.msg = Twist() # publish to cmd_vel, subscribe to speech output self.pub_ = rospy.Publisher('turtle1/cmd_vel', Twist) rospy.Subscriber('recognizer/output', String, self.speechCb) r = rospy.Rate(10.0) while not rospy.is_shutdown(): self.pub_.publish(self.msg) r.sleep() def speechCb(self, msg): rospy.loginfo(msg.data) if msg.data.find("full speed") > -1: if self.speed == 0.2: self.msg.linear.x = self.msg.linear.x*2 self.msg.angular.z = self.msg.angular.z*2 self.speed = 0.4 if msg.data.find("half speed") > -1: if self.speed == 0.4: self.msg.linear.x = self.msg.linear.x/2 self.msg.angular.z = self.msg.angular.z/2 self.speed = 0.2 if msg.data.find("forward") > -1: self.msg.linear.x = self.speed self.msg.angular.z = 0 elif msg.data.find("left") > -1: if self.msg.linear.x != 0: if self.msg.angular.z < self.speed: self.msg.angular.z += 0.05 else: self.msg.angular.z = self.speed*2 elif msg.data.find("right") > -1: if self.msg.linear.x != 0: if self.msg.angular.z > -self.speed: self.msg.angular.z -= 0.05 else: self.msg.angular.z = -self.speed*2 elif msg.data.find("back") > -1: self.msg.linear.x = -self.speed self.msg.angular.z = 0 elif msg.data.find("stop") > -1 or msg.data.find("halt") > -1: self.msg = Twist() self.pub_.publish(self.msg) def cleanup(self): # stop the robot! twist = Twist() self.pub_.publish(twist) if __name__=="__main__": rospy.init_node('voice_cmd_vel') try: voice_cmd_vel() except: pass
Example of the second node sending messages to the turnlsim node:
rostopic pub -1 recognizer/output std_msgs/String "forward"
rostopic pub -1 recognizer/output std_msgs/String "right"
After inputting the command, the little tortoise began to circle in place and succeeded.
The next step is how to recognize the speech and convert it to std_msgs/String format and send it out.
Voice to text
Yesterday's voice to text can be used, and later found that Baidu's voice recognition is simpler.
Add a Ros node and publish information.