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

threesin_variants = { ...
    'Standard', ...
    '3 outputs version', ...
    'XOR version', ...
    'Dead-Time correction', ...
    '3 outputs version XOR version', ...
    'Dead-Time correction - XOR version', ...
    };

threesin_variant_num = strmatch(get_param(block, 'threesin_variant'), threesin_variants, 'exact');

switch callback_fn
    case 'upper_channels'
        i_callback_channels(block, 8);
    case 'lower_channels'
        i_callback_channels(block, 9);
    case 'threesin_variant'
        i_callback_threesin_variant(block);
    case 'param_t'
        parameterspecs = i_getparameterspecs(threesin_variant_num);
        [validate] = mpc555_mtrctrl_validate_mask_value(block, parameterspecs(4));
        i_calculations(block,'param_t');
    case 'param_prescaler'
        parameterspecs = i_getparameterspecs(threesin_variant_num);
        [validate] = mpc555_mtrctrl_validate_mask_value(block, parameterspecs(5));
    case 'param_dt'
        parameterspecs = i_getparameterspecs(threesin_variant_num);
        [validate] = mpc555_mtrctrl_validate_mask_value(block, parameterspecs(6));
    case 'param_MPW'
        parameterspecs = i_getparameterspecs(threesin_variant_num);
        [validate] = mpc555_mtrctrl_validate_mask_value(block, parameterspecs(7));
    case 'param_theta_degrees'
        parameterspecs = i_getparameterspecs(threesin_variant_num);
        [validate] = mpc555_mtrctrl_validate_mask_value(block, parameterspecs(10));
        i_calculations(block,'param_theta_degrees');
    case 'calc_sin_freq'
        i_calculations(block,'calc_sin_freq');
    case 'calc_ampl'
        parameterspecs = i_getparameterspecs(threesin_variant_num);
        [validate] = mpc555_mtrctrl_validate_mask_value(block, parameterspecs(11));
        i_calculations(block,'calc_ampl');
    case 'fault_channels'
        parameterspecs = i_getparameterspecs(threesin_variant_num);
        [validate] = mpc555_mtrctrl_validate_mask_value(block, parameterspecs(12));
    case 'resolver_channels'
        parameterspecs = i_getparameterspecs(threesin_variant_num);
        [validate] = mpc555_mtrctrl_validate_mask_value(block, parameterspecs(13));
    case 'sync_channels'
        parameterspecs = i_getparameterspecs(threesin_variant_num);
        [validate] = mpc555_mtrctrl_validate_mask_value(block, parameterspecs(14));
    case 'param_sync_move'
        parameterspecs = i_getparameterspecs(threesin_variant_num);
        [validate] = mpc555_mtrctrl_validate_mask_value(block, parameterspecs(15));
    case 'param_sync_pw'
        parameterspecs = i_getparameterspecs(threesin_variant_num);
        [validate] = mpc555_mtrctrl_validate_mask_value(block, parameterspecs(16));        
    case 'param_sync_prescaler'
        parameterspecs = i_getparameterspecs(threesin_variant_num);
        [validate] = mpc555_mtrctrl_validate_mask_value(block, parameterspecs(17));        
    case 'param_resolver_move'
        parameterspecs = i_getparameterspecs(threesin_variant_num);
        [validate] = mpc555_mtrctrl_validate_mask_value(block, parameterspecs(18));        
    case 'param_resolver_prescaler'
        parameterspecs = i_getparameterspecs(threesin_variant_num);
        [validate] = mpc555_mtrctrl_validate_mask_value(block, parameterspecs(19));
    case 'inport_sinfreq_units'
        i_calculations(block,'inport_sinfreq_units');
    case 'init_threesin_get_data'
        varargout{1} = i_init_threesin_get_data(varargin{1});
    case 'init_threesin'
        [varargout{1}, varargout{2}] = i_init_threesin(block, varargin{:});
    otherwise
        error('Invalid callback');
