4704 views|3 replies

291

Posts

5

Resources
The OP
 

Supplementary chapter of motor PID control - Introduction to the serial port protocol of the Wildfire host computer [Copy link]

0 Introduction

The series of articles on motor PID control introduced earlier ( Advanced Motor Control 3 - PID Cascade Control ) have attracted the attention of many electronics enthusiasts, but also received some feedback on serial communication. The previous series of articles did not focus on the details of serial port software usage and the specific format of the communication protocol. This article will supplement the serial port protocol of the Wildfire PID Debugging Assistant.

The user interface of the Wildfire PID Debug Assistant is as follows. It is related to the serial communication protocol and is mainly divided into three parts:

  • Parameter area: including data channel selection, PID parameter setting and display, target value, cycle value setting and display

  • Start/Stop Zone: Controls the start and stop of the motor and the reset of the program

  • Data curve area: Receives data sent by the board and draws speed curves or position curves, etc.

Before introducing the serial port protocol, it is recommended to install a virtual serial port driver software to virtualize two serial ports on your computer to facilitate the analysis of serial port data.

For example, we generate two virtual serial ports on the computer. These two serial ports can be regarded as connected by a line . For example, we use WildFire PID Assistant and another serial port assistant software to connect these two virtual serial ports respectively . When WildFire PID Assistant sends data (such as pressing the start button), the serial port assistant software on the other end can receive and display the data sent by WildFire PID Assistant. In this way, we can analyze the data format of the data sent by WildFire PID Assistant.

1 Introduction to PID Debug Assistant Serial Port Protocol

Let’s first take a look at the serial port protocol data format of the WildFire PID Debug Assistant.

For the definition of serial port protocol, please refer to the introduction of Wildfire Forum: https://www.firebbs.cn/forum.php?mod=viewthread&tid=29923&extra=page%3D1

1.1 Instruction Packet Format

Serial port data is sent in packets, and the data format of each packet is as follows:

Number of bytes name content
4 bytes Baotou 0x59485A53
1bytes Data Channel 0xXX
4 bytes Packet length 0xXXXXXXXX
1bytes instruction 0xXX
1bytes Parameter 1 0xXX
... ... ...
1bytes Parameter n 0xXX
1bytes Checksum 0xXX

illustrate:

  1. All multi-byte low byte first

  2. The packet header is fixed to four bytes 0x59485A53

  3. Channel addresses 1 to 5 correspond to CH1 to CH5 on the software, CH1 is 0x01 , CH2 is 0x02

  4. The packet length is the length of all data from the packet header to the checksum

  5. The instruction is the corresponding function code

  6. Parameters are added when the command requires parameters

  7. Checksum mode - 8 bits

1.2 Instruction Classification

In the above data format, the "instruction" field should be paid special attention to, which indicates the specific meaning of this packet of data.

In addition, serial port data includes downlink data and uplink data . Downlink data means that the WildFire PID Assistant sends serial port data to the board according to the protocol packet format, and uplink data means that the board sends serial port data to the WildFire PID Assistant according to the protocol packet format.

1.2.1 Sending Data

Wildfire PID Assistant sends the set data or instructions to the board

Function instruction parameter
PID 0x10 3 floats
Target value 0x11 1 int
start up 0x12 none
stop 0x13 none
Reset 0x14 none
cycle 0x15 1 uint

1.2.2 Upload Data

The board uploads data or commands to the WildFire PID Assistant

Function instruction parameter
Target value 0x01 1 int
Actual value 0x02 1 int
PID 0x03 3 floats
start up 0x04 none
stop 0x05 none
cycle 0x06 1 uint

2 Data measurement and analysis

2.1 Measured and distributed data

To send test data, you can use only one computer for testing. Through the virtual serial port, connect the WildFire PID Assistant and another serial port assistant software respectively, send data to the other serial port assistant through the WildFire PID Assistant , display the received data in Hex format, and observe the data format.

2.1.1 Sending Start (0x12) Stop (0x13) Reset (0x14)

These three are instructions, there is no data parameter, the data length is 0x0B, which is 11 bytes

Baotou aisle Packet length instruction check
start up 53 5A 48 59 01 0B 00 00 00 12 6C
stop 53 5A 48 59 01 0B 00 00 00 13 6D
Reset 53 5A 48 59 01 0B 00 00 00 14 6E

2.1.2 Send target value (0x11) cycle (0x15)

These two instructions carry 1 byte of data, and the data length is 0x0F, which is 15 bytes.

Baotou aisle Packet length instruction Parameters (data) check
Target value 53 5A 48 59 01 0F 00 00 00 11 C8 00 00 00 37
cycle 53 5A 48 59 01 0F 00 00 00 15 64 00 00 00 D7

2.1.3 Send PID (0x10)

This instruction has 12 bytes of data (3 floats) and the data length is 0x17, which is 23 bytes.

Baotou aisle Packet length instruction Parameters (data) check
53 5A 48 59 01 17 00 00 00 10 9A 99 99 3F 00 00 20 40 9A 99 99 3E EB

Note: The PID data here is of float type and needs to be split into 4-byte Hex format when sending.

2.2 Measured Upload Data

To upload the test data, you need to download the program to the board. The board is connected to any serial port debugging software on the computer through the software to display the received data in Hex format and analyze the data format.

