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.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
ashah
  • 193
  • 1
  • 3
  • 14
  • 1
    I'm assuming that you've already looked [here](http://www.mathworks.com/academia/arduino-software/arduino-simulink.html). 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. – grungetta Oct 01 '12 at 10:04
  • 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. – grungetta Oct 01 '12 at 10:08
  • 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. – RichColours May 04 '14 at 09:21

1 Answers1

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

RYS
  • 412
  • 6
  • 19
RichColours
  • 650
  • 1
  • 4
  • 14
  • 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](http://www.mathworks.com/help/simulink/ug/about-run-on-target-hardware-feature.html) 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. – grungetta Sep 11 '14 at 20:58