Main Content

addObserver

Add observer to scenario simulation

Since R2022a

    Description

    example

    observer = addObserver(ScenarioSim,ObserverName,sysObjFileName) adds an observer named ObserverName implemented by the System object™ contained in file sysObjFileName, to the scenario simulation ScenarioSim.

    Examples

    collapse all

    Create ScenarioSim, a ScenarioSimulation object.

    rrApp = roadrunner('C:\Project\TestRoute')
    openScenario(rrApp,'TrajectoryCutIn');
    ScenarioSim = createSimulation(rrApp);
    

    Create the class mySysObserver, which inherits from matlab.System base class. The script file containing mySysObserver must also share the same name. In the stepImpl method, get the list of actors, and the simulation pace of an existing simulation. Use the resetImpl method to reset values of the required variables.

    classdef mySysObserver < matlab.System
    
        properties(Access=private)
             mScenarioSimObj;
             mScenarioSimActorList;
             mSimulationPace;
             mIsPacingTurnedOff;
        end
    
        methods
           % Constructor for the System object
           function obj = mySysObserver()
           end
        end
    
        methods(Access = protected)        
           % Reset the values
           function resetImpl(obj)
             obj.mScenarioSimObj = Simulink.ScenarioSimulation.find('ScenarioSimulation', ...
               'SystemObject', obj);
             obj.mScenarioSimActorList = [];
             obj.mIsPacingTurnedOff = false;
           end
    
           % Get the required parameter values
           function stepImpl(obj)
             mSimulationPace = obj.mScenarioSimObj.get('SimulationPace');
             mScenarioSimActorList = obj.mScenarioSimObj.get('ActorSimulation')
           end
    
           % Release the object and its resources
           function releaseImpl(~)
           end
        end
    end

    The System object in file mySysObserver is added as an observer named ReadPaceandActors to the simulation ScenarioSim.

    observer = addObserver(ScenarioSim,'ReadPaceandActors', 'mysysObserver')

    Input Arguments

    collapse all

    RoadRunner Scenario simulation, specified as a ScenarioSimulation object.

    Example: observer = addObserver(ScenarioSim,'RedCarObserver',Obj)

    Name of observer, specified as a string or character vector. You must choose a unique name with which to identify the observer.

    Example: 'RedCarObserver'

    Name of file containing the observer System object to add to a simulation, specified as a string or character vector. You must inherit the code implementation of the observer System object from the matlab.System base class.

    Output Arguments

    collapse all

    Flag to indicate result of addition of observer, returned as 0 or 1. If the observer is not added successfully, the value returned is 0.

    Version History

    Introduced in R2022a