Simulink support package for Arduino - Combining with cusom code?

I've recently purchased Matlab 2014a, and with it I've downloaded the Arduino Due support package. This is great, as I can essentially implement my controller on the arduino with no programming whatsoever - which is great, apart from the fact that I need to actually use code and libraries to obtain the value of the gyroscope over I2C, amongst many other things which are impossible (as far as I am aware) to do in Simulink.
Is there a way to somehow combine custom code with the power of the 'Run on target hardware' capability of Simulink? If not, isn't this a massive oversight?
If this is indeed the case, is there a way to obtain the Arduino code generated by Simulink to implement into my own code?

Answers (2)

Alex, you can implement MATLAB code (is this what you are referring to as custom code?) in Simulink, e.g., via the MATLAB function block (in User-Defined Functions).
To actually read the generated code you'd need the Embedded Coder Toolbox.

3 Comments

Sorry, I wasn't very clear about that - I was referring to C (Arduino) code. Basically, I am creating a Segway for a final year project in systems & control engineering. Currently, the Accelerometer/Gyro are communicating to the Arduino via I2C using a 3rd party I2C library and the motor controller comes supplied with it's own library, which is all contained within an Arduino project (C).
I was hoping that I could design the controller in Simulink and either export the code to a format that could be easily implemented in my existing sketch, or somehow include the C libraries in Simulink.
I've relentlessly tried compiling the code from Simulink using different methods (grt, grt(Visual Studio Project) etc...) and included all of the files in the simulink project but there are references and compiler errors everywhere which I just can't seem to get my head around.
Ideally, I'd be able to call the MODEL_initialise and MODEL_step functions from inside the Arduino project and save the outputs (motor voltages) to variables, which I can then apply to the motors using the library which is supplied with the motor controller.
Sorry for this somewhat lengthy reply, I'm just trying to fit as much info in as possible. Thanks for your help so far and I'd really appreciate it if you were able to point me in the right sort of direction with this one.
Check out the Legacy Code Tool.
Thanks. I've had a look and it can't seem to compile C++ files, which is what the libraries I'm using are compiled in. Any other solutions?

Sign in to comment.

Hallo together!
I wondered about the same thing. I would like to combine the capabilities of the Arduino support for Simulink with self written C-code. In my case it doesn't need to be the modified c-code used in Arduino sketches but "plain"/"real" c-code is just fine. Beeing able to use both I could design controller and filter in MATLAB/Simulink and at the same time code the rest in c.
So I'd be happy about ideas, too.
Best Regrads, Florian

2 Comments

In the end, I just ended up turning the Simulink controller into C code using the embedded coder, then integrated this with my existing code. Hope this helps.
Hey! Thank you for the reply. I will try this!

Sign in to comment.

Categories

Products

Asked:

on 8 Apr 2014

Community Treasure Hunt

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

Start Hunting!