end
end

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

parameterspecs(1).ParamName = 'param_ampl';
parameterspecs(1).PrettyName = 'Ampl';
parameterspecs(1).MinVal = 0;
parameterspecs(1).MaxVal = 2^15-1;
parameterspecs(1).DataType = 'int16';
parameterspecs(1).ArrayLengthMin = 1;
parameterspecs(1).ArrayLengthMax = 1;
parameterspecs(1).ErrorMessage = '';
parameterspecs(1).SpecialConditions = [];

parameterspecs(2).ParamName = 'param_theta';
parameterspecs(2).PrettyName = 'Theta';
parameterspecs(2).MinVal = -2^31;
parameterspecs(2).MaxVal = 2^31-1;
parameterspecs(2).DataType = 'int32';
parameterspecs(2).ArrayLengthMin = 1;
parameterspecs(2).ArrayLengthMax = 1;
parameterspecs(2).ErrorMessage = '';
parameterspecs(2).SpecialConditions = [];

parameterspecs(3).ParamName = 'param_dtheta';
parameterspecs(3).PrettyName = 'dTheta';
parameterspecs(3).MinVal = -2^31;
parameterspecs(3).MaxVal = 2^31-1;
parameterspecs(3).DataType = 'int32';
parameterspecs(3).ArrayLengthMin = 1;
parameterspecs(3).ArrayLengthMax = 1;
parameterspecs(3).ErrorMessage = '';
parameterspecs(3).SpecialConditions = [];

parameterspecs(4).ParamName = 'param_t';
parameterspecs(4).PrettyName = 'T';
parameterspecs(4).MinVal = 0;
parameterspecs(4).MaxVal = 2^16-1;
parameterspecs(4).DataType = 'uint16';
parameterspecs(4).ArrayLengthMin = 1;
parameterspecs(4).ArrayLengthMax = 1;
parameterspecs(4).ErrorMessage = '';
parameterspecs(4).SpecialConditions = [];

parameterspecs(5).ParamName = 'param_prescaler';
parameterspecs(5).PrettyName = 'prescaler';
parameterspecs(5).MinVal = 0;
parameterspecs(5).MaxVal = 2^16-1;
parameterspecs(5).DataType = 'uint16';
parameterspecs(5).ArrayLengthMin = 1;
parameterspecs(5).ArrayLengthMax = 1;
parameterspecs(5).ErrorMessage = '';
parameterspecs(5).SpecialConditions = [];

parameterspecs(6).ParamName = 'param_dt';
parameterspecs(6).PrettyName = 'DT';
parameterspecs(6).MinVal = 0;
parameterspecs(6).MaxVal = 2^16-1;
parameterspecs(6).DataType = 'uint16';
parameterspecs(6).ArrayLengthMin = 1;
parameterspecs(6).ArrayLengthMax = 1;
parameterspecs(6).ErrorMessage = '';
parameterspecs(6).SpecialConditions = [];

parameterspecs(7).ParamName = 'param_MPW';
parameterspecs(7).PrettyName = 'MPW';
parameterspecs(7).MinVal = 0;
parameterspecs(7).MaxVal = 2^16-1;
parameterspecs(7).DataType = 'uint16';
parameterspecs(7).ArrayLengthMin = 1;
parameterspecs(7).ArrayLengthMax = 1;
parameterspecs(7).ErrorMessage = '';
parameterspecs(7).SpecialConditions = [];

parameterspecs(8).ParamName = 'upper_channels';
parameterspecs(8).PrettyName = 'UPPER CHANNEL';
parameterspecs(8).MinVal = 0;
parameterspecs(8).MaxVal = 15;
parameterspecs(8).DataType = 'uint8';
parameterspecs(8).ArrayLengthMin = 3;
parameterspecs(8).ArrayLengthMax = 3;
parameterspecs(8).ErrorMessage = '';
parameterspecs(8).SpecialConditions = [];

