Main Content

Use ROS Logger App to Save ROS Messages from Simulink

Use ROS Logger app to record ROS messages during Simulink® simulation, and obtain a rosbag file with fully synchronized ROS messages saved during simulation.

In this example, you will:

  • Load pre-defined 3D simulation environment provided by Automated Driving Toolbox(TM).

  • Configure ROS message logging in ROS Logger app.

  • Visualize logged ROS messages in ROS Bag Viewer app.

Load 3D Simulation Environment

Use the prebuilt Large Parking Lot scene created using the Unreal Engine 3D simulation environment in Simulink. To interactively select a sequence of waypoints from a scene and generate a custom vehicle trajectory, refer to Select Waypoints for Unreal Engine Simulation (Automated Driving Toolbox) example.

% Extract scene for visualization
sceneName = 'LargeParkingLot';
[sceneImage, sceneRef] = helperGetSceneImage(sceneName);
hScene = figure;
helperShowSceneImage(sceneImage, sceneRef)

% Interactively Select Waypoints
hFig = helperSelectSceneWaypoints(sceneImage, sceneRef);

% Prepare smooth poses for simulation
if exist('refPoses','var')==0 || exist('wayPoints','var')==0
    % Load MAT-file containing preselected waypoints
    data = load('waypointsForROSLoggerAppParking');

    % Assign to caller workspace
numPoses = size(refPoses{1}, 1);

refDirections  = ones(numPoses,1);   % Forward-only motion
numSmoothPoses = 10 * numPoses;      % Increase this to increase the number of returned poses

[smoothRefPoses,~,cumLengths] = smoothPathSpline(refPoses{1}, refDirections, numSmoothPoses);

Configure ROS Message Logging

To configure saving options, open the ROS Logger app under SIMULATION> PREPARE> ROS Logger. You can enable/disable ROS messages for saving, define custom file name, and rename messages saved to rosbag file based on your preference.


After configuring with ROS Logger app, run these commands to setup model parameters and run the simulation.

modelName = 'LogROSMessageFrom3DSimulation';
% Configure the model to stop simulation at 5 seconds.
simStopTime = 5;
set_param(gcs, 'StopTime', num2str(simStopTime));

% Create a constant velocity profile by generating a time vector
% proportional to the cumulative path length.
timeVector = normalize(cumLengths, 'range', [0, simStopTime]);

% Create variables required by the Simulink model.
refPosesX = [timeVector, smoothRefPoses(:,1)];
refPosesY = [timeVector, smoothRefPoses(:,2)];
refPosesT = [timeVector, smoothRefPoses(:,3)];
% Run the simulation
Launching ROS Core...
.......Done in 7.9891 seconds.
Initializing ROS master on
Initializing global node /matlab_global_node_65044 with NodeURI http://ah-sransing-old:61320/ and MasterURI http://localhost:53417.
Simulation Complete. Start logging ROS bag file...
Successfully logged ROS bag file to LogROSMessageFrom3DSimulation_110722_09_35_40.bag.

After running the simulation, you can see a rosbag file generated in your current working directory. Long running simulations take some time to generate rosbag files. You can proceed to inspect the bag with "rosbag info" after you see the message “Successfully logged ROS bag file to…”.

Visualize Logged ROS Messages

Open the ROS Bag Viewer app


Click Open and browse for the generated bag file and load it. Inspect the Topic List and Source Details panels on the left side of the app to check the bag file info. Create Image and Point Cloud visualizers from the toolstrip. Play the bag file to visualize the logged messages.

% Shutdown ROS network and close all windows
Shutting down global node /matlab_global_node_65044 with NodeURI http://ah-sransing-old:61320/ and MasterURI http://localhost:53417.
Shutting down ROS master on
close_system(modelName, 0)