Main Content

Log ROS 2 Messages from Simulink to ROS 2 Bag File

This example shows how to save ROS 2 message data from Simulink® to a ROS 2 Bag File.

ROS 2 bag files are used for storing message data. Their primary use occurs in the logging of messages transmitted over a ROS 2 network. Using the ROS 2 Logger app, you can log ROS 2 messages from Simulink® models to ROS 2 bag files.

Open the Simulink model. This model is configured for ROS 2 Simulation. To configure a new model for ROS 2 simulation, under Apps, click Robot Operating System (ROS), select Robot Operating System 2 (ROS 2) and click Finish. You can specify the appropriate ROS 2 network configuration parameters by clicking ROS Network in Prepare section under Simulation tab in Simulink.

open_system("logROS2MessagesFromSimulinkExampleModel")

ROS 2 Logger Model

Follow these steps to configure the signals to be logged and the ROS 2 bag file parameters.

1. To log the bus signal to the ros 2 bag file, right click the bus signal and select Log Selected Signals.

2. Open the ROS 2 Logger app dialog by selecting ROS Logger under Simulation tab from Prepare section.

ROS 2 Logger App

3. Ensure that Enable ROS signal logging to ROS 2 Bag file is checked.

4. You can see the selected signal is checked under the Enable Column of the app.

5. The default name of the generated ROS 2 bag folder will be modelName_mmddyy_hh_mm_ss.db3. To provide a custom name, from the Logfolder name dropdown, select Custom. Then specify the desired Folder name.

6. You can then specify the Storage Format to sqlite3 or mcap. If you select sqlite3, you can specify Compression format, Compression mode, Cache size (Bytes), Split duration, and Split size (KB) in the app. For mcap storage format, you can also specify Storage Configuration Profile in addition to these aformentioned properties.

7. To stop logging the selected signal, uncheck the signal in the Enable Column.

8. Simulate the model.

sim("logROS2MessagesFromSimulinkExampleModel");
Simulation Complete. Start logging ROS bag file...
Successfully logged ROS bag file to logROS2MessagesFromSimulinkExampleModel_012723_21_10_31.

After simulation, logging messages to ROS 2 bag file begins and completes with the success message on the Diagnostic Viewer.