5.0

5.0 | 7 ratings Rate this file 140 Downloads (last 30 days) File Size: 728 KB File ID: #39354
image thumbnail

Device Drivers

by Giampiero Campa

 

02 Jan 2013 (Updated 02 Jan 2013)

Developing Simulink Device Driver Blocks: Step-By-Step Guide and Examples

| Watch this File

File Information
Description

This package contains a guide that explains, in a step-by-step fashion, how to develop device driver blocks (blocks that perform target-specific functions when executed on a target platform).

Example drivers for:

-) Arduino digital output
-) Arduino digital input
-) Arduino analog output
-) Arduino encoder read
-) DC Motor (Arduino AF Motor Shield)
-) Stepper Motor (Arduino AFMotor Shield)

are included.

While the examples are built using the Arduino as the hardware platform (specifically relying on the Simulink Support Package for Arduino), the method is the same for any other supported target.

Acknowledgements

This file inspired Adlx345 I2c Driver For Arduino Mega, Example Driver Blocks For Simulink Hardware Support Packages, Raspberry Pi Driver Block S Function, Raspberry Pi Dc Motor H Bridge Driver Block S Function, and Raspberry Pi Pwm Driver Block S Function.

Required Products MATLAB
Simulink
MATLAB release MATLAB 8.0 (R2012b)
Tags for This File  
Everyone's Tags
arduino(2), dc motor, device, driver(2), embedded, encoder(2), stepper motor(2)
Tags I've Applied
Add New Tags Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (19)
29 Apr 2013 Giampiero Campa

I'm glad that's working, Nathan.
Thanks Phil, that's great!

29 Apr 2013 phil

guys i have put together a little macro that will automatically make the necessary modification to the wrapper.c file
I found myself having to redo this for every rebuild of the S function and it was getting annoying.
The exe will add the extern "C" in front of the voids save/close and rename it to .cpp
gl - phil

https://dl.dropboxusercontent.com/u/64527727/externCautoit.rar

29 Apr 2013 Nathan Crosty

@Classroom Resources,
That was the trick. I can now use any library I wish in the S-Function builder. Thank you very much

29 Apr 2013 Nathan Crosty

@phil,
I added this line to my Library/Object/Source files pane in the s-function builder. It seems to take care of the path issue.

INC_PATH C:\arduino-1.0.3\libraries\Time

29 Apr 2013 Classroom Resources Team

Nathan, if you include a .cpp file then you should rename the generated wrapper function from .c to .cpp, and also open it and write: ' extern "C" ' before the two function calls.

Please have a look at page 20 of the guide, which explains how to do this, and let me know if it works.

28 Apr 2013 phil

@ Nathan...
did you put the included files in the current matlab folder?

28 Apr 2013 Nathan Crosty

Hello,
I tried to follow your example and it worked great for interfaces that are in Arduino.h such as digital and analog IO. I ran into problems when trying to include other Arduino Libraries.

I noticed that you had included "AFMotor.cpp" and "AFMotor.h" in your examples, which I guess is similar what I am attempting with "DS1307RTC.h" and "DS1307RTC.cpp" which are libraries for keeping time.

I can build the sfunction with the sfunction builder when it is structured like you have it in your examples. When I go to build the applicaiton, however, I run into an issue that seems to be related to cpp code when the compiler is expecting c code. I am using the Arduino integration package found here http://www.mathworks.com/matlabcentral/fileexchange/30277-embedded-coder-support-package-for-arduino

The compiler message I get when it attempts to build the header file with a class definition is this:

C:/ARDUIN~1.3/hardware/tools/avr/bin//avr-gcc -c -mmcu=atmega2560 -I. -DF_CPU=16000000 -Os -Wall -Wstrict-prototypes -std=gnu99 -I. -I.. -IC:/PROGRA~1/MATLAB/R2011a/rtw/c/ert -IC:/PROGRA~1/MATLAB/R2011a/extern/include -IC:/PROGRA~1/MATLAB/R2011a/simulink/include -IC:/PROGRA~1/MATLAB/R2011a/rtw/c/src -IC:/PROGRA~1/MATLAB/R2011a/rtw/c/src/ext_mode/common -Ic:/OU_SYSTEMS_ENG/GH/gh-dev/Simulink_Models/Drivers/GH_TopLevel_Test_arduino -Ic:/OU_SYSTEMS_ENG/GH/gh-dev/Simulink_Models/Drivers -Ic:/OU_SYSTEMS_ENG/GH/gh-dev/Simulink_Models -Ic:/OU_SYSTEMS_ENG/GH/gh-dev/Simulink_Models/Custom_Includes -Ic:/OU_SYSTEMS_ENG/GH/gh-dev/Simulink_Models/ArduinoML/blocks -IC:/arduino-1.0.3/hardware/arduino/variants/mega2560 -IC:/arduino-1.0.3/hardware/arduino/cores/arduino -IC:/arduino-1.0.3/libraries/Time -IC:/arduino-1.0.3/libraries/DS1307RTC -Ireferenced_model_includes -I../slprj/arduino/_sharedutils -IC:/ARDUIN~1.3/hardware/arduino/cores/arduino ../sfun_systime_get_wrapper.c -o sfun_systime_get_wrapper.o
In file included from ../sfun_systime_get_wrapper.c:39:
C:/arduino-1.0.3/libraries/DS1307RTC/DS1307RTC.h:12: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'DS1307RTC'
../sfun_systime_get_wrapper.c:40: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'sysTime'

