Main Content

Get Started with Simulink Support Package for Raspberry Pi Hardware

This example shows you how to use Simulink® Support Package for Raspberry Pi® Hardware to run a Simulink model on Raspberry Pi hardware.

Introduction

Simulink Support Package for Raspberry Pi Hardware enables you to create and run Simulink models on Raspberry Pi hardware. The support package includes a library of Simulink blocks for configuring and accessing Raspberry Pi peripherals and communication interfaces.

In this example you will learn how to configure a simple Simulink model to generate code for Raspberry Pi hardware and run the generated code on the board to periodically turn an LED on and off.

Prerequisites

If you are new to Simulink, we recommend completing Interactive Simulink Tutorials and Getting Started with Simulink.

Required Hardware

To run this example you need the following hardware:

  • Raspberry Pi board

Task 1 - Review Raspberry Pi Block Library

Simulink Support Package for Raspberry Pi Hardware provides an easy way to create algorithms that use Raspberry Pi peripherals by using blocks that can be added to your raspberrypi_ model. The blocks are used to configure the associated sensors and actuators, as well as to read and write data to them.

1. Enter slLibraryBrowser at the MATLAB® prompt. This opens the Simulink Library Browser.

2. In the Simulink Library Browser, navigate to Libraries > Simulink Support Package for Raspberry Pi Hardware. Right-click on the LED block and choose Add block to a new model. You will see a new Simulink model with an LED block.

3. Double-click the LED block. Review the block mask, which contains a description of the block and parameters for configuring the associated user LED. Click on View LED location button to see a diagram of user LEDs on Raspberry Pi hardware.

Task 2 - Configure Simulink Model for Code Generation

In this task, you will configure a simple model blinking an on-board LED to run on Raspberry Pi hardware.

*1.*Open the raspberrypi_gettingstarted_unconfigured Simulink model.

To configure the model for Raspberry Pi hardware:

a. Open the Modeling tab and press Ctrl+E to open Configuration Parameters dialog box.

b. Go to Hardware Implementation > Hardware board and select Raspberry Pi.

For convenience, a preconfigured Simulink model that generates code for Raspberry Pi hardware is also available.

Open the raspberrypi_gettingstarted Simulink model.

2. Verify the connectivity parameters for your Raspberry Pi hardware by following the steps below:

a. Open the Modeling tab and press Ctrl+E to open Configuration Parameters dialog box.

b. Go to Hardware Implementation > Target hardware resources and verify the device address settings.

The username and the password shown on the screen shot above shows the default username and password for the Raspbian Linux® distribution.

3. On the Hardware tab of the Simulink model, in the Mode section, select Run on board and then click Build, Deploy & Start to generate code for the Simulink model.

4. The generated code is built on theRaspberry Pi hardware and run automatically. When the model starts running on the Raspberry Pi hardware, observe that the user LED on the board blinks with a period of 1 second.

Task 3 - Stop Simulink Model Running on Raspberry Pi Hardware

Simulink Support Package for Raspberry Pi Hardware generates a Linux executable for each Simulink model you run on the Raspberry Pi hardware. The generated executable is an ELF file that has the same name as the Simulink model and is stored in your home directory on the Raspberry Pi hardware.

1. To run/stop a Simulink model, you use the runModel and stopModel methods of the raspberrypi communication object. First, create a communication object to the Raspberry Pi hardware:

r = raspberrypi;

This command generates a Raspberry Pi object that is your gateway to communicating with your Raspberry Pi hardware from MATLAB command line. Execute the following on the MATLAB command line to see if the raspberrypi_gettingstarted_unconfigured model is running:

isModelRunning(r,'raspberrypi_gettingstarted_unconfigured')

The isModelRunning method of raspberrypi returns true if the model is running on the Raspberry Pi hardware.

2. Execute the following on the MATLAB command line to stop the Simulink model you ran in Task 2:

stopModel(r,'raspberrypi_gettingstarted_unconfigured')

You should see the user LED stop blinking at this point. You can also use the isModelRunning method to test if the model has stopped.

3. To run a previously built Simulink model on your board, you use runModel method. In order to run the raspberrypi_gettingstarted_unconfigured model, execute the following on the MATLAB command line:

runModel(r,'raspberrypi_gettingstarted_unconfigured')

4. The output of the executable running on the Raspberry Pi hardware is re-directed to a log file. To see the contents of the log file for the executable corresponding to the raspberrypi_gettingstarted_unconfigured model, execute the following on the MATLAB command line:

system(r,'cat raspberrypi_gettingstarted_unconfigured.log')

In case of unexpected results when running a Simulink model on hardware, examine the contents of the log file for diagnostic messages. You can also bring the log file back to your host computer by executing the following on the MATLAB command line:

getFile(r,'raspberrypi_gettingstarted_unconfigured.log')

The getFile command above saves the log file to the current MATLAB directory.

See Also

Install and Get Started With Simulink Support Package for Raspberry Pi