Publish Ready-to-Run Actor Behaviors for Reuse and Simulation Performance
This topic explains how to generate ready-to-run actor behaviors using the publishActor
function, as well as general considerations when creating ready-to-run packages. For more
information about simulating RoadRunner scenarios with MATLAB® and Simulink®, see Overview of Simulating RoadRunner Scenarios with MATLAB and Simulink.
You can generate ready-to-run actor behaviors from your Simulink models using RoadRunner scenario blocks or MATLAB System (Simulink) blocks. Using ready-to-run
packages as your actor behaviors enables you to associate the same behavior with multiple
actors in your scenario, modify behavior parameters locally for each actor, and improve
simulation performance. This topic uses a simple Simulink model to explain the workflow. You can follow similar steps for other
behaviors that use MATLAB System blocks by opening the example and using the
mUpdateXDirectionWithMATLABSys.slx
model.
This topic assumes that you have:
Simulink and Simulink Coder™ licenses.
A RoadRunner license, and the product is installed. For more information, see Install and Activate RoadRunner (RoadRunner).
A RoadRunner Scenario license, and the product is installed.
A RoadRunner project folder named
MyRoadRunnerProject
. For more information, see RoadRunner Project and Scene System (RoadRunner).The RoadRunner scene file
SceneWithMutipleLanes.rrscene
and scenario fileLaneswithMultipleActors.rrscenario
in your projectMyRoadRunnerProject
. You can access these files in your working folder after you open the example in this topic.
Open Simulink Model for Moving Actors
Open mUpdateXDirection
. This is a simple Simulink model with RoadRunner Scenario Reader, RoadRunner Scenario Writer, and RoadRunner Scenario block and the UpdatePose_along_X_Axis
component.
In this model:
A Receive (Simulink) block converts messages from RoadRunner Scenario Reader block to bus signals.
UpdatePose_along_X_Axis
component processes the signals.The signals from
UpdatePose_along_X_Axis
component are converted to messages using a Send (Simulink) block.The messages are communicated with RoadRunner scenario using RoadRunner Scenario Writer block.
UpdatePose_along_X_Axis
component processes the Pose
element of the BusActorRuntime
bus signal by updating the x-direction displacement of the actor. SpeedFactor
parameter defines the magnitude of the displacement.
In the model, the SpeedFactor
parameter is configured to be a tunable parameter by RoadRunner. The steps for this configuration are identical to the case when you expose a model argument as a tunable parameter on the Model block at each level of the model hierarchy. For more information, see Configure Instance-Specific Values for Block Parameters in a Referenced Model (Simulink).
Generate Ready-to-Run Package from the Simulink Behavior Model
This section describes the steps to create the
LaneswithMultipleActors.rrscenario
RoadRunner scenario file in your example. To create this scenario, use the publishActor
function to generate ready-to-run actor behaviors from the
Simulink model. The section also shows how to associate the behavior with two
vehicles in LaneswithMultipleActors.rrscenario
.
Use this code to generate the package for your behavior model. This command publishes
mUpdateXDirection
as a ready-to-run package and creates anmUpdateXDirection.zip
file in your current directory.Simulink.publish.publishActor('mUpdateXDirection',PackageType='ReadyToRun')
In RoadRunner, open
LaneswithMultipleActors.rrscenario
and in the Library Browser, select theBehaviors
folder.To create a new behavior, right-click an empty space in the list of behaviors, pause on New, then select Behavior. Enter a name for your new behavior, such as
MyNewBehavior
. This animation shows how to complete these steps.On the Attributes pane, set Platform to
MATLAB/Simulink
. Specify File Name asmUpdateXDirection.zip
.Select Add Parameter, and add
SpeedFactor
as a parameter.Associate the behavior
MyNewBehavior
with the two vehicles in your scenario. For more information, see Overview of Simulating RoadRunner Scenarios with MATLAB and Simulink.From the Logic editor, select the
Sedan
action phase, and add aChange Behavior Parameter
action that sets the value ofSpeedFactor
parameter for each actor in your model. For this example, inLaneswithMultipleActors.rrscenario
scenario, the white sedanSedan
has aSpeedFactor
value of1
, and the blue sedanSedan2
has aSpeedFactor
value of2
.
Simulate the Scenario and Observe the Actor Behavior
This section uses LaneswithMultipleActors.rrscenario
scenario file from the
example, and describes the steps to simulate the scenario.
Specify the path to your RoadRunner installation folder using these commands, replacing
MyInstallationFolder
with the path to your RoadRunner installation. You need to run the commands in this step only the first time you are setting up the connection between RoadRunner and your MATLAB installation.RRInstallationFolder = "MyInstallationFolder"; s = settings; s.roadrunner.application.InstallationFolder.PersonalValue = RRInstallationFolder; s.roadrunner.application.InstallationFolder.TemporaryValue = RRInstallationFolder;
Note
Specify the full path to the folder containing the
AppRoadRunner.exe
executable. The default location of this executable on Windows® isC:\Program Files\RoadRunner R2022b\bin\win64
, whereC:\Program Files\RoadRunner R2022b
is your RoadRunner installation folder. The folder could be different on your computer.To
MyRoadRunnerProject
from MATLAB, use this command.rrApp = roadrunner('MyProjectLocation');
Open
LaneswithMultipleActors.rrscenario
scenario file.openScenario(rrApp,'LaneswithMultipleActors.rrscenario');
Create the simulation object to control the simulation from MATLAB.
rrSim = createSimulation(rrApp);
Start the simulation from the command line.
set(rrSim, 'SimulationCommand','Start');
Observe the difference in vehicle behavior because of the
SpeedFactor
parameter.
General Considerations for Generating Ready-to-Run Actor Behaviors
Consider these factors when you generate ready-to-run actor behaviors from Simulink models using RoadRunner scenario blocks or MATLAB System blocks:
To generate ready-to-run actor behaviors, you must use a fixed-step solver for your model.
To generate a behavior for a MATLAB System object™ you must use a MATLAB System block and a RoadRunner Scenario block. Open the
mUpdateXDirectionWithMATLABSys.slx
model, which uses a MATLAB System block with thehUpdatePoseAlongX.m
System object file.Based on your RoadRunner Scenario Reader block configuration, the block outputs a specific bus, such as
BusActorRuntime
. To see the bus data type, on the Debug tab, select Information Overlays, enable Bus Data Type, and update your model.To process this data in a MATLAB System block, use the
Simulink.ActorSimulation.load
function to learn bus type and the elements of the bus. For example:Simulink.ActorSimulation.load("BusActorRuntime");
To generate code for your ready-to-run actor behavior using a MATLAB System block, you must provide a default
struct
data to your bus. For example,hUpdatePoseAlongX.m
uses theSimulink.ActorSimulation.createMATLABStruct
function to providestruct
data forBusActorRuntime
.function out = stepImpl(obj,u) % Construct default output struct out = Simulink.ActorSimulation.createMATLABStruct('BusActorRuntime'); % Get ActorID actorSim = Simulink.ScenarioSimulation.find( ... 'ActorSimulation','SystemObject',obj); actorID = getAttribute(actorSim,"ID"); if ~isempty(u) pose = u(end).Pose; velocity = u(end).Velocity; displacement = velocity*obj.stepSize*obj.SpeedFactor; % Update pose pose(1,4) = pose(1,4) + displacement(1); % x % Update output out.Pose = pose; out.Velocity = velocity; out.ActorID = castIntToFi(actorID); end end
Consider these factors when you generate ready-to-run actor behaviors from MATLAB System object actor behaviors.
If you incorporated the
getAttribute
function of theActorSimulation
object, or thegetAttribute
function of theActorModel
object, then do not use thenumel
function to count the number of elements in the arrays returned by these functions. Instead, check for structure fields of array elements that specifically count the number of other array elements. For example, see theNumBulbConfiguration
field of theSignalConfiguration
structure in the TrafficSignalRuntime structure.
See Also
publishActor
| publishActorBehavior
| publishCustomAction
| publishCustomEvent