Create Simulink block for Arduino library like liquidcrystal
Asked Answered
D

1

6

I want to use Simulink to create programs for Arduino Uno hardware. But I would like to use existing libraries, like liquidcrystal, to handle output to an LCD display. How can I create an s-function that incorporates the liquidcrystal class file?

I think the s-function is the way to go, because the existing Simulink Arduino library blocks are actually s-functions that are masked. I am using the Simulink student version so I don't think I can compile C code from a model and incorporate it into a project in the Arduino IDE.

Dmitri answered 1/10, 2012 at 2:42 Comment(3)
I'm assuming that you've already looked here. The Arduino blocks from MathWorks shouldn't require any additional toolboxes to run. The student version of MATLAB/Simulink doesn't necessarily have any less functionality than the standard version, but you may be limited by what toolboxes you have installed. Use the ver command to get a list of toolboxes that you have available. That may greatly influence how you approach this problem.Continual
Also, can you give more info on the liquidcrystal class file. Is this an Arduino library that you're referring to? If you have some links that provide more info on the exact file that you'd like to be using, that would be very helpful in understanding exactly what you're aiming for.Continual
What version of MATLAB are you using? Use ver at the command prompt - do you have Real-time Workshop, Simulink Coder or Embedded Coder? If you have none of those code generation tools then you're going to struggle to create a program from Simulink, you'll only be able to run as simulation.Birdwatcher
B
1

I'll turn my comment into an answer ... :)

Okay so to create a program from Simulink you'll need code generation tools installed. ver will tell you if you have Simulink Coder, and optionally Embedded Coder. If you're using an older version of MATLAB then it's called Real-time Workshop. Without one of those you're not going to be code-generating anything.

So assuming you can code-generate:

S-functions are they way to go: http://www.mathworks.co.uk/help/simulink/s-function-basics.html

... although there are various tools to assist with creating S-functions.

In short, the process is:

  • Take a copy of the sfunc_basic.c (or something like that) S-function C file template and populate accordingly. This is not trivial by any means, it can take quite a while to get it to compile (see below) and not crash MATLAB when it gets instantiated in a model.
  • Compile with mex command (see the documentation).
  • Use block in model. Create yourself a test harness model that does something very simple. Use the minimum model blocks necessary to create a working solution.
  • Create a TLC file which will define how to use block data at compile and run time in the model C program. In here you will be making calls to the liquidcrystal library.
  • Code generate / fix errors with TLC and or S-function / fix errors in generated code / repeat until complete :)

All of those steps can be expanded on a lot. The process is a bit of a haul the first time, but you'll learn a lot about S-functions, the model.RTW file, TLC and debugging all of the above.

Oh yes, debugging the S-function can be a ball-acher. Be prepared to use something like visual studio to "attach" to the matlab process to debug it.

Rich

Birdwatcher answered 4/5, 2014 at 9:39 Comment(1)
One minor thing that I think is worth clarifying/adding: you're right that you need the toolboxes that you mentioned in order to generate C code, for example, but they aren't necessary simply to generate an executable targeted to Arduino. You can use Run-On-Target Hardware in base Simulink to deploy a Simulink model to Arduino. That said, the feature obviously is very limited compared to the toolboxes you mentioned, so maybe not the best approach here since the OP wants to incorporate external libraries.Continual

© 2022 - 2024 — McMap. All rights reserved.