Simulink Raspberry Pi Driver Blocks

June 13, 2015

Following on from adding support to wiringPi for the MCP4725 DAC, I wanted to add driver blocks to Simulink such that one could use them to create graphical models for the Raspberry Pi that could interface with the real-world – a workable alternative to expensive real-time targets.

Using the S-Function Builder and some other user created blocks (which didn’t work – see below) as a basis, the process isn’t too difficult thanks to the generic function prototypes wiringPi uses. All the low-level stuff is done in the library as explained in my previous post, the S-Function just has to call mcp4725Setup() at first step, followed by analogWrite() at each proceeding step. See the screenshots below.

‘Build’ creates the target language compile (.tlc) and .c code for the blocks such that Simulink Embedded Coder can incorporate them into the generated code. The process of generating, deploying to the Raspberry Pi and compiling over SSH is all very slick! Slick until compilation stops due to undefined references. The undefineds are the standard wiringPi functions – the Simulink makefile does not include the wiringPi library by default, even though the MATLAB Raspbian image has wiringPi installed. The error occurs with the older example blocks too so it looks like something changed at some point down the line.

Remote Build Makefile Setup

After a long time digging around in the remote build templates I finally found an undocumented command to get it working: xmakefilesetup. Run this in the MATLAB command window with the Raspberry Pi model open. The settings should be as below. Navigate to Linker > Arguments and add ‘lwiringPi’ on the end (this tells the linker to use the wiringPi library and so the wiringPi functions will be linked). If you’re using my ADC/DAC blocks with ADS1115 and MCP4725, you’ll also have to update the wiringPi library with my library by logging on via SSH and following these steps.

I created a model with four driver blocks with example usage that can be copied and pasted into other models – remember to ‘Build’ the s-functions in the model directories and install my version of wiringPi. It can be downloaded below.

rpi-driver-blocks