ROS与语音交互教程-ROS下使用pocketsphinx语音控制机器人运动
ROS与语音交互教程-ROS下使用pocketsphinx语音控制机器人运动
说明:
- 介绍如何在ROS下使用pocketsphinx语音控制机器人运动
步骤:
- 编写语音控制节点voice_cmd_vel.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import rospy
from geometry_msgs.msg import Twist
from std_msgs.msg import String
# 初始化ROS节点,声明一个发布速度控制的Publisher
rospy.init_node('voice_teleop')
pub = rospy.Publisher('/cmd_vel', Twist, queue_size=10)
r = rospy.Rate(10)
# 接收到语音命令后发布速度指令
def get_voice(data):
voice_text=data.data
rospy.loginfo("I said:: %s",voice_text)
twist = Twist()
if voice_text == "go":
twist.linear.x = 0.3
elif voice_text == "back":
twist.linear.x = -0.3
elif voice_text == "left":
twist.angular.z = 0.3
elif voice_text == "right":
twist.angular.z = -0.3
pub.publish(twist)
# 订阅pocketsphinx语音识别的输出字符
def teleop():
rospy.loginfo("Starting voice Teleop")
rospy.Subscriber("/recognizer/output", String, get_voice)
rospy.spin()
while not rospy.is_shutdown():
teleop()
代码解释:
通过一个Subscriber订阅/recognizer/output话题,接收到语音识别的结果后进入回调函数,简单处理后通过Publisher发布控制机器人运动的速度控制指令
- [Remote PC] 启动roscore
roscore
- [TurtleBot] 启动burger
roslaunch turtlebot3_bringup turtlebot3_robot.launch
- [Remote PC]运行,启动所有节点
roslaunch robot_voice voice_commands.launch
rosrun robot_voice voice_teleop.py
- 通过语音“go”,“back”,“left”,“right”,控制机器人运动
获取最新文章: 扫一扫右上角的二维码加入“创客智造”公众号