语音控制机器人小车运动科大讯飞SDK

zz/2024/3/1 16:17:29

在前面文章小车实现语音识别的基础之上,对小车实现语音控制运动

修改CMakeLists.txt文件

在这里插入图片描述
在末尾加上以下代码:

add_executable(sub_word src/sub_word.cpp)
target_link_libraries(sub_word ${catkin_LIBRARIES} )

修改发布文件

打开xfei_asr/src目录下的int_publish_speak.cpp文件,然后将下面的代码覆盖进去,请将appid填写为你在讯飞那申请的appid 。修改后的发布文件,将是全程自动唤醒不再是每次唤醒都需要手动输入代码。

#include <stdlib.h>#include <stdio.h>#include <string.h>#include <unistd.h>#include "qisr.h"#include "msp_cmn.h"#include "msp_errors.h"#include "speech_recognizer.h"#include <iconv.h>#include "ros/ros.h"#include "std_msgs/String.h"#define FRAME_LEN   640#define BUFFER_SIZE 4096int wakeupFlag   = 1 ;int resultFlag   = 0 ;static void show_result(char *string, char is_over){resultFlag=1;   printf("\rResult: [ %s ]", string);if(is_over)putchar('\n');}static char *g_result = NULL;static unsigned int g_buffersize = BUFFER_SIZE;void on_result(const char *result, char is_last){if (result) {size_t left = g_buffersize - 1 - strlen(g_result);size_t size = strlen(result);if (left < size) {g_result = (char*)realloc(g_result, g_buffersize + BUFFER_SIZE);if (g_result)g_buffersize += BUFFER_SIZE;else {printf("mem alloc failed\n");return;}}strncat(g_result, result, size);show_result(g_result, is_last);}}void on_speech_begin(){if (g_result){free(g_result);}g_result = (char*)malloc(BUFFER_SIZE);g_buffersize = BUFFER_SIZE;memset(g_result, 0, g_buffersize);printf("Start Listening...\n");}void on_speech_end(int reason){if (reason == END_REASON_VAD_DETECT)printf("\nSpeaking done \n");elseprintf("\nRecognizer error %d\n", reason);}/* demo recognize the audio from microphone */static void demo_mic(const char* session_begin_params){int errcode;int i = 0;struct speech_rec iat;struct speech_rec_notifier recnotifier = {on_result,on_speech_begin,on_speech_end};errcode = sr_init(&iat, session_begin_params, SR_MIC, &recnotifier);if (errcode) {printf("speech recognizer init failed\n");return;}errcode = sr_start_listening(&iat);if (errcode) {printf("start listen failed %d\n", errcode);}/* demo 10 seconds recording */while(i++ < 3)sleep(1);errcode = sr_stop_listening(&iat);if (errcode) {printf("stop listening failed %d\n", errcode);}sr_uninit(&iat);}/* main thread: start/stop record ; query the result of recgonization.* record thread: record callback(data write)* helper thread: ui(keystroke detection)*/void WakeUp(const std_msgs::String::ConstPtr& msg){printf("waking up\r\n");sleep(10000);wakeupFlag=1;}int main(int argc, char* argv[]){// 初始化ROSros::init(argc, argv, "voiceRecognition");ros::NodeHandle n;ros::Rate loop_rate(10);// 声明Publisher和Subscriber// 订阅唤醒语音识别的信号ros::Subscriber wakeUpSub = n.subscribe("voiceWakeup", 1000, WakeUp);   // 订阅唤醒语音识别的信号    ros::Publisher voiceWordsPub = n.advertise<std_msgs::String>("voiceWords", 1000);  ROS_INFO("Sleeping...");int count=0;while(ros::ok()){// 语音识别唤醒if (wakeupFlag){ROS_INFO("Wakeup...");int ret = MSP_SUCCESS;const char* login_params = "appid =, work_dir = .";const char* session_begin_params ="sub = iat, domain = iat, language = zh_cn, ""accent = mandarin, sample_rate = 16000, ""result_type = plain, result_encoding = utf8";ret = MSPLogin(NULL, NULL, login_params);if(MSP_SUCCESS != ret){MSPLogout();printf("MSPLogin failed , Error code %d.\n",ret);}printf("Demo recognizing the speech from microphone\n");printf("Speak in 10 seconds\n");demo_mic(session_begin_params);printf("10 sec passed\n");wakeupFlag=1;MSPLogout();}// 语音识别完成if(resultFlag){resultFlag=0;std_msgs::String msg;msg.data = g_result;voiceWordsPub.publish(msg);}ros::spinOnce();loop_rate.sleep();count++;}exit:MSPLogout(); // Logout...return 0;}

