Main Content

Generate Code to Manually Deploy a ROS 2 Node from Simulink

This example shows you how to generate C++ code from a Simulink® model to deploy as a standalone ROS 2 node. The code is generated on your computer and must be manually transferred to the target ROS device. No connection to the hardware is necessary for generated the code. For an automated deployment of a ROS 2 node, see Generate a Standalone ROS 2 Node from Simulink.

Prerequisites

Configure A Model for Code Generation

Configure a model to generate C++ code for a standalone ROS 2 node. The model is the proportional controller introduced in the Feedback Control of a ROS-Enabled Robot Over ROS 2 example.

  • Open the robot feedback control model configured for ROS 2.

open_system("robotFeedbackControllerROS2");
  • Under ROS tab, click Hardware settings. In the Hardware implementation pane, Hardware board settings section contains settings specific to the generated ROS 2 package, such as information to be included in the package.xml file. Change Maintainer name to ROS 2 Example User, click Apply.

  • The model requires variable-sized arrays. To enable this option, check variable-size signals under Code Generation > Interface > Software environment.

  • In the Solver pane, ensure that the solver Type is set to Fixed-step, and set Fixed-step size to 0.05. In generated code, the Fixed-step size defines the actual time step, in seconds, that is used for the model update loop (see Execution of Code Generated from a Model (Simulink Coder) (Simulink Coder)). It can be made smaller (e.g., 0.001 or 0.0001) but for current purposes 0.05 is sufficient.

  • Click OK.

Configure the Build Options for Code Generation

After configuring the model, you must specify the build options for the target hardware and set the folder or building the generated code.

Open the Configuration Parameters dialog. Under the Modeling tab, click Model Settings.

In the Hardware Implementation tab, under Target hardware resources, click the Build options group. Set the Build action to Build. This setting ensures that code generated for the ROS 2 node without building it on an external ROS 2 device.

Generate and Deploy the Code

In this task, you generate source code for ROS 2 node, manually deploy to Ubuntu Linux system, and build it on the Linux system.

  • In MATLAB®, change the current folder to a location where you have write permission.

  • Under the Simulation tab, in Prepare, select ROS Toolbox > ROS Network.

  • Set the Domain ID (ROS 2) of ROS 2 network. This example uses Domain ID as 25.

  • In ROS tab, from the Deploy section, click Build Model. If you get any errors about bus type mismatch, close the model, clear all variables from the base MATLAB workspace, and re-open the model. Click on the View Diagnostics link at the bottom of the model toolbar to see the output of the build process.

Once the build completes, a src folder which contains the package source code will be written to your folder.

Compress the src folder to a tar file by executing the following command in MATLAB Command Window:

>> tar('src.tar','src');

After generating the tar file, manually transfer it to the target machine. This example assumes you are using the virtual machine from Get Started with Gazebo and Simulated TurtleBot. The virtual machine is configured to accept SSH and SCP connections. If you are using your own Linux system, consult your system administrator for a secure way to transfer files.

Ensure your host system (the system with your src.tar file) has an SCP client. For Windows® systems, the next step assumes that PuTTY SCP client (pcsp.exe) is installed.

Use SCP to transfer the files to the user home director on the Linux virtual machine. Username is user and password is password. Replace <virtual_machine_ip> with your virtual machines IP address.

  • Windows host systems:

pscp.exe src.tar user@<virtual_machine_ip>:

  • Linux or macOS host systems:

scp src.tar user@<virtual_machine_ip>:

Build and Run the ROS 2 Node

On the Linux system, execute the following commands to create a ROS 2 workspace and decompress the source code. You may use an existing ROS 2 workspace.

mkdir ~/ros2_ws_simulink
tar -C ~/ros2_ws_simulink/ -xvf ~/src.tar

Build the ROS 2 node using the following command in Linux. Replace <path_to_ros2_ws> with the path to your ROS 2 workspace. In this example, the <path_to_ros2_ws> would be ~/ros2_ws_simulink. (Note: There might be some warnings such as unused parameters during the build process. These parameters are needed only for Simulink environment, it would not affect the build process). If the node uses custom messages, you must manually copy the necessary custom message packages in <path_to_ros2_ws>/msg folder before building the node.

cd <path_to_ros2_ws>
source /opt/ros/foxy/local_setup.sh
colcon build

Verify that the node executable was created using:

file ~/ros2_ws_simulink/install/robotfeedbackcontrollerros2/lib/robotfeedbackcontrollerros2/robotFeedbackControllerROS2

If the executable was created successfully, the command lists information about the file. The model is now ready to be run as a standalone ROS 2 node on your device.

(Optional) You can then run the node using these commands. Replace <path_to_ros2_ws> with the path to your ROS 2 workspace.

Double click Gazebo Empty and ROS Bridge on virtual machine desktop to set up the Gazebo environment. Setup environment variables and run the ROS 2 node using:

export ROS_DOMAIN_ID=25
source /opt/ros/foxy/local_setup.sh
~/<path_to_ros2_ws>/install/robotfeedbackcontrollerros2/lib/robotfeedbackcontrollerros2/robotFeedbackControllerROS2

Note: It is possible that the robot spins at an unexpected location, this is because the pose and world is offset in Gazebo. Restart virtual machine and rerun Gazebo and the node.

You can also use ros2 node to list all running nodes in the ROS 2 network. robotFeedbackControllerROS2 should be in the displayed list of nodes.

ros2('node','list')

Verify that this ROS 2 node publishes data on the ROS 2 topic, /cmd_vel, to control the motion of simulated robot.

ros2('topic','list')

If you cannot see the expected node and topic, try to set ROS_DOMAIN_ID using the setenv command in MATLAB Command Window.

setenv("ROS_DOMAIN_ID","25")