Incorporating control system designed in Simulink into ArduPilot on Pixhawk 2.1 with code generation

Hello all,

I am working with a vehicle which currently uses ArduPilot (ArduRover) and a Pixhawk 2.1. I have designed a novel control system in Matlab/Simulink. I would like to incorporate this control system into the vehicle’s current system, but I have limited experience with C++ so I would like to use one of Simulink’s code generation tools to make this easier. There is a lot of documentation from matlab/simulink on these tools, but my understanding is that I need a hardware support package to make the automatic code generation work. I found these and I would appreciate help with determining if either would be useful to me:

    this package seems to be designed for ArduPilot Mega 2.0 and not Pixhawk.

    this package is designed for Pixhawk, but specifically mentions PX4, although it seems to use this interchangeably with Pixhawk. My concern is that if it is meant for use specifically with the px4 control system I may not be able to generate useful code from it. The user guide is here:

I am anticipating this process will require generating code and then consulting with someone more knowledgeable in programming to incorporate into our existing control system, but I want to make sure that any code I am able to generate automatically is usable.

Thank you!

P.S. I apologize if I picked the wrong category for this


I am very much interested in the question you have posed for the ArduPilot community. Have you been able to sort this out?



What does your control system cover? taking the sensor input and pilot commands and calculating the outputs to the motors? or does it go further than that. I know a group that essentially replaced the attitude controller in the ardupilot software using the pictures to code feature in matlab/simulink. Sounded like they had to map the appropriate inputs and outputs to their controller in simulink. I don’t know the details but it sounds like pictures to code generated either the cpp or object file that they then included into the compilation process using the standard ardupilot compilation tools. Sorry I can’t help you any more. I definitely know that the compilation process was not contained within the matlab/simulink environment.

Hope this helps.

I played with code generation tools and the support packages I listed, but the output was more complex than I wanted so I shelved the project

This control system is for a marine unmanned surface vehicle that takes inputs of body centered velocity and acceleration in three degrees of freedom and inertial position and heading and would output signals that can be converted to motor speeds and angles.

When you say pictures to code do you mean the Simulink coder? I was looking for the correct setting to make this work with a PixHawk and ArduRover when I found the packages I listed in the first post. Thanks for the tip.


hi @mas30d, I have a question about your simulink model. I am currently trying to create a custom simulink model for a drone.

Any help will be greatly appreciated. I have sent you a dm.


Did u found eny solution ?
Me too Im using PX4 Autopilots Support package wich is created in 2018 april.
But I can’t start with , there are a lot of problems

I have done this successfully in the past. The key is to find the combination of Simulink code generation settings that give a sensible output. I don’t have access to the files at present but I should be able to share a project template in a month or two.

1 Like

Kind of new to the ardupilot, and I apologize if have already shared the project template, but being said have you made the project template and could you share it?

Hello, here you can find an interface for ArduCopter and ArduPlane:

ArduRover is not supported yet, but maybe someone could add it (the basic procedure to create the interface is described below in the “contribution” section of the readme file)?
I have already successfully used the ArduCopter interface several times for flight tests.

Hello again,

I have a question that is probably more directed to the developers.
Is there any interest in merging such an interface?

Some work would be needed to merge it, but actually it’s not that elaborate either.

Roughly speaking, the interface is structured as follows:
In ArduPilot there is a new flight mode, which grabs target inputs (stick inputs and waypoints) and measured values (from the state estimator) and stores them in two structs.
Then there is a Matlab/Simulink template, which also has these structs defined as input variables.
The user can then implement any controller (rate controller, position controller, position controller or combinations thereof) in Matlab/Simulink based on these input variables.
Finally, the Matlab/Simulink template provides an output for the servos/motors and an output for logging.
These outputs are defined as inputs in the new ArduPilot flight mode.
With the Simulink Coder a C++ class (three files) can be generated from the template. We are currently copying these files manually into our ArduPilot fork to update the custom flight mode.

I have recently successfully used the ArduCopter interface to test a new passive fault-tolerant flight control law:

I tested the ArduPlane interface with a tailsitter.



1 Like