parameterspecs(9).ParamName = 'lower_channels';
parameterspecs(9).PrettyName = 'LOWER CHANNEL';
parameterspecs(9).MinVal = 0;
parameterspecs(9).MaxVal = 15;
parameterspecs(9).DataType = 'uint8';
parameterspecs(9).ArrayLengthMin = 3;
parameterspecs(9).ArrayLengthMax = 3;
parameterspecs(9).ErrorMessage = '';
parameterspecs(9).SpecialConditions = [];

parameterspecs(10).ParamName = 'param_theta_degrees';
parameterspecs(10).PrettyName = 'Theta';
parameterspecs(10).MinVal = -180;
parameterspecs(10).MaxVal = +180;
parameterspecs(10).DataType = 'double';
parameterspecs(10).ArrayLengthMin = 1;
parameterspecs(10).ArrayLengthMax = 1;
parameterspecs(10).ErrorMessage = '';
parameterspecs(10).SpecialConditions = [];

parameterspecs(11).ParamName = 'calc_ampl';
parameterspecs(11).PrettyName = 'AMPLITUDE';
parameterspecs(11).MinVal = 0;
parameterspecs(11).MaxVal = 1;
parameterspecs(11).DataType = 'double';
parameterspecs(11).ArrayLengthMin = 1;
parameterspecs(11).ArrayLengthMax = 1;
parameterspecs(11).ErrorMessage = '';
parameterspecs(11).SpecialConditions = [];

parameterspecs(12).ParamName = 'fault_channels';
parameterspecs(12).PrettyName = 'Fault Channels';
parameterspecs(12).MinVal = 0;
parameterspecs(12).MaxVal = 15;
parameterspecs(12).DataType = 'uint8';
parameterspecs(12).ArrayLengthMin = 0;
parameterspecs(12).ArrayLengthMax = 1;
parameterspecs(12).ErrorMessage = '';
parameterspecs(12).SpecialConditions = [];

parameterspecs(13).ParamName = 'resolver_channels';
parameterspecs(13).PrettyName = 'Resolver Channels';
parameterspecs(13).MinVal = 0;
parameterspecs(13).MaxVal = 15;
parameterspecs(13).DataType = 'uint8';
parameterspecs(13).ArrayLengthMin = 0;
parameterspecs(13).ArrayLengthMax = 15;
parameterspecs(13).ErrorMessage = '';
parameterspecs(13).SpecialConditions = [];

parameterspecs(14).ParamName = 'sync_channels';
parameterspecs(14).PrettyName = 'Synchronization Channels';
parameterspecs(14).MinVal = 0;
parameterspecs(14).MaxVal = 15;
parameterspecs(14).DataType = 'uint8';
parameterspecs(14).ArrayLengthMin = 0;
parameterspecs(14).ArrayLengthMax = 15;
parameterspecs(14).ErrorMessage = '';
parameterspecs(14).SpecialConditions = [];

parameterspecs(15).ParamName = 'param_sync_move';
parameterspecs(15).PrettyName = 'Synchronisation offset';
parameterspecs(15).MinVal = -2^15;
parameterspecs(15).MaxVal = (2^15)-1;
parameterspecs(15).DataType = 'int16';
parameterspecs(15).ArrayLengthMin = 0;
parameterspecs(15).ArrayLengthMax = 15;
parameterspecs(15).ErrorMessage = '';
parameterspecs(15).SpecialConditions = [];

parameterspecs(16).ParamName = 'param_sync_pw';
parameterspecs(16).PrettyName = 'Synchronisation pulse width';
parameterspecs(16).MinVal = 0;
parameterspecs(16).MaxVal = 2^16;
parameterspecs(16).DataType = 'uint16';
parameterspecs(16).ArrayLengthMin = 0;
parameterspecs(16).ArrayLengthMax = 15;
parameterspecs(16).ErrorMessage = '';
parameterspecs(16).SpecialConditions = [];

