%    O2_SPEED - script to plot forward speed parameters of AUV
%
%    B. A. Moran

%    Copyright (C) 1997-1998
%    Massachussetts Institute of Technology
%    Sea Grant AUV Laboratory
%    $Id: o2_speed.m,v 1.1 2000/01/10 19:08:20 rob Exp $

if exist( 'm_thruster_current' ) ~= 1 | exist( 'm_propeller_speed' ) ~= 1
    warning( 'Required variables not parsed' )
    return
end

if exist( 'h_spd' ) ~= 1
    h_spd = figure;
else
    h_spd = figure(h_spd);
end

set( h_spd, 'Name', 'Speed Plots', 'DeleteFcn', 'clear h_spd' );

spdplot = (exist( 'm_propeller_speed' ) == 1) | ...
    (exist( 'm_thruster_rot' ) == 1);

if exist('m_thruster_current') == 1 & exist('c_speed') == 1
    
    if spdplot
	subplot( 2, 1, 1 )
    end
    cla
    o2_thamp( time, data(:,c_speed), data(:,m_thruster_current) );

    if spdplot
	subplot( 2, 1, 2 )
    end
end

if exist( 'm_propeller_speed' ) == 1

    cla
    o2_thspd( time, data(:,m_propeller_speed) );

elseif exist( 'm_thruster_rot' ) == 1

    special = [848444807 848449068 853949532 853951566 853952146 ...
	       853952757 853952920 853958621 853963328 853965002 853965119 ...
	       853966364 853967280 854038306 854048996 854049836 854051471 ...
	       854052026 854054062];

    if any( start == special )
	disp( ['Using special case: ' num2str(start)] );
	m_rot = hex2dec('ffff') - round(2e6*pi./data(:,m_thruster_rot));
	ind = find(m_rot == 0);
	m_rot(ind) = Inf*ones(size(ind));
	clear ind
    else
	m_rot = data(:,m_thruster_rot);
	if any( m_rot >= hex2dec('8000') & finite(m_rot) )
	    disp( 'Using pre 11/96 thruster rotation' );
	    bad = find( m_rot >= hex2dec('8000') );
	    m_rot(bad) = hex2dec('10000') - m_rot(bad);
	end
    end
    prop = (60./(16e-6*m_rot))/5.54;
    o2_thspd( time, prop );
end

clear spdplot
