%% Design of a New Reed Switch Level Sensor
%% Introduction
% DMO is refactoring the existing level sensor design from Doc
% Ricketts in order to place a longer lever sensor inside of 1 
% variable buoyancy tank. 


clear; close all; 

%% Switch Array Dimensions
% Following the same configuration used for a narrow, overlapping reed
% switch design, it is possible to determine how many switches can fit
% within a desired space.

%%
% Switch to switch pitch:
dx = 5.0;

%%
% Overall board length and tail length to create room for connections, in
% inches:
boardlength = 11.75;
taillength = 1;
arraylength = boardlength-taillength;
L = arraylength*25.4; %convert to mm

%%
% Calculate the number of elements that can fit, accounting for the
% horizontal component of the final switch in the array. 

N = floor((L-12.5)./dx )+1;
disp(['Number of elements is: ' num2str(N)]);

xmax = N*dx; %actual length of the array
x = 0:dx:xmax; %array x locations

%% Circuit Simulation

Vi = 12; %Input Voltage to the circuit
Ri = 100000; %Dividing resistor against the array (prevents short at one end)
b = 0.5; %voltage at x=0 (if flooded, short will read a zero value
Vmax = 9.5; % there is a 10V input level limit on the analog input device
m  = (Vmax - b)/xmax; %linear slope calculation for array behaviour

%% 
% Now we can calculate the desired output voltage of the divider, and thus
% calculate the total resistance of the variable series resistance.
Vo = m.*x +b;
Rv = Ri.*Vo ./ (Vi-Vo);

figure
plot(x,Rv);
title('Series Resistance of the Array Vs. Magnet Position');
xlabel('Magnet Position (mm)');
ylabel('Resistance (\Omega)');


%% 
% As a check, recalculate V_out along the array with these resistance
% values and plot.
figure;
%recal Vo
Vor = Vi.*(Rv./(Rv+Ri));
plot(x,Vo,x,Vor);
title('Voltage Output');
ylabel('Voltage (V)');
xlabel('Magnet Position (mm)');

%%
% Also, we would like to know how much current this sensor will draw from
% the system, as a check on the ratings of our components.
figure;

I = Vi./(Ri+Rv);
subplot(211);
plot (x,I*1000);
title('Power and Current');
ylabel('Current (mA)');

P = Vi.*I;
subplot(212);
plot(x,P*1000);
ylabel('Power (mW)');
xlabel('Magnet Position (mm)');

%%
% As a final result, it is important to know the individual values of the
% resistors to be used in the circuit, which is nothing more than the
% diffential of our Rv curve. 

Rvalues = diff(Rv);

figure; 
plot(x(1:end-1), diff(Rv),'s');
Title('Individual Resistor Values');
ylabel('Resistor Value (\Omega)');
xlabel('Array Position (mm)');


%% Magnetic Variation
% If the magnet triggers adjacent switches, let's determine the error in the 
% output voltage.

for n = 1:(length(Rvalues))
    if n ~=length(Rvalues)
        Rbad(n) = (1/sum(Rvalues(1:n))+ 1/Rvalues(n+1))^-1;
    else 
        Rbad(n) = Rvalues(n);
    end
end
Rvbad = [Rbad(1), cumsum(Rbad)];
Vobad = Vi.*(Rvbad./(Rvbad+Ri));

figure
plot (x, Vor,x, Vobad);
title('Erroneous Output Estimate');
ylabel('Voltage (V)');
xlabel('Magnet Position (mm)');
legend('Designed V_out','Erroneous V_out');

%%
% This slight error is acceptable and can be recalibration, linear
% behaviour is not affected. 

%close all; 


%%
% Now we need to see how actual, obtainable components

        

    

