树莓派开发—语音识别功能

文章目录

前言

前面学习了树莓派的串口通信,这一节结合了树莓派和语音模块,两者通过串口通信来实现语音识别功能。

[En]

Previously learned the raspberry pie serial communication, this section combines the raspberry pie and the voice module, the two through serial communication to achieve speech recognition function.

具体地,语音模块处理识别的指令并将其发送到树莓派,树莓派根据接收到的指令执行不同的语句和不同的操作。例如,控制不同的继电器,从而实现打开和关闭不同的灯的操作。

[En]

Specifically, the speech module processes the recognized instructions and sends them to the raspberry pie, which executes different statements and different operations according to the received instructions. For example, to control different relays, so as to achieve the operation of turning on and off different lights.

一、语音识别模块介绍

本次开发用到的语音模块是淘宝购买的,型号为:YS-LDV7
①本模块实际原理为 1 片 STC11 单片机+1 片 LD3320 组合形成的一款语音识别模块, 语音识别部分已写好驱动程序, 用于只需要对 STC 单片机进行编程加入自己的识别语句和控制程序即可。模块内部通信为SPI接口, 对外通信为串口
②语音识别模块可以接STM32、ARDUINO、STC单片机、树莓派吗?
答:可以,该模块通过串口进行通信。只要有串口的设备就可以对接。具体的串口接收程序取决于您连接的硬件,需要您自己编写相应的串口接收程序。

[En]

Answer: yes, the module communicates through serial port. As long as the equipment with serial port can be docked. The specific serial port receiving program depends on the hardware you connect, and you need to write the corresponding serial port receiving program yourself.

③有很多人在STC-ISP中找不到STC11L08XE,它就在STC11L60XE系列中。先找到STC11L60XE系列然后选STC11L08XE。
④keil打开源代码的时候会报下面的错误,不用理会。

树莓派开发—语音识别功能

; 二、语音识别模块的二次开发

本次开发是在LD3320模块提供的源代码基础上进行的二次开发。在开发前,需要将源代码读一遍,理清源代码的逻辑关系,在此基础上才能进行代码的修改,进行二次开发。

源代码大概流程:

void ExtInt0Handler(void) interrupt 0

    nAsrRes = LD_GetResult();
    User_handle(nAsrRes);

main
    Led_test();
    MCU_init();
    LD_Reset();
    UartIni();

    while(1){
        RunASR();
            LD_AsrStart();
                LD_Init_ASR();
        LD_AsrAddFixed();
        LD_AsrRun();
            ProcessInt0()
    }
  1. LD_AsrAddFixed(); 向LD模块添加关键词 在LDchip.c
  2. 识别出结果后,把数据从语音模块发出来!void User_handle(uint8 dat) main

1. 重点代码修改部分

理顺了整个源代码的执行流程后,不难理解,可以进行二次开发的地方是代码修改部分:

[En]

After straightening out the execution process of the entire source code, it is not difficult to understand that the place where secondary development can be carried out is the part of code modification:

①语音识别口令

树莓派开发—语音识别功能
②识别码
树莓派开发—语音识别功能
③识别结果串口输出
树莓派开发—语音识别功能

; 2. 二次开发部分

理顺了整个源代码的执行流程后,不难理解,可以进行二次开发的地方是代码修改部分:

[En]

After straightening out the execution process of the entire source code, it is not difficult to understand that the place where secondary development can be carried out is the part of code modification:

①修改词条

树莓派开发—语音识别功能

②识别码修改

树莓派开发—语音识别功能
③修改语音识别串口输出结果
[En]

③ modifies the output result of speech recognition serial port

树莓派开发—语音识别功能

第三,语音模块与中继组相结合控制所有灯光

[En]

Third, the voice module combined with the relay group to control all lights

二次开发的过程,如上所述,包括各种家用灯具的声控密码。如客厅灯、卧室灯、餐厅灯和二楼灯。

[En]

The process of secondary development, as mentioned above, includes voice control passwords for all kinds of household lights. Such as living room lights, bedroom lights, dining room lights and second floor lights.

1. 接线

第一个是布线。如前所述,语音识别模块与外部模块如51、32、覆盆子馅饼等之间使用串口通信,串口通信是全双工的,但这里不需要同时连接两条通信线路。因为语音识别的结果被直接发送到树莓派,所以树莓派根据结果(即指令)执行不同的动作。但覆盆子馅饼不需要向语音模块发送数据,这意味着两者只需要传输单独的数据。

[En]

