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

    function blk=slconstraints(c,sys,lb,ub)
% SLCONSTRAINTS  Create a simulink model of the design constraints
%
%  BLK=SLCONSTRAINTS(C,SYS,lb,ub) creates a simulink block BLK in the 
%  system SYS.  The block takes a vector of factor levels and returns 
%  the values 0/1 depending on whether the point is within the
%  constrained volume or not.
%  The optional parameters lb and ub are upper and lower constraints to 
%  include in the output.
%

%  Copyright 2000-2005 The MathWorks, Inc. and Ford Global Technologies, Inc.

cons = c.Constraints;

if nargin > 2,
    cif = coninputfactor( c.Factors, c.Factors, [], lb, ub );
    range = conrange( cif );
    cons = [cons, {range}];
end

nCons = length( cons );

% Add a subsystem
hSystem = get_param( sys, 'Handle' );
hSubsystem = i_AddBlock( 'built-in/Subsystem', hSystem, 'Constraints', ...
    'Position', iSimulinkPosition( 10, 10, 100, 50 ) );
blk = getfullname( hSubsystem );

% Add an inport to the sub-system
hInport = i_AddBlock( 'built-in/Inport', hSubsystem, 'Factors', ...
    'Position', iSimulinkPosition( 10, 40*nCons+5, 20, 20 ) );

% Add outports for "status" and "distances" to the sub-system
hOutStatus   = i_AddBlock( 'built-in/Outport', hSubsystem, 'Status', ...
    'Position', iSimulinkPosition( 500, 25, 20, 20 ) );
hOutDistance = i_AddBlock( 'built-in/Outport', hSubsystem, 'Distances', ...
    'Position', iSimulinkPosition( 500, 40*nCons+5, 20, 20 ) );

if nCons < 2,
% Need to move the distances outport away from the status outport
    set_param( hOutDistance, 'Position', iSimulinkPosition( 500, 75, 20, 20 ) );
end

if nCons < 1,
    % There are no constraints to build the sub-system for.
    set_param( hInport, 'Position', iSimulinkPosition( 10, 25, 20, 20 ) );

    % Connect the inport to a terminator. Give the "status" as 1 and the
    % "distances" as -1
    hTerminator = i_AddBlock( 'built-in/Terminator', hSubsystem, 'T', ...
            'Position', iSimulinkPosition( 100, 25, 20, 20 ), ...
            'ShowName', 'off' );
        
    hPositiveOne = i_AddBlock( 'built-in/Constant', hSubsystem, 'Constant', ...
        'Position', iSimulinkPosition( 400, 25, 20, 20 ), ...
        'ShowName', 'off', 'Value', '1' );

    hNegativeOne = i_AddBlock( 'built-in/Constant', hSubsystem, 'Constant', ...
        'Position', iSimulinkPosition( 400, 75, 20, 20 ), ...
        'ShowName', 'off', 'Value', '-1' );

    i_AddLine( hInport, 1, hTerminator, 1 );
    i_AddLine( hPositiveOne, 1, hOutStatus, 1 );
    i_AddLine( hNegativeOne, 1, hOutDistance, 1 );
    
else
    % Build constraints sub-system
    
    % Add an inequality
    hLessThan = i_AddBlock( 'built-in/RelationalOperator', hSubsystem, 'Compare', ...
        'Position', iSimulinkPosition( 400, 10, 20, 30 ), ...
        'ShowName', 'off', 'Operator', '<=' );

    % Add a constant
    hZeroConstant = i_AddBlock( 'built-in/Constant', hSubsystem, 'Constant', ...
        'Position', iSimulinkPosition( 350, 30, 20, 20 ), ...
        'ShowName', 'off', 'Value', '0' );

    % Wire the constant, the inequality, and the status outport 
    i_AddLine( hZeroConstant, 1, hLessThan, 2 );
    i_AddLine( hLessThan, 1, hOutStatus, 1 );

    if nCons == 1,
        % Don't use a mux block bu wire the constraint output direct to the
        % dustances outport
        hMux = hOutDistance;
        
    else
        % Put in a mux block
        hMux = i_AddBlock( 'built-in/mux', hSubsystem, 'Mux', ...
            'Position', iSimulinkPosition( 300, 5, 5, nCons * 80 ), ...
            'ShowName', 'off', ...
            'Inputs', sprintf( '%d', nCons ),...
            'DisplayOption', 'bar' );

        i_AddLine( hMux, 1, hLessThan, 1 );
        i_AddLine( hMux, 1, hOutDistance, 1 );
    end
    
    % Add the constraint blocks
    for i = 1:nCons,
        [junk, hConstraint] = slblock( cons{i}, getfullname( hSubsystem ) );

        y = 45 + (i-1) * 80;
        set_param( hConstraint, 'Position', iSimulinkPosition( 100, y, 100, 40 ) );

        hTerminator = i_AddBlock( 'built-in/Terminator', hSubsystem, 'T', ...
            'Position', iSimulinkPosition( 230, y+20, 20, 20 ), ...
            'ShowName', 'off' );

        i_AddLine( hInport, 1, hConstraint, 1 );
        i_AddLine( hConstraint, 1, hMux, i );
        i_AddLine( hConstraint, 2, hTerminator, 1 );
    end
    
    % If there is only one constraint then there is no mux block and the
    % output from the cosntraint block won't be connected to the inport of
    % the less than block. We need to make this connection
    if nCons == 1,
        i_AddLine( hConstraint, 1, hLessThan, 1 );
    end
end

end
%------------------------------------------------------------------------------|
function hBlock = i_AddBlock( srcBlock, tgtSystem, blockName, varargin )
tgt = sprintf( '%s/%s', getfullname( tgtSystem ), blockName );

hBlock = add_block( srcBlock, tgt, ...
    'MakeNameUnique', 'On', ...
    'LinkStatus', 'None', ...
    varargin{:} );

end
%------------------------------------------------------------------------------|
function i_AddLine( srcBlock, srcPort, tgtBlock, tgtPort )
% We assume that the srcBlock and tgtBlock belong to the same (sub) system.
% If they don't then there will no doubt be an error.
system = fileparts( getfullname( srcBlock ) );
src = sprintf( '%s/%d', get_param( srcBlock, 'Name' ), srcPort );
tgt = sprintf( '%s/%d', get_param( tgtBlock, 'Name' ), tgtPort );

add_line( system, src, tgt, 'autorouting', 'on' );

end
%------------------------------------------------------------------------------|
function position = iSimulinkPosition( left, top, width, height )
position = [left, top, left+width, top+height];

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