Multiwii Serial Communications

Post Reply
SteevyT
Posts: 7
Joined: Wed Jul 15, 2015 9:50 pm

Multiwii Serial Communications

Post by SteevyT »

I'm trying to figure out how to request data from a MultiWii 2.0 Pro and write data (specifically a "disarm" command) to it No longer using this method, will place Arduino between receiver and MW to change commands through receiver inputs as needed using an Arduino board. Unfortunately I have basically no experience in writing code using serial communications, and the documentation on the MSP is....less than helpful due to my lack of knowledge on communication between boards in general.

If I could just get example Arduino code on how to:
    1: Request specific data from the MultiWii through the serial connection (GPS, angle, and whether the flight control board is actually responding to be specific, but it looks like just changing one number determines which I get based on the documentation)
    2: Read the data requested (and save it to a variable if it isn't just "variable = data")
    3: Write a disarm command (sorry, I would just want an example of one and do the rest myself, but there is only one) No longer doing disarm this way.
that would be incredibly helpful.

I thought this would be relatively simple, and that I could just look through the MWOSD serial communications to figure it out, but I'm stuck.
Last edited by SteevyT on Tue Jul 21, 2015 4:59 pm, edited 3 times in total.

User avatar
haydent
Posts: 583
Joined: Sun Jun 17, 2012 1:35 am
Location: NSW, AU

Re: Multiwii Serial Communications

Post by haydent »

have a look at the osd projects to see how they do it

SteevyT
Posts: 7
Joined: Wed Jul 15, 2015 9:50 pm

Re: Multiwii Serial Communications

Post by SteevyT »

I've tried looking at the OSD projects. I have no clue what is going on in them because I have 0 experience doing communications between Arduinos, and I can't find any comments that are useful to me.

Edit: To put it slightly more bluntly, I have looked at the MultiWii code, I have looked at the MWOSD code, I have looked at a datalogger set of code I found for this. I've even tried directly copying them and just grabbing the data they pass around, and that doesn't even work. If they were commented in a way I could understand, I would not currently be here.

User avatar
haydent
Posts: 583
Joined: Sun Jun 17, 2012 1:35 am
Location: NSW, AU

Re: Multiwii Serial Communications

Post by haydent »

Can you post any code you have tried?

SteevyT
Posts: 7
Joined: Wed Jul 15, 2015 9:50 pm

Re: Multiwii Serial Communications

Post by SteevyT »

The more I play with it, the more I'm thinking I'm sending data as unsigned integers when I shouldn't. I've also left comments in so hopefully following my thoughts is easier, and easier to correct misconceptions.

Code: Select all

uint8_t request = 0;  //payload length for data requests
 uint8_t MSP_Attitude = 108; //identification for requesting current mode
 uint8_t kill = 1000; //change depending one what the AUX channel to disarm needs to be set to
 uint8_t State = 0; //initialize variable to read incoming state stuff
 int LED = 13;

void setup() {
 Serial.begin(115200); //sets baud rate
 

}

void loop() {

//////////////Begin Serial Transmit Communication module////////////////////////
  uint8_t checksum = 0; //initialize checksum

  Serial.write((byte *)"$M<", 3); //sends breamble bytewise 3 is number of bites used
  Serial.write(0); //sends length of payload (0 for requests, replace n_bytes as needed)
  checksum ^= 0; //updates checksum

  Serial.write(MSP_Attitude); //writes request/command
  checksum ^= MSP_Attitude;  //updates checksum
 
  Serial.write(checksum); //sends checksum


I'm not entirely sure what is going on with the first line of Serial.write, but it seems to send what it should. I don't get a response from the flight control board though which is why I'm thinking I'm sending incorrect data in the next 6 lines.

Then before I have issues with it, I'm not entirely sure how to compare what the flight control board sends back with set values in the Arduino. I've tried to get something to compile when I modified this https://github.com/renes/multiwiiDataLo ... datalogger, but I honestly can't figure out what is going on.

I tried to just save the values that came into https://github.com/renes/multiwiiDataLo ... protocol.h at lines 57 through 61, but I couldn't get anything to compile. It would compile without modifying it when I tried to compile the ones that only dealt with serial communications (protocol.cpp, protocol.h, and serialstate.h), so I'm thinking I'm not entirely understanding the Arduino programming either.

If I'm not mistaken, I do have to set up a loop to collect each byte of the serial communication individually then convert the array they end up in back into something readable. I'm not entirely sure how to do this, but I'll try different things once I get a response from the board. Right now my thinking is to read and watch for the $ then the M and finally the >, then loop through storing each bit that comes in and updating a checksum until the incoming byte equals the checksum.

In the MWOSD https://github.com/ShikOfTheRa/scarab-o ... Serial.ino, I think it's reading the serial beginning at line 53, but I couldn't even figure out where to start when following what is going on with that so I gave up on that quickly.

I've also decided that to disarm the board, it would be easier to set up the Arduino between the receiver and the board on the channel used to arm/disarm and have it send the disarm value when needed.

Sorry for being abrupt with my questions, I'm just stressed out over it since I only have a week to get it figured out.

Edits: Just updating to follow the latest thing I did, switched from requesting Status to Attitude. And added current idea on how to actually read what is coming in, but that may be a question better suited for Arduino forums. Bit should be byte.

User avatar
haydent
Posts: 583
Joined: Sun Jun 17, 2012 1:35 am
Location: NSW, AU

Re: Multiwii Serial Communications

Post by haydent »

just working on a stripped down version of the data logger for you as that seems like a good place to start.

also in regard to an arduino sketch you will generally need all the files. you cant just take out some of them and expect it to compile and run if thats what you tried...

User avatar
haydent
Posts: 583
Joined: Sun Jun 17, 2012 1:35 am
Location: NSW, AU

Re: Multiwii Serial Communications

Post by haydent »

here you go try running this, what arduino model are doing this on ?
https://github.com/hayden-t/multiwiiDataLogger

SteevyT
Posts: 7
Joined: Wed Jul 15, 2015 9:50 pm

Re: Multiwii Serial Communications

Post by SteevyT »

haydent wrote:here you go try running this, what arduino model are doing this on ?


Sorry, I guess that would be helpful.

Arduino Nano V3.1

There is a PWM output to a servo on pin 3, but if the data coming in from the serial can be converted to integers or I can convert the values I'm comparing to into uint8_t I think, the rest of the code should be easy, just a bunch of if statements moving the servo between two positions.

User avatar
haydent
Posts: 583
Joined: Sun Jun 17, 2012 1:35 am
Location: NSW, AU

Re: Multiwii Serial Communications

Post by haydent »

Ok, so nano will be a little hard to develeop on in your situation only having one serial port, so you wont be able to use the code i added for serial1

you can see the format the data is retrieved in by looking at the structs in the header file and the mw msp wiki

SteevyT
Posts: 7
Joined: Wed Jul 15, 2015 9:50 pm

Re: Multiwii Serial Communications

Post by SteevyT »

haydent wrote:Ok, so nano will be a little hard to develeop on in your situation only having one serial port, so you wont be able to use the code i added for serial1


I managed to check out an oscilloscope that can read digital signals (well, show that 1's and 0's are being sent anyway). I actually am getting responses from the flight control board with what I had written earlier.

haydent wrote:you can see the format the data is retrieved in by looking at the structs in the header file and the mw msp wiki


Here is what the table for MSP_ATTITUDE shows (I believe I use Serial.write(uint8_t(108)) to request it)
Image

It shows that it sends back the angle (x, y, and heading) as three 16 bit integers. What I'm still confused on is how are they separated from each other. Do they just come one immediately after or is there some sort of delimiter I need to watch for? Is it the same for any other data I want as to whether there are delimiters?

One last question for now, what exactly does the Serial.write((byte *) "$M<", 3) command do? It looks like it sends $ then M then < as single bytes, but I just want to be sure.

I'll also see if I can check out a Mega this afternoon or tomorrow morning.

User avatar
haydent
Posts: 583
Joined: Sun Jun 17, 2012 1:35 am
Location: NSW, AU

Re: Multiwii Serial Communications

Post by haydent »

have a closer look at the github code i put up, its all in there

there are no delimiter, the variable size stipulates where the data is, ends and next begins, so to decode MSP_ATTITUDE you just read out 2 lots of 8bits (16 bits/2 bytes)(Int 16) 3 times.

https://github.com/hayden-t/multiwiiDat ... ol.cpp#L41

$M< is a 'header' that indicated a command is next:
https://github.com/hayden-t/multiwiiDat ... ol.cpp#L31

SteevyT
Posts: 7
Joined: Wed Jul 15, 2015 9:50 pm

Re: Multiwii Serial Communications

Post by SteevyT »

haydent wrote:there are no delimiter, the variable size stipulates where the data is, ends and next begins, so to decode MSP_ATTITUDE you just read out 2 lots of 8bits (16 bits/2 bytes)(Int 16) 3 times.


Looks like this was my largest problem. Between the help I got from you, and how to combine the two sets of 8bits from a CS professor here, I think I have this figured out, hopefully. Thanks.

Post Reply