View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
APM2 Simulink Blockset

4.6 | 14 ratings Rate this file 78 Downloads (last 30 days) File Size: 1.69 MB File ID: #39037 Version: 1.0
image thumbnail

APM2 Simulink Blockset


Robert Hartley (view profile)


Simulink blockset for use with the ArduPilot Mega 2.0 hardware. Includes blocks to read external rad

| Watch this File

File Information

As part of developing a new graduate level Guidance, Navigation, and Control course at Embry-Riddle Aeronautical University, a Simulink blockset has been developed that will allow students to work solely in Matlab/Simulink for the development of GNC code. This code can be downloaded directly to the ArduPilot 2.0 integrated sensor and processing package allowing the students to easily develop small unmanned aerial vehicles. The blockset is useful in that it eliminates the requirement to have a knowledgeable programmer, and a knowledgeable electrical engineer to integrate hardware and software with the airframe. The included guide is intended to describe the functionality of the blockset, provide recommendations for how to implement control designs, and steps on how to embed the code onto the ArduPilot directly from Simulink. The blockset is in early development and there is room for improvement; it is expected that future students or the academic community as a whole will advance the capabilities going forward.

This blockset uses the new Run-On-Target-Hardware feature that first appeared in Matlab 2012a which allows Simulink users to auto-generate code from a Simulink model for select hardware targets. This feature was previously only available to owners of Simulink Coder and other similar toolboxes.

For more information, please see the HowTo document included in the .zip file. The authors of the blockset may be reached at:

Required Products Simulink
MATLAB release MATLAB 8.0 (R2012b)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (56)
27 Sep 2016 Luis Aguilar

Hi everyone i have trouble working in external mode, I read the solution to add #include FastSerial, but when I work in external model the simulink stop working, anybody works in external mode? Please help me
Another cuestion anybody knows if I can use analog outputs? And How I do it.
I am been using Ardupilot 2.8

Comment only
16 Feb 2016 Lincoln Yeo

Are there any updates for this blockset to be used on APM2.6?

Comment only
30 Oct 2015 LI JING

22 Jul 2015 adrien polidano

Th Sergio's solution is good, my problem was when I checked the GPS information, I was receiving 0. I changed the Baud rate to 115200 everywhere and I receive some real value ...

Good luck ;)

Comment only
15 Jul 2015 adrien polidano


I have the same problem than you Wenyan, I am using a APM 2.6 with a ublox 3dr GPS, and I can't achieve to use the GPS block. Someone would have found a solution ?


Comment only
12 Jun 2015 Wenyan

Wenyan (view profile)

I have problems when using ublox NEO-7M GPS, looks like the APM blockset is not supporting this GPS since it is a newer version. I have tried Sergio's solution, commented the L50, and replaced with AP_GPS_UBLOX gps(&Serial1); // solution, still not working, the APM2.6 ublox GPS driver is different to APM 2.0 driver, is there a way to fix this?

Thank you,

08 May 2015 Lawrence Jane

29 Mar 2015 ache bak

actualy i have a problem that been already mentioned :
"" The call to realtime_make_rtw_hook, during the after_make hook generated the following error:
The build failed with the following message: "C:/MATLAB/SupportPackages/R2013a/arduino-1.0/hardware/tools/avr/bin/avr-gcc" -

like @ Fotis say i move the APM floder into \MATLAB\Targets folder but the problem still exist,if some one can help with that and thank you :)

Comment only
23 Feb 2015 kheirou

I've this problem :
Error in S-function 'IMU_Serial_Example/6-DOF IMU': S-Function 'Arduino_IMU_sfcn' does not exist
cauld any one help me ? i'm using Matlab 2012b

Comment only
13 Feb 2015 chfakht chfakht

i need to Collect Sensor Data using arduino and simulink , the module must support code generation so i can deploy it into the arduino
please help

Comment only
09 Dec 2014 martin

martin (view profile)

Hi dear all,
After many tries I've got a simple error in uploading the code to the APM. Can anyone PLAESE help me? This is the error:

"""The call to realtime_make_rtw_hook, during the after_make hook generated the following error:
The build failed with the following message: A subdirectory or file ./ already exists. recipe for target './' failed
make: *** [./] Error 1"""



08 Nov 2014 will yin

really thanks

30 Oct 2014 Tim

Tim (view profile)

