% This script finds the coefficients for the sensor linearization / calibration % using the assumed form of the correction which is linear in the parameters{e,f,g,h} % %THIS SCRIPT WAS MODIFIED BY MICHAEL WIDRICH ON 5-6-05; % Lines 13 to 18 % Lines 24 to 34 % %THE CHANGES WERE ADDED TO MAKE THE INTERFACE MORE USER FRIENDLY TO THE %STUDENT. % %INPUTS % Sensor Location (1=lower, 2=upper) fprintf('\n\n'); fprintf('****************SENSOCAL.m*********************\n\n'); fprintf('Which sensor do you wish to calibrate? \n'); fprintf(' 1 = LOWER \n'); fprintf(' 2 = UPPER \n'); sensloc = input('Choice... '); % Actual magnet distance from mechanical stop (cm), Yactual=[0 0.5 1 2 3 4 5 6]'; % Raw data from laser sensor at each height % Y1raw=[28259 22928 18369 11924 8682 6523 4941 3819]'; % Y2raw=[28260 23685 18788 12216 8337 5806 4126 2898]'; fprintf('\n\n'); for index = 1:length(Yactual); fprintf('Please enter the value of Yraw at %0.2f cm in counts. \n\n', Yactual(index)); if sensloc == 1 Y1raw(index) = input(' Y1raw = '); fprintf('\n'); elseif sensloc == 2 Y2raw(index) = input(' Y2raw = '); fprintf('\n'); end end % Construct the correction function array % Solve for the coefficient vector efgh via regression using the backslash operator if sensloc==1 % Construct the correction function array X=[1./Y1raw sqrt(1./Y1raw) ones(size(Y1raw)) Y1raw]; efgh=X\Yactual % Create vector of evaluation points for correction function Yraw_eval=(2000:100:30000)'; % Evaluate function at points Ycal=[1./Yraw_eval sqrt(1./Yraw_eval) ones(size(Yraw_eval)) Yraw_eval]*efgh; e1=efgh(1),f1=efgh(2),g1=efgh(3),h1=efgh(4) % Plot plot(Y1raw,Yactual,'o',Yraw_eval,Ycal,'-'),grid,%axis([0 30000 -7 0]) xlabel('Raw Sensor Output (counts)') ylabel('Magnet Position (cm)') title('y1 Sensor Calibration / Linearization') end if sensloc==2 % Construct the correction function array X=[1./Y2raw sqrt(1./Y2raw) ones(size(Y2raw)) Y2raw]; efgh=X\(-Yactual); e2=efgh(1),f2=efgh(2),g2=efgh(3),h2=efgh(4) % Create vector of evaluation points for correction function Yraw_eval=(2000:100:30000)'; % Evaluate function at points Ycal=[1./Yraw_eval sqrt(1./Yraw_eval) ones(size(Yraw_eval)) Yraw_eval]*efgh; % Plot plot(Y2raw,-Yactual,'o',Yraw_eval,Ycal,'-'),grid,axis([0 30000 -7 0]) xlabel('Raw Sensor Output (counts)') ylabel('Magnet Position (cm)') title('y2 Sensor Calibration / Linearization') end