%CALIB_FINS - Generate calibration coefficients for elevator & rudder
%
%    [ee,rr] = calib_fins( fprefix )
%    [ee,rr] = calib_fins( [E E_he R R_he] ), E & R in radians
%
%B. A. Moran, 29-Jul-1997

%    Copyright (C) 1995-1998
%    Massachussetts Institute of Technology
%    Sea Grant AUV Laboratory
%    $Id: calib_fins.m,v 1.1 2000/01/10 19:07:46 rob Exp $	

function [ee,rr] = calib_fins( arg1 )

if isstr( arg1 )

    ii = findstr( arg1, '/' );

    if ~isempty(ii) label = arg1((max(ii)+1):end);
    else label = arg1;
    end

    Earg = sprintf( '%sE.dat', arg1 );
    Rarg = sprintf( '%sR.dat', arg1 );
else
    global run_name
    label = run_name;
    Earg = arg1(:,1:2);
    Rarg = arg1(:,3:4);
end

set( gcf, 'Name', ['Fin Calibration: ' label] );

subplot( 2, 1, 1 );
e = hecubic( Earg );
mapping = sprintf( '%sE: x = %.8gy^3 %+.7gy^2 %+.6gy %+.3g', ...
	label, e(1), e(2), e(3), e(4) );
title( mapping )
xlabel( 'M_ELEVATOR_ANGLE (deg)', 'Interpreter', 'none' )
ylabel( 'M_HE_ELEV_RAW (0-255)', 'Interpreter', 'none' );

code = [];
for ii = 0:3
    code = strvcat( code, sprintf('#define E%d   %+.8e', ii, e(4-ii)) );
end
disp( code );


subplot( 2, 1, 2 );
r = hecubic( Rarg );
mapping = sprintf( '%sR x = %.8gy^3 %+.7gy^2 %+.6gy %+.3g', ...
	label, r(1), r(2), r(3), r(4) );
title( mapping )
xlabel( 'M_RUDDER_ANGLE (deg)', 'Interpreter', 'none' )
ylabel( 'M_HE_RUD_RAW (0-255)', 'Interpreter', 'none' );


code = [];
for ii = 0:3
    code = strvcat( code, sprintf('#define R%d   %+.8e', ii, r(4-ii)) );
end
disp( code );

if nargout > 0
    ee = e;
    if nargout > 1
	rr = r;
    end
end
