% Linmot Shield Test Analysis
%  Finds the max and min of the voltage sample data and then computes the
%  field change for each z point.  Calibration is 1V = 1mT.  Linmot motor
%  had a maximum current of 8 A
 
z_positions = [110:-10:-110];

% Get Bx data

for i=1:length(z_positions)
  
   Vx1 = Load_testplt((2*i)-1);
   Vx2 = Load_testplt(2*i);
   Bx(i) = 10*mean([max(Vx1.Vsamp) - min(Vx1.Vsamp),max(Vx2.Vsamp) - min(Vx2.Vsamp)]);  % Units are now Gauss.  1mT = 10 G
   sigBx(i) =  10*std([max(Vx1.Vsamp) - min(Vx1.Vsamp),max(Vx2.Vsamp) - min(Vx2.Vsamp)]);
     Bx1(i) = 10*mean(max(Vx1.Vsamp) - min(Vx1.Vsamp));  % Used to estimate difference between two runs
     Bx2(i) =  10*mean(max(Vx2.Vsamp) - min(Vx2.Vsamp)); % Used to estimate difference between two runs
%    figure
%    plot(Vx1.Time, Vx1.Vsamp, 'bo')
%    hold
%     plot(Vx2.Time, Vx2.Vsamp,  'r*')
%     xlabel('Time (sec)');
%     ylabel('Volt (V)');
%     title(['Bx Hall probe analog output at z = ',num2str(z_positions(i)), ' mm']);
   
end
figure
plot_bars(z_positions, Bx, sigBx, 'bo');
xlabel('Z (mm)');
ylabel('Bx (G)');
title('Linmot Bx field during upward movement');

Bx_integral_8A = integrate(Bx, 1); % G-cm units when dx is 1
Bx1_integral_8A = integrate(Bx1, 1); % G-cm units when dx is 1.   Used to estimate difference between two runs
Bx2_integral_8A = integrate(Bx2, 1); % G-cm units when dx is 1    Used to estimate difference between two runs

for j=1:length(z_positions)
   
   Vy1 = Load_testplt((2*j)+45);
   Vy2 = Load_testplt((2*j)+ 46);
   By(j) = 10*mean([max(Vy1.Vsamp) - min(Vy1.Vsamp),max(Vy2.Vsamp) - min(Vy2.Vsamp)]);  % Units are now Gauss.  1mT = 10 G
   sigBy(j) =  10*std([max(Vy1.Vsamp) - min(Vy1.Vsamp),max(Vy2.Vsamp) - min(Vy2.Vsamp)]);
    By1(j) = 10*mean(max(Vy1.Vsamp) - min(Vy1.Vsamp));  % Used to estimate difference between two runs
     By2(j) =  10*mean(max(Vy2.Vsamp) - min(Vy2.Vsamp)); % Used to estimate difference between two runs
%     figure
%     plot(Vx1.Time, Vx1.Vsamp, 'bo')
%    hold
%     plot(Vx2.Time, Vx2.Vsamp, 'r*')
%     xlabel('Time (sec)');
%     ylabel('Volt (V)');
%     title(['By Hall probe analog output at z = ',num2str(z_positions(j)), ' mm']);
end

figure
plot_bars(z_positions, By, sigBy, 'bo');
xlabel('Z (mm)');
ylabel('By (G)');
title('Linmot By field during upward movement');

By_integral_8A  = integrate(By, 1); % G-cm units when dx is 1
By1_integral_8A  = integrate(By1, 1); % G-cm units when dx is 1.  Used to estimate difference between two runs
By2_integral_8A  = integrate(By2, 1); % G-cm units when dx is 1   Used to estimate difference between two runs