%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% PURPOSE: Look at Duty Cycle for Rudder and Elevator
% AUTHOR:  Nicole Tervalon
% DATE:    00/11/15
% NOTES:
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

if exist('metrabyte') ~= 1, return, end
  str = [' ',titleStr,' Plotted on ',myDate];
  PaperPosn = [.5 .5 3.75 2.5];
  FigurePosn = [5 30 815 900];



travrate = 30/2;  % elevator and rudder approximate travel rate (deg/sec)
oh = 0.00;    % overhead in actuator motion time (sec)
totdutyR = 0;
totdutyE = 0;
eltime = 0;

for i=1:(length(dataDC(:,1))-1),
   delt = dataDC(i+1,1)-dataDC(i,1);
   eltime = eltime + delt;
   delR = abs(dataDC(i+1,cDeltaR)-dataDC(i,cDeltaR))*r2d;
   delE = abs(dataDC(i+1,cDeltaE)-dataDC(i,cDeltaE))*r2d;
   dutyR(i) = (delR/travrate+oh)/delt;  % dutycycle (not %)
   dutyE(i) = (delE/travrate+oh)/delt;  % dutycycle (not %)
end
dutyR(i+1) = 0;
dutyE(i+1) = 0;

   avgdutyR = sum(dutyR)*delt/eltime*100;
   avgdutyE = sum(dutyE)*delt/eltime*100;


  if exist( 'dutyFig' ) ~= 1
    dutyFig = figure;
    winpos( position(1,:) );
  else
    figure(dutyFig);
  end
  set(dutyFig,'Name','Duty Cycles for Actuators','DeleteFcn','clear dutyFig');

  subplot(4,1,1)
  cla
  plot(dataDC(:,1), dutyR*100);
  axis([dataDC(1,1) dataDC(length(dataDC(:,1))) 0 100]);
  set(gca,'ytick',[0 10 20 30 40 50 60 70 80 90 100]);
  ylabel('Percentage')
  title(['Rudder Duty Cycle ',str]);
  grid on;
  zoom on;

  subplot(4,1,2)
  cla
  plot(dataDC(:,1), dutyE*100);
  axis([dataDC(1,1) dataDC(length(dataDC(:,1))) 0 100]);
  set(gca,'ytick',[0 10 20 30 40 50 60 70 80 90 100]);
  ylabel('Percentage');
%  xlabel('Elapsed Time (s)');
  title(['Elevator Duty Cycle ',str]);
  grid on;
  zoom on;

  intRangle=interp1(dataTC(:,1),dataTC(:,rudderAngle),dataDC(:,1),'spline');
  intEangle=interp1(dataTC(:,1),dataTC(:,elevatorAngle),dataDC(:,1),'spline');


  subplot(4,1,3)
  plot(dataDC(:,1), (intRangle-dataDC(:,cDeltaR))*r2d,'r', ...
     dataDC(:,1), (-intEangle-dataDC(:,cDeltaE))*r2d,'b');
%  line(dataDC(:,1), dataDC(:,cDeltaR)*r2d,'Color','r');
  ylabel('Degrees')
  title(['Angle Differences (meas-cmd)',str]);
  %  xlabel('Elapsed Time (s)');
  legend('rudder','elevator')
grid on;
zoom on;

  subplot(4,1,4)
  plot(dataDC(:,1), dataDC(:,cDeltaR)*r2d,'r', ...
     dataDC(:,1), dataDC(:,cDeltaE)*r2d,'b');
%  line(dataDC(:,1), dataDC(:,cDeltaE)*r2d, 'Color','r');
ylabel('Degrees')
xlabel('Elapsed Time (s)')
  title(['Commanded Angles',str]);
  legend('rudder','elevator')

%  xlabel('Elapsed Time (s)');
  grid on;
  zoom on;
%  set( gca, 'ydir', 'reverse' );
