esp32解析雷达的串口数据,雷达模组R24AVD1

时间:2024-04-20 12:14:39

#使用雷达模组R24AVD1,波特率9600

详情请阅读代码

/* UART Echo Example

   This example code is in the Public Domain (or CC0 licensed, at your option.)

   Unless required by applicable law or agreed to in writing, this
   software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
   CONDITIONS OF ANY KIND, either express or implied.
*/
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/uart.h"
#include "driver/gpio.h"
#include "sdkconfig.h"
#include "esp_log.h"

/**
 * This is an example which echos any data it receives on configured UART back to the sender,
 * with hardware flow control turned off. It does not use UART driver event queue.
 *
 * - Port: configured UART
 * - Receive (Rx) buffer: on
 * - Transmit (Tx) buffer: off
 * - Flow control: off
 * - Event queue: off
 * - Pin assignment: see defines below (See Kconfig)
 */

#define ECHO_TEST_TXD (35)
#define ECHO_TEST_RXD (36)
#define ECHO_TEST_RTS (UART_PIN_NO_CHANGE)
#define ECHO_TEST_CTS (UART_PIN_NO_CHANGE)

#define ECHO_UART_PORT_NUM      (UART_NUM_1)
#define ECHO_UART_BAUD_RATE     (9600)
#define ECHO_TASK_STACK_SIZE    (CONFIG_EXAMPLE_TASK_STACK_SIZE)

static const char *TAG = "UART TEST";

#define BUF_SIZE (1024)

// typedef struct
// {
//     uint8_t *u_rdata;
//     uint16_t u_rdata_len;
// } msg_uart_rdata_t; // 串口队列消息缓存

typedef enum{
    Deviceid=0x01,              //设备ID
    Softversion=0x02,           //软件版本
    Hardversion=0x03,           //硬件版本
    Protocolversion=0x04        //协议版本
}ID_QUERY_ADDRESS;

typedef enum{
    Environmental_state=0x05,   //环境状态
    Sign_parameter=0x06         //体征参数
}Radar_INFO_ADDRESS;

typedef enum{
    Threshold_gear=0x0c,        //阈值档位
    Scene_setting=0x10,         //场景设置
    Forced_entry_unmannd=0x12  //强制进入无人档位
}System_PARAMS_ADDRESS;

typedef enum{
    ID_QUERY=0x01,//Identification query
    Radar_Info=0x03,//Radar information
    System_Parms=0x04,//System parameters
}read_cmd_func;

typedef enum
{
    Read_Cmd=0x01,
    Write_Cmd=0x02,
    Passive_Report=0x03,
    Actively_Report=0x04
}Command_Function_Code;


//write_cmd_func
typedef enum{
    Threshold_gear_low=0x01,
    Threshold_gear_middle=0x02,
    Threshold_gear_high=0x03,
}Threshold_gear_value;

typedef enum{
    Scene_default=0x00,
    Scene_area_dection=0x01,
    Scene_Restroom,
    Scene_Bedroom,
    Scene_livingroom,
    Scene_Office,
    Scene_hotel
}Scene_setting_value;

typedef enum{
    Unused_forced=0x00,//不使用强制进入无人功能
    unmannd_time_10=0x01,//10s
    unmannd_time_30,//30s
    unmannd_time_60,//1min
    unmannd_time_120,//2min
    unmannd_time_300,//5min
    unmannd_time_600,//10min
    unmannd_time_1800,//30min
    unmannd_time_3600//60min
}Forced_unmannd_value;

typedef union
{
    unsigned char Byte[4];
    float Float;
}Float_Byte;

extern void parsing_recv_data(unsigned char *data,unsigned char len);

extern unsigned short int CalculateCrc16(unsigned char *lpuc_Frame, unsigned short int lus_Len);

static void write_cmd(void *arg){
    unsigned char data_len = 8;
    uint8_t cmd[8]={0};
    while(1)
    {
        cmd[0]= 0x55;//header
        cmd[1]= 0x07;//data len of hi
        cmd[2]= 0x00;//data len of lo
        cmd[3]= Read_Cmd;//0x02
        cmd[4]= Radar_Info;//0x04
        cmd[5]= Environmental_state;//0x10
        unsigned short int checksum = CalculateCrc16(cmd,data_len-2);
        cmd[data_len-2] = checksum >> 8;
        cmd[data_len-1] = checksum & 0xff;
        uart_write_bytes(ECHO_UART_PORT_NUM, (const char*)cmd, data_len);
        vTaskDelay(1000 / portTICK_PERIOD_MS);
         vTaskDelay(1000 / portTICK_PERIOD_MS);
          vTaskDelay(1000 / portTICK_PERIOD_MS);
        ESP_LOGI(TAG, "data,%x,Hi:0x%x,Lo:0x%x",checksum,cmd[data_len-2],cmd[data_len-1]);
    }
}

