Main Content

Programmatically Configure AUTOSAR Runnables and Events

This example shows how to programmatically configure AUTOSAR runnables and events.

This examples uses example model autosar_swc_expfcns using the autosar.api.getAUTOSARProperties and autosar.api.getSimulinkMapping objects and functions.

The behavior of an AUTOSAR software component is implemented by one or more runnables. An AUTOSAR runnable is a schedulable entity that is directly or indirectly scheduled by the underlying AUTOSAR operating system. Each runnable is triggered by RTEEvents, events generated by the AUTOSAR run-time environment (RTE). For each runnable, you configure an event to which it responds. Here are examples of AUTOSAR events to which runnables respond:

  • TimingEvent — Triggers a periodic runnable.

  • DataReceivedEvent or DataReceiveErrorEvent — Triggers a runnable with a receiver port that is participating in sender-receiver communication.

  • OperationInvokedEvent — Triggers a runnable with a server port that is participating in client-server communication.

  • ModeSwitchEvent — Triggers a runnable with a mode receiver port that is participating in mode-switch communication.

  • InitEvent (AUTOSAR schema 4.1 or higher) — Triggers a runnable that performs component initialization.

  • ExternalTriggerOccurredEvent — Triggers a runnable with a trigger receiver port that is participating in external trigger event communication.

Configure AUTOSAR TimingEvent for Periodic Runnable

This example uses model autosar_swc_expfcns to demonstrate how to programmatically add and configure periodic runnables and timing events.

Open the autosar_swc_expfcns example model and create the autosar.api.getAUTOSARProperties object.

hModel = "autosar_swc_expfcns";
open_system(hModel);
arPropsExport = autosar.api.getAUTOSARProperties(hModel);

Find the runnables present in the model using the find function.

swc = get(arPropsExport,"XmlOptions","ComponentQualifiedName");
ib = get(arPropsExport,swc,"Behavior");
runnables = find(arPropsExport,ib,"Runnable","PathType","FullyQualified");
runnables'
ans = 4x1 cell
    {'/pkg/swc/ASWC/IB/Runnable_Init'}
    {'/pkg/swc/ASWC/IB/Runnable1'    }
    {'/pkg/swc/ASWC/IB/Runnable2'    }
    {'/pkg/swc/ASWC/IB/Runnable3'    }

You can then loop through the runnables and list their property values. For more information regarding AUTOSAR properties see AUTOSAR Element Properties.

for ii=1:length(runnables)
    runnable = runnables{ii};
    rnName = get(arPropsExport,runnable,"Name");
    rnSymbol = get(arPropsExport,runnable,"symbol");
    rnCBIC = get(arPropsExport,runnable,"canBeInvokedConcurrently");
    fprintf("Runnable %s: symbol %s, canBeInvokedConcurrently %u\n",...
      rnName,rnSymbol,rnCBIC);
end
Runnable Runnable_Init: symbol Runnable_Init, canBeInvokedConcurrently 0
Runnable Runnable1: symbol Runnable1, canBeInvokedConcurrently 0
Runnable Runnable2: symbol Runnable2, canBeInvokedConcurrently 0
Runnable Runnable3: symbol Runnable3, canBeInvokedConcurrently 0

Declare variables to configure the properties of the runnables.

set(arPropsExport,runnables{2},"Name","myRunnable","symbol","myAlgorithm");
runnables = find(arPropsExport,ib,"Runnable","PathType","FullyQualified");
rnName = get(arPropsExport,runnables{2},"Name");
rnSymbol = get(arPropsExport,runnables{2},"symbol");
rnCBIC = get(arPropsExport,runnables{2},"canBeInvokedConcurrently");
fprintf("Runnable %s: symbol %s, canBeInvokedConcurrently %u\n",...
  rnName,rnSymbol,rnCBIC);
Runnable myRunnable: symbol myAlgorithm, canBeInvokedConcurrently 0

You can loop through the AUTOSAR timing events by specifying the event type and list the associated runnables.

events = find(arPropsExport,ib,"TimingEvent","PathType","FullyQualified");
for ii=1:length(events)
    event = events{ii};
    eventStartOn = get(arPropsExport,event,"StartOnEvent");
    fprintf("AUTOSAR event %s triggers %s\n",event,eventStartOn);
