Simulate RoadRunner Scenarios with Actors Modeled in Simulink
These examples show how to author RoadRunner actor behavior in Simulink® to model scenarios with different kinds of actor behaviors, and then associate these behaviors to an actor in RoadRunner.
These examples assumes that:
You have a RoadRunner license and the product is installed. For more information, see Install and Activate RoadRunner (RoadRunner).
You have a RoadRunner Scenario license and the product is installed.
You have created a RoadRunner project folder named
MyRoadRunnerProject
. For more information, see RoadRunner Project and Scene System (RoadRunner).You have created and saved a RoadRunner scene named
MyExampleScene
and a scenario namedMyExampleScenario
for your project.
Author RoadRunner Actor Behavior Using Simulink
Create a model using the RoadRunner Scenario block, the RoadRunner Scenario Reader block, and the RoadRunner Scenario Writer block.
RoadRunner Scenario block — Establish the model interface with a scenario.
RoadRunner Scenario Reader block — Read the world state, including actor pose, velocity, color, and supervisory actions.
RoadRunner Scenario Writer block — Write an actor state to the scenario and report errors and warnings.
This example shows the Simulink model SimulinkVehicle.slx
that contains these three blocks. The model reads ActorID
, Pose
, Velocity
, and AngularVelocity
values. Then, a constant value gainArg
is added to the Pose
value. The values from the gainArg
parameter moves the vehicle on the RoadRunner road. The updated values are then sent back to the actor in the scenario.
In the model, bus object definitions are manually loaded using the function below as model preload function.
load(fullfile(matlabroot,'toolbox','driving','drivingdata','rrScenarioSimTypes.mat'));
Author RoadRunner Actor Behavior Using User-Defined Actions or Events in Simulink
To receive and process user-defined actions or events from RoadRunner Scenario in Simulink, you must prepare the following files:
A MAT file that maps parameters of a user-defined action or event in RoadRunner Scenario to the fields of a bus object.
A Simulink model that reads user-defined action or event parameters from a MAT file and modifies actor behavior or scenario flow accordingly.
Create a Bus Object for a User-Defined Action or Event
Use the Type Editor (Simulink) to construct
a bus object for a user-defined action or event. The bus object must replicate the
parameter information of a user-defined action or event as specified in RoadRunner Scenario. Add Simulink.BusElement
objects to the bus that
correspond to each parameter. You can then define properties such as data type,
dimension, and complexity.
Note
User-defined action or event parameters support all data types supported by RoadRunner scenarios. For more information, see Parameter Data Types (RoadRunner Scenario).
After defining the bus object, you must export the bus object definition to a MAT file. For more information about creating a MAT file from a bus object, see Type Editor (Simulink).
Process User-Defined Actions Using Simulink
Create a Simulink model that reads a user-defined action from a MAT-file and uses it to update actor behavior.
This example shows the Simulink model hUDA_SL.slx
, which contains the following blocks.
RoadRunner Scenario — Associate Action Name as entered in RoadRunner Scenario with the name of the MAT-file (Bus object name).
RoadRunner Scenario Reader — Read the user-defined action at runtime. Set Topic Category to
Action
and Action Type toUser-Defined
. Enter the name of the action in the Action Name box; the corresponding MAT-file is read during scenario simulation. Use a second RoadRunner Scenario Reader block to input the actor parameters of the associated vehicle in RoadRunner Scenario.RoadRunner Scenario Writer — Convey completion of a user-defined action to a scenario simulation by publishing an Action Complete event. Set Topic Category to
Event
and Event Type toAction Complete
. Upon receiving an Action Complete event, the simulation proceeds to the next action phase with the changed actor behavior. Use a second RoadRunner Scenario Writer block to update the behavior of the respective vehicle.
model_name = 'hUDA_SL.slx';
open_system(model_name);
Associate Actor Behavior in RoadRunner and Simulate Scenario
In your RoadRunner scenario, select the Library Browser and then the
Behaviors
folder. Then, right-click an empty space to create a new behavior. For this step, you can select any folder in the Library Browser.On the Attributes pane, set Platform to
MATLAB/Simulink
. As the File Name, use the location of your fileSimulinkVehicle.slx
orhUDA_SL.slx
.If your Simulink model file is in your working folder, you can enter the name of your file together with its extension such as
.slx
.You can also use the full path to enter the location of your file, for example
MyLocation\SimulinkVehicle.slx
.
This action creates a new behavior that you can attach to actors in your scenario.
Add a new
CompactCar
to your scenarioMyExampleScenario
.To associate the Simulink model behavior to a RoadRunner actor, select
CompactCar
. Then, in the Attributes section, in the Behavior box, addMyNewBehavior
toCompactCar
by clicking and dragging the behavior icon to the box. Save your scenario.Use these commands to specify
MyInstallationFolder
as the path to your RoadRunner installation folder and create the connection between MATLAB® and RoadRunner for only the first MATLAB installation.RRInstallationFolder = "MyInstallationFolder"; s = settings; s.roadrunner.application.InstallationFolder.PersonalValue = RRInstallationFolder; s.roadrunner.application.InstallationFolder.TemporaryValue = RRInstallationFolder;
To open the RoadRunner project
MyRoadRunnerProject
from MATLAB, use this command.rrapp = roadrunner('MyProjectLocation');
Open the scene
MyExampleScene
.rrapp.openScene('MyExampleScene');
Open the scenario
MyExampleScenario
.rrapp.openScenario('MyExampleScenario');
Get the simulation object to control simulation from MATLAB.
ss = rrapp.createSimulation();
Start the simulation from the command line.
ss.set('SimulationCommand','Start');
For more information about simulating your scenario in RoadRunner or controlling a scenario simulation using MATLAB, see Overview of Simulating RoadRunner Scenarios with MATLAB and Simulink.