function slfocwrapperMSFunc(blk, activeLoop)
%   No help is available for P code.

%   Author: A. Zimmerman
%   Copyright 2019 The MathWorks, Inc.

% get mask values
values = get_param(blk,'MaskWSVariables');
blkWSVarNames = {values.Name};
tunerBlk = [blk '/PID Autotuner/Closed-Loop PID Autotuner'];

% use activeloop to get currently tuned loop
switch activeLoop
    case 1
        activeLoop = 'Daxis';
    case 2
        activeLoop = 'Qaxis';
    case 3
        activeLoop = 'Speed';
    case 4
        activeLoop = 'Flux';
end

typeidx = values(strcmp(blkWSVarNames,['PIDType' activeLoop])).Value;
formidx = values(strcmp(blkWSVarNames,['PIDForm' activeLoop])).Value;
IFidx = values(strcmp(blkWSVarNames,['IntegratorMethod' activeLoop])).Value;
DFidx = values(strcmp(blkWSVarNames,['FilterMethod' activeLoop])).Value;
HasIntegrator = (values(strcmp(blkWSVarNames,['PlantType' activeLoop])).Value==2);
ExperimentModeIdx = values(strcmp(blkWSVarNames,'ExperimentMode')).Value;

if values(strcmp(blkWSVarNames,['PlantSign' activeLoop])).Value==1
    LoopSign = 1;
else
    LoopSign = -1;
end
% obtain compiled Ts from run-time object
rto = get_param([tunerBlk '/GainTs'],'RuntimeObject');
Ts = rto.OutputPort(1).Data;
% obtain bandwidth (3rd in w) from run-time object (due to potential external source)
rto = get_param([tunerBlk '/GainWC'],'RuntimeObject');
targetBandwidth = rto.OutputPort(1).Data(3);
% obtain target PM from run-time object (due to potential external source)
rto = get_param([tunerBlk '/GainPM'],'RuntimeObject');
targetPM = rto.OutputPort(1).Data;
% obtain frequencies responses and dc gain from run-time object
switch ExperimentModeIdx
    case 1 %Sinestream
        rto = get_param([tunerBlk '/Frequency Response Estimator/Frequency Response Estimator/Variant Subsystem/Sinestream/CA/Assemble'],'RuntimeObject');
    case 2 %Superimposed
        rto = get_param([tunerBlk '/Frequency Response Estimator/Frequency Response Estimator/Variant Subsystem/Superimpose/Response Estimation'],'RuntimeObject');
    case 3 %PRBS
        rto = get_param([tunerBlk '/Frequency Response Estimator/Frequency Response Estimator/Variant Subsystem/PRBS/Experiment Data Processing'],'RuntimeObject');
end
hG5 = rto.OutputPort(1).Data;
% obtain nominal u and y from run-time object
rto = get_param([tunerBlk '/Frequency Response Estimator/Frequency Response Estimator/New Nominal Detector/Enabled Delay U'],'RuntimeObject');
u0 = rto.OutputPort(1).Data;
rto = get_param([tunerBlk '/Frequency Response Estimator/Frequency Response Estimator/New Nominal Detector/Enabled Delay Y'],'RuntimeObject');
y0 = rto.OutputPort(1).Data;
nominal = [u0;y0];
% compute gains
w5 = targetBandwidth*[1/10;1/3;1;3;10];

% discrete
[P,I,D,N,achievedPM] = slpidfivepoint(typeidx,formidx,w5,hG5,targetPM,HasIntegrator,LoopSign,Ts,IFidx,DFidx);

% get existing userdata
data = get_param([blk '/Tuning Parameters'],'UserData');

% save results as userdata
data.(activeLoop).P = P;
data.(activeLoop).I = I;
data.(activeLoop).D = D;
data.(activeLoop).N = N;
data.(activeLoop).nominal = nominal;
data.(activeLoop).plant = frd(hG5,w5,'Ts',Ts);
data.(activeLoop).achievedPM = achievedPM;
data.(activeLoop).targetBandwidth = targetBandwidth;
data.(activeLoop).targetPM = targetPM;
data.(activeLoop).typeidx = typeidx;
data.(activeLoop).formidx = formidx;
data.(activeLoop).Ts = Ts;
data.(activeLoop).IFidx = IFidx;
data.(activeLoop).DFidx = DFidx;
set_param([blk '/Tuning Parameters'],'UserData',data);
