%
%    HEADING - Comprehensive plots of yaw and yaw control
%
%    MIT SeaGrant
%
% Copyright (C) 1995-1997, Massachussetts Institute of Technology
% Sea Grant Underwater Vehicles Laboratory
% $Revision: 1.15 $, $Date: 2010/02/09 23:05:00 $

global titleStr;
global vehicleName

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

set( h_yaw, 'Name', [vehicleName,' Yaw Control'], 'DeleteFcn', 'clear h_yaw' );
set(gcf,'position',position)
if( exist('fontsize') == 0), fontsize = 14; end
if( fontsize ==12 ), linewidth = .5;end
fontweight = 'normal';
set(gca,'fontsize',fontsize,'fontweigh',fontweight)

time = dataDC(:,1);

if( exist('kxte') ) 
  nstrips=3;
else 
  nstrips =2;
end
stripno = 1;

if( nstrips == 3 )
  subplot( nstrips, 1, stripno )
  cla
%  stairs( time, dataDC(:,xTrackError) )
  hold on
  stairs( time, dataDC(:,kxte)*180/pi,'b' )
  stairs( time, dataDC(:,xteIntegral)*180/pi,'g' )
  stairs( time, dataDC(:,dPsi)*180/pi,'r' )
  stairs( time, dataDC(:,xTrackError),'m' )
  %set(gca,'ylim',[-12 12]);
  set(gca,'fontsize',fontsize,'fontweigh',fontweight)
  stripno=stripno+1;
  grid on
  ylabel('Heading Delta, Degrees')
  title([ 'Blue-Posn Error, Green-Integral, Red-Total Cmd, ', ...
	'Magenta-Posn Error, m; ', titleStr] );
  hold off
end

subplot( nstrips, 1, stripno )
cla

[xx,yy] = stairs( time, mod360(dataDC(:,cPsi)*180/pi) );

%ydat = 180/pi*dataNav(:,mPsi);
%ind = find( abs(diff(dataDC(:,mPsi))) > pi );
%if ~isempty(ind), ydat( ind ) = NaN; end

plotptr = plot( dataNav(:,1), mod360(dataNav(:,mPsi)*180/pi), xx, yy );
hold on

plot( dataDvl(:,1), mod360(dataDvl(:,dvlHeading) - 15.2), 'm' );
set(gca,'fontsize',fontsize,'fontweigh',fontweight)

if 0
  % From Jerome's lbl algorithm
if exist('heading_bias') == 1
  line(dataNav(:,1),...
       mod360((dataNav(:,heading_bias) + dataNav(:,mPsi))*180/pi),'color','c')
end
end

%lxmax   = get(plotptr(1), xmax);
%set(plotptr, 'xlim', [0 lxmax]);

%ydat = 180/pi*dataDC(:,tPsi);
%ind = find( abs(diff(data(:,tPsi))) > pi );
%if ~isempty(ind), ydat( ind ) = NaN; end

line( dataDC(:,1), mod360(dataDC(:,tPsi)*180/pi), 'Color', 'r' );
set(gca,'fontsize',fontsize,'fontweigh',fontweight)

title([ 'Heading: Commanded Bearing-Green; Smoothed Command-Red; Measured-Blue: ' ...
        titleStr] );
xlabel( 'Elapsed time (s)' );
ylabel( 'Heading (deg)' );
%legend( 'Meas.', 'Cmded', 'Smoothed',3);
zoom on
grid on
stripno=stripno+1;

subplot( nstrips, 1, stripno )
cla
stairs( dataDC(:,1), 180/pi*dataDC(:,psiProp), 'Color', 'r' );
hold on
stairs( dataDC(:,1), 180/pi*dataDC(:,psiInt),  'Color', [0 .75 .75] );
stairs( dataDC(:,1), 180/pi*dataDC(:,psiRate), 'Color', [0 .5 0] );
stairs( dataDC(:,1), 180/pi*dataDC(:,cDeltaR), 'Color', 'b' );
zoom on
grid on
set(gca,'fontsize',fontsize,'fontweigh',fontweight)
title( ['Rudder PID; Prop-Red; Deriv-Green; Rudder-Blue)'] );
%legend( 'Prop.', 'Int.', 'Deriv', 'Cmd. Rudder');
xlabel( 'Elapsed Time (s)' )
ylabel( 'Rudder Control Activity (deg)' )

linkaxes(get(gcf,'children'),'x')