parameterspecs(17).ParamName = 'param_sync_prescaler';
parameterspecs(17).PrettyName = 'Synchronisation prescaler';
parameterspecs(17).MinVal = 0;
parameterspecs(17).MaxVal = 2^16;
parameterspecs(17).DataType = 'uint16';
parameterspecs(17).ArrayLengthMin = 0;
parameterspecs(17).ArrayLengthMax = 15;
parameterspecs(17).ErrorMessage = '';
parameterspecs(17).SpecialConditions = [];

parameterspecs(18).ParamName = 'param_resolver_move';
parameterspecs(18).PrettyName = 'Resolver offset';
parameterspecs(18).MinVal = -2^15;
parameterspecs(18).MaxVal = (2^15)-1;
parameterspecs(18).DataType = 'int16';
parameterspecs(18).ArrayLengthMin = 0;
parameterspecs(18).ArrayLengthMax = 15;
parameterspecs(18).ErrorMessage = '';
parameterspecs(18).SpecialConditions = [];

parameterspecs(19).ParamName = 'param_resolver_prescaler';
parameterspecs(19).PrettyName = 'Resolver prescaler';
parameterspecs(19).MinVal = 0;
parameterspecs(19).MaxVal = 2^16;
parameterspecs(19).DataType = 'uint16';
parameterspecs(19).ArrayLengthMin = 0;
parameterspecs(19).ArrayLengthMax = 15;
parameterspecs(19).ErrorMessage = '';
parameterspecs(19).SpecialConditions = [];

parameterspecs(20).ParamName = 'inport_sinfreq_scalefactor';
parameterspecs(20).PrettyName = 'inport_sinfreq_scalefactor';
parameterspecs(20).MinVal = 0;
parameterspecs(20).MaxVal = inf;
parameterspecs(20).DataType = 'double';
parameterspecs(20).ArrayLengthMin = 0;
parameterspecs(20).ArrayLengthMax = 1;
parameterspecs(20).ErrorMessage = '';
parameterspecs(20).SpecialConditions = [];

parameterspecs(21).ParamName = 'inport_sinfreq_max';
parameterspecs(21).PrettyName = 'inport_sinfreq_max';
parameterspecs(21).MinVal = 0;
parameterspecs(21).MaxVal = inf;
parameterspecs(21).DataType = 'double';
parameterspecs(21).ArrayLengthMin = 0;
parameterspecs(21).ArrayLengthMax = 1;
parameterspecs(21).ErrorMessage = '';
parameterspecs(21).SpecialConditions = [];

%% Adjust values as appropriate
%%
%% Length of upper and lower channels
%% 8 is upper
%% 9 is lower
%% For XOR variants only 1 resolver and only 1 prescaler channel are
%% allowed
switch threesin_variant
    case 1 %% Standard
        parameterspecs(8).ArrayLengthMin = 3;
        parameterspecs(8).ArrayLengthMax = 3;
        parameterspecs(9).ArrayLengthMin = 3;
        parameterspecs(9).ArrayLengthMax = 3;
    case 2 %% Three outputs
        parameterspecs(8).ArrayLengthMin = 3;
        parameterspecs(8).ArrayLengthMax = 3;
        parameterspecs(9).ArrayLengthMin = 0;
        parameterspecs(9).ArrayLengthMax = inf;
    case 3 %% XOR version
        parameterspecs(8).ArrayLengthMin = 6;
        parameterspecs(8).ArrayLengthMax = 6;
        parameterspecs(9).ArrayLengthMin = 6;
        parameterspecs(9).ArrayLengthMax = 6;
        parameterspecs(13).ArrayLengthMax = 1;
        for a = 14:19
            parameterspecs(a).ArrayLengthMax = 1;
        end
    case 4 %% Dead Time Correction
        parameterspecs(8).ArrayLengthMin = 3;
        parameterspecs(8).ArrayLengthMax = 3;
        parameterspecs(9).ArrayLengthMin = 3;
        parameterspecs(9).ArrayLengthMax = 3;
    case 5 %% 3Out XOR
        parameterspecs(8).ArrayLengthMin = 6;
        parameterspecs(8).ArrayLengthMax = 6;
        parameterspecs(9).ArrayLengthMin = 0;
        parameterspecs(9).ArrayLengthMax = inf;