Hey there,
thanks again for this wonderful block set.
For a little college HIL-Simulation, we are trying to use the APM2.6 as our autopilot controlling the servos - he is supposed to get sensor data from outside using Simulink as well be programmed using Simulink.
The important part right now is sending data to APM.
The serial send and receive blocks do their job I think very well.
But I am struggling with communication: We can send a vector of bytes - thus include the target and the information for the target in the vector. But on the APM we receive the bytes not in a vector, rather in serial (making sense cause its a serial receive ;) )

Does anyone know or can get me a hint where to get information concerning reassembling the byte stream to get target and information out of it - all of that being able to assemble in simulink?

For clarification eg:
Target: Servo Channel 1
Information: Postion 1500 Microseconds
Target: Pitch
Information: +5 deg

I am also open for ideas not using the Serial-Receive Block but programming (with in Simulink) around it.

If the question is unclear pleas tell me!
Thank you very much in advance.

Comment only
15 Sep 2014 Sergio Biagioni

Hi Yashin,
This target cannot be used with the pixhawk.

Adam Polak developed a target specifically for the PX4 pixhawk. It can be found here:

Comment only
15 Sep 2014 yashin

yashin (view profile)


i can use this program for pixhawk ?


Comment only
06 Aug 2014 Wern-Juin Choy


The blocks run smoothly on continuous. What I meant is that after testing and that it works, I would upload it directly onto the quadrotor. Thus it would better if it could run on discrete and save time later on in trying to make it discrete.

Comment only
05 Aug 2014 Carlos

Carlos (view profile)

@Wern-Juin Choy

During simulations, almost all blocks in this package directly feedthrough signals and do nothing. For example, the IMU block has inputs, which are fed to the output during simulations, so running in continuous should be no problem. I shall test to see again tomorrow when I am back at my lab.

Comment only
05 Aug 2014 Wern-Juin Choy

Thank you for the reminder. I want to test the dynamic model with the APM2 Simulink Blockset. That is why I have to change it into discrete state so that I can use it along with the blocks in the blockset.

After simulation and correction, I would then deploy it onto APM2.5.

Comment only
05 Aug 2014 Carlos

Carlos (view profile)

[Re: Wern-Juin Choy]

You never want to run a simulation model in a micro controller for several reasons:

1) The purpose of the simulation model is so that you could test your controller before using it on your physical platform. When deployed, your vehicle's actual dynamic response replaces your simulation model.

2) The ATMEGA2560 chip is very slow and can only run in discrete time. You will never get a good sample rate if you decide to run a complex simulation model on it.

So the advice is to test your controller in MATLAB/SIMULINK using the dynamic model, then upload your controller to the APM2.5 for validation and deployment.

22 Jul 2014 Wern-Juin Choy

I would like to upload the dynamic model equation into the APM2.5. An example coding and simulink model can be found in Peter Corke's Robotics Toolbox which can be viewed on Matlab. The dynamics is coded in an S-function and is solved using continuous state solver. However, the manual in this blockset says that all completed model has to be run on fixed-step discrete solver.

I have tried the following but to no avail
1) changing the code from continuous input states to discrete states
2) Introducing ts = [1 0] where it solves in continuous state with minor sample time

I am guessing that I have to recode the S-function into a MEX or C file before I can use the Run On Hardware option in simulink.

Any additional help would be gladly appreciated.


Comment only
08 Jul 2014 Carlos

Carlos (view profile)

01 Jul 2014 Wern-Juin Choy

Looks like I do not have to re-install Matlab. All I have to do is to close and restart Matlab, run the script and the library appears in the simulink library.

Comment only
01 Jul 2014 Onur

Onur (view profile)

make sure the block set folders are added to Matlab search path before running the script that compiles the blocks. You can use Set Path in the Home Menu.

Comment only
01 Jul 2014 Wern-Juin Choy

Hi everyone,

I am having trouble running the example Simulink models. When I run the IMU_Serial_Example.slx file, I get a very long list of warnings. One such example is as follows:

Warning: Unable to load block diagram 'RC_Library'
In general\private\openmdl at 13
In general\private\openslx at 9
In open at 159
In uiopen at 196

When I look at the simulink the blocks are not there but replaced with a red box with detached border and in it saying 'Bad Link'.

I have followed the instructions stated in the How To Guide, i.e. Replacing the core/arduino with the one in this repository and inserting #if 0 and #endif in the wrappers. The files are also placed in the Matlab directory (C:\...\Documents\Matlab).

The version I am using is R2012b with APM 2.5. Any help is gladly appreciated.


Comment only
26 Jun 2014 Tim

Tim (view profile)

@Johan S Lovgren

