Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create Simulink block for Arduino library like liquidcrystal

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.

like image 329
ashah Avatar asked Oct 01 '12 02:10

ashah


1 Answers

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

like image 77
4 revs, 4 users 79% Avatar answered Sep 29 '22 10:09

4 revs, 4 users 79%