function output=SR8(u)
%This function is made for:
% AC with negative slope
% CW with negative slope
% HW with positive slope
% EH with positive slope

%https://blogs.mathworks.com/simulink/2014/08/06/how-do-you-hold-the-value-of-a-signal/

%input=u=[currentMVs, MVs at previous step, active MV]'
%the last element of the input for this function has the information of
%which MV was active before entering the function.  The initial value is 0.

% All internal signals v  are from 0 to 1
 
MV=4; %(length(u)-2)/2;     %number of MVs: 4
vmax=ones(MV,1);        % All internal signals v  are from 0 to 1
vmin=zeros (MV,1);

%v=u(1:MV);              %current suggestion from each controller for their corresponding valves
%vpast=u(MV+1:2*MV);     %previous state of each valve
u
active=u(9); %u(end-6);
u0active=u(10); %u(end-5);
disp('reset Int past in beginning')
resetIntpast=u(11:14) %u(end-1:end-4);
error=u(15); %u(end);

fprintf('active in the beginning %.0f \n', active)

    
%Proportional gains  2theta, 3 theta
Kc1 = -0.4; % -0.2667; %-0.4000; %-0.2; % -0.4000;
Kc2 =  -0.2500; %-0.1667; %-0.2500; %-0.1250; % -0.2500;
Kc3 = 0.1389; % 0.0926; %0.1389; %0.0694; %0.1389;
Kc4 = 0.3125; %0.2083; %0.3125; %0.1563; %0.3125;
% Set all outputs as suggested
%v=u(1:MV);
% 1,5:AC
% 2,6:CW
% 3,7:HW
% 4,8:EH

%Saturation for each MV
% for i = 1:4
%     if v(i)>=vmax(i)
%        v(i)=vmax(i);
%     end
%     if v(i)<=vmin(i)
%        v(i)=vmin(i);
%     end
% end 


if active==0
    resetIntpast=ones(4,1);  %We need it either 1 or -1
    resetInt=resetIntpast; %This does not matter, as long as error is zero in the beginning.
    newactive=0;
    if error>0
        active=3;
    end 
    if error<0
        active=2;
    end 
     for i = 1:4
        v(i)=0;
     end 
end 

 for i = 1:4
        u(i)=u(i)+u0active;  %add the bias. It will only be correct for the active one. We need this to compare
 end 
     
if active==1    % When AC is already active
    fprintf('u_1 %.0f', u(1));
    resetInt=resetIntpast;
    newactive=active;
    if u(1)>vmin(1)  %As long as it is still active, close the rest
        v(1)=u(1); %+u0active;  %here u0active should be 0, because it is from where AC starts when CW transfers to it.
        v(2)=vmax(2);
        v(3)=vmin(3);
        v(4)=vmin(4);
        if u(1) >= vmax(1)  %This is just to have max v1
            v(1)=vmax(1);
        end
    else            %If u1<= u1min
        newactive=2;  %transfer to 2
        disp('1 transferred to 2')
        u0active = 1 ; % when 2 receives from 1, it is saturated at max
        v(2)=Kc2*error+u0active; %u(2); % use v(2) u2
        resetInt(2)=resetIntpast(2)*(-1); %change sign, so that integrators reset
        
        v(1)=vmin(1);
        v(3)=vmin(3);
        v(4)=vmin(4);
    end
end 

if active==2
    disp('reset Int in 2')
    resetInt=resetIntpast
    
    newactive=active;
    if u(2)>vmin(2)     %as long as it is active and it can handle it
        v(2)=u(2); %+u0active;
        v(1)=vmin(1);
        v(3)=vmin(3);
        v(4)=vmin(4);
        if u(2) >= vmax(2)
            v(2)=vmax(2);
            newactive = 1;
            disp('2 transferred to 1')
            u0active = 0; %when 2 transfers to 1, 1 is at zero
            v(1)=Kc1*error+u0active;     %u(1);  %transfer to 1
            resetInt(1)=resetIntpast(1)*(-1); %change sign, so that integrators reset
        end
    else %when it is closed
        
        newactive=3;  %transfer to 3
        disp('2 transferred to 3')
        u0active = 0; 
        v(3)=Kc3*error+u0active;  %u(3);
        resetInt(3)=resetIntpast(3)*(-1); %change sign, so that integrators reset
        
        v(4)=vmin(4);
        v(1)=vmin(1);
        v(2)=vmin(2);
    end
end 

if active==3
    resetInt=resetIntpast;
    newactive=active;
    if u(3)>vmin(3)
        v(3)=u(3); %+u0active;
        v(1)=vmin(1);
        v(2)=vmin(2);
        v(4)=vmin(4);
        if u(3)>=vmax(3)
            v(3)=vmax(3);
            
            newactive=4;
            disp('3 transferred to 4')
            u0active = 0; 
            v(4)=Kc4*error+u0active; %u(4);  %transfer to 4
            resetInt(4)=resetIntpast(4)*(-1); %change sign, so that integrators reset
        end
    else %when v3 closes and we need cooling
       
       newactive=2; %transfer to 2
       disp('3 transferred to 2')
       u0active = 0; 
       v(2)=Kc2*error+u0active; %u(2);
       resetInt(2)=resetIntpast(2)*(-1); %change sign, so that integrators reset
       
       v(1)=vmin(1);
       v(3)=vmin(3);
       v(4)=vmin(4);
    end
end 

if active==4
    resetInt=resetIntpast;
    newactive=active;
    if u(4)> vmin(4)
         v(4)=u(4); %+u0active;
         v(1)=vmin(1);
         v(2)=vmin(2);
         v(3)=vmax(3);
        if u(4)>=vmax(4)
            v(4)=vmax(4);
        end
    else  %when v4 closes
       
        newactive=3;
        disp('4 transferred to 3')
        u0active= 1; 
         v(3)=Kc3*error+u0active; % u(3); %transfer to 3
         resetInt(3)=resetIntpast(3)*(-1); %change sign, so that integrators reset
          
         v(1)=vmin(1);
         v(2)=vmin(2);
         v(4)=vmin(4);
    end
     
end 


output=[v';newactive;u0active;resetInt] ; 
output
%The "active" variable ended up not being necessary,but it does not harm, so I left it there.

fprintf('active in the end %.0f \n', newactive);
fprintf('reset int %.0f \n', resetInt);
 
end