% Calc for acid pump control 
% 
% This is based on Ed Peltzers calcs for the HCL concentration and
% predicted currents
% 
% copyright (c) 2005 by W.J.Kirkwood of MBARI
% February 2, 2005 
% Last Modified 2/20/05 
% Revision: 1.0 
%
% ///////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
% 
% clean up from last activity 
clear all ; clf;
%
pH_SetPoint = 7.3;                % pH target value for control volume 
% pH_Valid_Max = 8.0;               % Maximum allowable value for inclusion in average
% pH_Valid_Min = 6.0;               % Minimum allowable value for inclusion in average
% PumpH20 = 720;                    % Sets the water pump at 720 mL/min
% PumpAcid = 720;                   % Sets the acid pump at 720 mL/min
% PumpFixedFlow = 1440;             % Sets the total pump flow at maximum current

% Main Control Algorithm for Pumps
% Concept: We would like the pump control to be exponential as the pH
% wanders from the set point. This makes our control take the form 
% y = f(x) + K = a*2^(b*(x-h))+k 
% y is the acid pump commanded output
%
% a = scalar multiplier and moves the zero crossover on the x axis, also
% change of sign of a flips the control curve around the x axis
a = .1;
%
% b = exponential multiplier and sets the rate of change of the slope of
% the control curve, also a sign change flips the control curve around the y axis
b = 1.5;
% 
% h is the x offset constant and sets the x crossover at y = 0
h = 1.0;
%
% K is the y offset constant and sets the y crossover at f(x) = 0
K = 0;                  % scaled in ml/min 
% 
%
% pH Bounds lower is - .1 pH unit and upper is + .1 pH unit for deadband
pH_BoundLow = pH_SetPoint - .10;
pH_BoundHigh = pH_SetPoint + .10; 
% 
% Control Tests for sign on equation variables a, b, and K to invert curve
% about dead band
%
%   if pH_Control < pH_BoundLow
         a1 = -a;
         b1 = -b;
         K1 = -K;
%   end
%          
% Set scaling for pump adjust
pH_Control = 0;
pH1_Control = 0;
i = 1;
j = i;
f = i;
g = i;
while i < 30
    pH_expo = b*(pH_Control - h);
    pH_X = a*(2.0^pH_expo);
    PumpAdjNum  = pH_X + K;
    pH_Data(1,i) = PumpAdjNum;
    pH_Data (2,j) = 0;     % pH_expo;
    pH_Data (3,f) = 0;     % pH_X;
    pH_Data (4,f) = pH_Control;
    pH_expo1 = b1*(pH1_Control - h);
    pH_X1 = a1*(2.0^pH_expo);
    PumpAdjNum1  = pH_X1 + K1;
    pH_Data(5,i) = PumpAdjNum1;
    pH_Data (6,j) = 0;      % pH_expo1;
    pH_Data (7,f) = 0;      % pH_X1;
    pH_Data (8,f) = pH1_Control;
    %
    pH_Data (9,i) = pH_BoundHigh - pH_SetPoint;
    pH_Data (10,i) = -pH_Data (9,i);
    pH_Control = pH_Control + .1;
    pH1_Control = pH1_Control - .1;
    i = i+1;
    j = i;
    f = i;
end
%
plot (pH_Data(4,:,:),pH_Data(1,:,:))
hold on
plot (pH_Data(8,:,:),pH_Data(5,:,:))
hold on
plot (pH_Data(4,:,:),pH_Data(9,:,:),'--r')
plot (pH_Data(8,:,:),pH_Data(10,:,:),'--r')
% Acid Flow Clipping Equation goes here 
%
%   if (PumpAdjNum > PumpAcidMax )
%       NewAcidNum = PumpAcidMax;
%   elseif (PumpAdjNum < PumpAcidMin )
%       NewAcidNum = PumpAcidMin;
%   else
%       NewAcidNum = PumpAdjNum;
%   end
% 
% Pump adjust test vs. pH 
%
%   if  pH_Control  > pH_BoundMax
%      PumpCmd
%
%
% slaving acid pump rate to the pH_Control variable
% pH_Delta = pH_SetPoint - pH_Control;                  % pH offset from desired value
%
% tests and settings for acid volume change
%   if pH_Delta < 0 
%          PumpAdjNum = pH_Delta_Old - pH_Delta;
%          PumpAcid = PumpAcid;                         % no change to pump input 
%          PumpFixedFlow =  Total_Flow - PumpAcid;      % Default total output from pumps in ml/sec
%   elseif pH_Delta > 0
%          PumpAcid = Total_Flow / 2;
%          PumpFixedFlow =  Total_Flow - PumpAcid;      % Default total output from pumps in ml/sec
%   else 
%          PumpAcid = PumpAcid;                         % no change in pump flow 
%   end      
%   pH_Delta_Old = pH_Delta;                            % resets pH for comparison to last value 
%   pH_PumpAcid_Old = pH_PumpAcid;                      % resets Acid Pump flow rate comparion to last value
%




% Shutdown and clean up 
% Stop time 

% fclose(pH_port1)
% fclose(pH_port2)
% fcloae(pH_port3) 


