File Exchange

image thumbnail

APM2 Simulink Blockset

version 1.0.0.0 (1.69 MB) by Robert Hartley
Simulink blockset for use with the ArduPilot Mega 2.0 hardware. Includes blocks to read external rad

35 Downloads

Updated 13 Nov 2012

View License

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:

apm2.simulink@gmail.com

Comments and Ratings (63)

Hi everyone, I would like to explain how I made this blockset work in Win10 and the identified problems with some MATLAB versions so that no one else has to go through.

1 - Install MATLAB 2013's (a or b does not matter) 32-bit version, regardless of your operating system.

* The reason for installing 32-bit version is that the MATLAB versions before 2015a does not support Win10 and you need a compiler supported by both MATLAB and Windows to prepare the APM2 .mex files. (Fortunately 32-bit versions install their own compilers)

* For 64-bit versions, you can try to install the supported SDK if you use Win7 or Microsoft Visual C++ programs for Win10, which are rather heavy-weight on the computers and not recommended.

2 - Go through the default Hardware Support Package installation from internet. Specify (and remember) the installation path. Select Arduino as hardware and let it install. At the end, it will give an error about the "arduino-1.0-windows.zip" file. Don't worry and just click OK.

* In 2012b, which is the blockset's origin version, an error occurs while downloading MATLAB compatibility files, which I couldn't find anywhere else so I couldn't make it work.

* In 2015 and future versions, the downloaded Arduino version is 1.6 and above, which are different by structure and not compatible with APM2 blockset.

3 - Go to the Arduino website and go to the older releases, download 1.0 version as .zip file. Copy this .zip file (named "arduino-1.0-windows.zip") to the "downloads" folder in the installation path of the support package.

* The default file is corrupted for the reasons I don't know. Just replace the .zip file with the one you downloaded.

4 - Go to MATLAB again and open Hardware Support Package install window. Select install from folder and select the "downloads" folder that you have copied the .zip file to.

* I have seen some versions still try to download the files when they are connected to internet. So it could be a good idea to disconnect before installing.

5 - And you are done! From there, just follow the "How To Guide" and it will all be working.

Hi how should I install the APM2 package/ blockset?

Thanks!

Alexis Cruz

Has anybody can already use this block set on matlab 2015a?

"" 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" -
cc1plus.exe: warning: command line option "-Wstrict-prototypes" is valid for Ada/C/ObjC but not for C++ ...........................
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

wenlong

I built the examples using R2015B 64bi, there were some errors and I'am a llitle sad

wenlong

Hi Justin,
I also encountered this. I copied apm2_simulink_blockset to the path r2015b/toolbox, the generated using mex. I made a new folder f:\apmtest, and I also copied .mexw64 files to this folder, there was no such errors now!

Justin Mellor

Compiler Error!
I have installed the home version of R2017a, installed the Arduino Support Packages for MATLAB and Simulink, as well as the APM2 Simulink Blockset. Installed Support for MinGW-w64 compiler 4.9.2 and successfully compiled the APM2 blocks, which all appear OK in ArduPilot2_lib. Also modified the IO_Wrapper files as specified. However when I try to build the included IMU_Serial_Example.slx model, I get this error:
In file included from IMU_Serial_Example.c:2:0:
IMU_Serial_Example.h:11:35: fatal error: Simulink_APM2_Wrapper.h: No such file or directory
#include "Simulink_APM2_Wrapper.h"
This file is clearly on the path. Have I missed something?
Any help would be very much appreciated!

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

Lincoln Yeo

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

LI JING

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 ;)

Hi,

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 ?

Thx

Wenyan

Wenyan (view profile)

Hi,
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,

ache bak

hi
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 :)

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

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

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.
IMU_Serial_Example.mk:138: recipe for target './' failed
make: *** [./] Error 1"""

Thanks,

Martin

will yin

really thanks

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.
Tim

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:

http://polakiumengineering.org/px4-development-kit-for-simulink/

yashin

yashin (view profile)

hello;

i can use this program for pixhawk ?

thx

@Carlos

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.

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.

@Carlos
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.

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.

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.

regards,
Erwin

Carlos

Carlos (view profile)

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.

Onur

Onur (view profile)

@Erwin
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.

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.

regards,
Erwin

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:
MATLAB/APM2_Simulink_Blockset/APM2/blocks
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
Regarts
Tim

@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

Documents-->MATLAB-->SupportPackages

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

follow the right path young padawan

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?

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.
Thanks.
Tim

Dan Lluch

@ 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

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.

Cheers,
Fotis

Qazi Ejaz

if any one wants (Sensors_example.mdl) example visit this link http://www.mathworks.com/matlabcentral/fileexchange/46663-sens-slx

Roman

Roman (view profile)

Hi,
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,

Roman

li

li (view profile)

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.

Cheers,
Fotis

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!

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.

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;

C:\MATLAB\SupportPackages\R2014a\arduino-1.0.5\hardware\arduino\cores\arduino\Simulink_APM2_Wrapper.cpp

// 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.

Best,
Sergio

Roman

Roman (view profile)

Hi,
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.

Thanks,
Roman

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.

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.

Thanks,
Fotis

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?

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...

Thanks

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!

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!

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?

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!

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! (APM2_Simulink_Blockset.zip)

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.

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 ?

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?

fz

fz (view profile)

can this work on ardupilot 2.5?

Thank you

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.

chao

chao (view profile)

useful

java python

phil

phil (view profile)

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

very useful stuff...! keep it up

MATLAB Release Compatibility
Created with R2012b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Discover Live Editor

Create scripts with code, output, and formatted text in a single executable document.


Learn About Live Editor

APM2/

APM2/blocks/

APM2/blocks/Analog_IO/

APM2/blocks/Baro/

APM2/blocks/DataTypeConversions/

APM2/blocks/Discrete_IO/

APM2/blocks/FlashMemory/

APM2/blocks/GPS/

APM2/blocks/IMU/

APM2/blocks/Magnetometer/

APM2/blocks/Pitot/

APM2/blocks/RCChannels/

APM2/blocks/Serial/

APM2/blocks/Timing/