%        for a = 14:19
%            parameterspecs(a).ArrayLengthMax = 1;
%        end        
    case 6 %% DT XOR
        parameterspecs(8).ArrayLengthMin = 6;
        parameterspecs(8).ArrayLengthMax = 6;
        parameterspecs(9).ArrayLengthMin = 6;
        parameterspecs(9).ArrayLengthMax = 6;
        for a = 14:19
            parameterspecs(a).ArrayLengthMax = 1;
        end        
end



end


function i_callback_channels(block, position)

%if strcmp(get_param(block, 'threesin_threeoutputs'), 'on') & eq(position,9)
%    return;
%end

threesin_variants = { ...
    'Standard', ...
    '3 outputs version', ...
    'XOR version', ...
    'Dead-Time correction', ...
    '3 outputs version XOR version', ...
    'Dead-Time correction - XOR version', ...
    };

threesin_variant_num = strmatch(get_param(block, 'threesin_variant'), threesin_variants, 'exact');
parameterspecs = i_getparameterspecs(threesin_variant_num);
[validate] = mpc555_mtrctrl_validate_mask_value(block, parameterspecs(position));

end

function i_callback_threesin_variant(block)

threesin_variant = get_param(gcb, 'threesin_variant');

switch threesin_variant
    case 'Standard'
        set_param(block, 'threesin_threeoutputs', 'off');
        set_param(block, 'threesin_xor', 'off');
        set_param(block, 'threesin_dt', 'off');
    case '3 outputs version'
        set_param(block, 'threesin_threeoutputs', 'on');
        set_param(block, 'threesin_xor', 'off');
        set_param(block, 'threesin_dt', 'off');
    case 'XOR version'
        set_param(block, 'threesin_threeoutputs', 'off');
        set_param(block, 'threesin_xor', 'on');
        set_param(block, 'threesin_dt', 'off');
    case 'Dead-Time correction'
        set_param(block, 'threesin_threeoutputs', 'off');
        set_param(block, 'threesin_xor', 'off');
        set_param(block, 'threesin_dt', 'on');
    case '3 outputs version XOR version'
        set_param(block, 'threesin_threeoutputs', 'on');
        set_param(block, 'threesin_xor', 'on');
        set_param(block, 'threesin_dt', 'off');
    case 'Dead-Time correction - XOR version'
        set_param(block, 'threesin_threeoutputs', 'off');
        set_param(block, 'threesin_xor', 'on');
        set_param(block, 'threesin_dt', 'on');
end

