Programmatically Configure AUTOSAR Sender-Receiver Communication
This example provides a programmatic workflow for configuring sender-receiver ports, interfaces, data elements, and runnables of model autosar_swc_expfcns
using the autosar.api.getAUTOSARProperties
object functions. Programmatic workflows are also provided for Simulink® mappings to AUTOSAR elements using the autosar.api.getSimulinkMapping
object functions.
Configure and Map Sender-Receiver Interface
Open example model autosar_swc_expfcns
and create the autosar.api.getAUTOSARProperties
object.
hModel = 'autosar_swc_expfcns';
open_system(hModel);
arProps = autosar.api.getAUTOSARProperties(hModel);
Add an AUTOSAR sender-receiver interface specifying the IsService
property as false using the interface package and name.
ifName = 'mySrIf'; ifPkg = get(arProps,'XmlOptions','InterfacePackage'); addPackageableElement(arProps,'SenderReceiverInterface',ifPkg,ifName,'IsService',false); ifPaths=find(arProps,[],'SenderReceiverInterface','PathType','FullyQualified')'
ifPaths = 3x1 cell
{'/pkg/if/Interface1'}
{'/pkg/if/Interface2'}
{'/pkg/if/mySrIf' }
Add data elements with setting the SwCalibrationAccess
property to ReadWrite
. For more information regarding AUTOSAR properties, see AUTOSAR Element Properties.
de1 = 'myDE1'; de2 = 'myDE2'; swCalibValue= 'ReadWrite'; add(arProps, [ifPkg '/' ifName],'DataElements',de1,'SwCalibrationAccess',swCalibValue); add(arProps, [ifPkg '/' ifName],'DataElements',de2,'SwCalibrationAccess',swCalibValue);
Add AUTOSAR receiver (require) and sender (provide) ports to interface mySrIf
.
rPortName = 'myRPort'; pPortName = 'myPPort'; aswcPath = find(arProps,[],'AtomicComponent','PathType','FullyQualified'); add(arProps,aswcPath{1},'ReceiverPorts',rPortName,'Interface',ifName); add(arProps,aswcPath{1},'SenderPorts',pPortName,'Interface',ifName);
Map Require and Provide Ports Using mapOutport
Create the autosar.api.getSimulinkMapping
object.
slMap=autosar.api.getSimulinkMapping(hModel);
Use the object functions get the outport RPort_DE2
and map it to port myRPort
. Setting the data access mode to ImplicitReceive
. For more information see the mapOutport
function.
[rPortName,rDataElementName,rDataAccessMode] = getInport(slMap,'RPort_DE2')
rPortName = 'RPort'
rDataElementName = 'DE2'
rDataAccessMode = 'ImplicitReceive'
mapInport(slMap,'RPort_DE2','myRPort',de2,'ImplicitReceive') [rPortName,rDataElementName,rDataAccessMode] = getInport(slMap,'RPort_DE2')
rPortName = 'myRPort'
rDataElementName = 'myDE2'
rDataAccessMode = 'ImplicitReceive'
Use the object functions get the outport PPort_DE1
and map it to port myPPort
. Set the data access mode to ImplicitSend
.
[pPortName,pDataElementName,pDataAccessMode] = getOutport(slMap,'PPort_DE1')
pPortName = 'PPort'
pDataElementName = 'DE1'
pDataAccessMode = 'ImplicitSend'
mapOutport(slMap,'PPort_DE1','myPPort','myDE1','ImplicitSend'); [pPortName,pDataElementName,pDataAccessMode] = getOutport(slMap,'PPort_DE1')
pPortName = 'myPPort'
pDataElementName = 'myDE1'
pDataAccessMode = 'ImplicitSend'
Set Properties of Sender-Receiver Data Elements
You can configure the SwCalibrationAccess
property of multiple AUTOSAR data elements using a nested for loop.
srIfs = find(arProps,[],'SenderReceiverInterface','PathType','FullyQualified')'
srIfs = 3x1 cell
{'/pkg/if/Interface1'}
{'/pkg/if/Interface2'}
{'/pkg/if/mySrIf' }
for i=1:length(srIfs) srIf = srIfs{i}; dataElements = get(arProps,srIf,'DataElements','PathType','FullyQualified'); swCalibValue = 'ReadWrite'; for ii=1:length(dataElements) dataElement = dataElements{ii}; set(arProps,dataElement,'SwCalibrationAccess',swCalibValue) end end
Configure Events for Runnable Activation
This section shows the property function syntax for adding an AUTOSAR TimingEvent
, DataReceivedEvent
, and DataReceiveErrorEvent
to a runnable in model autosar_swc_expfcns
.
For a DataReceiveErrorEvent
, you must specify a trigger. The trigger name includes the name of the AUTOSAR receiver port and data element that receive the event, for example: "RPort.DE1"
.
For OperationInvokedEvent
syntax, see Programmatically Configure AUTOSAR Client-Server Communication.
For ModeSwitchEvent
syntax, see Programmatically Configure AUTOSAR Mode-Switch Communication.
Open model autosar_swc_expfcns
and create the relevant autosar.api.getAUTOSARProperties
object.
hModel = 'autosar_swc_expfcns';
open_system(hModel);
arProps = autosar.api.getAUTOSARProperties(hModel);
Identify the AUTOSAR runnable for which you would like to add the TimingEvent
.
swc = get(arProps,"XmlOptions","ComponentQualifiedName"); ib = get(arProps,swc,"Behavior"); runnables = get(arProps,ib,"Runnables")'
runnables = 4x1 cell
{'ASWC/IB/Runnable_Init'}
{'ASWC/IB/Runnable1' }
{'ASWC/IB/Runnable2' }
{'ASWC/IB/Runnable3' }
runnable = "Runnable1"; timingEventName = "myTimingEvent"; add(arProps,ib,"Events",timingEventName,"Category","TimingEvent",... "Period",1,"StartOnEvent","ASWC/IB/Runnable2");
Add an AUTOSAR data received event.
dreEventName = "myDREEvent"; add(arProps,ib,"Events",dreEventName,"Category","DataReceiveErrorEvent",... "Trigger","RPort.DE1","StartOnEvent","ASWC/IB/Runnable2");
To pass validation, remove any redundant timing events in the AUTOSAR configuration.
events = get(arProps,ib,"Events"); delete(arProps,"ASWC/IB/Event_t_1tic_A") events = get(arProps,ib,"Events")'
events = 4x1 cell
{'ASWC/IB/Event_t_1tic_B'}
{'ASWC/IB/Event_t_10tic' }
{'ASWC/IB/myTimingEvent' }
{'ASWC/IB/myDREEvent' }