Thank Johan, this worked out quit good. I was able to bring the mex-File to work, it stated all successful. In my Simulink Library Browser a new Library appeared: ArduPilot 2 Target. So far so good.

But the Ardu Pilot 2 Target Library is empty. It states "No Blocks".
I can do a right click on it and do a "Open Ardu Pilot 2 Target Library". This opens a new window in which I do have the blocks. This seems to be the library located in:
Draging them into a Model though, they do not become usable blocks. Double clicking on the blocks within the Model these blocks state:
"Error evaluating 'OpenFcn' callback of SubSystem block 'untitled/Discrete IO'.
Undefined function or variable 'Discrete_IO_Library'. "

I tried something else:
I went one folder further down, eg. APM2/blocks/Serial. There is a Serial_Library.slx which I can open giving me three blocks I can actually use. With use I mean connect them and change Parameters. I thought grate, and started assembling them together by opening each of the libraries. But problems rase when I closed the Libraries again. The blocks of the Libraries I closed then started stating: Unresolved Link.

Also: While my workspace is within a folder of a block, everything is fine. But when I change the workspace to another block folder the Diagnostic Viewer states for example:
Error in S-function 'Test1/RC Write': S-Function 'Arduino_RCWrite_sfcn' does not exist

So I guess it all comes down to a not properly installed APM Library.

Can anybody help me with that?

Thank you very much

Comment only
10 Jun 2014 Johan S Lovgren

@Tim (on 26 May 2014)
Tim, i have mac and have installed the simulink blocks and got matlab to work with them. Note that the path in the guide is for windows. So the the path that you looking for is simply '/Users/computer_name/Documents/MATLAB/...'

just follow that path down to the right files.

When you run the mex-file that was included in the AMP2 folder, you will have to change the path also…
you will get the error: >> ArduinoBuildMexFiles
Error using cd
Cannot CD to /Users/computer_name/Documents/MATLAB/blocks/Serial/ (Name
is nonexistent or not a directory).

Error in ArduinoBuildMexFiles (line 14)
cd([ProjDir '/blocks/Serial/']);

change the code in the "ArduinoBuildMexfile.m"

from: cd([ProjDir '/blocks/IMU/']);

to some thing like this: cd(['/Users/comupter_name/Documents/MATLAB/SupportPackages/APM2_Simulink_Blockset/APM2//blocks/IMU/']);

OBS that i put the "APM2_Simulink_Blockset" in


so the path is: /Users/comupter_name/Documents/MATLAB/SupportPackages/APM2_Simulink_Blockset

follow the right path young padawan

Comment only
07 Jun 2014 Carlos

Carlos (view profile)

First of all many thanks to Robert Hartley for making this available to all of us, it has greatly eased my code development process. Does anyone know if it's possible to run this on R2014a with external mode?

26 May 2014 Tim

Tim (view profile)

Hey guys,
thank you very much for this work.

Does any body by chance know how to install this packege in the MATLAB 2014a for Mac Version? This Mac Version now includes the possibility for Run on Target Hardware. I do get all the Arduino folders but they are named differently (e.g. no cores folder) so I can't simply follow the HowTo on replacing folders and files.
That would be really great.

Comment only
20 May 2014 Dan Lluch

Dan Lluch (view profile)

@ Fotis -quick note on timing - This implementation doesn't use a RTOS (or the like), but does track using the timer onboard the Arduino. External mode is a bit expensive... So when exploring timing and throughput tests - a suggestion is to turn off external mode - but turn on the overrun detection (which allows you to flip a digital output bit if overrun occurs (e.g. led). In this way you can see if your execution is keeping up. HTH - Dan

Comment only
19 May 2014 Fotis

Fotis (view profile)

Hi guys,

thanks for your efforts! It seems like both the GPS and external mode are working. There are though some issues with the real time operation of the Simulink models when uploaded, even when using a single PID controller. I'll try to do some more testing and see how it goes.


Comment only
19 May 2014 Qazi Ejaz

if any one wants (Sensors_example.mdl) example visit this link

Comment only
14 May 2014 Roman

Roman (view profile)

Thank you Sergio, your solution brought our GPS module to life!
We are also using APM 2.6 and the uBlox LEA-6H.
I hope others are as lucky as we are!

Best Regards,


Comment only
11 May 2014 li

li (view profile)

06 May 2014 Fotis

Fotis (view profile)

Hi guys,

thanks for the great news! I will try to implement and test these changes asap and get back to you.