2.2.1 Upload start (0x04)/stop (0x05) status

Baotou aisle Packet length instruction check
start up 53 5A 48 59 01 0B 00 00 00 04 5E
stop 53 5A 48 59 01 0B 00 00 00 05 5F

2.2.2 Upload target value (0x01)/periodic value (0x06)

Baotou aisle Packet length instruction Parameters (data) check
Target value 53 5A 48 59 01 0F 00 00 00 01 70 3A 00 00 09
cycle 53 5A 48 59 01 0F 00 00 00 06 xx xx xx xx xx

2.2.3 Upload PID parameters (0x03)

Baotou aisle Packet length instruction Parameters (data) check
53 5A 48 59 01 17 00 00 00 03 CD CC 4C 3D 00 00 00 00 00 00 00 00 8B

2.2.4 Upload actual value (0x02)

Here, the motor speed and position values are uploaded to the WildFire PID Assistant to display the position curve or speed curve.

Baotou aisle Packet length instruction Parameters (data) check
53 5A 48 59 01 0F 00 00 00 02 2D 00 00 00 8D

3 Serial port transceiver program

Let's take a look at the main code related to serial port data sending and receiving:

3.1 Send

Group data by format:

/**
 * [url=home.php?mod=space&uid=159083]@brief[/url] 设置上位机的值
 * @param cmd:命令
 * @param ch: 曲线通道
 * @param data:参数指针
 * @param num:参数个数
 * @retval 无
 */
void set_computer_value(uint8_t cmd, uint8_t ch, void *data, uint8_t num)
{
    static packet_head_t set_packet;

    uint8_t sum = 0;  // 校验和
    num *= 4;      // 一个参数 4 个字节

    set_packet.head = FRAME_HEADER;   // 包头 0x59485A53
    set_packet.ch  = ch;       // 设置通道
    set_packet.len = 0x0B + num;   // 包长
    set_packet.cmd = cmd;       // 设置命令

    sum = check_sum(0, (uint8_t *)&set_packet, sizeof(set_packet));    // 计算包头校验和
    sum = check_sum(sum, (uint8_t *)data, num);              // 计算参数校验和

    usart1_send((uint8_t *)&set_packet, sizeof(set_packet));  // 发送数据头
    usart1_send((uint8_t *)data, num);             // 发送参数
    usart1_send((uint8_t *)&sum, sizeof(sum));         // 发送校验和
}

Send a packet of data to the host computer one by one

void usart1_send(u8*data, u8 len)
{
    u8 i;
    for(i=0;i<len;i++)
    {
        while(USART_GetFlagStatus(USART1,USART_FLAG_TC)==RESET); 
        USART_SendData(USART1,data[i]);  
    }
}

3.2 Receiving

Use interrupt to receive serial port data:

//=======================================
//串口1中断服务程序
//=======================================
uint8_t Recv1[128]={0};//串口接收缓存
u8 rx_cnt=0;//接收数据个数计数变量
int sizecopy=128;

void USART1_IRQHandler(void)             
{
    uint8_t data;//接收数据暂存变量
    uint8_t bufcopy[128];//最多只取前64个数据

    if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //接收中断
    {
        data = USART_ReceiveData(USART1);              
        Recv1[rx_cnt++]=data;//接收的数据存入接收数组 
        USART_ClearITPendingBit(USART1,USART_IT_RXNE);
    } 
    
    if(USART_GetITStatus(USART1, USART_IT_IDLE) != RESET)//空闲中断
    {
        data = USART1->SR;//串口空闲中断的中断标志只能通过先读SR寄存器,再读DR寄存器清除!
        data = USART1->DR;
        
        //清空本地接收数组
        memset(bufcopy,0,sizecopy);
        memcpy(bufcopy,Recv1,rx_cnt);//有几个复制几个
        protocol_data_recv(bufcopy, rx_cnt);
        memset(Recv1,0,sizecopy);
        rx_cnt=0;
    }
} 

The received data is saved in the serial port receive buffer Recv1 array, and then the data can be parsed according to the protocol format.

4 Conclusion

This article introduces the serial port protocol format of the WildFire PID Assistant, including the data format for sending and uploading. It also compares and analyzes the Hex format data of the serial port data obtained and the definition of the protocol, so that the format of the serial port data can be better understood.

If the board does not respond to the command sent by the Wildfire PID Assistant, or the board cannot display the curve when uploading PID data, you should first check whether the format of the serial port data is correct . If not, you should check whether the serial port receiving and sending functions in your program are written correctly. Only when the serial port data conforms to the prescribed protocol format can data communication be carried out correctly.

If the serial port data format is normal but the motor still does not rotate, you need to check whether the hardware wiring is normal and whether you need to modify the parameters in the program according to your motor encoder parameters .

Latest reply

Just in use, thanks for sharing   Details Published on 2022-3-24 19:46

赞赏

1

查看全部赞赏

 
 

321

Posts

1

Resources
2
 
Very good explanation, very detailed, collected
 
 
 

15

Posts

0

Resources
3
 

What a detailed article. I quickly glanced over it and then read it carefully.

 
 
 

174

Posts

0

Resources
4
 

Just in use, thanks for sharing

 
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list