Code covered by the BSD License  

Highlights from
MPC555 Motor Control Function Blockset

MPC555 Motor Control Function Blockset

by

 

20 Mar 2006 (Updated )

Additional I/O blocks for MPC555 target - specifically targetting TPU functionality

mpc555_mtrctrl_tpu_comm2(block, action, varargin)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% FUNCTION [VARARGOUT] = MPC555_MTRCTRL_TPU_COMM2(BLOCK, ACTION, VARARGIN)
%
% Function to handle block mask callbacks and initialisation functions
% for motor commutation (COMM) function block
%
function [varargout] = mpc555_mtrctrl_tpu_comm2(block, action, varargin)
switch lower(action)
    case 'statetimes'
        i_callback_statetimes(block);
    case 'stateoutput'
        i_callback_statetimes(block);
    case 'stateinitial'
        i_callback_stateinitial(block);
    case 'specifychannelby'
        i_callback_specifychannelby(block);
    case 'commmode'
        i_callback_specifychannelby(block);
    case 'initialisation'
        [varargout{1}, varargout{2}, varargout{3}, varargout{4}] = ...
            i_initialisation(block, varargin{1}, varargin{2}, ...
            varargin{3}, varargin{4}, varargin{5});
    otherwise
        error(['Action ' action ' not found!']);
end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% function parameterspecs = i_getparameterspecs()
% 
function parameterspecs = i_getparameterspecs()

parameterspecs(1).ParamName = 'numPins';
parameterspecs(1).PrettyName = 'NO_OF_PINS';
parameterspecs(1).MinVal = 1;
parameterspecs(1).MaxVal = 8;
parameterspecs(1).DataType = 'uint16';
parameterspecs(1).ArrayLengthMin = 1;
parameterspecs(1).ArrayLengthMax = 1;
parameterspecs(1).ErrorMessage = '';
parameterspecs(1).SpecialConditions = [];

parameterspecs(2).ParamName = 'stateInitial';
parameterspecs(2).PrettyName = 'INITIAL_STATE';
parameterspecs(2).MinVal = 0;
parameterspecs(2).MaxVal = +inf;
parameterspecs(2).DataType = 'uint16';
parameterspecs(2).ArrayLengthMin = 1;
parameterspecs(2).ArrayLengthMax = 1;
parameterspecs(2).ErrorMessage = '';
parameterspecs(2).SpecialConditions = [];
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% function i_callback_statetimes(block)
% 
function i_callback_statetimes(block)

% Check that the value that has been entered is within range
parameterspecs = i_getparameterspecs();
[valid_value] = mpc555_mtrctrl_validate_mask_value(block, parameterspecs(1));

% Bring the number of pins into the local context
numPins = str2num(get_param(block, 'numPins'));
% Bring the required output state into the local context
stateoutput = get_param(block, 'stateOutput');
stateoutput = eval(stateoutput);
% Bring the commutation mode (sensored or sensorless) into the local
% context
commmode = get_param(block, 'commMode');

% If we are in sensored more
if strcmp( commmode, 'Sensored' )
    % The number of states must be the length of the array of outputs
    % specified
    numStates = size(stateoutput,2);
    % Initialise an array to contain the state times
    stateTimes = zeros(1,numStates); 
    % If in sensored mode then restrict number of states to 8 or 16
    if ~( numStates == 8 || numStates == 16 )
        message = 'In sensored mode there must be either 8 or 16 states defined';
        mpc555_mtrctrl_mask_error(block, message, 'Error');
        return;
    end
else
    % Bring the state times into the local context (specified in TCR1 clock
    % ticks)
    stateTimes = str2num(get_param(block, 'stateTimes'));
    % Guess the number of states from the length of the vector of state times
    numStates = min([size(stateTimes(:),1),size(stateoutput,2)]);
end


% If there are more than 22 states then fail, as this is Not Allowed
if numStates < 1 || numStates > 22
    message = 'A maximum of 22 states are allowed';
    mpc555_mtrctrl_mask_error(block, message, 'Error');
    return;
end

% Set the number of states in the mask
set_param(block, 'numStates', num2str(numStates));

% Only perform calculations if the number of states is consistent
if size(stateTimes(:),1) == numStates && size(stateoutput,2) == numStates
    USTATE_VECTOR=zeros(1,numStates);
    for a=1:numStates
        if size(stateoutput{a},2)~=numPins
            message = ['Outputs for exactly ',num2str(numPins),' pins must be specified'];
            mpc555_mtrctrl_mask_error(block, message, 'Error');
            return;
        end
        
        current_op_str = stateoutput{a};
        current_op_len = size(current_op_str(:),1);
        current_op_unused = 8-current_op_len;                  
        current_op_str_rev = '00000000';
        
        for b=1:current_op_len
            current_op_str_rev(b+current_op_unused)=current_op_str(current_op_len+1-b);
        end
             
        current_op=bitxor((bin2dec(current_op_str_rev)),2^(current_op_len)-1);
        
        USTATE_VECTOR(a) = bitor(bitshift(stateTimes(a),8), current_op);
    end
    % Write the calculated states to the USTATE_VECTOR parameter
    set_param(block, 'USTATE_VECTOR', mat2str(USTATE_VECTOR));