end
AUTOSAR event /pkg/swc/ASWC/IB/Event_t_1tic_A triggers ASWC/IB/myRunnable
AUTOSAR event /pkg/swc/ASWC/IB/Event_t_1tic_B triggers ASWC/IB/Runnable2
AUTOSAR event /pkg/swc/ASWC/IB/Event_t_10tic triggers ASWC/IB/Runnable3

Set the name and configure the event as a TimingEvent.

set(arPropsExport,events{1},"Name","myEvent");
events = find(arPropsExport,ib,"TimingEvent","PathType","FullyQualified");
eventStartOn = get(arPropsExport,events{1},"StartOnEvent");
fprintf("AUTOSAR event %s triggers %s\n",events{1},eventStartOn);
AUTOSAR event /pkg/swc/ASWC/IB/myEvent triggers ASWC/IB/myRunnable

Map the Simulink exported function Runnable1 to the AUTOSAR runnable myRunnable.

slMapExport = autosar.api.getSimulinkMapping(hModel);
mapFunction(slMapExport,"Runnable1","myRunnable");
The function name value 'Runnable1' is obsolete and will be removed in a future
release. For valid function name values, use
autosar.api.getSimulinkMapping(modelName).find("Functions").
The function name value 'Runnable1' is obsolete and will be removed in a future
release. For valid function name values, use
autosar.api.getSimulinkMapping(modelName).find("Functions").
arRunnableName = getFunction(slMapExport,"Runnable1")
The function name value 'Runnable1' is obsolete and will be removed in a future
release. For valid function name values, use
autosar.api.getSimulinkMapping(modelName).find("Functions").
arRunnableName = 
'myRunnable'

Configure and Map Runnables

This example uses model autosar_swc_counter to demonstrate how to add initialization and periodic runnables and configure timing events associated with those runnables. An example is also provide don how to programmatically map runnables to Simulink functions.

Open example model autosar_swc_counter, and create the relevant autosar.api.getAUTOSARProperties object.

hModel = "autosar_swc_counter";
open_system(hModel);
arPropsCounter = autosar.api.getAUTOSARProperties(hModel);

Define and add AUTOSAR initialization and periodic runnables, myInitRunnable and myPeriodicRunnable respectively.

initRunnable = "myInitRunnable";
periodicRunnable = 'myPeriodicRunnable';
swc = get(arPropsCounter,"XmlOptions","ComponentQualifiedName")
swc = 
'/Company/Powertrain/Components/autosar_swc_counter'
ib = get(arPropsCounter,swc,"Behavior")
ib = 
'autosar_swc_counter/ASWC_IB'
add(arPropsCounter,ib,"Runnables",initRunnable);
add(arPropsCounter,ib,"Runnables",periodicRunnable);

Add an AUTOSAR timing event, myPeriodicEvent.

eventName = "myPeriodicEvent";
add(arPropsCounter,ib,"Events",eventName,"Category","TimingEvent","Period",1,...
    "StartOnEvent",[ib '/' periodicRunnable]);

Create the autosar.api.getSimulinkMapping object.

slMapCounter = autosar.api.getSimulinkMapping(hModel);

Map the initialization runnable, myInitRunnable to the Simulink InitializeFunction. Map the periodic runnable, myPeriodicRunnable, to the Simulink StepFunction.

mapFunction(slMapCounter,"InitializeFunction",initRunnable);
The function name value 'InitializeFunction' is obsolete and will be removed in
a future release. For valid function name values, use
autosar.api.getSimulinkMapping(modelName).find("Functions").
The function name value 'InitializeFunction' is obsolete and will be removed in
a future release. For valid function name values, use
autosar.api.getSimulinkMapping(modelName).find("Functions").
mapFunction(slMapCounter,"StepFunction",periodicRunnable);
The function name value 'StepFunction' is obsolete and will be removed in a
future release. For valid function name values, use
autosar.api.getSimulinkMapping(modelName).find("Functions").
The function name value 'StepFunction' is obsolete and will be removed in a
future release. For valid function name values, use
autosar.api.getSimulinkMapping(modelName).find("Functions").

To pass validation, remove redundant initialize and step runnables in the AUTOSAR configuration.

runnables = get(arPropsCounter,ib,"Runnables");
delete(arPropsCounter,[ib,'/Runnable_Init']);
delete(arPropsCounter,[ib,'/Runnable_Step']);
runnables = get(arPropsCounter,ib,"Runnables")
runnables = 1x2 cell
    {'autosar_swc_counter/ASWC_IB/myInitRunnable'}    {'autosar_swc_counter/ASWC_IB/myPeriodicRunnable'}