Comment only
06 May 2014 Adrien

Adrien (view profile)

Hi everyone,

Fotis: You are lucky these days... because Sergio seems to have fix the GPS problem and I've just found a solution to run the blocks in "EXTERNAL MODE"!!!
Actually that's revealing to work quite well for me and this is very simple.

For instance with Matlab R2012b, you just have to go at the file "rtiostream_serial.cpp" located at C:\MATLAB\Targets\R2012b\arduinomega2560\src

Add the command line #include "FastSerial.h" under 'Arduino.h' one. (In the same way than #if 0 in the wrappers)

And that's all, you'll should be able to read the angular velocities on a scope with the IMU_block (for example).

Best regards!

Comment only
05 May 2014 Moss Ritter

Hi Sergio,

Thanks for the success story. Based on what you learned perhaps you can advise me. I am using APM version 2.5 and Mediatek v2.0.

Comment only
05 May 2014 Sergio Biagioni

Hi Everyone,
To fix the GPS Sensor, try the following. If you are using a Ublox GPS Sensor instead of Mediatek, change the line (~L.50) which defines the gps object in;


// AP_GPS_MTK16 gps(&Serial1); // L. 50, original code, comment out
AP_GPS_UBLOX gps(&Serial1); // solution

This blockset was built for the ArduPilot Mega 2.0 with the Mediatek GPS Sensor. Newer Ardupilot's (i.e. APM 2.6) use a different GPS sensor and code.

This fix worked for me. I'm using an APM2.6 with a uBlox LEA-6H GPS Sensor. Let me know if this doesn't work for you. If thats the case, please let me know exactly which version of APM you're using and the exact GPS Sensor.


Comment only
29 Apr 2014 Roman

Roman (view profile)

we are using this Blockset successfully with Matlab 2013a on our Ardupilot 2.6. The sensors on the APM work quite nicely, but as others here we didn't bring the GPS sensor to work. All we get is all zeros with both GPS sensors, the uBlox and the Mediatek GPS modules.
If anyone got the GPS to work we would really appreciate if you could share your solution with us.


Comment only
12 Apr 2014 Ken Salter

I put my APM2 folder into ProgramFiles/Matlab and it works with the caveats you sited. I need not place the directory in Targets.

Comment only
12 Apr 2014 Fotis

Fotis (view profile)

Hi guys,

we found the cause of this problem. Actually, we just had to place the APM2 folder in the \MATLAB\Targets folder. Everything worked fine afterwards.

Well, actually almost everything... The GPS as Ken pointed out is not working. Also, the external mode is not working either, which is a big disappointment.

Anyone has the GPS and external mode working? It seems like that when the code is downloaded and I open the serial monitor directly from the Arduino IDE the signals are good. Nevertheless, no external mode in Simulink.

Any ideas or suggestions are welcome.


Comment only
10 Apr 2014 Ken Salter

I have used the APM2 blockset with 2012a on 64 bit windows. The gyro, accelerometer and magnetometer all work quite nicely. I have used Madwicks algorithm to make a nice AHRS. My big issues is that the MTK GPS block doesn't work. It just spits out zeros even though I have a blue light on the GPS. Has anyone gotten the GPS block to work?

Comment only
10 Apr 2014 Fotis

Fotis (view profile)

Hi Adrien!

I tried your suggestion but unfortunately I get the same results. It seems that this is a quite common issue. I'll try to look more into this.

Any other suggestions guys are really welcome, as it's somewhat frustrating...


Comment only
09 Apr 2014 Adrien

Adrien (view profile)

To Fotis: Hi! I read your comment and I MAYBE have a lead for your problem.

First of all "The call to realtime_make_rtw_hook, during the after_make hook generated the following error" appears for a lot of problems... So it's difficult to understand the problem.

If you use Windows 64 bits, it seems to bring more problems than the 32bits one. If it's the case, try to install "windows SDK" (Visual C++ within) and this could resolve the problem.
I hope it would be helpful!

Comment only
09 Apr 2014 Fotis

Fotis (view profile)

Hi! Thanks for this very interesting work. I have tried to use the blockset, after having followed all the installation steps described in the manual, but unfortunately the simulink code cannot be compiled.

I'm using APM 2.6 and I have tried both 2012b and 2013a without any results. I get an error "The call to realtime_make_rtw_hook, during the after_make hook generated the following error:
The build failed with the following message: "C:/MATLAB/Targets/R2012b/arduino-1.0/hardware/tools/avr/bin/avr-gcc" -I"C:/Users/SACL_Network/Documents/MATLAB/ArduPilot/APM2/Magnetometer_test_v1_rtt"....."
Also I get messages along the lines "cc1plus.exe: warning: command line option "-Wstrict-prototypes" is valid for Ada/C/ObjC but not for C++", so I assume it is related to the C codes run under C++.

Any help would be greatly appreciated, as this compilation error seems quite common.

Thanks again for your efforts!

Comment only
02 Apr 2014 Jesús

Jesús (view profile)

Hi. I also encountered some problems during setup and introducing example. I couldn't find the Sensors_example.mdl file, to start learning how to use this tool.

Does someone know where could I find it?

Comment only
26 Mar 2014 Thomas

Thomas (view profile)

Thanks for Your work, I would love to test it, but unfortunately I can not properly instal it. I'm using Matlab 2012a and run into the first problem during Chapter 2.6 when folders needed to be added to Matlab via SetPath. I didn't have Auxiliary folder, don't know if it counts or not. Then when I try to run ArduinoBuildMexFiles.m, it says
"Error using cd
Cannot CD to C:\MATLAB/blocks/Serial/ (Name is nonexistent or not a directory).

Error in ArduinoBuildMexFiles (line 14)
cd([ProjDir '/blocks/Serial/']);"

I also could not find the Sensors Example.mdl file mentioned in Chapter 4 and claimed to be found in '/Arduino Code/ArduPilot/-
APM2/'. It seems like the complete Arduino code folder is missing for me. How is it possible? Or am I searching for it in the wrong place?

Thanks for Your help in advance!

Comment only
26 Mar 2014 Adrien

Adrien (view profile)

The mail adress is closed, so in case one of the developpers read this:

Hi guys! First of all, thank you for your work and for your helpful Simulink APM blocks! (

I'm doing a project about control laws onto unsteady models and I use a quadrotor for the application. The access to the simulink "External mode" would be really great for this kind of project.

But I have a problem with this mode when I use your blocks...

In fact, the external mode is functionnal with the "arduino support package" from Mathworks, but as soon as I replace the folder 'arduino' with 164 elements (path: C:/.../cores/arduino)
I lost the ability to run in external mode, but the arduino and APM2 blocks are functionnal in "normal mode"...

Does the "External mode" enable with yours APM_Simulink_blocksets?

Maybe it's because I use an ArduPilot 2.5 card and you made these blocks for APM2.0... (But I use Matlab 2012b)

Any answers would be very helpful, thanks a lot!
Best regards

NB: The APM2 blocks seem to work on Matlab 2013a but not on Matlab 2013b.

Comment only
23 Mar 2014 Francescodario Cuzzocrea

I've downloaded this blockset, but unfortunately I wasn't able to made it work.
It seems that the zip files is missing of some blocks, because when I run the ArduinoConfigureScript I get this error during the build of mex files :

Error using cd
Cannot CD to C:\Users\Francescodario\Documents\MATLAB\APM2_Simulink_Blockset\APM2\Arduino\blocks\ADC_IMU (Name is
nonexistent or not a directory).

The entiry folder called "Arduino" with the blocks ADCImu, Analog, etc, is missing.

Can someone help me ?

Comment only
16 Feb 2014 Zhi Liu

I am currently trying to get gyro readings into MATLAB using the APM2 Simulink Blockset.

I believe I have everything set up correctly. But when I try out the example “IMU_Serial_Example.slx” (“Tools -> Run on Target Hardware -> Run”), nothing happens. On the bottom, it says “Model successfully downloaded to ‘Arduino Mega 2560’ ”, but nothing show up after that. On the workspace, I saw a “tout” variable. That’s it…

Is it working properly? If so, how exactly could I get the gyro readings to show up in MATLAB?

Comment only
02 Jan 2014 fz

fz (view profile)

can this work on ardupilot 2.5?

Thank you

Comment only
14 Nov 2013 Rolfe Dlugy-Hegwer

Anyone familiar with this issue:

Comment only
11 Nov 2013 Gerhard

Has anybody of you guys already tried to invoke the DMP on the IMU? I am currently in extending the Blockset in that point and was wondering if anyone has done this before or could provide me with some experience.

04 Nov 2013 chao

chao (view profile)


26 Sep 2013 Sumit Pokhrel

06 Mar 2013 phil

phil (view profile)

Dr. Anderson must be very pleased with this! ;)

very useful stuff...! keep it up

16 Nov 2012 Udomsak Boonprasert

Contact us