安装语音识别模块
首先我们需要安装语音识别的模块,可以选择pocketphinxk开源包
在这里插入图片描述
但是pocketphinx识别,需要自己导入识别文本 ,很麻烦,因此我们选用科大讯飞SDK。
进入科大讯飞注册: https://www.xfyun.cn/?ch=xfow
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
记住APPid 和 linux MSC

cd catin_ws/src
git clone https://github.com/ncnynl/xf-ros.git

把CMakeLists.txt修改

target_link_libraries(
   asr_sample
   ${catkin_LIBRARIES} 
   /home/ubu/catkin_ws/src/xfei_asr/lib/libmsc.so -ldl -pthread
 )

把 ubu 换成 自己的主机名

再打开catin_ws/src/xfei_asr/src下的所有C文件和cpp文件

ctrl+f 搜索appid
在这里插入图片描述
把appid换成在科大讯飞注册的appid
然后把下载的SDK中的lib下选择适合自己系统的
在这里插入图片描述
放到xfei_asr/src/lib中
进到src中 编写cpp文件

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 str3 = "后退。";
    string str4 = "向左转。";
    string str5 = "向右转。";
    string str6 = "停止。";
    if(str1 == str2)
    {
        vel_cmd.linear.x = 1;
        vel_cmd.angular.z = 0;
        pthread_create(&pth_[0],NULL,vel_ctr,NULL);
    }
    if(str1 == str3)
    {
        vel_cmd.linear.x = -1;
        vel_cmd.angular.z = 0;
        pthread_create(&pth_[1],NULL,vel_ctr,NULL);
    }
    if(str1 == str4)
    {
        vel_cmd.linear.x = 0;
        vel_cmd.angular.z = 1;
        pthread_create(&pth_[2],NULL,vel_ctr,NULL);
    }
    if(str1 == str5)
    {
        vel_cmd.linear.x = 0;
        vel_cmd.angular.z = -1;
        pthread_create(&pth_[3],NULL,vel_ctr,NULL);
    }
    if(str1 == str6)
    {
        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",5);
    ros::Subscriber sub = n.subscribe("voiceWords",5,callback);
cout<<"您好!你可以语音控制啦!"<<endl;
cout<<"前进:向前行驶"<<endl;
cout<<"后退:向后行驶"<<endl;
cout<<"向左转:向左调头"<<endl;
cout<<"向右转:向右调头"<<endl;
cout<<"停止"<<endl;
    ros::spin();
}

在CMakeLists.txt中添加要编译新的cpp文件

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

安装E100开源机器人

这里有非常快速简单的教程

https://github.com/gaitech-robotics/RIA-E100

 pub = n.advertise<geometry_msgs::Twist>("/ria_base_controller/cmd_vel",5);
 

这个代码向E100小车话题发送移动的信息

为了把语音识别修改为持续唤醒,修改 iat_publish.cpp
重写wakeUp函数:

void WakeUp(const std_msgs::String::ConstPtr& msg)
{
    printf("waking up\r\n");
    usleep(700*1000);
    wakeupFlag=1;
}
void Wake()
{
    printf("waking up\r\n");
    usleep(700*1000);
    wakeupFlag=1;
}

修改为主动唤醒
修改后代码如下:



/*

* 语音听写(iFly Auto Transform)技术能够实时地将语音转换成对应的文字。

*/

 

#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 4096

 

int wakeupFlag   = 0 ;

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");

    else

        printf("\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++ < 10)

        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");

    usleep(700*1000);

    wakeupFlag=1;

}



void Wake()

{

    printf("waking up\r\n");

    usleep(700*1000);

    wakeupFlag=1;

}

 

int main(int argc, char* argv[])

{

    // 初始化ROS

    ros::init(argc, argv, "voiceRecognition");

    ros::NodeHandle n;

    ros::Rate loop_rate(5);

 

    // 声明Publisher和Subscriber

    // 订阅唤醒语音识别的信号

    ros::Subscriber wakeUpSub = n.subscribe("voiceWakeup", 500, WakeUp);   

    // 订阅唤醒语音识别的信号    

    ros::Publisher voiceWordsPub = n.advertise<std_msgs::String>("voiceWords", 500);  

 

    ROS_INFO("Sleeping...");

    int count=0;

    while(ros::ok())

    {

        // 语音识别唤醒

        if (wakeupFlag){

            ROS_INFO("Wakeup...");

            int ret = MSP_SUCCESS;

            const char* login_params = "appid = 5fd079c6, 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 5 seconds\n");

 

            demo_mic(session_begin_params);

 

            printf("5 sec passed\n");

        

            wakeupFlag=0;

            MSPLogout();

        }

 

        // 语音识别完成

        if(resultFlag){

            resultFlag=0;

            std_msgs::String msg;

            msg.data = g_result;

            voiceWordsPub.publish(msg);

	    printf("pub\n");

        }



 	Wake();

        ros::spinOnce();

        loop_rate.sleep();

        count++;

    }

 

exit:

    MSPLogout(); // Logout...

 

    return 0;

}

最后编译工作空间:

 cd ~/catkin_ws && catkin_make

执行:

roslaunch e100_sim gazebo.launch
rosrun xfei_asr sub_word
rosrun xfei_asr iat_publish_speak
rosrun xfei_asr iat_publish

在这里插入图片描述

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