The first is the wiring. As mentioned earlier, serial communication is used between the speech recognition module and external modules such as 51, 32, raspberry pie, etc., and serial communication is full-duplex, but here, there is no need to connect both lines of the communication. because the result of speech recognition is sent directly to the raspberry pie, the raspberry pie performs different actions according to the result (that is, the instruction). But the raspberry pie does not need to send data to the voice module, which means that the two only need to transmit individual data.

树莓派开发—语音识别功能

; 2. 树莓派程序

结合树莓派外设开发基础—wiringPi库树莓派外设开发基础—串口通信。我们来编写树莓派程序,实现语音识别控制各种灯,也就是语音识别控制继电器组。

#include
#include
#include
#include
#include
#include

#define SWITCH1 21
#define SWITCH2 22
#define SWITCH3 23
#define SWITCH4 24

int initwiringPiSetup()
{
    if (wiringPiSetup() == -1)
    {
        printf("init error\n");
        exit(-1);
    }
}

void initPin()
{
    pinMode(SWITCH1,OUTPUT);
    pinMode(SWITCH2,OUTPUT);
    pinMode(SWITCH3,OUTPUT);
    pinMode(SWITCH4,OUTPUT);
    digitalWrite(SWITCH1,HIGH);
    digitalWrite(SWITCH2,HIGH);
    digitalWrite(SWITCH3,HIGH);
    digitalWrite(SWITCH4,HIGH);
}

int main()
{
    int fd;
    int n_read;
    char cmd[128]={'\0'};

    initwiringPiSetup();
    initPin();

    fd = serialOpen("/dev/ttyAMA0",9600);

    while(1)
    {
        n_read = read(fd,cmd,sizeof(cmd));
        if(strlen(cmd)==0)
        {
            printf("overtime...\n");
            continue;
        }
        if(strstr(cmd,"KKTD") != NULL)
        {
            digitalWrite(SWITCH1,LOW);
            printf("ke ting deng yi da kai\n");
        }
        if(strstr(cmd,"GKTD") != NULL)
        {
            digitalWrite(SWITCH1,HIGH);
            printf("ke ting deng yi guan bi\n");
        }
        if(strstr(cmd,"KWSD") != NULL)
        {
            digitalWrite(SWITCH2,LOW);
        }
        if(strstr(cmd,"GWSD") != NULL)
        {
            digitalWrite(SWITCH2,HIGH);
        }
        if(strstr(cmd,"KCTD") != NULL)
        {
            digitalWrite(SWITCH3,LOW);
        }
        if(strstr(cmd,"GCTD") != NULL)
        {
            digitalWrite(SWITCH3,HIGH);
        }
        if(strstr(cmd,"KELD") != NULL)
        {
            digitalWrite(SWITCH4,LOW);
        }
        if(strstr(cmd,"GELD") != NULL)
        {
            digitalWrite(SWITCH4,HIGH);
        }
        if(strstr(cmd,"KQBD") != NULL)
        {
            digitalWrite(SWITCH1,LOW);
            digitalWrite(SWITCH2,LOW);
            digitalWrite(SWITCH3,LOW);
            digitalWrite(SWITCH4,LOW);
        }
        if(strstr(cmd,"GQBD") != NULL)
        {
            digitalWrite(SWITCH1,HIGH);
            digitalWrite(SWITCH2,HIGH);
            digitalWrite(SWITCH3,HIGH);
            digitalWrite(SWITCH4,HIGH);
        }
        memset(cmd,'\0',sizeof(cmd)/sizeof(char));
    }
    return 0;
}

代码仍然比较简单,成功实现了语音指令操作接力。

[En]

The code is still relatively simple, and the voice instruction operation relay is successfully realized.

树莓派开发—语音识别功能

疑问:
语音模块通过串口传输过来的数据是”KKTD”、”GKTD”等这些字符串,但当我用语句:

        if(strcmp(cmd,"KKTD") == 0)
        {
            digitalWrite(SWITCH1,LOW);
            printf("ke ting deng yi da kai\n");
        }
        if(strcmp(cmd,"GKTD") == 0)
        {
            digitalWrite(SWITCH1,HIGH);
            printf("ke ting deng yi guan bi\n");
        }

使用strcmp()函数时,根本无法使继电器动作,也没有调试信息输出,显然语音模块通过串口传输失败。必须要用 strstr() 函数才行,不知道为什么。

Original: https://blog.csdn.net/little_rookie__/article/details/120125905
Author: 不加糖
Title: 树莓派开发—语音识别功能

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/512225/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球