Main Content

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 named MyExampleScenario 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.

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).

Bus elements of a bus object that correspond to user-defined action parameters.

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 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 Event and 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

  1. 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.

    Menu to create a new behavior

  2. On the Attributes pane, set Platform to MATLAB/Simulink. As the File Name, use the location of your file SimulinkVehicle.slx or hUDA_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.

    Attributes of the new behavior.

    This action creates a new behavior that you can attach to actors in your scenario.

    Icon representing the new behavior created using MATLAB.

  3. Add a new CompactCar to your scenario MyExampleScenario.

  4. To associate the Simulink model behavior to a RoadRunner actor, select CompactCar. Then, in the Attributes section, in the Behavior box, add MyNewBehavior to CompactCar by clicking and dragging the behavior icon to the box. Save your scenario.

    The vehicle and the new behavior that is attached to the vehicle.

  5. 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;
  6. To open the RoadRunner project MyRoadRunnerProject from MATLAB, use this command.

    rrapp = roadrunner('MyProjectLocation');
  7. Open the scene MyExampleScene.

    rrapp.openScene('MyExampleScene');
  8. Open the scenario MyExampleScenario.

    rrapp.openScenario('MyExampleScenario');
  9. Get the simulation object to control simulation from MATLAB.

    ss = rrapp.createSimulation();

  10. 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.

Related Topics