www.gusucode.com > mbcmodels 工具箱 matlab 源码程序 > mbcmodels/@xregmodswitch/modelbuild.m
function [new_sys,EvM] = modelbuild(m,parent,name,DO_PEV,trueModel) %MODELBUILD Build a simulink model for the switch model % % [SYS, EVAL_SYS] = MODELBUILD(M, PARENTSYS, NAME, DO_PEV) % Copyright 2005-2014 The MathWorks, Inc. and Ford Global Technologies, Inc. if nargin > 4 % This allows a derived class to be downcast to a model and then call modelbuild % but still pass the actual model m = trueModel; end if nargin < 4 DO_PEV = 1; end BI = 'built-in/'; loop=1; while loop if isempty(find_system(parent,'SearchDepth',1,'Name',name)) loop=0; else name=[name,'(' num2str(loop) ')']; loop=loop+1; end end sys = [parent,'/',name]; % Add a basic switch model subsystem to start with new_sys = add_block('mbcSLModels/TwoStage/xregmodswitchEval',sys); % break library link set_param(new_sys,'linkstatus','none'); EvM = []; % Wire up the link from the input to the selection sfunction add_line(sys, 'X/1', 'Demux1/1','autorouting','on'); % Create a PEV output if required Ypos = get_param([sys '/Y'], 'Position'); if DO_PEV % Create a PEV output hPEVOut = add_block([BI 'Outport'], [sys '/PEV']); else % Terminate the PEV from the contained models hPEVOut = add_block([BI 'Terminator'], [sys '/PEV']); end set_param(hPEVOut, 'position', Ypos+[0 40 0 40]); add_line(sys, 'Models/2', 'PEV/1','autorouting','on'); % Set parameters into the mask. [N,ng] = size(m.OpPoints); tol = getAbsoluteTolerance(m); set_param(sys, ... 'Userdata', struct('Sites', m.OpPoints, ... 'Tol', tol, ... 'NSites', size(m.OpPoints, 1), ... 'NSwitchFactors', size(m.OpPoints, 2)), ... 'UserDataPersistent','on',... 'MaskInitialization', 'xregmaskinitialization', ... 'Mask', 'on'); % Construct the innards of the Models subsystem ModelBlockRoom = 70; sysModels = [sys '/Models']; set_param([sysModels '/Demux'], ... 'Outputs', num2str(N+1), ... 'Position', [100 90 105 100+(N+1)*ModelBlockRoom]); set_param([sysModels '/OutputSelect'],'NumInputPorts', num2str(N+1), ... 'Position', [670 90 710 100+(N+1)*ModelBlockRoom]); if DO_PEV % Demux the signal from the output selector switch add_block([BI 'Demux'], [sysModels, '/DemuxOut'], 'Outputs', '2', ... 'Position', [770 130 775 195]); add_line(sysModels, 'OutputSelect/1', 'DemuxOut/1', 'autorouting', 'on'); add_line(sysModels, 'DemuxOut/1', 'Y/1', 'autorouting', 'on'); add_line(sysModels, 'DemuxOut/2', 'PEV/1', 'autorouting', 'on'); else % Add a NaN constant output for PEV hPEVConst = add_block([BI 'Constant'], [sysModels '/PEVOut'], 'Value', 'NaN'); PEVpos = get_param([sysModels '/PEV'], 'Position'); set_param(hPEVConst, 'Position', PEVpos - [75 0 75 0]); add_line(sysModels, 'PEVOut/1', 'PEV/1', 'autorouting', 'on'); % Connect Y directly to YMerge add_line(sysModels, 'OutputSelect/1', 'Y/1', 'autorouting', 'on'); end % Wire up default (no model site hit) block add_line(sysModels, 'Demux/1', 'NoModel/Enable', 'autorouting', 'on'); if DO_PEV % Mux the Y and PEV and wire to the merge add_block([BI 'Mux'], [sysModels, '/MuxNoModel'], ... 'Position', [470 90 475 130], 'Inputs', '2', ... 'DisplayOption', 'bar'); add_line(sysModels, 'NoModel/1', 'MuxNoModel/1'); add_line(sysModels, 'NoModel/2', 'MuxNoModel/2'); add_line(sysModels, 'MuxNoModel/1', 'OutputSelect/2', 'autorouting', 'on'); else % Terminate dummy PEV output add_block([BI 'Terminator'], [sysModels, '/PevTerm'], ... 'Position', [430 110 450 130]); add_line(sysModels, 'NoModel/2', 'PevTerm/1', 'autorouting', 'on'); % Wire Y to the merge add_line(sysModels, 'NoModel/1', 'OutputSelect/2', 'autorouting', 'on'); end % Create and wire up the model sites TopPos = 100+ModelBlockRoom; models = m.ModelList.Models; for n = 1:N if isa(models{n},'xregmodel') [hInSys, hOutSys] = i_MakeModelBlock(models{n}, sysModels, TopPos, n, DO_PEV); InName = get(hInSys, 'Name'); add_line(sysModels, 'X/1', [InName, '/1'], 'autorouting', 'on'); add_line(sysModels, sprintf('Demux/%d', n+1), [InName, '/Enable'], 'autorouting', 'on'); OutName = get(hOutSys, 'Name'); add_line(sysModels, [OutName '/1'], sprintf('OutputSelect/%d', n+2), 'autorouting', 'on'); TopPos = TopPos + ModelBlockRoom; end end % Make an enabled subsystem for the given model function [hEnSys, hOut] = i_MakeModelBlock(m, sys, TopPos, n, DO_PEV) BI = 'built-in/'; hEnSys = add_block(sprintf('%sSubsystem', BI), sprintf('%s/Model%d', sys, n), ... 'Position', [300 TopPos 400 TopPos+40]); ModelBlockName = get_param(hEnSys, 'Name'); ModelBlockPath = [get_param(hEnSys, 'Parent') '/' ModelBlockName]; hModel = modelbuild(m, ModelBlockPath, 'Model', DO_PEV); set_param(hModel, 'Position', [200 100 300 140]); if isa(m, 'localmod') % Parameters for local model P = double(m); add_block([BI 'Constant'], [ModelBlockPath, '/Param'], ... 'Position', [50 100 70 120], 'Value', ['[' num2str(P(:)') ']']); add_line(ModelBlockPath, 'Param/1', 'Model/1', 'autorouting', 'on'); % Input variable add_block([BI 'Inport'], [ModelBlockPath, '/X'], 'Position', [50 140 70 160]); add_line(ModelBlockPath, 'X/1', 'Model/2', 'autorouting', 'on'); if length(get_param(hModel, 'InputSignalNames'))>2 % Datum input add_block([BI 'Constant'], [ModelBlockPath, '/Datum'], ... 'Position', [50 180 70 200], 'Value', '0'); add_line(ModelBlockPath, 'Datum/1', 'Model/3', 'autorouting', 'on'); end else % Input variable add_block([BI 'Inport'], [ModelBlockPath, '/X'], 'Position', [50 100 70 120]); add_line(ModelBlockPath, 'X/1', 'Model/1', 'autorouting', 'on'); end % Output add_block([BI 'Outport'], [ModelBlockPath, '/Y'], 'Position', [450 100 470 120]); add_line(ModelBlockPath, 'Model/1', 'Y/1', 'autorouting', 'on'); % Enabling block add_block([BI 'EnablePort'], [ModelBlockPath, '/Enable'], 'Position', [220 20 280 50]); if DO_PEV % dot product block add_block([BI, 'DotProduct'],[ModelBlockPath,'/Dot Product'], 'Position', [350 150 370 170]); add_line(ModelBlockPath, 'Model/2', 'Dot Product/1', 'autorouting', 'on'); add_line(ModelBlockPath, 'Model/2', 'Dot Product/2', 'autorouting', 'on'); % Pev outport add_block([BI 'Outport'], [ModelBlockPath, '/PEV'], 'Position', [450 150 470 170]); add_line(ModelBlockPath, 'Dot Product/1', 'PEV/1', 'autorouting', 'on'); % Add a mux block outside the subsystem hOut = add_block([BI 'Mux'], sprintf('%s/MuxModel%d', sys, n), ... 'Position', [470 TopPos 475 TopPos+40], 'Inputs', '2', ... 'DisplayOption', 'bar'); MdlName = sprintf('Model%d', n); MuxName = sprintf('MuxModel%d', n); add_line(sys, [MdlName '/1'], [MuxName, '/1']); add_line(sys, [MdlName '/2'], [MuxName, '/2']); % get correct variance matrix from cell array set_param(hModel,'R',sprintf('R{%d}',n)); else hOut = hEnSys; end