Retrieve and Map Signals and States

This example uses model autosar_swc_counter to show how to programmatically map signals and states to AUTOSAR variable types.

hModel = "autosar_swc_counter";
open_system(hModel);
slMapCounter = autosar.api.getSimulinkMapping(hModel);

Using the autosar.api.getSimulinkMapping object, slMapCounter, and the object function find get signals and their names as well as states present in the model.

listOfSignals = find(slMapCounter,"Signals")
listOfSignals = 1×3

  510.0007  514.0035  519.0035

namesOfSignals = get_param(listOfSignals, "Name")
namesOfSignals = 3x1 cell
    {'equal_to_count'}
    {'sum_out'       }
    {'switch_out'    }

listOfStates = find(slMapCounter,"States")
listOfStates = 
"autosar_swc_counter/X"

Map the signals to an AUTOSAR variable type. For more information, see the mapSignal function.

mapSignal(slMapCounter,listOfSignals(1),"StaticMemory")
mapSignal(slMapCounter,listOfSignals(2),"ArTypedPerInstanceMemory")
mapSignal(slMapCounter,listOfSignals(3),"Auto")

Map the state to AUTOSAR variable type StaticMemory. For more information, see the mapState function.

mapState(slMapCounter,'autosar_swc_counter/X','',"StaticMemory")

Configure Events for Runnable Activation

This example shows the property function syntax for adding an AUTOSAR TimingEvent, DataReceivedEvent, and DataReceiveErrorEvent to a runnable in a model. For a DataReceivedEvent or DataReceiveErrorEvent, you specify a trigger. The trigger name includes the name of the AUTOSAR receiver port and data element that receives the event, for example, RPort.DE1.

Open the example model autosar_swc_expfcns and create the relevant autosar.api.getAUTOSARProperties function.

hModel = "autosar_swc_expfcns";
open_system(hModel);
arPropsExport = autosar.api.getAUTOSARProperties(hModel);

Using the name and behavior of the software component, add a timing event to a specified runnable, in this case Runnable1.

swc = get(arPropsExport,"XmlOptions","ComponentQualifiedName")
swc = 
'/pkg/swc/ASWC'
ib = get(arPropsExport,swc,"Behavior")
ib = 
'ASWC/IB'
runnables = get(arPropsExport,ib,"Runnables")'
runnables = 4x1 cell
    {'ASWC/IB/Runnable_Init'}
    {'ASWC/IB/myRunnable'   }
    {'ASWC/IB/Runnable2'    }
    {'ASWC/IB/Runnable3'    }

runnable = 'Runnable2';
timingEventName = 'myTimingEvent';
add(arPropsExport,ib,"Events",timingEventName,"Category",'TimingEvent',...
    "Period",1,'StartOnEvent',[ib '/' runnable]);

Add a DataReceivedEvent and DataReceiveErrorEvent to runnable myRunnable.

drEventName = "myDREvent";
add(arPropsExport,ib,"Events",drEventName,"Category","DataReceivedEvent",...
    "Trigger","RPort.DE1",'StartOnEvent',[ib '/' runnable]);
dreEventName = "myDREEvent";
add(arPropsExport,ib,"Events",dreEventName,"Category","DataReceiveErrorEvent",...
    "Trigger","RPort.DE1",'StartOnEvent',[ib '/' runnable]);

To pass validation, remove redundant timing events in the AUTOSAR configuration.

events = get(arPropsExport,ib,"Events")'
events = 6x1 cell
    {'ASWC/IB/myEvent'       }
    {'ASWC/IB/Event_t_1tic_B'}
    {'ASWC/IB/Event_t_10tic' }
    {'ASWC/IB/myTimingEvent' }
    {'ASWC/IB/myDREvent'     }
    {'ASWC/IB/myDREEvent'    }

delete(arPropsExport,[ib,'/Event_t_1tic_B'])
events = get(arPropsExport,ib,"Events")'
events = 5x1 cell
    {'ASWC/IB/myEvent'      }
    {'ASWC/IB/Event_t_10tic'}
    {'ASWC/IB/myTimingEvent'}
    {'ASWC/IB/myDREvent'    }
    {'ASWC/IB/myDREEvent'   }

See Also

Objects

Functions

Properties

Related Topics