function [ShaftPosition, CircuitControl, Settings, Enforce, Userdata] = ...
          simscript_sixstep(Outputs,Settings,Userdata,Circuit,Drive,Geometry,Mesh,Windings,A,cell_p,cell_t,cell_Nu,path)
% Simulation script file to be used with function InvertedCircuit.m
% Six-step PWM
% Copyright 2008-2024, Vladimir Kuptsov

Vdc=Drive.Vdc;
CommutationAdvanceAngle=Drive.CommutationAdvanceAngle_sixstep;
SwitchDutyCycle=Drive.SwitchDutyCycle_sixstep;
SixStepOptions=Drive.SixStepOptions;
Is_max=Drive.Is_max_sixstep;
Is_hyst=Is_max*Drive.Is_hyst_perc_sixstep/100;
fspwm=Drive.fspwm;                   % PWM sampling frequency
Vdc_perc=Drive.Vdc_perc_sixstep;     % Vdc percentage imposed by PWM
delta_t=Settings.DF_timestep;
if isempty(Outputs.Rotang)
    rotang=Userdata.rotang;
    Ia=Userdata.Ia;
    Ib=Userdata.Ib;
    Ic=Userdata.Ic;
else
    rotang=Outputs.Rotang(end);
    Ia=sum(Outputs.Ia(:,end));
    Ib=sum(Outputs.Ib(:,end));
    Ic=sum(Outputs.Ic(:,end));
end
gamma0=Outputs.gamma0; 
nPolePairs=Outputs.nPolePairs;
if isfield(Userdata,'Vswitch')
    Vswitch=Userdata.Vswitch;
    pwmtimer=Userdata.pwmtimer;    
else
    Vswitch=1;
    pwmtimer=0;
end
CircuitControl.vdc=Vdc; 

commangle=CommutationAdvanceAngle*pi/180;
if strcmp(Windings.statorcircuit,'DeltaConnection')
    commangle=commangle+pi/6;
end
theta=rotang*nPolePairs-gamma0+commangle;
if theta<0
    while theta<0
        theta=theta+2*pi;
    end
else
    while theta>2*pi
        theta=theta-2*pi;
    end
end
if theta<0 || theta>2*pi
    error('theta error')
end
theta=theta*180/pi;
% Switching
if strcmp(SwitchDutyCycle,'120')   % 120 degrees switch duty cycle (2 switches opened at the same time)
    if theta>30 && theta<150
        CircuitControl.switch_a1=1;
        CircuitControl.switch_a2=0;
    elseif theta>210 && theta<330
        CircuitControl.switch_a1=0;
        CircuitControl.switch_a2=1;  
    else
        CircuitControl.switch_a1=0;
        CircuitControl.switch_a2=0;
    end
    if (theta>0 && theta<30) || theta>270
        CircuitControl.switch_b1=1;
        CircuitControl.switch_b2=0;
    elseif theta>90 && theta<210
        CircuitControl.switch_b1=0;
        CircuitControl.switch_b2=1;
    else
        CircuitControl.switch_b1=0;
        CircuitControl.switch_b2=0;
    end
    if theta>150 && theta<270
        CircuitControl.switch_c1=1;
        CircuitControl.switch_c2=0;
    elseif (theta>0 && theta<90) || theta>330
        CircuitControl.switch_c1=0;
        CircuitControl.switch_c2=1;
    else
        CircuitControl.switch_c1=0;
        CircuitControl.switch_c2=0;
    end
elseif strcmp(SwitchDutyCycle,'180')   % 180 degrees switch duty cycle (3 switches opened at the same time)
    if theta>=0 && theta<180
        CircuitControl.switch_a1=1;
        CircuitControl.switch_a2=0;
    else
        CircuitControl.switch_a1=0;
        CircuitControl.switch_a2=1;
    end
    if (theta>=0 && theta<60) || theta>=240
        CircuitControl.switch_b1=1;
        CircuitControl.switch_b2=0;
    else
        CircuitControl.switch_b1=0;
        CircuitControl.switch_b2=1;
    end
    if theta>=120 && theta<300
        CircuitControl.switch_c1=1;
        CircuitControl.switch_c2=0;
    else
        CircuitControl.switch_c1=0;
        CircuitControl.switch_c2=1;
    end
else
    error('Undefined switch duty cycle')
end
if strcmp(SixStepOptions,'Six-step with limited maximum current')
    if max(abs([Ia Ib Ic]))>Is_max
        Vswitch=0;
    end
    if ~Vswitch && max(abs([Ia Ib Ic]))<Is_max-Is_hyst
        Vswitch=1;
    end
    if ~Vswitch
        CircuitControl.switch_a1=0;
        CircuitControl.switch_a2=0;
        CircuitControl.switch_b1=0;
        CircuitControl.switch_b2=0;
        CircuitControl.switch_c1=0;
        CircuitControl.switch_c2=0;
    end
else
    Vswitch=1;
end
if strcmp(SixStepOptions,'Six-step with variable DC voltage')
    if 100*pwmtimer/(1/fspwm)>Vdc_perc
        CircuitControl.switch_a1=0;
        CircuitControl.switch_a2=0;
        CircuitControl.switch_b1=0;
        CircuitControl.switch_b2=0;
        CircuitControl.switch_c1=0;
        CircuitControl.switch_c2=0;
    end
    pwmtimer=pwmtimer+delta_t;
    if pwmtimer>(1/fspwm)
        pwmtimer=0;
    end
else
    pwmtimer=0;
end
Userdata.Vswitch=Vswitch;
Userdata.pwmtimer=pwmtimer;   


ShaftPosition=[];
% keep the same speed settings
if strcmp(Settings.DF_SpeedDependency,'Fixed speed simulation')
    Enforce.speed=2*pi*Settings.DF_Speed/60;
elseif strcmp(Settings.DF_SpeedDependency,'Variable speed simulation')
    Enforce=[];
end

