%% Create a plot of Current Sensor performance
clear all; close all;

dist = [0.5:.5:8.5];
%res  = [5600, 5600, 6300, 6800, 7700, 8400, 9210, 10150, 11640, ...
%    13500, 15200,18000, 20400, 23700, 30390, 40280, 51500];
%resv = diff(res);
%resv'/1000
resv = [
    150
    150
158
160
165
169
174
180
187
191
200
205
210
220
226
232
240
249
261
267
280
287
301
316
324
340
357
365
383
402
422
442
464
487
511
536
576
604
634
680
715
750
806
866
931
976
1070
1140
1240
1330
1470
1580
1740
1910
];
resv = resv';
res = cumsum(resv);

 
plot(res);
xlabel('Distance Across Sensor (in.)');
ylabel('Resistance Value (\)');

%% now model circuit with a 3.9K resistor in series with this
Ri = 10000; %39000
Vin = 12; %vdc
Vout = Vin.*(res./(res+Ri));

figure;
plot(Vout);

figure;
plot(dist,res);


%% 


clear; close all; 

dx = 5.0;
boardlength = 11.75;
taillength = 1;
arraylength = boardlength-taillength

L = arraylength*25.4; %convert to mm

N = floor((L-12.5)./dx )+1
xmax = N*dx;
x = 0:dx:xmax;

Vi = 12;%vdc
Ri = 5000;
b = 0.5;%V
Vmax = 9.5; % there is a 10V input level limit on the Ain
m  = (Vmax - b)/xmax;
Vo = m.*x +b;
Rv = Ri.*Vo ./ (Vi-Vo);



figure
plot(x,Rv);

figure;
%recal Vo
Vor = Vi.*(Rv./(Rv+Ri));
plot(x,Vo,x,Vor);

figure;
%Check Current 
I = Vo./Rv;
plot (x,I);

Rvalues = diff(Rv);

figure; 
plot(x(1:end-1), diff(Rv),'s');
Title('individual resistor values');


%% now what happens if the array behaves badly...
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
figure

plot (x(1:end-1), Rbad,x(1:end-1), Rvalues);
        

    


figure(1)
