MAVLink and Arduino: step by step

Well now I have my problems solved…Much time lost to a marginal jumper cable on a prototype board. Now I can both monitor with the logic analyzer and send and receive commands using the menu system.

Thought I was defeated.
Then I thought that the only thing I hadn’t investigated was the function mavlink_parse_char I threw these lines in

	case MAVLINK_PARSE_STATE_GOT_COMPID:
		rxmsg->msgid = c;
                Serial.print("msgid ");
                Serial.println(c);

Suddenly I got all these messages that the function is actually rejecting as improperly formatted. HEARTBEAT and REQUEST_DATA_STREAM come through ok, but there is actually a lot of other messages that are rejected. Too many to list.
@pauljeff does your library handle mavlink2? Could there be a format change affecting the library’s ability to parse messages?

For anyone having similar problems where messages are not being decoded, here is the Mavlink v2 library. Drop it into the Arduino/libraries folder, and it should work. Make sure the protocol is set to MAVlink 2 in Mission Planner for that port.

Drive

2 Likes

I’ve run into the exact same issue today - the old MAVlink library on a teensy was working perfectly with Copter 4.1. on Ardupilot, upgrading to 4.2.2 has bricked compatibility. Seems like the decoding criteria or the ID for their criteria within mavlink_parse_char has been modified :frowning:.

Will give your updated library a go tomorrow, if it works you’ve properly saved me!

Following up on my last message - updating to the new MAVlink library didn’t help, messages were still being received but were almost always failing the mavlink_parse_char check (I had one heartbeat pass after running for around 10 minutes). But for whatever reason, checking the MAVlink message version before this has solved everything?!

I put in the following lines of code before the mavlink_parse_check and after the variables have been initialised:

if (msg.magic == MAVLINK_STX_MAVLINK1) {
Serial.printf(“This is a MAVLink 1 message\n”);
}

I found this on the official Mavlink website, within the documentation on running MAVlink in C. It will print if the message is V1 (I.E. your transmitter is configured incorrectly for this example).

It gave no output but miraculously this has fixed the failed parsing! Even after commenting out the code and restarting the teensy and Pixhawk it’s all back to working perfectly :smile:. Unsure how it has managed to fix the issue but hey I’ll take it. Hopefully this may help anyone else struggling.

Hey just a thought. I read somewhere that ArduPilot’s behaviour around whether it sends in MAVLink v1 or v2 depended on what it heard on the port. So if you send it some v2 it moves to v2 for its TX. I can’t recall where I saw it and don’t know if its the case really, but could what you guys are seeing be triggered by what version MAVLink packets you are sending to the AutoPilot?
In the same post I think there was talk about that behaviour changing soon
too, so maybe thats what changed in AP v4.1.x vs 4.2.x?

@pauljeff you are correct. And the behaviour did change on 4.2

I have a pixracer and after upgrading the wifi board no longer would communicate with it. I had to load mavlink2 onto the wifi board for it to work.

Mike

Hi Leiro
can you please provide me with the code.
this is a decade old post but if you have it available i would appreciate the help.

Hi Loannis can you please provide me the code.

Sir can you please share the code and details of how you managed to send data from arduino to the AP? and back to GCS.

Hi, i found this part in the code that does nothing ? or i miss something? How to print particular value if there is no input data ?

case MAVLINK_MSG_ID_PARAM_VALUE:  // #22: PARAM_VALUE
      {
        /* Message decoding: PRIMITIVE
         *    mavlink_msg_param_value_decode(const mavlink_message_t* msg, mavlink_param_value_t* param_value)
         */
        //mavlink_message_t* msg;
        mavlink_param_value_t param_value;
        mavlink_msg_param_value_decode(&msg, &param_value);
        
        char* id = param_value.param_id;
        float val = param_value.param_value;
        uint16_t count = param_value.param_count;
        uint16_t ind = param_value.param_index;
        uint8_t typ = param_value.param_type;
        Serial.println("Paramter info-------------");
        Serial.print("ID: "); Serial.println(id);
        Serial.print("value: "); Serial.println(val);
        Serial.print("count: "); Serial.println(count);
        Serial.print("index: "); Serial.println(ind);
        Serial.print("type: "); Serial.println(typ);
        
        
        
      }
      break;

Hi @sitas,
ArduPilot will send a message of with msg_id = MAVLINK_MSG_ID_PARAM_VALUE (#22) when it is sending an individual parameter. I use this code to see any responses when I issue a PARAM_REQUEST_READ (#20) to request a specific parameter from ArduPilot. In my Pulsar-MAVLink-Tester code if you select option [9] you can generate a PARAM_REQUEST_READ, and you should if everything works get a PARAM_VALUE back from ArduPilot.

Thanks Paul.

  1. But in the posted codes from the this thread start i have not seen that this MAVLINK_MSG_ID_PARAM_VALUE"(#22) message serial printed any data for anyone. Are you saying that this command works only with PARAM_REQUEST _READ (#20) command ?

  2. How to install Pulsar-MAVLink-Tester library to arduino nano or mega ? .

Hi @sitas, if you are not using PARAM_REQUEST_READ or SET then don’t worry about PARAM_VALUE.
I’m sorry I can’t help you with installing the Tester on a Nano or Mega, I use it on boards that are equivalent to an Arduino Zero, that is they have SAMD21/51 Microcontrollers.
Others are running MAVLink code on different Arduino’s successfully, I suggest you take a small amount of the Tester code and get that working, then build up from there. Good Luck :+1:

Paul, thanks for helping. I taked a small amount of the Tester code modified and get it working. :slight_smile:

That’s awesome @sitas, well done!

Hi everyone,
I have managed to build the connection between arduino nano and pixhawk 2.4.8
The problem is that I am only receiving message id=0.
Can anyone help me solve this issue?

Hi @Zain_Siddiqui, can you share your code and more about what you are seeing, then maybe we can help you. Also is your autopilot configured to talk MAVLink on the port you are connected to?
What version of ArduPilot are you using?
Have you checked the recent comments above regarding MAVLink v1 vs v2.
Cheers

hi everyone

I’m new to pixhawk and don’t have much expirience with coding. I am building an autonomous surfboard and I’m using a stepper motor as a steering. I’m using a pixhawk and arduino to do all this and I want a pixhawk to control the stepper motor while its connected to an arduino. The problem is I don’t know how I can do that. anyone who knows how to do that please help.