How do you program the Arduino in Simulink so you can read the sensors via I2C and SPI?

Hey.
I'm trying to build a control system in Simulink and then transfer it to an Arduino MEGA. The problem is that the accelerometer I've only communicate via I2C or SPI protocol and I do not know how it builds up in Simulink. I am relatively new to creating my own C \ C + + code in Simulink. Does anyone know how I can solve this problem? I thought on creating a separate block from the library that I have for the sensor that I use when I do programming in Arduinos own software.
Matlab version: 2012a Operating System: Windows 7 Arduino: MEGA Sensor: ADXL345

回答 (4 件)

Kaustubha Govind
Kaustubha Govind 2012 年 4 月 17 日

0 投票

If you know how to do this in C using Arduino libraries, you can generate a custom S-function block, using the Legacy Code Tool.
Tim Rothmann
Tim Rothmann 2012 年 5 月 9 日

0 投票

how does it work to include some arduino libraries like wire.h for example? i always get some compile errors. can you give me some small example?

9 件のコメント

Kaustubha Govind
Kaustubha Govind 2012 年 5 月 10 日
Tim: Could you post your compile errors. I don't have any example for Arduino, but the documentation I pointed to has a generic C example.
Tim Rothmann
Tim Rothmann 2012 年 5 月 11 日
so i just coded the LCT example with including wire.h additionaly.
i guess the most important thing is "syntax error: identifier __attribute__".
i know that __attribute__ is a specific gcc identifier and not standard c/c++.
so what can i do?
system: win 7
compiling with a linux machine?
Kaustubha Govind
Kaustubha Govind 2012 年 5 月 11 日
How would you compile such Arduino-specific code for running on a Windows host (outside of MATLAB)? Perhaps that same compiler needs to be used. Unfortunately, MEX supports a limited subset of compilers: http://www.mathworks.com/support/compilers/R2012a/win64.html
But perhaps you can build a DLL separately and call into that DLL from a MEX-function. You may need to worry about compatibility between Visual Studio and the Arduino compiler though.
Tim Rothmann
Tim Rothmann 2012 年 5 月 11 日
outside of matlab the arduino IDE uses some kind of avr-gcc like winavr.
MEX only supports the MS compilers.
right now i'm trying to use gnumex to use cygwin/mingw with matlab...can i use this wie LCT?or maybe with the s-fcn-builder?
i dont know if its possible to build DLLs with avr-gcc...i dont think so.
Kaustubha Govind
Kaustubha Govind 2012 年 5 月 12 日
I think you can use Legacy Code Tool to generate the S-function and then use gnumex to compile it into a MEX-file.
Tim Rothmann
Tim Rothmann 2012 年 5 月 15 日
so, after trying the whole weekend i have still some problems. sometimes the linker yells and sometimes other things dont work.
i wonder if there is anyone who done this before?just communicate with some sensors...
Kaustubha Govind
Kaustubha Govind 2012 年 5 月 15 日
Don't know too much about this myself, but there is the Arduino Support Package (http://www.mathworks.com/academia/arduino-software/) - you may try examining those files to see if they're doing something similar.
Tim Rothmann
Tim Rothmann 2012 年 5 月 28 日
after spending hours and hours now i'm trying to do the other way and generating C-code from simulink and import it to eclipse.
are there some papers about code generation for atmega?especially to acces inputs and outputs?
Kaustubha Govind
Kaustubha Govind 2012 年 5 月 29 日
Tim: Not sure what you mean by "to access inputs and outputs" - do you mean accessing sensor/actuator signals? Typically there needs to be a blockset containing blocks corresponding to the various board peripherals that represent the sensors and actuators.

サインインしてコメントする。

Zhuo Li
Zhuo Li 2013 年 5 月 2 日
編集済み: Zhuo Li 2013 年 5 月 2 日

0 投票

These two links may be helpful to your case
ADLX345 i2c Driver for Arduino Mega
I2C block for ArduinoIO simulink package

1 件のコメント

joshua hadi
joshua hadi 2013 年 5 月 6 日
Hello Zhuo Li, I want to ask about using adxl 345 in simulink, could we divide the x,y,z output reading into the 3 blocks (x,y,z blocks) because i saw your i2c read block only can give 1 output arrow. thanks.

サインインしてコメントする。

Mohit Khandelwal
Mohit Khandelwal 2018 年 9 月 10 日
% create an arduino object
a = arduino('com3', 'uno');
% start the loop to blink led for 10 seconds
for i = 1:10
writeDigitalPin(a, 'D11', 1);
pause(0.5);
writeDigitalPin(a, 'D11', 0);
pause(0.5);
end
% end communication with arduino
clear a

カテゴリ

ヘルプ センター および File ExchangeArduino Hardware についてさらに検索

製品

質問済み:

2012 年 4 月 6 日

回答済み:

2018 年 9 月 10 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by