添加指令文件

创建新的文件命名为sub_word.cpp 粘贴以下代码。具体识别控制指令执行请按需修改

#include<ros/ros.h>
#include<geometry_msgs/Twist.h>
#include<std_msgs/String.h>
#include<pthread.h>
#include<iostream>
#include<stdio.h>using namespace std;
ros::Publisher pub;
geometry_msgs::Twist vel_cmd;
pthread_t pth_[5];void* vel_ctr(void* arg)
{while(true){pub.publish(vel_cmd);ros::spinOnce();sleep(1);}return 0;
}void callback(const std_msgs::String::ConstPtr& msg)
{cout<<"接收到指令:"<<msg->data.c_str()<<endl;string str1 = msg->data.c_str();string str2 = "前进。";string str2_1 = "up.";string str3 = "后退。";string str3_1 = "back.";string str4 = "左转。";string str4_1 = "left.";string str5 = "右转。";string str5_1 = "right.";string str6 = "停。";string str6_1 = "stop.";if(str1 == str2 || str1==str2_1){vel_cmd.linear.x = 1;vel_cmd.angular.z = 0;pthread_create(&pth_[0],NULL,vel_ctr,NULL);}if(str1 == str3 || str1 == str3_1){vel_cmd.linear.x = -1;vel_cmd.angular.z = 0;pthread_create(&pth_[1],NULL,vel_ctr,NULL);}if(str1 == str4 || str1 == str4_1){vel_cmd.linear.x = 0;vel_cmd.angular.z = 1;pthread_create(&pth_[2],NULL,vel_ctr,NULL);}if(str1 == str5 || str1 == str5_1){vel_cmd.linear.x = 0;vel_cmd.angular.z = -1;pthread_create(&pth_[3],NULL,vel_ctr,NULL);}if(str1 == str6 || str1 == str6_1){vel_cmd.linear.x = 0;vel_cmd.angular.z = 0;pthread_create(&pth_[0],NULL,vel_ctr,NULL);}    
}int main(int argc, char** argv)
{ros::init(argc, argv, "sub_word");ros::NodeHandle n;pub = n.advertise<geometry_msgs::Twist>("/ria_base_controller/cmd_vel",10);ros::Subscriber sub = n.subscribe("voiceWords",10,callback);
cout<<"语音控制程序已开启"<<endl;
cout<<"中文关键字控制字:前进 后退 左转 右转 停"<<endl;
cout<<"英文关键字控制字:UP BACK LEFT RIGHT STOP"<<endl;
cout<<"程序将识别标准的普通话和英语单词"<<endl;ros::spin();
}

测试

测试前记得编译啊

依次在终端执行以下命令,然后对麦克风说控制命令。建议打开翻译等软件说标准的普通话或英文,识别度更高

roslaunch e100_sim gazebo.launch
rosrun xfei_asr iat_publish_speak
rosrun xfei_asr sub_word

成功控制小车运动。
在这里插入图片描述


http://www.ngui.cc/zz/1568536.html

相关文章

windows自带桌面远程控制

