Prokura Innovations

Developing in Ardupilot(2)

Until now, we’ve developed a custom flight mode named PAYLOADRELEASE and we have successfully run this flight mode from ground control station. This will work perfectly if we have flight mode in which we takeoff from the ground station, do something in the air and land (if we manage to add all this code to the run( ) function of mode_payloadrelease.cpp file). But this is always not the case. Sometimes you’ve to perform tasks while being in the air while doing the mission. So for this purpose, we have to create a mavlink command (Like MAV_CMD_NAV_LAND, MAV_CMD_NAV_LOITER). After creating a mavlink command, we have to use this mavlink command to trigger the run( ) function in mode_payloadrelease.cpp , that we’ve created in the previous article. So first let’s make ardupilot know our mavlink command. 

Create mavlink command

  1. Decide what will be the name of mavlink command. If you’re trying to do navigation, decide to use the mavlink command in the form MAV_CMD_NAV_NAME. Although I’ve made this mavlink command to trigger flight mode, you can make it to trigger DO commands and SET commands. For DO and SET commands mavlink command will be in format MAV_CMD_DO_NAME and MAV_CMD_SET_NAME respectively.
    I’ve set the name to MAV_CMD_NAV_PAYLOAD_RELEASE since this mavlink command will trigger flight mode which will be doing some navigation. 

  2. Add the new message definition to the common.xml or ardupilotmega.xml file in the mavlink submodule. I’ve written in ardupilotmega.xml.
xmlfile (1)

You can assign the value of mavlink command to any number unassigned. (* Value of the navigation command should be less than 100 but I’ve put 31015. I’ll change it once I’ve to run the command from the ground control station in auto mission mode. For now, for simplicity assign value starting from 31015 or any number unassigned)

I’ve assigned param1 as mass of the payload, param2 as cross sectional area of the payload, and param3 as coefficient of drag of the payload. This is the data field. You can assign data upto param4 in the navigation command since param5, param6, param7 is used for latitude, longitude and altitude respectively.  

3. Build the code. Now Open the file ardupilotmega.h. You can see MAV_CMD_NAV_PAYLOAD_RELEASE in the enum assigned to the value that you’ve assigned. 


4. Now, We have to make Ardupilot know our command, and discern the data sent from the ground control station embedded in the mavlink command and sent as param. For this open AP_mission.h. In the AP_Mission class, add a structure with the data you sent from the ground control station (mass, area, Cd in our case). 


This structure will store data we sent from the ground control station.  Now we have a structure which will store the data we received from the ground control station. We have to instantiate the structure so that we can use this structure. Under AP_Mission.h there is a union named Content which stores all the content of the mavlink command.  

Scroll down in AP_Mission.h file, and under Content union, instantiate the Payload_Release structure.


Now we have a structure to store all the received data from mavlink command. Now let’s store some value from the mavlink command received to this structure. 


5. Extend the mavlink_int_to_mission_cmd ( ) function in AP_Mission.cpp file to discern Mavlink command data and storing inside the structure ‘payload_release’ we’ve created before. 

payload release

On the side note, this function, mavlink_int_to_mission_cmd ( ) , converts the mavlink command into an AP_Mission::Mission_Command Structure. This function gets called once when we upload the mission from gcs, to store all the commands in eeprom.

6. Now, whenever a command from eeprom gets popped to execute one by one by drone, a function named  mission_cmd_to_mavlink_int ( ) in AP_Mission.cpp gets called. What this function does is – it converts an AP_Mission::Mission_Command object to a mavlink message which can be sent to the GCS. So Extend this function for your mavlink command. 


7. In stored_in_location ( ) under AP_Mission.cpp, add a case statement for your mavlink command. This will make sure that your command is stored in the location.


8. Finally, In type() function, under AP_Mission.cpp, add another case statement for your mavlink command. 

payload release

Now doing all these steps and running your code, your mavlink command can be recognized by ardupilot and now your mavlink command can be incorporated inside the ardupilot codebase.  

Scope of this article

After implementing the content of this article, you can successfully add your own mavlink command to the ardupilot flight stack.

Future work

  • To trigger your drone to do something with this mavlink command while being on auo mission
  • To make this mavlink command trigger flight mode while being on auto mission

Side Note:

  • Remember, All the code you’ve implemented from this article is done in ardupilot flight stack. To call this mavlink command from the ground control station, the software side of gcs should be edited.
  • To make a mavlink command do something whether its for triggering flight mode, or its for doing something like moving a servo involves the same process. For flight mode, you have to trigger the run( ) function of flight mode. For doing something, by not creating flight mode, you have to add a function for your task in ModeAuto class in mode.h. 

Recent Posts

Quad drone services prokura innovations nepal

Drone Project

The primary objective of a project is to deliver blood and essential medical supplies from well facilitated zone to health posts of rural zones using drones.

Read More »
Octocopter drone services prokura innovations nepal

Developing in Ardupilot(3)

Until now, You’ve made a custom flight mode and a custom mavlink message. There may come a time when you have to trigger this flight mode in an auto mission.

Read More »

Description of Author

  • Abhish Khanal is a Robotics Engineer at Prokura Innovations Nepal, where Abhish develops software for drones, including designing hardware for drones and research in the field of Artificial Intelligence and Machine Learning. Abhish has completed his bachelor's degree from Institute of Engineering, Pulchowk campus and has participated in various national and international robotics competitions. Abhish loves reading, travelling, and music.

Leave a Reply

Your email address will not be published. Required fields are marked *