Main Content

Log ROS Messages from Simulink to a Rosbag Logfile

A rosbag or bag is a file format in ROS for storing message data. These bags are often created by subscribing to one or more ROS topics, and storing the received message data in an efficient file structure. Using the ROS Logger app, you can log ROS messages from Simulink® models to rosbag files.

Open the Simulink model. This model is configured for ROS Simulation. To configure a new model for ROS simulation, under Apps, select Robot Operating System (ROS) and specify the appropriate ROS network configuration parameters.

open_system("logROSMessagesFromSimulinkExampleModel")

To open the ROS Logger app dialog, under Simulation tab, from Prepare section, select ROS Logger.

Follow these steps to configure the signals to be logged and the rosbag file parameters:

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

  • Select the signals to be logged in the rosbag file by checking their respective checkboxes under the Enable column.

  • The default name of the generated rosbag file will be modelName_mmddyy_hh_mm_ss.bag. To provide a custom name, from the Logfile name dropdown, select Custom. Then specify the desired File name.

  • Specify the Compression format to be uncompressed, bz2 or lz4.

  • Specify the desired chunk size for writing, in bytes.

Initialize the ROS network.

rosinit
Launching ROS Core...
Done in 0.58632 seconds.
Initializing ROS master on http://172.29.195.13:60590.
Initializing global node /matlab_global_node_29016 with NodeURI http://dcc667538glnxa64:40995/ and MasterURI http://localhost:60590.

Simulate the model.

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

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