Guy on Simulink

Simulink & Model-Based Design

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.

Empty S-Function

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

Simulink Model that can Tweet

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.

TLC that can Tweet

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:

I received a Tweet from my Raspberry Pi!

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.

|

Comments

To leave a comment, please click here to sign in to your MathWorks Account or create a new one.