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
MyExampleSceneand a scenario named
MyExampleScenariofor 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.
This example shows the Simulink model
SimulinkVehicle.slx that contains these three blocks. The model reads
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.
Author RoadRunner Actor Behavior Using User-Defined Actions in Simulink
To receive and process user-defined actions from RoadRunner Scenario in Simulink, you must prepare the following files:
A MAT-file that maps parameters of a user-defined action in RoadRunner Scenario to the fields of a bus object.
A Simulink model that reads user-defined action parameters from a MAT-file and modifies actor movement accordingly.
Create User-Defined Action MAT-File
Create a bus object representing a user-defined action to then export into a MAT-file.
Use the Type Editor to create a
Simulink.Bus object for a
user-defined action . Add
Simulink.BusElement objects to the bus
object that correspond to the parameters of the user-defined action. You can then
define properties such as data type, dimension, and complexity for the
The data type of all user-defined action parameters is a string.
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
Actionand Action Type to
User-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
Eventand Event Type to
Action 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
Vehiclesfolder. 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 file
If your Simulink model file is in your working folder, you can enter the name of your file together with its extension such as
You can also use the full path to enter the location of your file, for example
This action creates a new behavior that you can attach to actors in your scenario.
Add a new
CompactCarto your scenario
To associate the Simulink model behavior to a RoadRunner actor, select
CompactCar. Then, in the Attributes section, in the Behavior box, add
CompactCarby clicking and dragging the behavior icon to the box. Save your scenario.
Use these commands to specify
MyInstallationFolderas 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
MyRoadRunnerProjectfrom MATLAB, use this command.
rrapp = roadrunner('MyProjectLocation');
Open the scene
Open the scenario
Get the simulation object to control simulation from MATLAB.
ss = rrapp.createSimulation();
Start the simulation from the command line.
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.