% Enable and disable mask parameters as appropriate
enable_conds(1).param_names = {'threesin_variant'};
enable_conds(1).param_conditions = {'Standard'};
enable_conds(1).to_enable = {'param_dt', 'lower_channels', 'param_MPW'};
enable_conds(1).to_disable = {};
enable_conds(1).to_hide = {};
enable_conds(1).reciprocal = 0;
enable_conds(2).param_names = {'threesin_variant'};
enable_conds(2).param_conditions = {'3 outputs version'};
enable_conds(2).to_enable = {'param_MPW'};
enable_conds(2).to_disable = {'lower_channels', 'param_dt'};
enable_conds(2).to_hide = {};
enable_conds(2).reciprocal = 0;
enable_conds(3).param_names = {'threesin_variant'};
enable_conds(3).param_conditions = {'XOR version'};
enable_conds(3).to_enable = {'param_dt', 'lower_channels'};
enable_conds(3).to_disable = {'param_MPW'};
enable_conds(3).to_hide = {};
enable_conds(3).reciprocal = 0;
enable_conds(4).param_names = {'threesin_variant'};
enable_conds(4).param_conditions = {'Dead-Time correction'};
enable_conds(4).to_enable = {'param_dt', 'lower_channels','param_MPW'};
enable_conds(4).to_disable = {};
enable_conds(4).to_hide = {};
enable_conds(4).reciprocal = 0;
enable_conds(5).param_names = {'threesin_variant'};
enable_conds(5).param_conditions = {'3 outputs version XOR version'};
enable_conds(5).to_disable = {'param_dt', 'lower_channels','param_MPW'};
enable_conds(5).to_enable = {};
enable_conds(5).to_hide = {};
enable_conds(5).reciprocal = 0;
enable_conds(6).param_names = {'threesin_variant'};
enable_conds(6).param_conditions = {'Dead-Time correction - XOR version'};
enable_conds(6).to_enable = {'param_dt', 'lower_channels'};
enable_conds(6).to_disable = {'param_MPW'};
enable_conds(6).to_hide = {};
enable_conds(6).reciprocal = 0;

mpc555_mtrctrl_maskparam(enable_conds);

end


function varargout = i_calculations(block,paramname, varargin)

param_updates(1).ParamName = 'param_ampl';
param_updates(1).NeedsBlock = 0;
param_updates(1).Depends(1).Name = 'calc_ampl';
param_updates(1).Handle = @i_h_calc_ampl;

param_updates(2).ParamName = 'param_dtheta';
param_updates(2).NeedsBlock = 1;
param_updates(2).Depends(1).Name = 'calc_sin_freq';
param_updates(2).Depends(2).Name = 'param_t';
param_updates(2).Handle = @i_h_calc_dtheta;

param_updates(3).ParamName = 'param_dtheta_degrees';
param_updates(3).NeedsBlock = 1;
param_updates(3).Depends(1).Name = 'calc_sin_freq';
param_updates(3).Depends(2).Name = 'param_t';
param_updates(3).Handle = @i_h_calc_dtheta_degrees;

param_updates(4).ParamName = 'param_theta';
param_updates(4).NeedsBlock = 0;
param_updates(4).Depends(1).Name = 'param_theta_degrees';
param_updates(4).Handle = @i_h_calc_theta;

param_updates(5).ParamName = 'calc_pwm_freq';
param_updates(5).NeedsBlock = 1;
param_updates(5).Depends(1).Name = 'param_t';
param_updates(5).Handle = @i_h_calc_pwm_freq;

param_updates(6).ParamName = 'inport_sinfreq_scalefactor';
param_updates(6).NeedsBlock = 1;
param_updates(6).Depends(1).Name = 'inport_sinfreq_units';
param_updates(6).Depends(2).Name = 'param_t';
param_updates(6).Handle = @i_h_calc_scalefactor;

param_updates(7).ParamName = 'inport_sinfreq_max';
param_updates(7).NeedsBlock = 1;
param_updates(7).Depends(1).Name = 'inport_sinfreq_units';
param_updates(7).Depends(2).Name = 'param_t';
param_updates(7).Handle = @i_h_calc_scalefactormax;

paramnum = [];

% Now if a parameter name other than 'all' has been called then calculate
% the dependencies and add them to the list of what should be updated

if ~strcmp(paramname, 'all')
    for curupd = 1:length(param_updates)
        for curdep = 1:length(param_updates(curupd).Depends)
            if strcmp(param_updates(curupd).Depends(curdep).Name, paramname)
                paramnum = [paramnum, curupd];
            end
        end
    end
    %
    if isempty(paramnum)
        return;
    end
else
    %  Otherwise we want to calculate dependencies for all
    paramnum = 1:length(param_updates);
    %  Set the list for varargin referencing to be 1;
    current_in = 1;
    current_out = 1;
end

