# Guy and Seth on Simulink

## A Tweeting S-Function for the Raspberry Pi

I recently bought a Raspberry Pi. After trying a few of the demos included with the Simulink support package for Raspberry Pi, I began thinking about what I could do next.

Just for fun, I thought... could I get a Simulink model deployed on the Raspberry Pi to send Tweets?

Sending Tweets From Linux

In case you were not aware, the Raspberry Pi is running a Linux-based operating system. Knowing that, I thought that if I could find a way to Tweet from the Linux shell, I could Tweet from an S-function. (as you know... if you can program it, you can put it in an S-function)

I did some search and figured out that the simplest way to send Tweets from the Linux shell was using the SuperTweet.net Twitter API.

You first need to install cURL. In a Linux shell, execute:

sudo apt-get install curl

Once this is done, you can send Tweets using cURL and a line like:

curl -u USERNAME:PASSWORD -d status="My First Tweet Test" http://api.supertweet.net/1.1/statuses/update.json

Now that I know I can send Tweets programmatically, I know I can send Tweets from Simulink generated code!

An S-Function that sends Tweets

If you know how to accomplish something in C/C++, there are always many ways to incorporate that in the code generated from Simulink.

The S-Function Builder and the Legacy Code Tool are two ways to help this process. Those two tools will help you to create C and TLC wrappers to inline your C code within the C code generated by Simulink.

Personally, for simple cases like this one, I prefer to write the S-function and TLC. I feel like this gives me a better understanding of the process.

For my first test, I wrote a very simple S-function, no input and no output that does nothing in simulation.

I placed it inside a triggered subsystem because I do not want to send a Tweet at every time step.

Finally, I wrote a TLC file to inline the S-function. For this simple test, all I had to do it use system command to execute the same line as I tested previously in a shell.

I hit Ctrl+B to build the model and download it on the Raspberry Pi automatically. Then I went back to my web browser, refreshed the page, and got pretty impressed when I saw:

What's next?

Now that I know I can make simulink models for my Raspberry Pi that send Tweets, I could try a few more things. I could input a few signals into my S-function to add data to my Tweets. I could define a few different strings to be Tweeted depending on some signal value. I could put the C code sending the Tweet in a seperate C file to make the inlining simpler, etc...

Let me know if you have suggestions on what I could do with my Raspberry Pi and Simulink!

Now it's your turn?

Do you have low-cost target hardware supported by Simulink? What kind of funky application did you implement? Let us know by leaving a comment here.

### 6 Responses to “A Tweeting S-Function for the Raspberry Pi”

1. Eric replied on :

Hi Guy, it’s been a long time since I’ve worked with S-functions, but if you wanted to have the code work during simulation then you would have put it into the S-function right, and then also the “same” code into the TLC file. What’s the recommended way to reuse the same code in both an S-function and a TLC file? It seems like bad code practice to have to write/maintain code in two places instead of keeping it in one central function/file, so I’m sure there must already be a good way to avoid this. Also, I’m curious about the S-function Builder vs the Legacy Code Tool – their functionality seemingly overlaps quite a bit, is there a plan to migrate to one or the other? Cheers

2. Guy Rouleau replied on :

@Eric, very good comment. Since my host is machine runs on Windows and the target is Linux, I thought my Linux shell syntax would not be relevant on Windows. However I just checked today and it seems cURL can also be installed on Windows. So it might work with minor midifications.

When the same functionality can be used for simulation and generated code, typically you create a C S-function wrapper for simulation, and a TLC wrapper for the code generation. Both calling the same functionality. This process can be easily automated with Signal Builder and Legacy Code Tool.

About those two overlapping, your are right. In many cases using one versus the other depends if you prefer using a GUI (s-function builder) or write code (Legacy Code Tool).

When you analyze those two tools in details, you will notice that they have different limitations and specific features. For example S-function Builder allows you to create wrapper around mdlOutput, mdlDerivative and mdlUpdate (to manage states if your function has some). Legacy Code Tool only does mdlOutput.

I am not aware of short terms plans to migrate or combine those.

3. Eric replied on :
4. Guy Rouleau replied on :

That is exactly it, thank you very much for sharing the link.

5. Robert Samson replied on :

You should check out the library at http://www.temboo.com. There are all sorts of APIs (including twitter) that allow you to do a bunch of stuff from a raspberry pi

6. Sam Nazari replied on :

This is great!

 Name (required) E-mail (required, will not be published) Website (optional) Spam protection (required): What is 6 + 8 ?

Wrap code fragments inside <pre> tags, like this:

<pre class="code">
a = magic(3);
sum(a)
</pre>


If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).

Guy Rouleau and Seth Popinchalk are Application Engineers for MathWorks. They write here about Simulink and other MathWorks tools used in Model-Based Design.

These postings are the author's and don't necessarily represent the opinions of MathWorks.