Main Content

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 the MATLAB® script listing below to the MATLAB command window.

Alternatively, you can copy the script file configAutosarArchModel.m to your working folder and run the file. To copy the script to your working folder, enter this MATLAB command:

copyfile(fullfile(matlabroot,'help/toolbox/autosar/examples/configAutosarArchModel.m'),'.')
% 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
controller = addComponent(archModel,'Controller');
actuator = addComponent(archModel,'Actuator');
set(actuator,'Kind','SensorActuator');
layout(archModel); 

% Add ports to architecture model and the Sensors composition
addPort(archModel,'Receiver',{'APP_HwIO', 'TPS_HwIO'});
addPort(archModel,'Sender','ThrCmd_HwIO');
addPort(composition,'Receiver',{'TPS_HwIO','APP_HwIO'});
addPort(composition,'Sender',{'APP_Percent', 'TPS_Percent'});


% Link components to implementation models
% Add path to implementation model
addpath(fullfile(matlabroot,'/examples/autosarblockset/main'));
pedalSnsr = find(composition,'Component','Name','PedalSnsr');
linkToModel(pedalSnsr,'autosar_tpc_pedal_sensor');
throttleSnsr = find(composition,'Component','Name','ThrottleSnsr');
%linkToModel(throttleSnsr,'autosar_tpc_throttle_sensor1');
linkToModel(actuator, 'autosar_tpc_actuator');
linkToModel(controller, 'autosar_tpc_controller')

% add ports to throttle sensor component and create behavior model
addPort(throttleSnsr,'Sender','TPS_Percent');
addPort(throttleSnsr,'Receiver','TPS_HwIO');
createModel(throttleSnsr);

% implement internal behavior for throttle sensor. Here, we simply adjust
% datatypes on the ports.
set_param('ThrottleSnsr/In Bus Element', 'OutDataTypeStr', 'uint16');
set_param('ThrottleSnsr/Out Bus Element', 'OutDataTypeStr', 'single');

% connect composition and components based on matching port names
connect(archModel,composition,controller);
connect(archModel,controller,actuator);
connect(archModel,[],composition);
connect(archModel,actuator,[]);
connect(composition, [], pedalSnsr);
connect(composition, [], throttleSnsr);
connect(composition, pedalSnsr, []);
connect(composition, throttleSnsr, []);
connect(archModel,composition, controller);

% can also use API to connect specific ports
ThrCmd_Percent_pport = find(controller, 'Port', 'Name', 'ThrCmd_Percent');
ThrCmd_Percent_rport = find(actuator, 'Port', 'Name', 'ThrCmd_Percent');
connect(archModel, ThrCmd_Percent_pport, ThrCmd_Percent_rport);

layout(archModel);  % Auto-arrange layout

% 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

% simulate the architecture model
sim(modelName);
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