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_tsm(block, callback_fn, varargin)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% function varargout = mpc555_mtrctrl_tpu_tsm(block, callback_fn, varargin)
%
function varargout = mpc555_mtrctrl_tpu_tsm(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 'param_pin_seq_array'
        [validate] = mpc555_mtrctrl_validate_mask_value(block, i_getparameterspecs(callback_fn));
        i_callback_param_pin_seq_array(block);
    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_tsm_get_data'
         varargout{1} = i_init_tsm_get_data(varargin{1});
     case 'init_tsm'
         [varargout{1}, varargout{2}] = i_init_tsm(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 = {'param_current_position',  'STARTING POSITION',        0,          2^16-1,     'uint16',   1,                  1,                  [],             [];
     'param_start_period',      'START_PERIOD',             0,          2^15-1,     'uint16',   1,                  1,                  [],             [];
     'param_slew_period',       'SLEW_PERIOD',              0,          2^15-1,     'uint16',   1,                  1,                  [],             [];
     'param_pin_seq_array',     'PIN SEQUENCE ARRAY',       0,          1,          'uint8',    16,                 16,                 [],             [];
     'param_pin_seq',           'PIN SEQUENCE',             0,          2^16-1,     'uint16',   1,                  1,                  [],             [];
     'param_table_values',      'TABLE VALUES',             0,          2^8-1,      'uint8',    1,                  15*16,              [],             [] ...
     };

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

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Callback function for turning array of pins into a hex number
%
function i_callback_param_pin_seq_array(block)

param_pin_seq_array = str2num(get_param(block, 'param_pin_seq_array'));

if length(param_pin_seq_array) ~= 16
    return;
end

final = 0;
for n=1:16
    if param_pin_seq_array(n)
        final = final + 2^(16-n);
    end
end

set_param(block, 'param_pin_seq', num2str(final));

end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Initialisation function for Table Stepper Motor Function
%
function [cmdstring, paramvaluesout] = i_init_tsm(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_tsm_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