Configure AUTOSAR Architecture Model Programmatically

An AUTOSAR architecture model provides resources and a canvas for developing AUTOSAR composition and component models. You develop the software architecture by using graphical user interfaces, equivalent architecture modeling functions, or both. AUTOSAR Blockset provides functions for these architecture related tasks.

TasksFunctions
Create, load, open, save, or close an AUTOSAR architecture modelautosar.arch.createModel
autosar.arch.loadModel
close
open
save
Add, connect, or remove AUTOSAR components, composition, and portsaddComponent
addComposition
addPort
connect
destroy
importFromARXML
layout
Find AUTOSAR elements and modify propertiesfind
get
set
Define component behavior by creating or linking Simulink® modelscreateModel
linkToModel
Add Basic Software (BSW) service component blocks for simulating BSW service callsaddBSWService
Export composition and component ARXML descriptions and generate component code (requires Embedded Coder®)export
getXmlOptions
setXmlOptions

This example script:

  1. Creates and opens an AUTOSAR architecture model.

  2. Adds a composition and components.

  3. Adds architecture, composition, and component ports.

  4. Connects architecture, composition, and component ports.

  5. Creates and links Simulink implementation models for components.

  6. Arranges architecture model layout based on heuristics.

  7. Sets component and port properties.

  8. Removes a component.

  9. Searches for elements at different levels of the architecture model hierarchy.

  10. Lists property values for composition ports.

To run the script, copy commands from this MATLAB® code to the MATLAB command window, or copy the file configAutosarArchModel.m from matlabroot/help/toolbox/autosar/examples (cd to folder) to a working folder.

% configAutosarArchModel.m
%
% Configure AUTOSAR architecture model.
% This script creates models Controller1.slx and Actuator.slx.
% To rerun the script, remove the models from the working folder.

% Create and open AUTOSAR architecture model
modelName = 'myArchModel';
archModel = autosar.arch.createModel(modelName);

% Add a composition
composition = addComposition(archModel,'Sensors');

% Add 2 components inside Sensors composition
names = {'PedalSnsr','ThrottleSnsr'};
sensorSWCs = addComponent(composition,names,'Kind','SensorActuator');
layout(composition);  % Auto-arrange composition layout

% Add components at architecture model top level (call layout to arrange blocks)
addComponent(archModel,'Controller1');
actuator = addComponent(archModel,'Actuator');
set(actuator,'Kind','SensorActuator');

% Add architecture ports
addPort(archModel,'Receiver',{'TPS_Hw','APP_Hw'});
addPort(archModel,'Sender','ThrCmd_Hw');

% Add composition ports
addPort(composition,'Receiver',{'TPS_Hw','APP_Hw'});
addPort(composition,'Sender',{'TPS_Perc','APP_Perc'});

% Add component ports
controller = find(archModel,'Component','Name','Controller1');
addPort(controller,'Receiver',{'TPS_Perc','APP_Perc'});
addPort(controller,'Sender','ThrCmd_Perc');
addPort(actuator,'Receiver','ThrCmd_Perc');
addPort(actuator,'Sender','ThrCmd_Hw');

% At top level, connect composition and components based on matching port names
connect(archModel,composition,controller);
connect(archModel,controller,actuator);

% Connect specified arch root ports to specified composition and component ports
connect(archModel,archModel.Ports(1),composition.Ports(1));
% Use find to construct port specifications
connect(archModel,...
    find(archModel,'Port','Name','APP_Hw'),...
    find(composition,'Port','Name','APP_Hw'));
connect(archModel,actuator.Ports(2),archModel.Ports(3));
% ALTERNATIVELY, connect architecture root ports based on matching port names
% connect(archModel,[],composition);
% connect(archModel,actuator,[]);

% Create implementation models for controller and actuator components
createModel(controller);
createModel(actuator);

% Link implementation model to PedalSnsr component inside Sensors
pedalSnsr = find(composition,'Component','Name','PedalSnsr');
% Add path to implementation model
addpath(fullfile(matlabroot,'/examples/autosarblockset/main'));
linkToModel(pedalSnsr,'autosar_tpc_pedal_sensor');

layout(archModel);  % Auto-arrange layout

% Set properties
set(composition.Ports(1),'Name','NewPortName1'); % Names for Sensors composition ports
set(composition.Ports(3),'Name','NewPortName2');
set(find(controller,'Port','Name','TPS_Perc'),...
    'Name','NewPortName3');     % Port name for Controller1 component & implementation
set(controller,'Kind','ServiceProxy');  % Component type for Controller1 component
set(controller,'Name','Instance1');     % Name for Controller1 component

% Destroy component ThrottleSensor inside Sensors
throttleSnsr = find(composition,'Component','Name','ThrottleSnsr');
destroy(throttleSnsr);

% Find components in architecture model top level only
components_in_arch_top_level = find(archModel,'Component')
% Find components in all hierarchy
components_in_all_hierarchy = find(archModel,'Component','AllLevels',true)
% Find ports for composition block only
composition_ports = find(composition,'Port')

% List Kind and Name property values for composition ports
for ii=1:length(composition_ports)
    Port = composition_ports(ii);
    portName = get(Port,'Name');
    portKind = get(Port,'Kind');
    fprintf('%s port %s\n',portKind,portName);
end
components_in_arch_top_level = 
  2×1 Component array with properties:
    Name
    Kind
    Ports
    ReferenceName
    Parent
    SimulinkHandle

components_in_all_hierarchy = 
  3×1 Component array with properties:
    Name
    Kind
    Ports
    ReferenceName
    Parent
    SimulinkHandle

composition_ports = 
  4×1 CompPort array with properties:
    Kind
    Connected
    Name
    Parent
    SimulinkHandle

Receiver port NewPortName1
Receiver port APP_Hw
Sender port NewPortName2
Sender port APP_Perc

See Also

| | |

Related Topics