Main Content

Generate a ROS 2 Component Node from Simulink

ROS 2 component nodes provide enhanced modularity and efficiency when integrating multiple different algorithms for robotic applications. Unlike traditional nodes that run as separate processes, you can dynamically load and execute ROS 2 component nodes during runtime in a shared process. This enables more efficient resource utilization and easy testing of individual algorithms in a complex robotic system.

You can use Simulink® to generate code for a model as a ROS 2 component shared library and deploy it to a device running ROS 2. On the device running ROS 2, you can then load the generated component.

Prerequisites

Configure Simulink Model for ROS 2 Component Node Generation

Follow these steps to configure a Simulink model for ROS 2 component node generation:

  1. Open your Simulink model.

  2. On the ROS tab of the Simulink Toolstrip, in the Prepare section, click Hardware settings. In the Hardware implementation pane, Hardware board settings section contains settings specific to the generated ROS 2 package, such as the information to be included in the package.xml file.

  3. In the left pane, select Solver and, in the Solver selection section, ensure that Solver Type is set to Fixed-step, and set Fixed-step size to desired value. In generated code, the fixed-step size defines the actual time step, in seconds used for the model update loop.

  4. On the ROS tab, from the Deploy Type section, select the deployment type as Component Node.

Generate ROS 2 Component Node from Simulink

Follow these steps to generate ROS 2 component node from Simulink:

  1. On the ROS tab, in the Connect section, specify the ROS Network as Robot Operating System 2 (ROS 2).

  2. From the Deploy To drop down, specify Localhost, if you are deploying to the local machine. If you are deploying to a remote device, specify Remote Device and configure the remote device settings by selecting Manage Remote Device.

  3. In the Deploy section drop down, click Build Model. If you are deploying to the local machine, click Generate Code.

  4. Click on the View Diagnostics link at the bottom of the model toolbar to see the output of the build process. Simulink generates a shared library for the model, and copies it to the local or remote device, and builds the ROS 2 workspace.

Load ROS 2 Components on Deployed Device

After you deploy the ROS 2 component shared library for the model, you can load the component on the ROS 2 device in one of these ways:

  • Run-time composition using component container

  • Compile-time composition using hardcoded nodes

  • Composition using a launch file

This example shows the steps to load the ROS 2 component with run-time composition using a component container. For more information about the other two methods, see Composing multiple nodes in a single process in the ROS 2 documentation.

The deployed ROS 2 component node has these characteristics:

  • ROS 2 package name — Name of the Simulink model, in lower case

  • Component node class name with namespace — SLROS2::SLROSComponent

Note that a Simulink model always deploys as a single ROS 2 component node whose node class type is SLROS2::SLROSComponent. The component node inherits only the name of the ROS 2 package from the Simulink model name.

Start the component container in a terminal. You must source setup.bash under your ROS 2 workspace before starting the component container.

$ ros2 run rclcpp_components component_container

In a new terminal, load the ROS 2 component node. You must source setup.bash in this new terminal as well.

$ ros2 component load /ComponentManager packageName SLROS2::SLROSComponent

To check whether the component has been loaded, open a new terminal and enter this command.

$ ros2 component list

If the component has been loaded correctly, the corresponding ROS 2 node is now discoverable on the network. Verify that the node is listed on the network.

$ ros2 node list

Next Steps

For an example of deploying ROS 2 component nodes for a sign following robot application, see Generate and Deploy ROS 2 Component Nodes for Sign Following Robot Using Simulink.

See Also

Topics