% 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

% 
% Test matrices for pH change (bounce) 
% P = rand (1,10000);
PP(1,:) = 1:1000;
PP(2,:) = sin(rand (1,1000));
PP(3,:) = .01:.01:10;
figure (1) 
plot (PP (1,:), PP(2,:)) 
%
% 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
a1 = -a;
b1 = -b;
K1 = -K;
MaxCurrent = 30; 

% ////////////////////// Pump Flow Control \\\\\\\\\\\\\\\\\\\\\\\
% Two pumps run as an inverse ratio to maintain a set over flow volume
% uses the estimated current relationship. 
% ............ Note: Pumps ordered are 4.8 ml / rev ............... 
Current = input (' Enter current between 10 and 30 cm/sec : ');  

    if Current < 10
        Current = 10;    
    elseif Current > 30
        Current = 30;
    else
        Current = Current;
    end

Current   % Prints current for check 

   if Current == 10
          Total_Flow = 480;             % Minimum ml/min from combined pumps
          PumpAcid = Total_Flow / 2;
          % PumpFixedFlow =  Total_Flow - PumpAcid;  % Default total output from pumps in ml/sec
   elseif Current == 30
          Total_Flow = 1440;             % Maximum ml/min from combined pumps
          PumpAcid = Total_Flow / 2;
          % PumpFixedFlow =  Total_Flow - PumpAcid;  % Default total output from pumps in ml/sec
   else 
          Total_Flow = 1440 * (Current / MaxCurrent); % Minimum ml/min from combined pumps
          PumpAcid = Total_Flow / 2;
          % PumpFixedFlow =  Total_Flow - PumpAcid;  % Default total output from pumps in ml/sec
   end
%
pH_SetPoint = input (' Entet pH desired set point within 6.0 and 8.0 : '); 

    if pH_SetPoint < 6.0
        pH_SetPoint = 6.0;    
    elseif pH_SetPoint > 8.0
        pH_SetPoint = 8.0;
    else
        pH_SetPoint = pH_SetPoint;
    end

% Initiate indices for counters 
i = 1;
j = i;
f = i;
g = i;

% PumpAcidMax  = PumpAcid;
% PumpAcidMin = -PumpAcid;
%

% Set scaling for pump adjust

while i < 1000
    if PP(2,i) > .33
       PP(2,i) = .33;
    end
    
    if i < 200 | i > 600
        pH_Measured (1,i) = 7.0 + PP(2,i);              % fake high pH measurement for testing 
    else
        pH_Measured (1,i) = 7.0 - PP(2,i);              % fake low pH measurement for testing 
    end
    
    if pH_SetPoint < pH_Measured                    % test for increase acid pumping 
        pH_Delta = pH_SetPoint - pH_Measured;
        pH_Control = 10 * pH_Delta;
        pH_expo = b*(pH_Control - h);               % just stuff to make following math easier
        pH_X = a*(2.0^pH_expo);
        PumpAdjNum  = pH_X + K;
    elseif pH_SetPoint > pH_Measured                % test for increase acid pumping 
        pH_Delta = pH_Measured - pH_SetPoint;
        pH_Control = 10 * pH_Delta;
        pH_expo = b*(pH_Control - h);               % just stuff to make folliwng math easier
        pH_X = a*(2.0^pH_expo);
        PumpAdjNum  = pH_X + K;
    else 
        pH

        
    % ..... inserts model Data for plotting purposes ...... 
        pH_Data (1,i) = PumpAdjNum;
        pH_Data (2,i) = PP(2,i);
        pH_Data (3,i) = pH_Measured
        pH_Data (4,i) = pH_Delta;                   
        pH_Data (5,i) = 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;        
    PumpAdjNum  = pH_X + K;             % sets the amount of pump adjustment
            if PumpAdjNum > 1
                PumpAdjNum = 1;
            end
    i = i+1;
    j = i;
    f = i;
end
%
figure(2)
scatter (pH_Data(4,:,:),pH_Data(1,:,:))
hold on
% plot (pH_Data(8,:,:),pH_Data(5,:,:))
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) 
