所有代码已上传至github网站:
github链接: https://github.com/YuemingBi/ros_practice/tree/master
ROS如何编译科大讯飞语音识别功能包,以前的文章里有写过https://blog.csdn.net/weixin_42361804/article/details/104145238。如果使用这种方式编译,只实现了科大讯飞的语音功能,本章作业的其他接口还要自行补充。
下载科大讯飞SDK和我提供的ch8功能包,然后把SDK的libmsc.so拷贝到/usr/lib文件夹下,把ch8功能包的所有PPID改成自己的PPID(使用查找和替换),之后就可以编译使用了。
本章作业需要ch6和ch8功能包一起编译,其中ch8的launch文件和cpp文件如下图所示:
<launch>
<node name="iat_publish" pkg="robot_voice" type="iat_publish" output="screen"/>
<node name="voice_teleop" pkg="robot_voice" type="voice_teleop" output="screen"/>
<node name="voice_assistant" pkg="robot_voice" type="voice_assistant" output="screen"/>
</launch>
此launch文件启动三个节点,其中iat_publish是语音输入和语音识别的节点,voice_teleop是根据语音识别的结果控制机器人执行某种动作的节点,voice_assistant是根据语音识别的结果给出回应并语音输出的节点。
int main(int argc, char* argv[])
{
ros::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;
int ret = MSP_SUCCESS;
int upload_on = 1; /* whether upload the user word */
/* login params, please do keep the appid correct */
const char* login_params = "appid = 5d3ff09c, work_dir = .";
int aud_src = 0; /* from mic or file */
/*
* See "iFlytek MSC Reference Manual"
*/
const char* session_begin_params =
"sub = iat, domain = iat, language = zh_cn, "
"accent = mandarin, sample_rate = 16000, "
"result_type = plain, result_encoding = utf8";
/* Login first. the 1st arg is username, the 2nd arg is password
* just set them as NULL. the 3rd arg is login paramertes
* */
ret = MSPLogin(NULL, NULL, login_params);
if (MSP_SUCCESS != ret) {
printf("MSPLogin failed , Error code %d.\n",ret);
goto exit; // login fail, exit the program
}
while(ros::ok())
{
// 语音识别唤醒
if(wakeupFlag)
{
ROS_INFO("Wakeup...");
printf("Demo recognizing the speech from microphone\n");
printf("Speak in 8 seconds\n");
demo_mic(session_begin_params);
printf("8 sec passed\n");
wakeupFlag=0;
}
// 语音识别完成
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;
}
此节点订阅了语音唤醒的话题/voiceWakeup
,当接收到此话题的信号之后开始语音识别,然后将语音识别的结果发布给/voiceWords
#include "ros/ros.h"
#include "geometry_msgs/Twist.h"
#include "std_msgs/String.h"
class SubscribeAndPublish
{
public:
SubscribeAndPublish()
{
pub = n.advertise<geometry_msgs::Twist>("cmd_vel", 1000);
sub = n.subscribe("voiceWords", 1000, &SubscribeAndPublish::callback, this);
}
void callback(const std_msgs::String::ConstPtr& msg)
{
std::string dataString = msg->data;
if(dataString.find("前进") != std::string::npos || dataString.find("向前") != std::string::npos){
//ROS_INFO("go forward");
geometry_msgs::Twist msg;
msg.linear.x = 0.3;
msg.angular.z = 0;
pub.publish(msg);
}
else if(dataString.find("后退") != std::string::npos || dataString.find("向后") != std::string::npos){
//ROS_INFO("go back");
geometry_msgs::Twist msg;
msg.linear.x = -0.3;
msg.angular.z = 0;
pub.publish(msg);
}
else if(dataString.find("左转") != std::string::npos || dataString.find("向左") != std::string::npos){
//ROS_INFO("turn right");
geometry_msgs::Twist msg;
msg.linear.x = 0;
msg.angular.z = 0.3;
pub.publish(msg);
}
else if(dataString.find("右转") != std::string::npos || dataString.find("向左") != std::string::npos){
//ROS_INFO("turn left");
geometry_msgs::Twist msg;
msg.linear.x = 0;
msg.angular.z = -0.3;
pub.publish(msg);
}
else if(dataString.find("转圈") != std::string::npos || dataString.find("转圈圈") != std::string::npos){
geometry_msgs::Twist msg;
msg.linear.x = 0.3;
msg.angular.z = 0.3;
pub.publish(msg);
}
}
private:
ros::NodeHandle n;
ros::Publisher pub;
ros::Subscriber sub;
};
int main(int argc, char **argv)
{
ros::init(argc, argv, "voice_teleop");
SubscribeAndPublish SAPObject;
ros::spin();
return 0;
}
此节点订阅了/voiceWords
话题,即语音识别的结果,然后根据语音识别的结果向/cmd_vel
话题发布控制机器人速度的消息。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include "robot_voice/qtts.h"
#include "robot_voice/msp_cmn.h"
#include "robot_voice/msp_errors.h"
#include "ros/ros.h"
#include "std_msgs/String.h"
/* wav音频头部格式 */
typedef struct _wave_pcm_hdr
{
char riff[4]; // = "RIFF"
int size_8; // = FileSize - 8
char wave[4]; // = "WAVE"
char fmt[4]; // = "fmt "
int fmt_size; // = 下一个结构体的大小 : 16
short int format_tag; // = PCM : 1
short int channels; // = 通道数 : 1
int samples_per_sec; // = 采样率 : 8000 | 6000 | 11025 | 16000
int avg_bytes_per_sec; // = 每秒字节数 : samples_per_sec * bits_per_sample / 8
short int block_align; // = 每采样点字节数 : wBitsPerSample / 8
short int bits_per_sample; // = 量化比特数: 8 | 16
char data[4]; // = "data";
int data_size; // = 纯数据长度 : FileSize - 44
} wave_pcm_hdr;
/* 默认wav音频头部数据 */
wave_pcm_hdr default_wav_hdr =
{
{ 'R', 'I', 'F', 'F' },
0,
{'W', 'A', 'V', 'E'},
{'f', 'm', 't', ' '},
16,
1,
1,
16000,
32000,
2,
16,
{'d', 'a', 't', 'a'},
0
};
/* 文本合成 */
int text_to_speech(const char* src_text, const char* des_path, const char* params)
{
int ret = -1;
FILE* fp = NULL;
const char* sessionID = NULL;
unsigned int audio_len = 0;
wave_pcm_hdr wav_hdr = default_wav_hdr;
int synth_status = MSP_TTS_FLAG_STILL_HAVE_DATA;
if (NULL == src_text || NULL == des_path)
{
printf("params is error!\n");
return ret;
}
fp = fopen(des_path, "wb");
if (NULL == fp)
{
printf("open %s error.\n", des_path);
return ret;
}
/* 开始合成 */
sessionID = QTTSSessionBegin(params, &ret);
if (MSP_SUCCESS != ret)
{
printf("QTTSSessionBegin failed, error code: %d.\n", ret);
fclose(fp);
return ret;
}
ret = QTTSTextPut(sessionID, src_text, (unsigned int)strlen(src_text), NULL);
if (MSP_SUCCESS != ret)
{
printf("QTTSTextPut failed, error code: %d.\n",ret);
QTTSSessionEnd(sessionID, "TextPutError");
fclose(fp);
return ret;
}
printf("正在合成 ...\n");
fwrite(&wav_hdr, sizeof(wav_hdr) ,1, fp); //添加wav音频头,使用采样率为16000
while (1)
{
/* 获取合成音频 */
const void* data = QTTSAudioGet(sessionID, &audio_len, &synth_status, &ret);
if (MSP_SUCCESS != ret)
break;
if (NULL != data)
{
fwrite(data, audio_len, 1, fp);
wav_hdr.data_size += audio_len; //计算data_size大小
}
if (MSP_TTS_FLAG_DATA_END == synth_status)
break;
printf(">");
usleep(150*1000); //防止频繁占用CPU
}
printf("\n");
if (MSP_SUCCESS != ret)
{
printf("QTTSAudioGet failed, error code: %d.\n",ret);
QTTSSessionEnd(sessionID, "AudioGetError");
fclose(fp);
return ret;
}
/* 修正wav文件头数据的大小 */
wav_hdr.size_8 += wav_hdr.data_size + (sizeof(wav_hdr) - 8);
/* 将修正过的数据写回文件头部,音频文件为wav格式 */
fseek(fp, 4, 0);
fwrite(&wav_hdr.size_8,sizeof(wav_hdr.size_8), 1, fp); //写入size_8的值
fseek(fp, 40, 0); //将文件指针偏移到存储data_size值的位置
fwrite(&wav_hdr.data_size,sizeof(wav_hdr.data_size), 1, fp); //写入data_size的值
fclose(fp);
fp = NULL;
/* 合成完毕 */
ret = QTTSSessionEnd(sessionID, "Normal");
if (MSP_SUCCESS != ret)
{
printf("QTTSSessionEnd failed, error code: %d.\n",ret);
}
return ret;
}
std::string to_string(int val)
{
char buf[20];
sprintf(buf, "%d", val);
return std::string(buf);
}
void voiceWordsCallback(const std_msgs::String::ConstPtr& msg)
{
char cmd[2000];
const char* text;
int ret = MSP_SUCCESS;
const char* session_begin_params = "voice_name = xiaoyan, text_encoding = utf8, sample_rate = 16000, speed = 50, volume = 50, pitch = 50, rdn = 2";
const char* filename = "tts_sample.wav"; //合成的语音文件名称
std::cout<<"I heard :"<<msg->data.c_str()<<std::endl;
std::string dataString = msg->data;
if(dataString.find("前进") != std::string::npos
|| dataString.find("向前") != std::string::npos)
{
char helpString[100] = "太阳当空照,花儿对我笑";
text = helpString;
std::cout<<text<<std::endl;
}
else if(dataString.find("后退") != std::string::npos
|| dataString.find("向后") != std::string::npos)
{
char helpString[100] = "后退,回首美丽的风景";
text = helpString;
std::cout<<text<<std::endl;
}
else if(dataString.find("左转") != std::string::npos
|| dataString.find("向左") != std::string::npos)
{
char helpString[100] = "峰回路转,向左转";
text = helpString;
std::cout<<text<<std::endl;
}
else if(dataString.find("右转") != std::string::npos
|| dataString.find("向左") != std::string::npos)
{
char helpString[100] = "峰回路转,向右转";
text = helpString;
std::cout<<text<<std::endl;
}
else if(dataString.find("转圈") != std::string::npos
|| dataString.find("转圈圈") != std::string::npos)
{
char helpString[100] = "爱的魔力转圈圈";
text = helpString;
std::cout<<text<<std::endl;
}
else
{
text = msg->data.c_str();
}
/* 文本合成 */
printf("开始合成 ...\n");
ret = text_to_speech(text, filename, session_begin_params);
if (MSP_SUCCESS != ret)
{
printf("text_to_speech failed, error code: %d.\n", ret);
}
printf("合成完毕\n");
popen("play tts_sample.wav","r");
sleep(1);
}
void toExit()
{
printf("按任意键退出 ...\n");
getchar();
MSPLogout(); //退出登录
}
int main(int argc, char* argv[])
{
int ret = MSP_SUCCESS;
const char* login_params = "appid = 5d3ff09c, work_dir = .";//登录参数,appid与msc库绑定,请勿随意改动
/* 用户登录 */
ret = MSPLogin(NULL, NULL, login_params);//第一个参数是用户名,第二个参数是密码,第三个参数是登录参数,用户名和密码可在http://www.xfyun.cn注册获取
if (MSP_SUCCESS != ret)
{
printf("MSPLogin failed, error code: %d.\n", ret);
//goto exit ;//登录失败,退出登录
toExit();
}
ros::init(argc,argv,"TextToSpeech");
ros::NodeHandle n;
ros::Subscriber sub =n.subscribe("voiceWords", 1000, voiceWordsCallback);
ros::spin();
exit:
printf("按任意键退出 ...\n");
getchar();
MSPLogout(); //退出登录
return 0;
}
此节点订阅了语音合成结果的话题/voiceWords
,然后根据语音识别结果作出回应并语音合成。从回调函数voiceWordsCallback()
中可以看到前进、后退、左转、右转、转圈的五种情况。
roslaunch robot_voice voice_teleop.launch
roslaunch robot_gazebo view_robot_kinect_rplidar.launch
rostopic pub /voiceWakeup std_msgs/String "data: ''"
语音输入“前进”或“向前”,机器人向前运动,并回答“太阳当空照,花儿对我笑”
rostopic pub /voiceWakeup std_msgs/String "data: ''"
语音输入“后退”或“向后”,机器人向后运动,并回答“后退,回首美丽的风景”
rostopic pub /voiceWakeup std_msgs/String "data: ''"
语音输入“左转”或“向左”,机器人向左转,并回答“峰回路转,向左转”
rostopic pub /voiceWakeup std_msgs/String "data: ''"
语音输入“右转”或“向右”,机器人向右转,并回答“峰回路转,向右转”
rostopic pub /voiceWakeup std_msgs/String "data: ''"
语音输入“转圈”,机器人转圈,并回答“爱的魔力转圈圈”