Prokura Innovations

Developing in Ardupilot(1)

When walking through the codebase of Ardupilot flight stack, you might get overwhelmed by the length of the code. Developing code in this flight stack and using it for your own custom purpose can be really handy sometimes. So for that, let’s see how flight modes are created in Ardupilot.

Creating custom flight modes in copter:

You can see a lot of flight modes that drones in ardupilot perform such as RTL (Return to launch), Land (Land the drone), Loiter (Loiter the drone) etc. Creating one of these custom flight modes on your own to perform specific functions in Ardupilot is really easy. There are steps which need to be performed to develop your custom flight mode. Let’s dig into those steps and discuss why they need to be performed. 


  1. Pick a name for the new flight mode and add it to the bottom of enum class Number in modes.h  just like “PAYLOADRELEASE” has been added below.

This enum defines all the flight modes available in copter. So adding your custom flight mode in this enum helps assigning name to flight mode. Now, Every flight mode in that enum has seperate class in mode.h which defines the variables, functions used to perform specific tasks for this mode. So let’s create seperate class for our custom flight mode PAYLOADRELEASE.

2. Define a new class for the custom mode in mode.h. You can copy a similar existing mode’s class definition and just change the class name(i.e. Copy and rename “class ModeStabilize” to “class ModePayloadRelease”


The new class should inherit from the Mode class and implement run( ), name( ), name4( ), and optionally init( ) functions. 

The function init( )  can be used to check the conditions for safely entering inside this flight mode. For eg. if you have to enter inside the PAYLOAD RELEASE flight mode, you should have payload inside your UAV. If there is a payload inside the UAV, return true or else false from this init( ) function.

The function run( ) runs in loop to make sure what you’ve assigned for the UAV to do runs in the background. For ex. If you print(“I am here”) inside the run( ) function, then “I am here” will constantly be printed if you change to PAYLOAD RELEASE flight mode.

The function require_GPS( ), has_manual_throttle( ), allows_arming( ), is_autopilot( ) is required. I don’t know why, but is *required.

3. So now, these run( ) and init( ) methods should be implemented. So for this we create ‘mode_.cpp” (in our case mode_payloadrelease.cpp). Every other flight mode has their own mode_(flight_mode).cpp for example: mode_loiter.cpp, mode_land.cpp. 

This new file “mode_payloadrelease.cpp” should implement the init( ) method which will be called when the vehicle first enters the mode. This function should return true if it is OK for the vehicle to enter the mode, false if it cannot. This file “mode_payloadrelease.cpp” should also implement the run( ) method which is an update method (called 400 times per second) which performs tasks for this flight mode. 


You can see that inside the run( ) function, there is gcs( ).send_text( ) function. This function is the same as the print function but sends data to the ground control station (gcs). We will see the printed text later on when we run this flight mode in simulation.

4. Now we have to call this flight mode (PAYLOADRELEASE) from Ardupilot codebase. But we have flight mode in class form. So now instantiate the ModePayloadRelease class in Copter.h. You can see the class ModePayloadRelease instantiated below. You can also define MACRO to enable or disable PAYLOADRELEASE flight mode inside config.h but that is not necessary. 

5. In mode.cpp add the new mode to the mode_from_mode_num( ) function to create the mapping between the mode’s number and the instance of the class.


Remember in step 1), We’ve given PAYLOADRELEASE flight mode the number 27. So whenever number 27 is called from somewhere (we don’t know where – for now),  PAYLOADRELEASE flight mode should be called. That’s why we set ret as an object of the ModePayloadRelease class. Later in this function we return ret; which is we return object ‘mode_payloadrelease’. This object ‘mode_payloadrelease’ runs init( ), and run( ) function for that flight mode. 

6. Add the new flight mode (PAYLOADRELEASE) to the list of valid @Values: for the FLTMODE1 ~ FLTMODE6 parameters in parameters.cpp (Search for “FLTMODE1”). I’ve added 27:Payload_release in the end of @Values: for every FLTMODE (FLTMODE1 ~ FLTMODE6)


Running above written code in Ardupilot:

Before running code in simulation, you should build the code and solve any error that occured in the process. Here are some screenshots of what it will look like when we call our flight mode from the ground control station. 


We have to write mode 27 in the terminal while running the arducopter in simulation to run PAYLOADRELEASE flight mode. We can see that It prints ‘Inside Payload Drop run function’ in equal interval of time when it enters inside the run( ) function of PAYLOADRELEASE flight mode. So, from gcs we send the number 27 to the Ardupilot codebase through mavlink message. (You can edit the code of GCS software like QGC to insert your custom flight mode inside.)


Scope of this article

After implementing the content of this article, you can successfully implement your custom flight mode from ground control station manually. Position control and attitude control of the UAV can be added inside flight mode for UAV to remain in the air. 

Future work

  • To make a mavlink message to trigger this flight mode.
  • To perform the function of this flight mode while being in a mission.

Recent Posts


GIT and It’s workflow

Git is a Distributed Version Control tool that is used to store different versions of a file in a remote or local repository. Git is a distributed version control system

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 *