Skip to content
Snippets Groups Projects
Commit 4a91e4e6 authored by Timo Ravensbergen's avatar Timo Ravensbergen
Browse files

Merge branch '108-add-support-for-simple-busdefs-in-wrapper-class' of...

Merge branch '108-add-support-for-simple-busdefs-in-wrapper-class' of ssh://git.iter.org/pcs/scdds into 109-clean-up-of-scdds-class-methods
parents f9994c71 6dd67db7
No related branches found
No related tags found
No related merge requests found
function [names,buses] = wrapper_template_inBus_def()
% simple script to inherit the algo bus in the wrapper layer to test the
% addbus feature in the latter
% [ SCDDS - Simulink Control Development & Deployment Suite ] Copyright SPC-EPFL Lausanne 2022.
% Distributed under the terms of the GNU Lesser General Public License, LGPL-3.0-only.
run algo_template_inBus_def;
wrapper_template_inBus = algo_template_inBus;
names{1} = 'wrapper_template_inBus';
buses{1} = wrapper_template_inBus;
end
\ No newline at end of file
...@@ -17,6 +17,8 @@ classdef wrapper_template_test < SCDDSwrapper_test ...@@ -17,6 +17,8 @@ classdef wrapper_template_test < SCDDSwrapper_test
obj.wrapper = wrapper_template_class('wrapper_template'); obj.wrapper = wrapper_template_class('wrapper_template');
obj.wrapper = obj.wrapper.addalgo(obj_algo); obj.wrapper = obj.wrapper.addalgo(obj_algo);
obj.wrapper = obj.wrapper.addbus('',@() wrapper_template_inBus_def()); % test bus adder for wrapper
end end
end end
end end
...@@ -8,6 +8,10 @@ classdef (Abstract) SCDDSclass_wrapper < SCDDSclass_component ...@@ -8,6 +8,10 @@ classdef (Abstract) SCDDSclass_wrapper < SCDDSclass_component
buildable = true buildable = true
createdd = true % automatically create fresh dd during wrapper init createdd = true % automatically create fresh dd during wrapper init
end end
properties(SetAccess=private, Hidden=true)
buslist
end
methods methods
function obj = SCDDSclass_wrapper(name,dt) function obj = SCDDSclass_wrapper(name,dt)
...@@ -20,6 +24,8 @@ classdef (Abstract) SCDDSclass_wrapper < SCDDSclass_component ...@@ -20,6 +24,8 @@ classdef (Abstract) SCDDSclass_wrapper < SCDDSclass_component
obj.timing = struct('dt',dt); obj.timing = struct('dt',dt);
assert(~isempty(which(obj.mdlname)),... assert(~isempty(which(obj.mdlname)),...
'could not find %s for SCD component %s',obj.mdlname,obj.name); 'could not find %s for SCD component %s',obj.mdlname,obj.name);
obj.buslist = [];
end end
function updatetemplatetp(obj) function updatetemplatetp(obj)
...@@ -78,10 +84,14 @@ classdef (Abstract) SCDDSclass_wrapper < SCDDSclass_component ...@@ -78,10 +84,14 @@ classdef (Abstract) SCDDSclass_wrapper < SCDDSclass_component
set_param(obj.name,'DataDictionary',obj.ddname); set_param(obj.name,'DataDictionary',obj.ddname);
end end
% initialize underlying algos
for ii=1:numel(obj.algos) for ii=1:numel(obj.algos)
obj.algos(ii).init; obj.algos(ii).init;
end end
% initialize and add buses to wrapper
obj.addbusestowrapperdd;
end end
function setup(obj) function setup(obj)
...@@ -135,6 +145,84 @@ classdef (Abstract) SCDDSclass_wrapper < SCDDSclass_component ...@@ -135,6 +145,84 @@ classdef (Abstract) SCDDSclass_wrapper < SCDDSclass_component
end end
end end
end end
%% Bus setter
function obj = addbus(obj,name,source)
assert(~isequal(name,source),'bus source file and name cannot not be the same')
% Register a bus from a file to be added to dd
ii = numel(obj.buslist)+1;
obj.buslist(ii).name = name;
obj.buslist(ii).source = source;
end
end
methods (Hidden = true)
%% dd manipulation helper functions
function addbusestowrapperdd(obj)
% Add buses to data dictionary from .m file descriptions
dictionaryObj = Simulink.data.dictionary.open(obj.ddname);
designDataObj = getSection(dictionaryObj, 'Design Data');
busList = obj.buslist;
for ii=1:numel(busList)
mybusSource = busList(ii).source;
busName = busList(ii).name;
if ischar(mybusSource)
% User specified a file that contains a bus definition
% (typically exported from bus editor)
fprintf('Adding bus %s from %s to %s\n',busName,mybusSource,obj.ddname);
assert(logical(exist(mybusSource,'file')),'%s does not exist',mybusSource)
eval(mybusSource); % eval bus script here
assert(exist(busName,'var')~=0,...
'no variable %s found despite running script %s',busName,which(mybusSource))
names{1} = busName; buses{1} = eval(busName);
sourcename = mybusSource;
elseif isa(mybusSource,'function_handle')
% user specified a function that returns cell arrays of
% bus names and bus objects. busNames is ignored in this case
[names,buses] = mybusSource();
sourcename = func2str(mybusSource);
else
error('Bus source %s is neither a fcn handle nor script definition',class(mybusSource))
end
for jj=1:numel(buses) % loop over buses to be added
fprintf('adding bus %25s from function %s to %s\n',...
names{jj},sourcename,obj.ddname)
obj.replaceorcreateddentry(designDataObj,names{jj},buses{jj});
end
end
% Save if necessary
if dictionaryObj.HasUnsavedChanges
dictionaryObj.saveChanges;
end
end
function replaceorcreateddentry(obj,designDataObj,entry,value)
if designDataObj.exist(entry)
oldEntry = designDataObj.getEntry(entry);
assert(numel(oldEntry)==1,'multiple entries found for %s',entry)
if isequal(oldEntry.getValue,value)
fprintf('%s: keep old value of %s since not changed\n',obj.name,entry);
else
oldEntry.setValue(value); % replace
fprintf('%s: replaced value of %s since it changed\n',obj.name,entry);
end
else
fprintf(' %s: added new %s\n',obj.name, entry);
designDataObj.addEntry(entry,value);
end
end
end end
end end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment