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_bldc(block, callback_fn, varargin)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% function varargout = mpc555_mtrctrl_tpu_bldc(block, callback_fn, varargin)
%
function varargout = mpc555_mtrctrl_tpu_bldc(block, callback_fn, varargin)
% This is the entry function to allow the appropriate callback functions to
% be run

% Get a list of parameters which would be valid to pass to this block
callback_params = get_param(block, 'MaskNames');

% Get a list of parameters which have specifications which must be adhered
% to
[f,c] = i_parameterspecs();

% Create a list of parameters which will be numerically validated
params_to_validate = c(:,1);

% Create a list of parameters to ignore
params_to_ignore = callback_params;
for a = 1:length(params_to_validate)
   paramtokill = strmatch(params_to_validate{a}, params_to_ignore, 'exact');
   params_to_ignore(paramtokill) = '';
end

switch callback_fn
    case params_to_validate
        [validate] = mpc555_mtrctrl_validate_mask_value(block, i_getparameterspecs(callback_fn));
    case params_to_ignore
        % Just save that the parameter has changed
        mpc555_mtrctrl_mask_copy_values(block, 'save');
        return;        
     case 'init_bldc_get_data'
         varargout{1} = i_init_bldc_get_data(varargin{1});
     case 'init_bldc'
         [varargout{1}, varargout{2}] = i_init_bldc(block, varargin{:});
    otherwise
        error(['Invalid callback ', callback_fn]);
end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% function [f,c] = i_parameterspecs()
%
function [f,c] = i_parameterspecs()

f = {'ParamName',               'PrettyName',               'MinVal',   'MaxVal',   'DataType', 'ArrayLengthMin',   'ArrayLengthMax',   'ErrorMessage', 'SpecialConditions'};

c = {'channel_faultpin',        'Fault Channels',           0,          15,         'uint8',    0,                  1,                  '',             []; ...
     'channel_resolver',        'Resolver Channels',        0,          15,         'uint8',    0,                  15,                 '',             []; ...
     'channel_sync',            'Synchronization Channels', 0,          15,         'uint8',    0,                  15,                 '',             []; ...
     'param_dc',                'Initial duty cycle',       -2^15,      (2^15)-1,   'int16',    1,                  1,                  '',             []; ...
     'param_t',                 'T',                        0,          (2^16)-1,   'uint16',   1,                  1,                  '',             []; ...     
     'param_dt',                'DT',                       0,          (2^16)-1,   'uint16',   1,                  1,                  '',             []; ...          
     'param_mpw',               'MPW',                      0,          (2^16)-1,   'uint16',   1,                  1,                  '',             []; ...               
     'param_sync_move',         'Synchronisation offset',   -2^15,      (2^15)-1,   'int16',    0,                  15,                 '',             []; ...
     'param_sync_pw',           'Synchronization pulse width', 0,       2^16-1,     'uint16',   0,                  15,                 '',             []; ...
     'param_sync_presc',        'Synchronization prescaler',0,          2^16-1,     'uint16',   0,                  15,                 '',             []; ...
     'param_res_move',          'Resolver offset',          -2^15       2^15-1,     'int16',    0,                  15,                 '',             []; ...
     'param_res_presc',         'Resolver prescaler',       0,          2^16-1,     'uint16',   0,                  15,                 '',             []; ...
     };

end

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

% Get the raw table of parameterspecs
[f,c] = i_parameterspecs();

% Convert the table to a cell structure
parameterspecs = cell2struct(c,f,2);

switch nargin
    case 0 
        % If no parameters are specified when the function is called return
        % the whole list
        return;
    case 1
        % If a parameter is specified then return the specification
        % table for only this item
        paramnumber = strmatch(varargin(1), c(:,1), 'exact');
        if isempty(paramnumber)
           error(['Invalid parameter name ', varargin(1)]);
        else
           parameterspecs = parameterspecs(paramnumber); 
        end
    otherwise
        error('Invalid number of parameters')

end

end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Initialisation function for Brushless DC function
%
function [cmdstring, paramvaluesout] = i_init_bldc(block, paramvaluesin, structurename)

[f,c] = i_parameterspecs();

% Just create the output structure so we can add to it later
paramvaluesout.c = 1;

OutputValues = c(:,1);

%% Build up the string
cmdstring = '';
for currentparam = 1:length(OutputValues)
    cmdstring = [cmdstring,OutputValues{currentparam},' = ', structurename,'.', OutputValues{currentparam},'; '];
end

% In this case, just get the whole list of parameter specs.
parameterspecs = i_getparameterspecs();

for b = 1:length(OutputValues)
    currentvalidatename = OutputValues{b};
    currentvalidatenum = 0;
    for a = 1:length(parameterspecs)
        if strcmp(parameterspecs(a).ParamName, currentvalidatename)
            currentvalidatenum = a;
        end
    end
    if currentvalidatenum == 0
        error('Invalid parameter name');
    else
        % Run the standard validation routines
        [validate] = mpc555_mtrctrl_validate_value(...
            paramvaluesin.(currentvalidatename), ...
            parameterspecs(currentvalidatenum));
        if ~validate.Overall
            %%%%% Do this if not validated correctly
            str_message = mpc555_mtrctrl_validate_error(parameterspecs(currentvalidatenum), validate);
            error(str_message);
        else
            %% If validated correctly copy to the output
            field_temp = paramvaluesin.(currentvalidatename);
            paramvaluesout.(currentvalidatename) = field_temp;
        end
    end
end

end

function [cmdstring] = i_init_bldc_get_data(structurename)

% Cell array of strings containing the variables which should be passed
% into the initialisation function

[f,c] = i_parameterspecs();
InputValues = c(:,1);

%% Build up the string
cmdstring = '';

for currentparam = 1:length(InputValues)
    cmdstring = [cmdstring, structurename,'.',InputValues{currentparam},' = ', InputValues{currentparam},'; '];
end

end

Contact us