Main Content

Get Started with Robot Operating System on Raspberry Pi

This example shows you how to build a standalone robot operating system (ROS) node from a Simulink® model on a Raspberry Pi® hardware board.


In this example, you configure a model to generate C++ code for a standalone ROS node that runs on the Raspberry Pi board. You use Raspberry Pi Simulink blocks with a ROS Subscribe block to make the Raspberry Pi user LED blink. If you are new to ROS, review the ROS Toolbox.

ROS is a communication layer that allows different components of a robot system to exchange information in the form of messages. A component sends a message by publishing it to a particular topic. Other components receive the message by subscribing to that topic. ROS Toolbox provides an interface between MATLAB® and Simulink and the ROS environment that enables you to test and verify applications on ROS-enabled hardware such as Raspberry Pi boards. ROS Toolbox supports C++ code generation, enabling you to generate a ROS node from a Simulink model and deploy it to a ROS network.

In this example, you learn how to:

  • Set up the ROS environment on the Raspberry Pi board.

  • Create and run a Simulink model on the Raspberry Pi board to send and receive ROS messages.

  • Work with the data in the ROS messages.


Required Hardware

This example uses the Raspberry Pi 4B board.

Task 1 - Get Started

In this task, you start a ROS master on the host computer and send messages to Raspberry Pi hardware using a ROS communication interface.

1. First, start a ROS master on the host computer.

rosinit('NodeHost',<IP address of your computer>)

For example, if the IP address of your host computer is, use the following command.


2. Use the rosnode list command to list all the nodes in the ROS network. The only node available is the global MATLAB node created by rosinit.

rosnode list

Next, list the available ROS topics.

rostopic list

The output must include only the /rosout topic used for console log messages.

3. Create an interactive Linux® shell terminal to communicate with your Raspberry Pi board.

r = raspberrypi

These commands log you in with your username and password. This example assumes that you are using the Raspbian Linux.

4. On the interactive Linux command shell, initialize the ROS environment and set the ROS master to the IP address of the host computer and monitor messages coming from the host computer using.

pi@raspberrypi:~ $ source ~/catkin_ws/devel/setup.bash
pi@raspberrypi:~ $ export ROS_MASTER_URI=http://<Enter your host computer's IP address>:11311
pi@raspberrypi:~ $ rostopic echo /rosout

The last command does not return an output and waits for a new message to be published to the /rosout topic. At the MATLAB command line, execute the following commands to verify if your Raspberry Pi has subscribed to the /rosout topic.

rostopic info /rosout

The subscribers list includes the IP address of your Raspberry Pi board.

5. To publish a log message to the /rosout topic from the host computer, execute the following commands at the MATLAB command prompt.

b = rospublisher('/rosout');
msg = rosmessage(b);
msg.Msg = 'Hello Raspberry Pi!'

When MATLAB executes the send(b,msg) command, you can view the message you sent from your host computer in the Raspberry Pi Linux shell.

6. Press Ctrl+C at the Raspberry Pi Linux command shell to stop listening to the messages published to the /rosout topic.

Task 2 - Configure Simulink Model to Generate ROS Node

In this task, you configure a model to generate C++ code for a standalone ROS node that runs on your Raspberry Pi board.

1. Open the rosberrypi_getting_started Simulink model.

2. On the Modeling tab in the Simulink model, press Ctrl+E to open the Configuration Parameters dialog box.

Review the settings in the Hardware Implementation pane. The Hardware board settings section contains information related to the ROS package, such as the details that you include in the package.xml file, the ROS Catkin workspace that you use to build the model, and other details.

3. The model is configured to generate a ROS node in the ROS Catkin workspace, ~/catkin_ws, and automatically deploy to the Raspberry Pi board. On the ROS tab in the Simulink model, click Build & Run to start the deployment process.

Click the View Diagnostics link at the bottom of the model to view the output of the build process.

Task 3 - Run and Verify ROS Node

In this task, you run the newly built ROS node and verify its behavior using a MATLAB command line interface for ROS.

1. The Raspberry Pi Getting Started with ROS model receives messages published to the /led topic and sets the state of the Raspberry Pi user LED based on the contents of the messages. First, verify that the board is subscribed to the topic.

rostopic info /led

You can see the IP address of your Raspberry Pi in the subscribers list.

2. Create a ROS publisher for the /led topic.

b = rospublisher('/led')

The publisher b uses the std_msgs/Bool message type to represent the on and off state of the LED.

3. Send messages to the Raspberry Pi board to make the user LED blink after every 0.5 seconds.

msg = rosmessage(b);
for k = 1:10
    msg.Data = 1;
    msg.Data = 0;

4. Once you are done verifying the ROS node, stop it.


You can restart the ROS node at any time by executing the following command at the MATLAB command prompt.


The second argument specifies the Catkin workspace to use to build the ROS node.

Task 4 - Run Simulink Model in External Mode

You can run the rosberrypi_getting_started Simulink model in external mode where you can monitor the status of the LED on your Raspberry Pi hardware. Connect the Dashboard Scope block to the data signal. On the ROS tab of the Simulink model, in the Run on Hardware section, click Monitor & Tune and observe the LED output in the scope.

See Also