Programmatically Configure AUTOSAR Mode-Switch Communication
This example uses example models mAutosarMsConfig
and mAutosarMsConfigAfter
to demonstrate the use of the autosar.api.getAUTOSARProperties
and autosar.api.getSimulinkMapping
object functions to configure Mode-Switch interfaces and ports with Simulink.
The supporting file mdgModes.m
declares AUTOSAR mode declaration group mdgModes
for use with the mode switch interface.
The mode declaration group enumerated mode values are:
STARTUP(0)
RUN(1)
SHUTDOWN(2)
Open example model mAutosarMsConfig
and create the autosar.api.getAUTOSARProperties
object.
hModel = 'mAutosarMsConfig';
open_system(hModel);
arProps = autosar.api.getAUTOSARProperties(hModel);
Add Mode-Switch Interface and Ports
Add an AUTOSAR Mode-Switch interface myMsIf
. Set the ModeGroup
property of myMsIf
to mdgModes
, and the IsService
property of myMsIf
to true
. For information regarding properties see AUTOSAR Element Properties.
ifName = 'myMsIf'; modeGroup = 'mdgModes'; ifPkg = get(arProps,"XmlOptions","InterfacePackage"); addPackageableElement(arProps,"ModeSwitchInterface",ifPkg,ifName,"IsService",true); add(arProps,[ifPkg '/' ifName],"ModeGroup",modeGroup) ifPaths=find(arProps,[],"ModeSwitchInterface","PathType","FullyQualified")
ifPaths = 1x1 cell array
{'/pkg/if/myMsIf'}
Add an AUTOSAR mode-receiver port to interface myMsIf
.
mrPortName = 'myMRPort'; aswcPath = find(arProps,[],"AtomicComponent","PathType","FullyQualified"); add(arProps,aswcPath{1},"ModeReceiverPorts",mrPortName,"Interface",ifName);
Configure Mode-Switch Events
Add and define an AUTOSAR event to runnable Runnable1
. Set the Category
of the event to ModeSwitchEvent
.
msRunnable = 'Runnable1'; msEventName = 'myMSEvent'; swc = get(arProps,"XmlOptions","ComponentQualifiedName"); ib = get(arProps,swc,"Behavior"); runnables = get(arProps,ib,"Runnables")'
runnables = 4x1 cell
{'ASWC/Behavior/Runnable_Init'}
{'ASWC/Behavior/Runnable1' }
{'ASWC/Behavior/Runnable2' }
{'ASWC/Behavior/Runnable3' }
add(arProps,ib,"Events",msEventName,"Category","ModeSwitchEvent",... "Activation","OnTransition", ... "StartOnEvent", [ib '/' msRunnable]);
Declare AUTOSAR Mode Declaration Group
Apply the data type mdgModes
to Simulink inport MRPort
.
set_param([hModel,'/MRPort'],"OutDataTypeStr","Enum: mdgModes") get_param([hModel,'/MRPort'],"OutDataTypeStr");
Apply data type mdgModes
and value STARTUP
to '/Runnable1_subsystem/Enumerated Constant'
.
set_param([hModel,'/Runnable1_subsystem/Enumerated Constant'],"OutDataTypeStr","Enum: mdgModes") set_param([hModel,'/Runnable1_subsystem/Enumerated Constant'],"Value","mdgModes.STARTUP")
Configure ModeSwitchEvent
Port and Trigger
To pass validation, remove any redundant timing events in the AUTOSAR configuration.
events = get(arProps,ib,'Events'); delete(arProps,[ib,'/Event_t_1tic_A']) events = get(arProps,ib,'Events')'
events = 3x1 cell
{'ASWC/Behavior/Event_t_1tic_B'}
{'ASWC/Behavior/Event_t_10tic' }
{'ASWC/Behavior/myMSEvent' }
Export the mode declaration group to an AUTOSAR data type package in XML.
mdgPkg = get(arProps,"XmlOptions","DataTypePackage"); mdgPath = [mdgPkg '/' modeGroup]; initMode = [mdgPath '/STARTUP']; addPackageableElement(arProps,"ModeDeclarationGroup",mdgPkg,modeGroup,"OnTransitionValue",100)
Add modes to the mode declaration group and specify the InitialMode
.
add(arProps,mdgPath,'Mode','STARTUP','Value',0) add(arProps,mdgPath,'Mode','RUN','Value',1) add(arProps,mdgPath,'Mode','SHUTDOWN','Value',2) set(arProps,mdgPath,'InitialMode',initMode)
Set the ModeGroup
property for the M-S interface. For information regarding AUTOSAR Properties, see AUTOSAR Element Properties.
set(arProps,[ifPkg '/' ifName '/' modeGroup],'ModeGroup',mdgPath)
Set the port and trigger for AUTOSAR property ModeSwitchEvent
.
expTrigger = {[mrPortName '.STARTUP'], [mrPortName '.SHUTDOWN']}; set(arProps,[ib '/' msEventName],'Trigger',expTrigger);
Map Mode-Switch Ports to Simulink Inports
Create the autosar.api.getSimulinkMapping
object.
slMap=autosar.api.getSimulinkMapping(hModel);
Map the Simulink inport, MRPort
, to the AUTOSAR mode receiver port, myMRPort
.
[arPortName,arDataElementName,arDataAccessMode] = getInport(slMap,'MRPort'); mapInport(slMap,'MRPort',mrPortName,modeGroup,"ModeReceive"); [arPortName,arDataElementName,arDataAccessMode] = getInport(slMap,'MRPort')
arPortName = 'myMRPort'
arDataElementName = 'mdgModes'
arDataAccessMode = 'ModeReceive'
To pass validation, ensure inport Runnable1
inherits (set to -1) sample time.
set_param([hModel,'/Runnable1'],'SampleTime','-1')
Map Mode Receiver Ports
Using model mAutosarMsConfigAfter
find AUTOSAR mode receiver ports, loop through the ports and list the associated mode-switch interfaces. Modify the Interface
property for the port MsIf2
.
Open model mAutosarMsConfigAfter
and create an autosar.api.getAUTOSARProperties
object.
hModel = "mAutosarMsConfigAfter";
open_system(hModel);
afterProps = autosar.api.getAUTOSARProperties(hModel);
Find the AUTOSAR mode receiver ports.
arPortType = "ModeReceiverPort"; aswcPath = find(afterProps,[],"AtomicComponent","PathType","FullyQualified"); mrPorts = find(afterProps,aswcPath{1},arPortType,"PathType","FullyQualified")'
mrPorts = 1x1 cell array
{'/pkg/swc/ASWC/myMRPort'}
Loop through the ports and list the associated interfaces.
for ii=1:length(mrPorts) mrPort = mrPorts{ii}; portIf = get(afterProps,mrPort,"Interface"); fprintf("Port %s has M-S interface %s\n",mrPort,portIf); end
Port /pkg/swc/ASWC/myMRPort has M-S interface myMsIf
Set the Interface
property for AUTOSAR port MsIf2
. For more information regarding AUTOSAR properties, see AUTOSAR Element Properties.
set(afterProps,mrPorts{1},"Interface","MsIf2") portIf = get(afterProps,mrPort,"Interface"); fprintf("Port %s has M-S interface %s\n",mrPorts{1},portIf);
Port /pkg/swc/ASWC/myMRPort has M-S interface MsIf2
Create the relevant autosar.api.getSimulinkMapping
object.
slMap = autosar.api.getSimulinkMapping(hModel);
[arPortName, arDataElementName, arDataAccessMode] = getInport(slMap,"MRPort");
Use the autosar.api.getSimulinkMapping
object to map the Simulink inport, MRPort
, to AUTOSAR port, myMRPort
.
mapInport(slMap,"MRPort","myMRPort","mdgModes","ModeReceive") [arPortName,arDataElementName,arDataAccessMode] = getInport(slMap,"MRPort")
arPortName = 'myMRPort'
arDataElementName = 'mdgModes'
arDataAccessMode = 'ModeReceive'