% 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_8A(i) = 10*mean([max(Vx1.Vsamp) - min(Vx1.Vsamp),max(Vx2.Vsamp) - min(Vx2.Vsamp)]);  % Units are now Gauss.  1mT = 10 G
   sigBx_8A(i) =  10*std([max(Vx1.Vsamp) - min(Vx1.Vsamp),max(Vx2.Vsamp) - min(Vx2.Vsamp)]);
     Bx1_8A(i) = 10*mean(max(Vx1.Vsamp) - min(Vx1.Vsamp));  % Used to estimate difference between two runs
     Bx2_8A(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_8A, sigBx_8A, 'bo');
xlabel('Z (mm)');
ylabel('Bx (G)');
title('Linmot Bx field during upward movement, Max Current = 8 A');

Bx_integral_8A = integrate(Bx_8A, 1); % G-cm units when dx is 1
Bx1_integral_8A = integrate(Bx1_8A, 1); % G-cm units when dx is 1.   Used to estimate difference between two runs
Bx2_integral_8A = integrate(Bx2_8A, 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_8A(j) = 10*mean([max(Vy1.Vsamp) - min(Vy1.Vsamp),max(Vy2.Vsamp) - min(Vy2.Vsamp)]);  % Units are now Gauss.  1mT = 10 G
   sigBy_8A(j) =  10*std([max(Vy1.Vsamp) - min(Vy1.Vsamp),max(Vy2.Vsamp) - min(Vy2.Vsamp)]);
    By1_8A(j) = 10*mean(max(Vy1.Vsamp) - min(Vy1.Vsamp));  % Used to estimate difference between two runs
     By2_8A(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_8A, sigBy_8A, 'bo');
xlabel('Z (mm)');
ylabel('By (G)');
title('Linmot By field during upward movement, Max Current = 8 A');

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


% Get Bx & By backgrounds

 Vx_background_8A = Load_testplt(96);
  Bx_background_8A = 10*(max(Vx_background_8A.Vsamp) - min(Vx_background_8A.Vsamp)) % Units are Gauss.  3mT = 3V, so 1mT/V = 10 G/V
  
  
  Vy_background_8A = Load_testplt(95);
  By_background_8A = 10*(max(Vy_background_8A.Vsamp) - min(Vy_background_8A.Vsamp)) % Units are Gauss.  3mT = 3V, so 1mT/V = 10 G/V
  
  