static void echo_task(void *arg)
{
    /* Configure parameters of an UART driver,
     * communication pins and install the driver */
    uart_config_t uart_config = {
        .baud_rate = ECHO_UART_BAUD_RATE,
        .data_bits = UART_DATA_8_BITS,
        .parity    = UART_PARITY_DISABLE,
        .stop_bits = UART_STOP_BITS_1,
        .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
        .source_clk = UART_SCLK_DEFAULT,
    };
    int intr_alloc_flags = 0;

#if CONFIG_UART_ISR_IN_IRAM
    intr_alloc_flags = ESP_INTR_FLAG_IRAM;
#endif

    ESP_ERROR_CHECK(uart_driver_install(ECHO_UART_PORT_NUM, BUF_SIZE * 2, 0, 0, NULL, intr_alloc_flags));
    ESP_ERROR_CHECK(uart_param_config(ECHO_UART_PORT_NUM, &uart_config));
    ESP_ERROR_CHECK(uart_set_pin(ECHO_UART_PORT_NUM, ECHO_TEST_TXD, ECHO_TEST_RXD, ECHO_TEST_RTS, ECHO_TEST_CTS));

    // Configure a temporary buffer for the incoming data

    uint8_t *data = (uint8_t *) malloc(BUF_SIZE);
 
    while (1) 
    {
        
        // Read data from the UART
        int len = uart_read_bytes(ECHO_UART_PORT_NUM, data, (BUF_SIZE - 1), 20 / portTICK_PERIOD_MS);
        // Write data back to the UART
        //uart_write_bytes(ECHO_UART_PORT_NUM, (const char *) data, len);
        if (len) {
            data[len] = '\0';
            //ESP_LOGI(TAG, "Recv str: %s,len= 0x%x", (char *) data,len);
            // for(int i = 0; i < len; i++) {
            //     ESP_LOGI(TAG, "data,0x%x", data[i]);
            // }
            
            if(data[0] == 0x55){//header
                if(len > 3){
                    unsigned char func_type = data[3];
                    switch (func_type)
                    {
                    case 0x01://读命令
                        /* code */
                        break;
                    case 0x02://写命令
                        break;
                    case 0x03://被动上报
                        break;
                    case 0x04://主动上报
                    if(len > 4)
                    {
                        unsigned char state = data[4];
                        switch (state)
                        {
                        case 0x01:
                            //ESP_LOGI(TAG, "上报模块标识 ---> ");
                            break;
                        case 0x03:
                            //ESP_LOGI(TAG, "上报雷达信息 ---> ");
                            break;
                        case 0x05:
                            //ESP_LOGI(TAG, "上报其他信息 ---> ");
                            break;
                        default:
                            break;
                        }
                    }
                    if(len > 5 && data[4] == 0x03)
                    {
                        if(data[5] == 0x05){//环境状态
                        //ESP_LOGI(TAG, "环境状态 --->");
                            if(len > 8){
                                if(data[6] == 0x00 && data[7] == 0xFF && data[8] == 0xFF){//无人状态
                                    ESP_LOGI(TAG, "无人状态  ");
                                }
                                if(data[6] == 0x01 && data[7] == 0x00 && data[8] == 0xFF){//有人静止
                                    ESP_LOGI(TAG, "有人静止  ");
                                }
                                if(data[6] == 0x01 && data[7] == 0x01 && data[8] == 0x01){//有人运动
                                    ESP_LOGI(TAG, "有人运动  ");
                                }
                            }
                        }
                        if(data[5] == 0x06){//运动体征参数
                            ESP_LOGI(TAG, "运动体征参数<--->");
                            if(len > 9){
                            Float_Byte fb;
                            fb.Byte[0] = data[6];
                            fb.Byte[1] = data[7];
                            fb.Byte[2] = data[8];
                            fb.Byte[3] = data[9];
                            ESP_LOGI(TAG, " param: %f \r\n",fb.Float);
                            }
                        }
                        if(data[5] == 0x07){//接近远离状态
                            //ESP_LOGI(TAG, "接近远离状态 --->");
                            if(len > 8){
                                unsigned char state = data[8];
                                switch (state)
                                {
                                case 0x01:
                                    ESP_LOGI(TAG, "无");
                                    break;
                                case 0x02:
                                    ESP_LOGI(TAG, "接近状态 ");
                                    break;
                                case 0x03:
                                    ESP_LOGI(TAG, "远离状态 ");
                                    break;
                                case 0x04:
                                    ESP_LOGI(TAG, "持续接近 ");
                                    break;
                                case 0x05:
                                    ESP_LOGI(TAG, "持续远离 ");
                                    break;
                                default:
                                    break;
                                }
                            }
                        }
                    }
                    break;
                    default:
                        ESP_LOGI(TAG, "parsing,unkown type 0x%x", func_type);
                        break;
                    }
                }
            }
            
        }
    }
    free(data);
}

