How To Integrate Arduino Libraries With Matlab Simulink
How To Integrate Arduino Libraries With Matlab Simulink
This manual shows you in details how to integrate Arduino libraries with Matlab
Simulink on the example of AFMotor library.
Requirements:
1. Arduino Mega and Arduino IDE
2. Adafruit Motor Shield
3. AFMotor library
4. DC Motor
5. Matlab (at least 2012b)
6. Arduino Support from Simulink
1. Copy necessary libraries from Arduino folder (AFMotor.h and AFMotor.cpp). You
will find these files in ArduinoDirectory\arduino-1.0.1-windows\arduino-
1.0.1\libraries\AFmotor
#ifndef MATLAB_MEX_FILE
#define ARDUINO 100
#include <Arduino.h>
#include "AFMotor.h"
#include "AFMotor.cpp"
AF_DCMotor motor(1);
#endif
10. Open Outputs panel
a. Add this code (compare it with code from Arduino IDE):
#ifndef MATLAB_MEX_FILE
// turn on motor
motor.setSpeed(PWM[0]);
motor.run(DIR[0]);
#endif
Remember that both inputs PWM and DIR are pointers! Therefore you
call theses input using [0].
11. Add necessary Constant sources that will control DC Motor. Remember that both
inputs must be uint8 type!
12. Find file sfAFMotor_wrapper.c in your project’s folder and change its type to .cpp
13. Open it and find function sfAFMotor_Outputs_wrapper. Add extern "C" before the
name of this function. It should look like this:
References:
1. http://www.mathworks.se/hardware-support/arduino-simulink.html
2. http://www.minseg.com/arduino-downloads