function output=SR7(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
active=u(end-1);
error=u(end);

% 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 
fprintf('active in the beginning %.0f', active)

if active==0
    if error>0
        active=3;
    end 
    if error<0
        active=2;
    end 
     for i = 1:4
        v(i)=0;
     end 
end 

if active==1    % When AC is already active
    fprintf('u_1 %.0f', u(1));
    if u(1)>vmin(1)  %As long as it is still active, close the rest
        v(1)=u(1);
        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
        v(2)=u(2); % use v(2) u2
        active=2;  %transfer to 2
        disp('1 transferred to 2')
        v(1)=vmin(1);
        v(3)=vmin(3);
        v(4)=vmin(4);
    end
end 

if active==2
    if u(2)>vmin(2)     %as long as it is active and it can handle it
        v(2)=u(2);
        v(1)=vmin(1);
        v(3)=vmin(3);
        v(4)=vmin(4);
        if u(2) >= vmax(2)
            v(2)=vmax(2);
            v(1)=u(1);  %transfer to 1
            active=1;
            disp('2 transferred to 1')
        end
    else %when it is closed
        v(3)=u(3);
        active=3;  %transfer to 3
        disp('2 transferred to 3')
        v(4)=vmin(4);
        v(1)=vmin(1);
        v(2)=vmin(2);
    end
end 

if active==3
    if u(3)>vmin(3)
        v(3)=u(3);
        v(1)=vmin(1);
        v(2)=vmin(2);
        v(4)=vmin(4);
        if u(3)>=vmax(3)
            v(3)=vmax(3);
            v(4)=u(4);  %transfer to 4
            active=4;
            disp('3 transferred to 4')
        end
    else %when v3 closes and we need cooling
       v(2)=u(2);
       active=2; %transfer to 2
       disp('3 transferred to 2')
       v(1)=vmin(1);
       v(3)=vmin(3);
       v(4)=vmin(4);
    end
end 

if active==4
    if u(4)> vmin(4)
         v(4)=u(4);
         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
        v(3)= u(3); %transfer to 3
        active=3;
        disp('4 transferred to 3')
         v(1)=vmin(1);
         v(2)=vmin(2);
         v(4)=vmin(4);
    end
     
end 

% % Close AC if CW is enough
% % So, it is better to activate AC a little bit before. 
% %if vpast(2)>=vmax(2) %-0.05
% if v(2)<vmax(2) %- 0.05
%   %  if u(1)<vpast(1)  %If it is closing, let it close
%   %      v(1)=u(1);
%   %  else
%         v(1)=vmin(1); %Use AC when CW is (close to) not enough
%      
%    % end
% end 
% 
% %Close EH if HW is enough
% %Same as for AC
% %if vpast(3)>=vmax(3) %-0.05
% if v(3)<vmax(3) %-0.05
%  %   if u(4)<vpast(4)
%   %      v(4)=u(4);
%   %  else
%          v(4)=vmin(4);  %Use EH when HW is (close to) not enough
%   %  end
%    
% end 
% 
% %% Use this
% %If you were heating, before, close CW
% if vpast(3)>vmin(3)
%     v(2)=vmin(2);
% end
% 
% %If you were cooling, before, close HW
% if vpast(2)>vmin(2)
%     v(3)=vmin(3);
% end

%% This cycles
% %If you were heating, before, close CW
% if u(3)>vmin(3)
%     v(2)=vmin(2);
% end
% 
% %If you were cooling, before, close HW
% if u(2)>vmin(2)
%     v(3)=vmin(3);
% end

% %If you were heating, before, close CW
% if vpast(3)>vmin(3)
%     v(2)=vmin(2);
% end
% 
% %If you were cooling, before, close HW
% if vpast(2)>vmin(2)
%     v(3)=vmin(3);
% end
output=[v';active] ; 
%The "active" variable ended up not being necessary,but it does not harm, so I left it there.
v
fprintf('active in the end %.0f', active);
 
end