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 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 4.7935 seconds.
Initializing ROS master on
Initializing global node /matlab_global_node_73151 with NodeURI http://ah-avijayar:49165/ and MasterURI http://localhost:49302.
Simulation Complete. Start logging ROS bag file...
Successfully logged ROS bag file to LogROSMessageFrom3DSimulation_072222_14_13_24.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 Ground Truth Labeler app


Then, select FILE>Import>Add Signals. Change the Source type to 'Rosbag' and browse for the new generated rosbag file. You can then start visualizing and labeling logged data.

% Shutdown ROS network and close all windows
Shutting down global node /matlab_global_node_73151 with NodeURI http://ah-avijayar:49165/ and MasterURI http://localhost:49302.
Shutting down ROS master on
close_system(modelName, 0)