else
    return;
end

end

function i_callback_stateinitial(block)
    stateTimes = str2num(get_param(block, 'stateTimes'));
    numStates = size(stateTimes(:),1); 
    
    parameterspecs = i_getparameterspecs();
    parameterspecs(2).MaxVal = numStates - 1;
    [valid_value] = mpc555_mtrctrl_validate_mask_value(block, parameterspecs(2));
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% function i_callback_specifychannelby(block)
% 
function i_callback_specifychannelby(block)

specifychannelby = get_param(block, 'specifychannelby');

enable_conds(1).param_names = {'commMode', 'specifychannelby'};
enable_conds(1).param_conditions = {'Sensorless Match', 'Parameter Alias'};
enable_conds(1).to_enable = {'paramalias', 'specifychannelby', 'stateTimes'};
enable_conds(1).to_disable = {'counterchannel', 'counterparam'};
enable_conds(1).to_hide = {};
enable_conds(1).reciprocal = 0;

enable_conds(2).param_names = {'commMode', 'specifychannelby'};
enable_conds(2).param_conditions = {'Sensorless Match', 'Channel and Parameter Index'};
enable_conds(2).to_enable = {'counterchannel', 'counterparam', 'specifychannelby', 'stateTimes'};
enable_conds(2).to_disable = {'paramalias'};
enable_conds(2).to_hide = {};
enable_conds(2).reciprocal = 0;

enable_conds(3).param_names = {'commMode'};
enable_conds(3).param_conditions = {'Sensored'};
enable_conds(3).to_enable = {'paramalias'};
enable_conds(3).to_disable = {'specifychannelby', 'stateTimes', 'counterchannel', 'counterparam'};
enable_conds(3).to_hide = {};
enable_conds(3).reciprocal = 0;

mpc555_mtrctrl_maskparam(enable_conds);
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% BLOCK INITIALISATION
% 
function [numPins, numStates, USTATE_VECTOR, stateInitial] = ...
    i_initialisation(block, numPins, stateTimes, stateOutput, commMode, stateInitial)

parameterspecs = i_getparameterspecs();

% First validate numPins
[validate] = mpc555_mtrctrl_validate_value(numPins, parameterspecs(1));
if ~validate.Overall
    str_message = mpc555_mtrctrl_validate_error(parameterspecs(1), validate)
    error(str_message);
end

% If we are in sensored more
if commMode == 1
    % The number of states must be the length of the array of outputs
    % specified
    numStates = size(stateOutput,2);
    % Initialise an array to contain the state times
    stateTimes = zeros(1,numStates); 
    % If in sensored mode then restrict number of states to 8 or 16
    if ~( numStates == 8 || numStates == 16 )
        error('In sensored mode there must be either 8 or 16 states defined');
    end
else
    % Guess the number of states from the length of the vector of state times
    numStates = min([size(stateTimes(:),1),size(stateOutput,2)]);
end


% If there are more than 22 states then fail, as this is Not Allowed
if numStates < 1 || numStates > 22
    message = 'A maximum of 22 states are allowed';
    error(message);
end

% Only perform calculations if the number of states is consistent
if size(stateTimes(:),1) == numStates && size(stateOutput,2) == numStates
    USTATE_VECTOR=zeros(1,numStates);
    for a=1:numStates
        if size(stateOutput{a},2)~=numPins
            message = ['Outputs for exactly ',num2str(numPins),' pins must be specified'];
            error(message);
            return;
        end
        current_op_str = stateOutput{a};
        current_op_len = size(current_op_str(:),1);
        current_op_unused = 8-current_op_len;                  
        current_op_str_rev = '00000000';        
        for b=1:current_op_len
            current_op_str_rev(b+current_op_unused)=current_op_str(current_op_len+1-b);
        end
        current_op=bitxor((bin2dec(current_op_str_rev)),2^(current_op_len)-1);
        USTATE_VECTOR(a) = bitor(bitshift(stateTimes(a),8), current_op);
    end
else
    error('The state times vector and state output vector must be of equal length');

end

parameterspecs(2).MaxVal = numStates - 1;
[validate] = mpc555_mtrctrl_validate_value(stateInitial, parameterspecs(2));
if ~validate.Overall
    error('Please specify a valid initial state');
end

end

Contact us