简体   繁体   中英

How do I read gyro information from CleanFlight using MSP?

使用 Arduino,我将如何使用 MultiWii 串行协议从飞行控制器中的陀螺仪获取姿态?

The following is based on just getting gyro attitude information, although it includes some information about using MSP in general. The example code referenced can be foundhere . The most important part of this that I couldn't find anywhere else (a friend had figured it out and let me in on the secret) is the Data section below.


MultiWii Serial Protocol

Firstly, let's look at how MSP works. I found this link (alternative link here ) very useful in understanding this, but I'll summarize here. There are three types of message that can be sent.

  1. Command -- a message sent to the flight controller which has some information to be sent.
  2. Request -- a message sent to the flight controller asking for some information to be returned.
  3. Response -- a message sent by the flight controller with information responding to a request.

MSP messages have a specific structure. They have a header, size, type, data and checksum, in that order.

I found this diagram from here very helpful: 命令结构

Header

The header is three bytes and contains the message start characters "$M" and a character showing which direction the message is going. "<" denotes going to the flight controller (command and request), ">" denotes coming from the flight controller (response).

Size

The fourth byte is the length (in bytes) of the data section. For example, if the data section had three INT 16 variables then the size byte would be 6.

Type

The type byte specifies what information is being sent in the message. You can find a list of types here . An example of this would be MSP_ATTITUDE which has type number of 108.

Data

The data is where all the information is sent. Request messages have no data in them. Commands and responses do, because they contain information. The types of data returned can again be found here .

The difficult part of the data section is that the bytes are reversed in order, and this is extremely poorly documented. So, for example, if I get the following two bytes in this order:

10011010
01001111

You would think that should become 10011010 01001111 but in fact does not. It would actually become 01001111 10011010 .

In the example code, this operation is done as follows:

int16_t roll;
byte c;                       // The current byte we read in.
c = mspSerial.read();         // The first sent byte of the number.
roll = c;                     // Put the first sent byte into the second byte of the int 16.
c = mspSerial.read();         // The second sent byte of the number.
roll <<= 8;                   // Move the first sent byte into the first byte of the int16.
roll += c;                    // Put the second sent byte into the second byte of the int 16.
roll = (roll & 0xFF00) >> 8 | (roll & 0x00FF) << 8; // Reverse the order of bytes in the int 16.

Checksum

The final byte of an MSP message is the checksum. "The checksum is the XOR of size, type and payload bytes". For a request message the checksum is equal to the type.

Example

An example response message for an "MSP_ATTITUDE" request would be as follows:

00100100 -- '$' - Byte 1 of the header.
01001101 -- 'M' - Byte 2 of the header.
00111110 -- '>' - Byte 3 of the header.
00000110 -- '6' - The size byte.
01101100 -- '108' - The type number corresponding to "MSP_ATTITUDE".
11100010 -- The first sent byte of the roll INT16.
11111111 -- The second sent byte of the roll INT16.
00010010 -- The first sent byte of the pitch INT16.
00000000 -- The second sent byte of the pitch INT16.
11000010 -- The first sent byte of the yaw INT16.
00000000 -- The second sent byte of the yaw INT16.
10100111 -- The checksum byte.

Roll would become: 11111111 11100010 = -30 . Pitch would become: 00000000 00010010 = 18 . Yaw would become: 11000010 00000000 = 194 .

As documented here the roll and pitch are in units of 1/10th of a degree. So the final values would be as follows:

Roll = -3.0
Pitch = 1.8
Yaw = 194

Setting up CleanFlight

To get these values the flight controller must be correctly configured to use MSP. I'll assume you've already got CleanFlight Configurator running.

You may want to use the main Serial connection for something else while your code is running, so we'll use the Soft Serial 2 port for this (pins 7 and 8 on the left of the board).

Go to the "Configuration" tab and scroll down to "Other Features". Make sure "SOFTSERIAL" and "TELEMETRY" are on. Save and Reboot.

Go to the "Ports" tab and set the "Data" column for "SOFTSERIAL2" to be active and set to 9600 (you could also use 19200 if you so desired, higher values may not work on the Arduino end). Save and Reboot.

The flight controller is now correctly configured.


Setting up the Arduino

To setup the Arduino, simply upload theexample code to the Arduino. Connect Pin 12 on the Arduino to Pin 7 on the left-hand side of the Naze board, and Pin 11 on the Arduino to Pin 8 on the left-hand side of the Naze board.

联系

Opening a Serial connection to the Arduino should now output Roll, Pitch and Yaw.

滚动、俯仰和偏航


Using for other MSP communication

Although the codehere is an example using MSP_ATTITUDE, the same theory applies to any of the MSP communications. The main differences would be a command message requiring data to be setup correctly (I haven't tested the code with that purpose in mind), and the readData function would need to have the switch statement modified depending on the data being received.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM