2

I'm new in Mavlink, I want to add a new message in the Mavlink protocol and send it each second periodically. How can I do it?

mpromonet
  • 11,326
  • 43
  • 62
  • 91
Oscar MV
  • 63
  • 1
  • 1
  • 7
  • What is the purpose of the message? Who will send the message and who will receive it? – squilter Apr 20 '16 at 22:08
  • What I want is send the message each second periodicaly from my dron to my PC using 3DR. The purpose is knowing how could I do it and it also helps me to complete my final degree proyect. – Oscar MV Apr 20 '16 at 23:40
  • Okay, but what does the message contain? – squilter Apr 22 '16 at 15:15
  • The message contains two identifiers, the first one is the controller ID and the second one is for identify the message. I use the type uint8_t for both. – Oscar MV Apr 23 '16 at 09:58
  • Have you looked into existing messages? You might be able to cram that data into a message like `debug`, `statustext`, `sys_status` or `heartbeat` – squilter Apr 23 '16 at 18:46
  • I'm trying to add the two fields in the "heartbeat" message but I have had a problem with the command ./libraries/GCS_MAVLink/generate.sh, which regenerate the include files that will allow the "new" message to be recognised in the main code. When I execute this command the new fields should be added to mavlink_msg_heartbeart.h, but it is not my case, the message remains unchanged. Any ideas? – Oscar MV Apr 28 '16 at 15:33
  • No, you should not ever edit an existing message. I was thinking that you would use the some of the existing fields that have a similar purpose. – squilter Apr 28 '16 at 20:39
  • So if I understand correctly, you mean I should insert the data I want to send, within the heartbeat message without modifying the fields it have, right? It will be easier than create a new message with the field that I need? – Oscar MV Apr 29 '16 at 11:23

2 Answers2

2

Here you can find detailed steps about how to add new message to mavlink protocol and how you handle it.

  1. Ensure you have the latest ArduPilot code and Mavproxy installed.
  2. Decide what type of message you want to add.
  3. Add the new message definition to the common.xml or ardupilotmega.xml file in the mavlink submodule.

  4. Add functions to the main vehicle code to handle sending or receiving the command.

Soubhi M. Hadri
  • 133
  • 1
  • 15
0

It depends on what autopilot you are using. If you're using ardupilot then you would need to add a new xml message definition in ardupilot/modules/mavlink/message_definitions/v1.0/ardupilotmega.xml.

You can look at the other messages to see how it should be formatted. Just make sure you choose an id that is unused.

Next you need to decide how to put this in the code. You could place it in the data_stream_send task by adding the message id to, say, STREAM_EXTRA3. This will send your message as often as the other data is sent there. As part of that you will need to define the function to actually pack your data structure using the function generated by pymavgen, the message id and enumerations. This is what I have done in my own project for ASH_DATA. You can see the changes I've made in my repository for reference. Note that some of those include changes to incorporate reception of ash data on the pixhawk and adding the data to a log file.

Given that you want to run this once a second you may want to add to the one_second_loop task or create your own task that simply calls the try_send_message function using your new message id.

You will of course need to incorporate the new message in your gcs so you can actually receive it, but that's another matter.

Hopefully this can nudge others in the right direction who are trying to do the same.