void app_main(void)
{
    xTaskCreate(echo_task, "uart_echo_task", ECHO_TASK_STACK_SIZE, NULL, 10, NULL);
    //xTaskCreate(write_cmd, "write_cmd", 2048, NULL, 10, NULL);
}

以及校验和函数加进来

#include <stdlib.h>
#include "esp_log.h"

static const char *TAG = "protocol";

const unsigned char cuc_CRCHi[256]={
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 
0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40};

const unsigned char cuc_CRCLo[256]=
{
0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06, 0x07, 0xC7,
0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD, 0x0F, 0xCF, 0xCE, 0x0E,
0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09, 0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9,
0x1B, 0xDB, 0xDA, 0x1A, 0x1E, 0xDE, 0xDF, 0x1F, 0xDD, 0x1D, 0x1C, 0xDC,
0x14, 0xD4, 0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3,
0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3, 0xF2, 0x32, 
0x36, 0xF6, 0xF7, 0x37, 0xF5, 0x35, 0x34, 0xF4, 0x3C, 0xFC, 0xFD, 0x3D,
0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A, 0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38, 
0x28, 0xE8, 0xE9, 0x29, 0xEB, 0x2B, 0x2A, 0xEA, 0xEE, 0x2E, 0x2F, 0xEF, 
0x2D, 0xED, 0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26, 
0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60, 0x61, 0xA1, 
0x63, 0xA3, 0xA2, 0x62, 0x66, 0xA6, 0xA7, 0x67, 0xA5, 0x65, 0x64, 0xA4, 
0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F, 0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB, 
0x69, 0xA9, 0xA8, 0x68, 0x78, 0xB8, 0xB9, 0x79, 0xBB, 0x7B, 0x7A, 0xBA, 
0xBE, 0x7E, 0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5, 
0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71, 0x70, 0xB0,
0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92, 0x96, 0x56, 0x57, 0x97,
0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C, 0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E,
0x5A, 0x9A, 0x9B, 0x5B, 0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89,
0x4B, 0x8B, 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C,
0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, 0x43, 0x83,
0x41, 0x81, 0x80, 0x40};  

static unsigned short int us_CalculateCrc16(unsigned char *lpuc_Frame, unsigned short int lus_Len)
{
    unsigned char luc_CRCHi = 0xFF;
    unsigned char luc_CRCLo = 0xFF;
    int li_Index=0;
    
    while(lus_Len--)
    {
        li_Index = luc_CRCLo ^ *( lpuc_Frame++);
        luc_CRCLo = ( luc_CRCHi ^ cuc_CRCHi[li_Index]);
        luc_CRCHi = cuc_CRCLo[li_Index];
    }
    return (unsigned short int )(luc_CRCLo << 8 | luc_CRCHi);
}


unsigned short int CalculateCrc16(unsigned char *lpuc_Frame, unsigned short int lus_Len){
    return us_CalculateCrc16(lpuc_Frame, lus_Len);
}
void Actively_report_info(unsigned char *data,unsigned char len){
    if(len > 4){
        if(data[4] == 0x01){//上报模块标识
            ESP_LOGI(TAG, "上报模块标识 type ");
        }
        if(data[4] == 0x03){//上报雷达信息
            ESP_LOGI(TAG, "- type ");
        }
        if(data[4] == 0x05){//上报其他信息
            ESP_LOGI(TAG, "上报其他信息 type ");
        }
    }
}

void parsing_recv_data(unsigned char *data,unsigned char len)
{
    if(len > 0){
        if(data[0] == 0x55){//header
            if(len > 3){
                unsigned char func_type = data[3];
                switch (func_type)
                {
                case 0x01://读命令
                    /* code */
                    break;
                case 0x02://写命令
                    break;
                case 0x03://被动上报
                    break;
                case 0x04://主动上报
                    Actively_report_info(data,len);
                    break;
                default:
                    ESP_LOGI(TAG, "parsing,unkown type 0x%x", func_type);
                    break;
                }
            }
        }
    }
}

可及时调整串口输出频率