6 months ago (03-27)  Technical application |   First to comment  16 
post score 0 times, average 0.0

First, the command packet protocol design

In the remote control smart car system, the remote client controls the smart car through command communication.In the design of this paper, only the speed direction control instruction is involved. In order to consider the scalability of the system: because there may be more types of instructions for subsequent requirements, this article has designed the instruction code for the instruction data. The instruction consists of the instruction code and the instruction data area; consider To the client and the server to better handle the instruction, the instruction word length is 32 bits, and the int data type word length is equal to the int data processing when the server and the client socket socket to send and receive data .In studies similar to this design, char characters are often used to control the direction. I think it is inadequate. Because a char type can quantify and access data, it can save network traffic and ensure real-time performance.  

Instruction code (4bit) Command data area (28bit)

  When the command code is 1, the speed direction control command of the sending vehicle is indicated, and the command data package is a schematic diagram:  

0 3 4 11 12 13 20 twenty one 31
Instruction code (4bit) Servo angle (8bit) Direction (1bit) Speed ​​(8bit) Reserved field (11bit)

  1. Servo angle: 8bit, control steering angle (A), value range 0-180 A=90 degrees, servo is in the middle position; A<90, servo is right; A>90 hours The steering gear is to the left. 2. Direction: 1 bit, control car forward and backward (D), value 0 or 1 D = 0, which means that the car back; D = 1, that the car forward. 3. Speed: 8bit, the traveling speed of the control car (S) The range of values ​​is the data range that the Arduino PWM pins can write, which is 0-255. 4. Reserved fields for the expansion of possible future requirements

Second, the instruction packet packet and unpack

According to the protocol design of the instruction in the previous section, we can encapsulate and unpack the int data by shifting and logical operations. The packet of the instruction is to store the corresponding instruction data in the corresponding position of the 32-bit data structure. The following is an example of the packet and de-envelope process with instruction content codec=1, angle a=40, direction d=1, and speed v=150. (For readability, the following The calculated value is expressed using 32-bit binary):

  1. Moves the designated code c=1 left to 28 bits:

c<<28 = 0001 0000 0000 0000 0000 0000 0000 0000

  1. The binary (101000) representation of a is left shifted by 20 bits:

a<<20 = 0000 0010 1010 0000 0000 0000 0000 0000

  1. Binary (1) of d is left shifted by 19 bits:

d<<19 = 0000 0000 0000 1000 0000 0000 0000 0000

  1. The binary (10010110) of v is left shifted by 11 bits:

d<<19 = 0000 0000 0000 0100 1011 0000 0000 0000

  1. The final instruction is obtained by ORing the above calculated temporary value:

Cmd = 0001 0010 1010 11001011 0000 0000 0000 In summary, we get the command packet calculation formula: cmd = (c<<28) | (a<<20) | (d<<19) | (v<<11) Equivalent to ((c<<17)|(a<<9)|(d<<8)| v) << 11 The unpacking of the instruction is to fetch data from the corresponding position of the received instruction data (int). In the following, the data is unpacked by taking the command cmd = 313307136 (binary: 0001 0010 1010 11001011 0000 0000 0000) as an example:

  1. Move the cmd to the right by 11 bits and remove the reserved field. At this time, the last bit of the speed data area (8 bits) is moved to the last bit, and the result is assigned to cmd. The last 8 bits shifted by the “extract” operation are the result of the AND operation. speed:

Cmd >> 11 = 0001 0010 1010 11001011 0 v = cmd | (1111 1111) = 1001 0110

  1. Cmd shifts 8 bits to the right and removes the speed field. At this time, the last bit of the direction data area (1 bit) is moved to the last bit, and the result is assigned to cmd. The last bit at the end of the shift after the shift operation is “taken out” by the AND operation. direction:

Cmd >> 8 = 0001 0010 1010 1 d = cmd | (1) = 1

  1. Cmd shifts 8 bits to the right and removes the speed field. At this time, the last bit of the direction data area (1 bit) is moved to the last bit, the result is assigned to cmd, and the last 8 bits at the end of the shifted “tuck” by the AND operation are angle:

Cmd >> 1 = 0001 0010 1010 d = cmd | (1111 1111) = 0010 1010 To sum up, we get the instruction to solve the package: cmd = cmd >> 11; v = cmd & 0xFF; d = (cmd > > 8) & 1; a = (cmd >> 9) & 0xFF;

Third, the instruction packet package, unpack the program implementation

Define the "vector" structure of the car speed direction:

  According to the packet calculation formula above, you can use the following C++ code to implement the packet function and pass in the auto vector structure to return int data:

Implement the unwrap function to pass int data to the carVector structure pointer

IV. Communication instruction packet unpacking test

Write the following program to test the experiment. This program first constructs a CarVector with a speed of 40, retreats, and a declination angle of 150. After unpacking it, it is unpacked, and the parsed speed, direction and declination are the same as those of the CarVector. , indicating that the test passed.

 

 

本文已在版权印备案,受著作权法律保护,未经允许不得转载!如需转载请联系作者或访问版权印获取授权,如果你觉得这篇文章对你有用,可以点击下面的“赞助作者”打赏作者!

转载注明原文出处:Baiyuan's Blog>>http://wangbaiyuan.cn/en/4-pcduino-communication-instruction-design-platform-of-smart-car-2.html

Post comment

Style

No Comment

登录

Forget password?

您也可以使用第三方帐号快捷登录

切换登录

注册

TW