Main Content

Generate a Standalone ROS 2 Node from Simulink

This example shows you how to generate and build a standalone ROS 2 node from a Simulink® model. You configure a model to generate C++ code for a standalone ROS 2 node. Then, build and run the ROS 2 node on your host computer.


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. Alternatively, call open_system("robotControllerROS2").

  • Under ROS tab, in Prepare, 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.

  • 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 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)). It can be made smaller (e.g., 0.001 or 0.0001) but for current purposes 0.05 is sufficient.

  • Specify external ROS packages as dependencies: To specify external ROS 2 packages as dependencies to the generated ROS 2 node, specify appropriate toolchain options. In the Configuration parameters, under Code Generation > Toolchain Settings, specify the Build configuration as Specify from the drop-down. Then, you can specify the Required Packages, Include Directories, Link Libraries, Library Paths and Defines based on the external ROS 2 packages that you wish to integrate with the generated ROS 2 node.

  • Click OK.

Generate the C++ ROS 2 Node

In this task, you generate code for a standalone ROS 2 node, and automatically build, and run it on the host computer.

  • 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.

  • If you are deploying to a local device, set the environment variable ROS_DOMAIN_ID to the value 25, using setenv in MATLAB.

  • Set the ROS Middleware (ROS 2) of ROS 2 network. This example uses ROS Middleware as rmw_cyclonedds_cpp.

  • If you are deploying to a local device, set the environment variable RMW_IMPLEMENTATION to rmw_cyclonedds_cpp, using setenv in MATLAB.

  • In ROS tab, from the Deploy section dropdown, click Build & Run. 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 code generation completes, the ROS 2 node builds in the present working folder starts to run automatically in a synchronous fashion based on the sample time of the model. When running in Windows®, a Command window opens. Do not close the window, but use Ctrl+C to shutdown the ROS 2 node.

Use ros2 node to list all running nodes is the ROS 2 network. robotControllerROS2 should be in the displayed list of nodes.


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