% For each of the parameters which needs to be updated
for current_update = 1:length(paramnum)
    % Build up a string of arguments which needs to be passed
    % to the helper function
    parameterstring = {};
    if param_updates(paramnum(current_update)).NeedsBlock
        parameterstring = [parameterstring, {block}];
    end

    for curdep = 1:length(param_updates(paramnum(current_update)).Depends)
        % If we are doing a normal mask-based version
        if ~strcmp(paramname, 'all')
            changedvalue = str2num(get_param(block, param_updates(paramnum(current_update)).Depends(curdep).Name));
        else
            changedvalue = varargin{current_in};
            current_in = current_in + 1;
        end
        parameterstring = [parameterstring, {changedvalue}];
    end


    % Call the function to calculate the new value
    updatedvalue = param_updates(paramnum(current_update)).Handle(parameterstring{:});
    if ~strcmp(paramname, 'all')
        updatedvaluestr = num2str(updatedvalue);
        set_param(block, param_updates(paramnum(current_update)).ParamName, updatedvaluestr)
    else
        varargout{current_out} = updatedvalue;
        current_out = current_out + 1;
    end
end

end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% CALCULATE THE TRUE VALUE OF PARAM_AMPL
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function param_ampl = i_h_calc_ampl(calc_ampl)
param_ampl = mpc555_mtrctrl_calc_range(calc_ampl, ...
    0, 1, ...
    0, 2^15-1, ...
    'int16');
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% CALCULATE DTHETA FROM DESIRED FREQUENCY AND PWM PULSE PERIOD
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function dtheta = i_h_calc_dtheta(block, frequency, param_t)
% Get the clock frequency
[clock, clockvalid] = mpc555_mtrctrl_getclock(block, 'TCR1');

proportion_of_rotation = frequency * param_t / clock;

% Scale the value to a proportion of a complete rotation
dtheta = mpc555_mtrctrl_calc_range(proportion_of_rotation, ...
    -0.5, 0.5, ...
    -2^31, 2^31-1, ...
    'int32');
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% CALCULATE DTHETA (in degrees) FROM DESIRED FREQUENCY AND PWM PULSE PERIOD
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function dtheta_degrees = i_h_calc_dtheta_degrees(block, frequency, param_t)
% Get the clock frequency
[clock, clockvalid] = mpc555_mtrctrl_getclock(block, 'TCR1');

proportion_of_rotation = frequency * param_t / clock;

% Scale the value to a proportion of a complete rotation
dtheta_degrees = mpc555_mtrctrl_calc_range(proportion_of_rotation, ...
    -0.5, 0.5, ...
    -180, 180, ...
    'double');
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% CALCULATE THE PWM CARRIER FREQUENCY FOR DISPLAY
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function frequency = i_h_calc_pwm_freq(block, param_t)

% Get the clock frequency
[clock, clockvalid] = mpc555_mtrctrl_getclock(block, 'TCR1');

% PWM frequency is TCR clock frequency divided by PWM period
frequency = clock / param_t;

end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% CALCULATE THE RAW VALUE OF THETA FROM THE DEGREES VALUE
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function param_theta = i_h_calc_theta(param_theta_degrees)
% Scale the value to a proportion of a complete rotation
param_theta = mpc555_mtrctrl_calc_range(param_theta_degrees, ...
    -180, 180, ...
    -2^31, 2^31, ...
    'int32');
if param_theta == 2^31
    param_theta = param_theta - 1;
end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% CALCULATE THE SCALING FACTOR FOR FREQUENCY ON INPUT PORTS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function scalefactor = i_h_calc_scalefactor(block, units, param_t)

% Get the clock frequency anyway
[clock, clockvalid] = mpc555_mtrctrl_getclock(block, 'TCR1');

units = get_param(block, 'inport_sinfreq_units');

