Getting Started with Robot Operating System (ROS) on Raspberry Pi
This example shows you how to generate and build a standalone ROS node from a Simulink® model on the Raspberry Pi™ hardware.
In this example, you will configure a model to generate C++ code for a standalone ROS node that runs on the Raspberry Pi board. You will use Raspberry Pi Simulink blocks together with a ROS Subscribe block to blink the Raspberry Pi user LED. If you are new to ROS, we strongly recommend reviewing ROS Toolbox documentation.
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, such as "/odometry". Other components receive the message by subscribing to that topic. The ROS toolbox provides an interface between MATLAB® and Simulink and the Robot Operating System (ROS) that enables you to test and verify applications on ROS-enabled hardware such as Raspberry Pi. It 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 will 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 data in ROS messages
This example requires ROS Toolbox™.
Review the Get Started with ROS example
Review the Get Started with ROS in Simulink® example.
Raspberry Pi 4B board
To open the Simulink model, run this command in the MATLAB® Command Window.
Task 1 - Get Started
The Raspbian Linux® image provided by the Simulink Support Package for Raspberry Pi hardware includes a ROS Melodic installation. In this task, you will start a ROS master on the host computer and send messages to Raspberry Pi hardware using 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 10.10.10.2, use the following command:
rosnode list to see all nodes in the ROS network. Note that the only available node is the global MATLAB node created by
Next, list the available ROS topics:
The output should just list
/rosout topic used for console log messages.
3. Create an interactive Linux shell terminal to communicate with your Raspberry Pi board:
r = raspberrypi openShell(r)
The commands above will log you in with your username and password. Note that in the rest of this example, we assume that you logged in with the default username for the Raspbian Linux, pi.
4. On the interactive Linux command shell, initialize 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 and waits for a new message published to the
/rosout topic. On the MATLAB command line, execute the following commands to see that your Raspberry Pi has subscribed to the
rostopic info /rosout
The IP address of your Raspberry Pi board should show up in the subscriber list.
5. To publish a log message to the
/rosout topic from the host computer, execute the following commands on the MATLAB prompt:
b = rospublisher('/rosout'); msg = rosmessage(b); msg.Msg = 'Hello Raspberry Pi!' send(b,msg);
send(b,msg) is executed, you should see that the contents of the message you sent from your host computer is printed on the Raspberry Pi Linux shell.
6. Execute a Ctrl+C On the Raspberry Pi Linux command shell to stop listening messages published to
Task 2 - Configure Simulink Model to Generate ROS Node
In this task, you will configure a model to generate C++ code for a standalone ROS node that runs on your Raspberry Pi board.
1. Open the Raspberry Pi Getting Started with ROS model.
2. Go to the Modeling tab and press Ctrl+E to open Configuration Parameters dialog box.
Review the settings in the Hardware Implementation pane of the Configuration Parameters dialog, The Hardware board settings section contains settings specific to the generated ROS package, such as information to be included in the
package.xml file, ROS Catkin workspace being used for model build, etc. In
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. Go to Robot tab and click on Build & Run button to start deployment process.
Click on the View Diagnostics link at the bottom of the model toolbar to see the output of the build process.
Task 3 - Run and Verify ROS Node
In this task, you will run the newly-built ROS node and verify its behavior using a MATLAB command line interface for ROS.
1. Raspberry Pi Getting Started with ROS model receives messages published on the
/led topic and sets the state of the Raspberry Pi user LED based on the contents of this message. First, verify that a new topic called
/led has been generated:
rostopic info /led
You should see the IP address of your Raspberry Pi in the subscribers list.
2. Create a ROS publisher for the
b = rospublisher('/led')
std_msgs/Bool message type to represent the on / off state of the LED.
3. Send messages to Raspberry Pi board to blink the user LED for 10 seconds with a period of 0.5 second:
msg = rosmessage(b); for k = 1:10 msg.Data = 1; send(b,msg); pause(0.5); msg.Data = 0; send(b,msg); pause(0.5); end
4. Once you are done verifying the ROS node, stop it by executing the following on the MATLAB prompt:
You can re-start the ROS node at any time by executing the following command on the MATLAB prompt:
The second argument specifies the Catkin workspace used to build the ROS node.
Advanced Topics and Troubleshooting
Raspberry Pi ROS Melodic Installation The Raspbian Linux image provided by the Simulink Support Package for Raspberry Pi Hardware includes a ROS Melodic installation. A the time of writing, there were no binary packages available for ROS Melodic. Hence, ROS Melodic was installed from sources following the instructions provided here. To keep the ROS Melodic installation small, only
sensor_msgs ROS packages have been installed. A ROS Catkin workspace has been created to build Simulink models,
catkin_ws. You should use the
catkin_ws as your default Simulink model workspace to reduce compilation time.
Starting a ROS Master on Raspberry Pi The
raspberrypi object provides two methods to start and stop a ROS master on the Raspberry Pi board:
To start a ROS master on the Raspberry Pi, execute the following on the MATLAB prompt:
r = raspberrypi; startroscore(r);
To stop the
roscore application running on the Raspberry Pi:
When using a ROS master running on your Raspberry Pi, set ROS Master network address for simulation accordingly.