被控制方设置 1.打开系统属性&#xff08;winpause&#xff09; 2.进入远程设置 3.在远程协助里勾选允许远程协助连接计算&#xff0c;在远程桌面桌面上选择允许运行任意版本远程桌面的计算机连接 4.进入用户管理界面&#xff0c;设置账号和密码&#xff08;必须设置密码&#…

伺服控制系统

本文非原创&#xff0c;内容摘自网络&#xff0c;文末注明出处&#xff0c;侵权必删&#xff01;伺服电机为了达到生产的精准控制,电机一般采用三环控制,这主要是为了使伺服电机系统形成闭环控制&#xff0c;所谓三环就是3个闭环负反馈PID调节系统。电压映射电流变化&#xff0…

车身域控制器

随着整车发展&#xff0c;电气架构越来越复杂&#xff0c;为了降低整车成本&#xff0c;减少整车线束复杂度&#xff0c;支持面向客户的整车功能&#xff0c;车身域控制器扩展算力&#xff0c;能够兼容传统BCM功能&#xff0c;同时集成空调算法、门控逻辑、胎压监控等整车控制策…

控制工程专业英语词汇

电路 中文英文电路、网络Network电路Circuitry––电阻器Resistor电感器Inductor电容器Capacitor晶体管Transistor半导体Semiconductor晶体Crystal锗Germanium硅Silicon无源网络Passive network有源网络Active network特性、特性曲线Characteristic欧姆Ohm法拉第Faraday电荷El…

计算机控制器的简写,工业控制常用英语及缩写

集散控制系统——Distributed Control System(DCS)现场总线控制系统——Fieldbus Control System(FCS)监控及数据采集系统——Supervisory Control And Data Acqusition(SCADA)可编程序控制器——Programmable Logic Controller(PLC)可编程计算机控制器——Programmable Comput…

自动化/控制工程专业英语01——什么是控制[考研/保研面试]

自动化/控制工程专业英语整理第一弹——对“控制”的理解 eg.面试问题&#xff1a;“用英文解释一下什么是控制”在本文中就有答案~ 一、相关词汇&#xff08;干货&#xff0c;一定要熟悉&#xff09; disturbance n.扰动 parameter n.参…

不同强度等级下混凝土的弹性模量、轴心抗压强度标准值fck、轴心抗拉强度标准值ftk、轴心抗压强度设计值fcd、轴心抗拉强度设计值ftd (规范值)

之前一直在网上找混凝土的弹性模量等参数&#xff0c;但网上给的数据参差不齐&#xff0c;有的存在较大差别&#xff0c;混凝土弹性模量及其抗压、抗拉强度作为基本参数&#xff0c;在使用时应具备较高的准确性。基于此目的&#xff0c;通过查阅相应规范后&#xff0c;本文给出…

ftk学习记(首篇)

【 声明&#xff1a;版权全部&#xff0c;欢迎转载&#xff0c;请勿用于商业用途。 联系信箱&#xff1a;feixiaoxing 163.com】 非常早之前就知道ftk了&#xff0c;当时主要是由于买了李先静的书&#xff0c;所以知道了这么一个项目。由于对这样的gui的代码非常感兴趣。所以在…

ftk挂载镜像 加vm 实现仿真

没有仿真软件怎么办&#xff0c;偶尔机会刷到一篇文章&#xff0c;使用ftk和vm进行仿真下面开始实际操作。 这里用到的软件 VMware Workstation 16 pro FTK Imager 这里我用的是中文版 一、使用ftk挂载出镜像 选择镜像加载 二、选择需要挂载的镜像 选择需要挂载的镜像 点击…

java jta_分布式事务(二)Java事务API(JTA)规范

一、引子既然出现了分布式场景(DTP模型), 大java也及时制定出一套规范来给各大应用服务器、数据库/mq等厂商使用&#xff0c;以方便管理互通---》JTA闪亮登场。JTA(Java Transaction API)&#xff0c;即Java事务API规范。JTA规范指定了事务管理器(TM)与分布式事务系统中涉及的各…