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
Specify the platform kind of an AUTOSAR architecture modelsetPlatform
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

Programmatically Create and Configure Architecture Model

This example script shows:

  1. Creates and opens an AUTOSAR architecture model.

  2. Sets the platform kind of the architecture model to the Classic Platform explicitly.

  3. Adds a composition and components.

  4. Adds architecture, composition, and component ports.

  5. Connects architecture, composition, and component ports.

  6. Creates and links Simulink® implementation models for components.

  7. Arranges architecture model layout based on heuristics.

  8. Sets component and port properties.

  9. Removes a component.

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

  11. Lists property values for composition ports.

To run the script, copy commands from the MATLAB® script listing below to the MATLAB® command window.

% 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);

% autosar.arch.createModel creates a classic architecture model by default
% Use setPlatform to explicitly set the platform kind
setPlatform(archModel,'Classic');

% 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
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
Receiver port TPS_HwIO
Receiver port APP_HwIO
Sender port APP_Percent
Sender port TPS_Percent
% simulate the architecture model
sim(modelName);

See Also

| | |

Related Topics