ROS melody speech recognition learning

Keywords: angular github Python Mobile

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

#!/usr/bin/env python
""" 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):
        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():
    def speechCb(self, msg):

        if"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"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"forward") > -1:    
            self.msg.linear.x = self.speed
            self.msg.angular.z = 0
        elif"left") > -1:
            if self.msg.linear.x != 0:
                if self.msg.angular.z < self.speed:
                    self.msg.angular.z += 0.05
                self.msg.angular.z = self.speed*2
        elif"right") > -1:    
            if self.msg.linear.x != 0:
                if self.msg.angular.z > -self.speed:
                    self.msg.angular.z -= 0.05
                self.msg.angular.z = -self.speed*2
        elif"back") > -1:
            self.msg.linear.x = -self.speed
            self.msg.angular.z = 0
        elif"stop") > -1 or"halt") > -1:          
            self.msg = Twist()

    def cleanup(self):
        # stop the robot!
        twist = Twist()

if __name__=="__main__":

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.

Published 2 original articles, praised 0, visited 10
Private letter follow

Posted by yshaf13 on Sat, 08 Feb 2020 04:04:06 -0800