% 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 
% 
%

% 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

% 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; 
    
% Initiate indices for counters 
i = 1;

% PumpAcidMax  = PumpAcid;
% PumpAcidMin = -PumpAcid;
%

% Set scaling for pump adjust

while i < 1000                                     % iterates for testing = 1000 pH readings
    if PP(2,i) > .33                               % takes RANDM matrix and normalizes it to max readings
       PP(2,i) = .33;
    end
    
    if i < 200 | i > 600
        pH_Measured = pH_SetPoint + PP(2,i);       % fake high pH measurement for testing 
    else
        pH_Measured = pH_SetPoint - PP(2,i);       % fake low pH measurement for testing 
    end
    
    if pH_SetPoint < pH_Measured                    % test for increasing acid pumping 
        pH_Delta = pH_SetPoint - pH_Measured;
            if pH_Delta > .33                       % limits max corection to 100% 
                pH_Delta = .33;
            end
        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;                     % sets the percentage to increase acid flow
            if PumpAdjNum > 1                       % stops any corrections larger than 100 percent = max pumping
                PumpAdjNum = 1;                     % NOTE: this shouldn't be needed, but I put it in as backup 
            end
        
    elseif pH_SetPoint > pH_Measured                % test for reducing acid pumping 
        pH_Delta = pH_Measured - pH_SetPoint;
            if pH_Delta < - .33                     % limits max corection to 100% 
                pH_Delta = - .33;
            end        
        pH_Delta1 = abs(pH_Delta);                       % adjusts sign convention for proper curve fit
        pH_Control = 10 * pH_Delta1;
        pH_expo = b1*(pH_Control - h);              % just stuff to make folliwng math easier
        pH_X = a1*(2.0^pH_expo);
        PumpAdjNum  = pH_X + K;                     % sets the percentage to reduce acid flow 
            if PumpAdjNum > 1                       % stops any corrections larger than 1.0 = max pumping
                PumpAdjNum = 1;                     % NOTE: this shouldn't be needed, but I put it in as backup 
            end        
        
    else 
        PumpAdjNum = pH_Data(1,i);                  % fills data slot with last pump adjustment amount 
    end


    % ..... 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_Data (6,i) = pH_BoundHigh - pH_SetPoint;           % just used for giving dead band line + side on graph
        pH_Data (7,i) = -pH_Data (6,i);                       % just used for giving dead band line - side on graph
        pH_Data (8,i) = i;

    i = i+1;
end
%
figure(2) 
plot (pH_Data(8,:,:),pH_Data(3,:,:))

figure(3)
scatter (pH_Data(5,:,:),pH_Data(1,:,:))
hold on
% plot (pH_Data(8,:,:),pH_Data(5,:,:))
plot (pH_Data(4,:,:),pH_Data(6,:,:),'--r')
% plot (pH_Data(8,:,:),pH_Data(7,:,:),'--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) 