switch units
    case 'Hz'
        proportion_of_rotation = 1 * param_t / clock;
        % Scale the value to a proportion of a complete rotation
        scalefactor = mpc555_mtrctrl_calc_range(proportion_of_rotation, ...
            -0.5, 0.5, ...
            -2^31, 2^31-1, ...
            'double');

    case 'Rad/sec'
        proportion_of_rotation = (1/(2*pi)) * param_t / clock;
        % Scale the value to a proportion of a complete rotation
        scalefactor = mpc555_mtrctrl_calc_range(proportion_of_rotation, ...
            -0.5, 0.5, ...
            -2^31, 2^31-1, ...
            'double');
                
    case 'Specify raw value of dTheta'
        scalefactor = 1;
end
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% CALCULATE THE SATURATION VALUE FOR FREQUENCY ON INPUT PORTS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function saturationvalue = i_h_calc_scalefactormax(block, units, param_t)

scalefactor = i_h_calc_scalefactor(block, units, param_t);

saturationvalue = (2^31-1)/scalefactor;

end

function [cmdstring, paramvaluesout] = i_init_threesin(block, paramvaluesin, structurename)

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

OutputValues = { 'param_ampl';
    'param_dtheta';
    'param_theta';
    'param_t';
    'param_prescaler';
    'param_dt';
    'param_MPW';
    'param_sync_move';
    'param_sync_pw';
    'param_sync_prescaler';
    'param_resolver_move';
    'param_resolver_prescaler';
    'upper_channels';
    'lower_channels';
    'fault_channels';
    'resolver_channels';
    'sync_channels';
    'inport_sinfreq_scalefactor';
    'inport_sinfreq_max'};

%% Build up the string
cmdstring = '';
for currentparam = 1:length(OutputValues)
    cmdstring = [cmdstring,OutputValues{currentparam},' = ', structurename,'.', OutputValues{currentparam},'; '];
end
%% Firstly perform calculations to ensure up to date information
[paramvaluesin.param_ampl, ...
    paramvaluesin.param_dtheta, ...
    paramvaluesin.param_dtheta_degrees, ...
    paramvaluesin.param_theta, ...
    paramvaluesin.calc_pwm_freq, ...
    paramvaluesin.inport_sinfreq_scalefactor, ...
    paramvaluesin.inport_sinfreq_max] = i_calculations(block, 'all', ...
    paramvaluesin.calc_ampl, ...
    paramvaluesin.calc_sin_freq, ...
    paramvaluesin.param_t, ...
    paramvaluesin.calc_sin_freq, ...
    paramvaluesin.param_t, ...
    paramvaluesin.param_theta_degrees, ...
    paramvaluesin.param_t, ...
    paramvaluesin.inport_sinfreq_units, ...
    paramvaluesin.param_t, ...
    paramvaluesin.inport_sinfreq_units, ...    
    paramvaluesin.param_t);

%% Now validate values to ensure they are within range
parameterspecs = i_getparameterspecs(paramvaluesin.threesin_variant);

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_threesin_get_data(structurename)

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

InputValues = { %% 'param_ampl', %%  No need for this - it's calculated from calc_ampl
    %% 'param_dtheta', %% No need for this, it's calculated from param_t, and calc_sin_freq
    %% 'param_theta',
    'param_t'; %% This _IS_ required
    'param_prescaler'; %% This _IS_ required
    'param_dt'; %% This _IS_ required
    'param_MPW'; %% This _IS_ required
    'param_sync_move'; %% This _IS_ required
    'param_sync_pw'; %% This _IS_ required
    'param_sync_prescaler'; %% This _IS_ required
    'param_resolver_move'; %% This _IS_ required
    'param_resolver_prescaler'; %% This _IS_ required
    'calc_ampl'; %% This _IS_ required
    'calc_sin_freq'; %% This _IS_ required
    'param_theta_degrees';
    'upper_channels';
    'lower_channels';
    'fault_channels';
    'resolver_channels';
    'sync_channels';
    'inport_sinfreq_units';
    'threesin_variant'};

%% Build up the string
cmdstring = '';

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

end

Contact us