Servers by jDrones

How to create your own custom messages in MAVlink?

(Sanjay J) #1

I’m just a beginner in drones and I wanted to create a custom MAVlink message to the flight controller and the flight controller needs process the data off this message. I did go through the ‘MavLink Tutorial for Absolute Dummies’, I’m clear about how the xml file must look like but I’m still not sure of the procedure to follow. Could someone briefly explain to me the steps for creating a custom message in MAVlink and the ways to implement this in the ground station and the flight controller.
Thank you

(Nikita) #2

Hello, year ago I tried to get some data to arduplane and send it to gcs. Mainly I was referring to

How I did it.

  1. We should change firmware for our drone.
    Download source code, and add your msg to mavlink message definitions

     <message id="227" name="TEST_INFO">
         <description>Test Info</description>
         <field name="test_temp1" type="float">Temp1</field>
         <field name="test_temp2" type="float">Temp2</field>
         <field name="test_voltage" type="float">Voltage</field>
         <field name="test_rpm" type="float">RPM</field>

Choose ID according to notes in this xml file.

Build firmware, so we mavlink will be updated with our message.

Open Plane.h and add variables for this new msg.

// Test vars
float temp1;
float temp2
float volt1;
float rpm1;

Lets modify GCS_Mavlink.cpp, in order to send our new msg.

I added it to the end of function

 void Plane::send_extended_status1(mavlink_channel_t chan)
 // sending our msg

Somewhere You should make handler of your data.

  1. Change code of GCS.

I modified Mission Planner. First, You should change mavlink too. Add the same message to


Then update mavlink via running regenerate.bat.
Now we should see definetion of our message in Mavlink.cs

Lets add our data to “status” tab in Flight Data window.

Open and modify CurrentState.cs

 public class Mavlink_Sensors

Add new variables for our msg.

 // Test Info 
 public float test_temp1 { get; set; }
 public float test_temp2 { get; set; }
 public float test_voltage { get; set; }
 public float test_rpm { get; set; }

Now we should parse and output our data, look at

public void UpdateCurrentSettings(System.Windows.Forms.BindingSource bs, bool updatenow, MAVLinkInterface mavinterface, MAVState MAV)

And add something like that

mavLinkMessage = MAV.getPacket((uint) MAVLink.MAVLINK_MSG_ID.TEST_INFO);

if (mavLinkMessage != null) 
                    var aero = mavLinkMessage.ToStructure<MAVLink.mavlink_test_info_t>();

                    test_temp1 = (float) (aero.test_temp1);
                    test_temp2 = (float) (aero.test_temp2);
                    test_voltage = (float) (aero.test_voltage);
                    test_rpm = (float) (aero.test_rpm);

After that I made new window and outputted this data how I wanted.

label1.Text = MainV2.comPort.MAV.cs.test_rpm.ToString("R");

I hoped that can help you a little.

(Sanjay J) #3

Thank you Nikita for your quick reply!
This sure did help me but I’m still stuck with a few doubts. I Guess if I explain my use case it would be easier to answer my doubts. I want to create a permission checker on the flight controller such that only a verified PIN can allow the flight controller to arm.
So, for this I’ll include the xml message in common.xml.
Then as per your instructions I will build the new firmware. After this I can make required additions in the copter.h file, as I’m using a copter and then modify GCS_Mavlink.cpp? I want to verify if this is the right procedure to be followed on flight controller. Also, the function to check if PIN is accepted can be included in GCS_Mavlink.cpp?
Very sorry if these are silly questions. I’m an absolute beginner in this.
Thank you

(Nikita) #4

I made new file, something like
test_check.cpp in ArduPlane folder.
Wrote my handler of the sensor.

void Plane::test_sensor()

Then added this new handler inside scheduler in ArduPlane.cpp

SCHED_TASK(aerostart_engine_sensor, 5,   1500),

Add our function to Plane.h

Add it inside

class Plane : public AP_HAL::HAL::Callbacks {

At the end of private functions.
If I’m not mistaken that’s all that I did.