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_validate_value( param_value, ...
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% function [validation] = ... 
%    mpc555_mtrctrl_validate_value( param_value, parameterspecs )
%
% This function is called to validate the input of a number passed in
% argument 'param_value'
%
% Arguments are:
%   'param_value':      The value of the parameter being validated
%   'parameterspecs':   Structure containing the specification to
%                       validate against.
%
%   'parameterspecs.ParamName':           Name of parameter in the mask of
%                                         the specified block
%   'parameterspecs.PrettyName':          A "pretty" name to use in errors
%   'parameterspecs.MinVal':              Minimum value that the parameter
%                                         may take
%   'parameterspecs.MaxVal':              Maximum value that the parameter
%                                         may take
%   'parameterspecs.DataType':            Data type of the parameter
%   'parameterspecs.ArrayLengthMin':      Minimum length of array if array
%   'parameterspecs.ArrayLengthMax':      Maximum length of array if array
%   'parameterspecs.SpecialConditions':   Any special conditions
%
%   Special conditions may be specified as SpecialConditions(n).Condition
%   and                                    SpecialConditions(n).ErrorMsg
%
% The returned parameter 'validation' is a structure of the following form:
%   'validation.Overall':                 Is overall validation correct
%   'validation.Range':                   Is range validated correctly
%   'validation.DataType':                Has data type validated correctly
%   'validation.Length':                  Has length validated correctly
%   'validation.Conditions':              Index for 'special' conditions
%                                         that have NOT validated correctly

function [validation] = mpc555_mtrctrl_validate_value( param_value, ...
    parameterspecs)

% First of all check the array length
if ~iscell(parameterspecs.ArrayLengthMin)
    if length(param_value) < parameterspecs.ArrayLengthMin
        validation.Length = false;
    elseif length(param_value) > parameterspecs.ArrayLengthMax
        validation.Length = false;
    else
        % If length is within specifications we can validate as true
        validation.Length = true;
    end
else
    if size(param_value,1) < parameterspecs.ArrayLengthMin{1} || ...
            size(param_value,2) < parameterspecs.ArrayLengthMin{2}
        validation.Length = false;
    elseif size(param_value,1) > parameterspecs.ArrayLengthMax{1} || ...
            size(param_value,2) > parameterspecs.ArrayLengthMax{2}
        validation.Length = false;
    else
        % If length is within specifications we can validate as true
        validation.Length = true;
    end
end

% Take a backup of the parameter value
param_value_backup = param_value;

% Now, apply saturation to each element of the array
below_min = find(lt(param_value, parameterspecs.MinVal));
above_max = find(gt(param_value, parameterspecs.MaxVal));
param_value(below_min) = parameterspecs.MinVal;
param_value(above_max) = parameterspecs.MaxVal;

% Now check if the value entered was in this range
if ~isequal(param_value_backup, param_value)
   validation.Range = false;
else
   validation.Range = true; 
end

% Remove fractional part if integral value is needed (i.e. perform a cast)
param_value = cast(param_value, parameterspecs.DataType);

% Now check if the value entered was in this range
if ~isequal(param_value_backup, param_value)
   validation.DataType = false;
else
   validation.DataType = true; 
end

% All special conditions met for the moment
validation.Conditions = [];
if length(parameterspecs.SpecialConditions) ~= 0
   for i = 1:length(parameterspecs.SpecialConditions)
       condition_i = eval(parameterspecs.SpecialConditions(i).Condition);
       if condition_i
           validation.Conditions = [validation.Conditions, i];
       end
   end
end


% Now calculate overall validation
validation.Overall = validation.Range & validation.DataType & ...
    validation.Length & isempty(validation.Conditions);

end

Contact us