Please let me know if you have any thoughts. I am wondering if this is still an issue with the newer Arduino support in later versions of ML/Simulink.

20 Apr 2013 Joshua Hurst

Thanks Giampy!

With your driver guide we were able to make driver blocks for:

- MPU6050 3 axis accelerometer/gyroscope
- HC-SR04 ultrasonic range/distance sensor
- L3GD20 3 axis gyroscope

And with all this we made a miniature Segway! You can find all the drivers and supporting libraries for the listed sensors here:

http://www.minseg.com/downloads

Thanks again!

20 Apr 2013 Joshua Hurst  
20 Apr 2013 phil

Thank you! It compiles and runs on the Arduino Mega. The screen however does not show characters properly and its all scrambled. I am not sure what is happening... :(
When using the arduino IDE everything works fine. Here is a zip with some screenshots. I would appreciate if you could perhaps have a look and see if i am missing something...
thanks again - phil (pprotschka@gmail.com)
link:
https://dl.dropboxusercontent.com/u/64527727/screenshots/screenshots.zip

19 Apr 2013 Giampiero Campa

Phil, i think the variable "lcd" needs to be defined as a global. Try to define it in the libraries pane, after all the includes, see page 16 of the guide.

19 Apr 2013 phil

./io_wrappers.cpp: In function 'void Serial_read(int, int, uint8_t*, int*)':
./io_wrappers.cpp:40: warning: 'libFcnOutput' may be used uninitialized in this function
cc1plus.exe: warning: command line option "-Wstrict-prototypes" is valid for Ada/C/ObjC but not for C++
cc1plus.exe: warning: command line option "-std=gnu99" is valid for C/ObjC but not for C++
./DDvers1_wrapper.cpp: In function 'void DDvers1_Outputs_wrapper(const boolean_T*, const real_T*, const real_T*, int_T)':
./DDvers1_wrapper.cpp:73: error: 'lcd' was not declared in this scope
./DDvers1_wrapper.cpp:77: error: 'lcd' was not declared in this scope
In file included from ./ert_main.c:18:
C:/MATLAB/Targets/R2012b/arduino-1.0/hardware/arduino/cores/arduino/Arduino.h:24:1: warning: "true" redefined
In file included from ./DisplayDriverv1.h:23,
from ./ert_main.c:17:
./rtwtypes.h:158:1: warning: this is the location of the previous definition
In file included from ./ert_main.c:18:
C:/MATLAB/Targets/R2012b/arduino-1.0/hardware/arduino/cores/arduino/Arduino.h:25:1: warning: "false" redefined
In file included from ./DisplayDriverv1.h:23,
from ./ert_main.c:17:
./rtwtypes.h:154:1: warning: this is the location of the previous definition

20 Mar 2013 Sebastian D'Amico  
19 Mar 2013 Roni Peer

Great walk through for newbies!

06 Mar 2013 phil

just what i was looking for!
it would be great if you guys could share your driver blocks!

i am currently writing one for an LCD display...quite a challenge.

28 Feb 2013 Giampiero Campa

NOTE: If you are working with external libraries and you get an “undefined reference” error that means that your code references objects defined elsewhere (in other files) and, at linking time, the linker cannot find where they are.

In this case you need to make sure that all the .c and .cpp files of the library you are using are in the current MATLAB folder and that they are all included in the "Includes" field of the "Libraries" pane of the S-Function Builder (include the .c and .cpp files directly not the .h files).

Also, make sure that you read the last section (i.e. the last 2 pages) of the driver guide, entitled: "Working with external libraries".

07 Feb 2013 Arkadi

Great tutorial, Thank you.

21 Jan 2013 Judicael Aubry

This is exactly what I was searching for. A Step-by-Step guide to develop new and custom Simulink blocks for Arduino targets.

Thank you !

09 Jan 2013 Tucker McClure

This is a good introduction to making all the device code I need inside blocks so that I can just drag and drop blocks and get all the processor-specific code. Say I need to add a new encoder. Copy and paste the block and change the pin numbers. Done. Thanks, Giampiero!

Updates
02 Jan 2013

The PDF document was slightly refined.

Contact us