Main Content

addObserver

Add observer to scenario simulation

    Description

    example

    observer = addObserver(ScenarioSim,'ObserverName',sysObj) adds an observer named ObserverName, represented by System object™ sysObj, to the scenario simulation ScenarioSim.

    Examples

    collapse all

    Create ScenarioSim, a Simulink.ScenarioSimulation object.

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

    Create the class mySysObserver, which inherits from matlab.System base class. 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;
        end
    
        methods
            % Constructor for the System object
            function obj = mySysObserver()
        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 represented by 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 Simulink.ScenarioSimulation object.

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

    Name of observation actor, specified as a string or character vector.

    Example: 'RedCarObserver'

    Observer to add to the simulation, specified as a MATLAB® System object. You must inherit the code implementation of System object sysObj 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