Four-axis aircraft 1.8 Communication Protocol Drafting, Protocol Verification and flight control output verification

Source: Internet
Author: User

Original article. You are welcome to repost it.

In fact, the communication protocol has been developed over the last weekend. This week, the communication protocol is mainly parsed. Then, through the implementation of the communication protocol, the remote control and the transmission of flight control information are added, from Flight Control to remote control, then from remote control to computer, through MATLAB Real attitude information and motor output control information. This chapter describes the implementation process step by step.

1: Communication between remote control and flight control.

2: drafting of communication protocols.

3: Implementation and resolution of communication protocols.

4: remote control is used to control the flight control and the flight control posture is used to upload data through NRF.

First, the video that uses remote control to control the flight control and the flight control posture to upload data through NRF. Let's see the effect and explain it later;

The video is being released.

Video address

 

 

1: Communication between remote control and flight control

With the foundation of the ring buffering and communication in the previous chapter, we can verify the working effect of the ring buffering and communication.

With the ring buffer, it is relatively simple to send and receive data. To send data, we only need to push the data to the buffer, when receiving data, push the data into the buffer when the receiving is interrupted. We have a task dedicated to communication management to determine whether there is data in the buffer. If there is data in the sending buffer, we will read, send, and then pop the data, if there is data in the receiving buffer, read the data, parse the data according to the protocol, and then pop.

According to the above instructions, we send data on the remote control, and then the flight control receives the data and returns the data to the remote control .. The specific communication effect is as follows.

On the left is the serial output information of the remote control, and on the right is the output information of the flight control. In the figure, txds receives the ACK packet, txed sends the packet, rxed receives the packet, and trmax retries the set number of times before receiving the ACK packet. This means that the packet fails to be sent. There are still some failed sending, but there is no handling of failed sending, and there is no judgment .. We can try again in a few milliseconds after the pop fails ..

2: drafting of communication protocols

Communication protocols need to be simple, with as little data as possible to transmit as much information as possible...

First, we classify the protocols into three categories: control, query, and active reporting. Therefore, two bytes are needed, and six more are occupied after two bytes, there are 64 combinations, which should be enough for us to use now, and data will be transmitted according to various commands later. The Protocol is roughly as follows:

 

According to the protocol, we define the following struct:

1 typedef Union 2 {3 u8 mode; // flight mode parameter 4 com_att_t ATT; // pose Information 5 u32 height; // height parameter 6 com_pid_t PID; // PID information 7 u8 retpara; // return information 8 com_sensor_t sensor; // sensor data 9 u8 battery; // battery power 10} comm_para_tu; 11 12 typedef struct13 {14 u8 command: 6; // 0 ~ Command 15 u8 comtype: 2; // 6 ~ 7-bit command type 16} order_t; 17 18 19 typedef struct20 {21 u8 Len; // data length 22 order_t order; // command type 23 comm_para_tu para; // parameter 24} comm_data_ts;

Comm_data_ts is the struct used for communication. According to the Protocol, there are data lengths, command types, and corresponding commands, followed by parameters. To send data, you only need to assign a value to this struct. After receiving the data, you only need to find the corresponding command according to the order of the body, and then read the data according to the corresponding parameters, in this way, when operating on the upper layer, we do not need to worry about the bytes of the communication protocol, but only need to operate on the struct.

 

3: Implementation and Analysis of communication protocols

To facilitate code implementation, we have written a parsing function for each command. So how did we find this function? We create a struct with function pointers for processing functions corresponding to commands. We only need to perform command matching. After matching, we will call the corresponding function pointer to call the corresponding function, this completes the parsing.

The struct is as follows:

1/* command callback structure */2 typedef struct 3 {4 void (* comm_callback) (comm_data_ts * MSG); 5 u8 command; 6} subcomm_fun; 7 8 9 typedef struct10 {11 const subcomm_fun * Subcomm; // point to sub-command 12 u8 comtype; // command type 13 u8 Len; // number of sub-commands, optimized search time 14} comm_fun;

Because our commands are divided into two layers, we first parse the command type, then search for the next layer of commands, and then run the corresponding function pointer. For example, the initialization of the above struct is as follows:

1 // query class command corresponds to Function Definition 2 // note, be sure to modify the fly_query_fun_len macro definition 3 const subcomm_fun fly_query_fun [] = 4 {5 {6 fly_query_sensor, 7 fly_query_sensor 8}, 9 10 {11 seconds, 12 seconds}, 14 15 {16 fly_query_system_sta, 17 fly_query_system_sta18}, 19 20 {21 fly_query_battery, 22 fly_query_battery23}, 24 }; 25 # define fly_query_fun_len 426 27 28 // command type defines 29 subcommands // note, be sure to modify comm_fun_len macro definition 30 const comm_fun fly_fun [] = 31 {32 {33 fly_ctrl_fun, 34 fly_ctrl, 35 rows}, 37 38 {39 fly_query_fun, 40 fly_query, 41 fly_query_fun_len42 }, 43 44 {45 fly_report_fun, 46 fly_report, 47 fly_report_fun_len48}, 49 };

After the above body is initialized, We will parse it according to the command. The parsing code is as follows:

1 void comm_decode (comm_data_ts * MSG) 2 {3 u8 I, K; 4 5 for (I = 0; I <comm_fun_len; I ++) 6 {7/* query message command */8 If (MSG-> order. comtype = fly_fun [I]. comtype) 9 {10 for (k = 0; k <fly_fun [I]. len; k ++) 11 {12 if (fly_fun [I]. subcomm [K]. command = MSG-> order. command) 13 {14 fly_fun [I]. subcomm [K]. comm_callback (MSG); 15 break; 16} 17} 18 break; 19} 20 21} 22 23}

In this way, code reuse is very high, and it is very easy to add and delete commands. However, you can change the command type to the enumeration type to avoid errors.

The resolution result is as follows:

On the left is the debug information of NRF of the remote control, and on the right is the debug information parsed after the remote control receives the data. The remote control uses the pass button of the handle to send different commands for testing. It runs well through actual observation.

4: remote control is used to control the flight control and the flight control attitude is used to upload data through NRF

All of the above functions are combined to achieve wireless attitude collection through protocols.

First, describe our operation method: The flight control regularly sends Sensor Information and posture information to the remote control, remotely sends the information to MATLAB through the serial port, and implements real-time reality. The information we uploaded here adds the control information of the motor, including the total throttling and the separate throttling of each motor. Through this, we can check whether the operation logic of the flight control on the propeller is correct. At the same time, remote control can send remote control commands to control the target attitude. Figure:

The column chart 12345 on the left contains the values of moto1, moto2, moto3, moto4, and throttle.

The motor position is shown as follows:

 

The word is ugly. If you do not understand it at the time, you can go to the following website to see that the motor's orientation and angle are somewhat different, but the principle is the same.

Web: http://bbs.21ic.com/icview-605405-1-1.html

At last, the online test video can be found at the top. After the test, the motor output logic is normal and safe .. After that, add the power control and adjust the PID. You should be able to fly ..

Four-axis aircraft 1.8 Communication Protocol Drafting, Protocol Verification and flight control output verification

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.