www.gusucode.com > mbcdesign 工具箱 matlab 源码程序 > mbcdesign/@constar/pslAddBlock.m

    function hBlock = pslAddBlock(con, hSystem)
%PSLADDBLOCK Add a Simulink block for the constraint to the given system
%
%  HBLOCK = PSLADDBLOCK(CON, HSYSTEM)
%
%  By default this method copies the block with the same name as the class
%  from the "mbcSLConstraints" library and puts it the given system. If
%  anything more complicated ie required then this method should be
%  overloaded. Note that "slSetParameters" is used to set parameters for
%  the block.
%
%  See also CONSTAR, CONBASE/PSLADDBLOCK, CONBASE/SLSETPARAMETERS.

%  Copyright 2005-2014 The MathWorks, Inc.

% Load the library with the constraint blocks
load_system( 'mbcSLConstraints' );

%
% Start with the star-shaped constraint template block from the library.
%
% Form the string for the source block
src = sprintf( 'mbcSLConstraints/%s', class( con ) );
% Form the string for the target block
tgt = sprintf( '%s/%s - %s', getfullname( hSystem ), getName( con ), class( con ) );
% add the new block
hBlock = add_block( src, tgt, 'MakeNameUnique', 'On', 'LinkStatus', 'None' );

%
% Add blocks for the coding and inverse coding
%
code_sys = codebuild( con.Model, tgt , 'Code Inputs' );
i_replace_block( [tgt, '/Coding goes here'], code_sys );

incode_sys = codebuild( con.Model, tgt , 'Inverse Code Outputs', true );
i_replace_block( [tgt, '/Inverse coding goes here'], incode_sys );

%
% Add a block for the RBF model
%
% Need to ensure that the inputs have been setup
xi = xinfo( con.Model ); 
xi.Names = num2cell( char( '@' + (1:nFactors( con )) ) );
con.Model = xinfo( con.Model, xi ); 

%>> rbf_sys = modelbuild( con.Model, tgt, 'Model', false );
rbf_sys = evalbuild( con.Model, tgt );	
simSetEvalParam(     con.Model, tgt );

% Position the RBF model block in the place where the model placeholder is
i_replace_block( [tgt, '/Model goes here'], rbf_sys );

%
% Add a block for the radius transform
%
switch lower( con.Transform ),
    case {'none', 'identity'},
        trans_blk = 'Radius Inverse Transform (none)';
    case 'log',
        trans_blk = 'Radius Inverse Transform (log)';
    case 'mccallum',
        trans_blk = 'Radius Inverse Transform (McCallum)';
    otherwise
        error(message('mbc:constar:InvalidState4', con.Transform));
end
trans_blk = add_block( ['mbcSLConstraints/', trans_blk], [tgt, '/', trans_blk], ...
    'MakeNameUnique', 'On', 'linkstatus', 'none' );

% Position the transform block in the place where the placeholder is
i_replace_block( [tgt, '/Radius Transform goes here'], trans_blk );

%
% Set up parameters
%
northPole = zeros( size( con.Center ) );
northPole(end) = 1;

set_param( tgt, 'C', ['[', sprintf( '%g ', con.Center ), ']'] );
set_param( [tgt, '/Map to sphere'], ...
    'C',  ['[', sprintf( '%g ', zeros( size( con.Center ) ) ), ']'], ...
    'NP', ['[', sprintf( '%g ', northPole ), ']'] );

%-------------------------------------------------------------------------------
function pos = i_replace_block( oldblk, newblk )
pos = get_param( oldblk, 'position' );
delete_block( oldblk );
set_param( newblk, 'position', pos );

%------------------------------------------------------------------------------|
% EOF
%------------------------------------------------------------------------------|