exp='Exp181';

save([exp,'_segundafaseROLL'], 'roll_ref', 'roll', 'ailerons', 'roll_rate')
save([exp,'_inicioYAW'], 'yaw_ref', 'yaw', 'rudder', 'yaw_rate')
save([exp,'_Guidance'],'XTRK','TRK','DTRK','TRKERR_captura','dist_wp1','TRKERR')
save([exp,'_Vcontrol'],'throttle','Energy','ALT_err','Vel_err','Pitch','ailerons','Dpitch')



close all
whitebg([0 0.2 0.3])

plot(roll_ref,'c');hold on
plot(roll)
legend('Roll Requerido','Roll de la nave','Location','Best');
title('Evolución del Roll de la aeronave','FontWeight','bold','FontSize',12)
xlabel('Tiempo (s)','FontWeight','bold');
ylabel('Roll','FontWeight','bold');
grid on

figure
plot(ailerons)
title('Deflexión de los alerones en tanto por 1','FontWeight','bold','FontSize',12);
xlabel('Tiempo (s)','FontWeight','bold');
ylabel('Deflexión','FontWeight','bold');
grid on

figure
plot(roll_rate);hold on
plot([0 roll_rate.time(end)],[0.60 0.60]  ,'-','Color',[1 0.1 0.05],'Linewidth',2);hold on
plot([0 roll_rate.time(end)],[-0.60 -0.60]  ,'-','Color',[1 0.1 0.05],'Linewidth',2);hold on
title('Ratio de rotación (rad/s)','FontWeight','bold','FontSize',12);
legend('Ratio de rotación','Límite de Diseño','Location','Best');

xlabel('Tiempo (s)','FontWeight','bold');
ylabel('Deflexión','FontWeight','bold');
axis([0 roll_rate.time(end) min(roll_rate.data)-0.3 max(roll_rate.data)+0.3])
grid on

figure
plot(yaw_ref,'c');hold on
plot(yaw);hold on
plot([0 yaw_rate.time(end)],[0.25 0.25],'r-');hold on
plot([0 yaw_rate.time(end)],[-0.25 -0.25],'r-');hold on
legend('Betta','Betta de la nave','Límite de diseño','Location','Best');
title('Evolución del Yaw de la aeronave','FontWeight','bold','FontSize',12)
xlabel('Tiempo (s)','FontWeight','bold');
ylabel('Yaw','FontWeight','bold');
grid on

figure
plot(rudder)
title('Deflexión del timón en tanto por 1','FontWeight','bold','FontSize',12);
xlabel('Tiempo (s)','FontWeight','bold');
ylabel('Deflexión','FontWeight','bold');
grid on

figure
plot(yaw_rate);hold on;
title('Ratio de rotación para el yaw (rad/s)','FontWeight','bold','FontSize',12);
xlabel('Tiempo (s)','FontWeight','bold');
ylabel('Deflexión','FontWeight','bold');
grid on


figure
plot(XTRK,'y');hold on
plot([0 XTRK.time(end)],[0 0],'r-');hold on
legend('XTRK','Límite de diseño','Location','Best');
title('XTRK','FontWeight','bold','FontSize',12)
xlabel('Tiempo (s)','FontWeight','bold');
ylabel('XTRK','FontWeight','bold');
grid on


figure
plot(TRK,'y');hold on
plot(DTRK,'r-');hold on
legend('TRACK','DTRK','Location','Best');
title('TRKERR','FontWeight','bold','FontSize',12)
xlabel('Tiempo (s)','FontWeight','bold');
ylabel('TRK','FontWeight','bold');
grid on

figure
plot(TRK,'y');hold on
legend('TRACK','Location','Best');
title('TRACK','FontWeight','bold','FontSize',12)
xlabel('Tiempo (s)','FontWeight','bold');
ylabel('TRK','FontWeight','bold');
grid on

figure
plot(TRKERR_captura,'y');hold on
legend('TRKERR','Location','Best');
title('TRKERR en la ley de Captura','FontWeight','bold','FontSize',12)
xlabel('Tiempo (s)','FontWeight','bold');
ylabel('TRK','FontWeight','bold');
grid on

figure
plot(TRKERR,'y');hold on
legend('TRKERR','Location','Best');
title('TRKERR','FontWeight','bold','FontSize',12)
xlabel('Tiempo (s)','FontWeight','bold');
ylabel('TRK','FontWeight','bold');
grid on

figure
plot(dist_wp1.data,TRKERR_captura.data);hold on
plot(6*ones(1,length(dist_wp1.data)),TRKERR_captura.data,'r-');hold on;
plot(3*ones(1,length(dist_wp1.data)),TRKERR_captura.data,'r-');hold on;
plot(dist_wp1.data,2*ones(1,length(dist_wp1.data)),'r-');hold on;
plot(dist_wp1.data,-2*ones(1,length(dist_wp1.data)),'r-');hold on;
legend('TRKERR','Limite de diseño','Location','Best');
title('TRKERR según distancia','FontWeight','bold','FontSize',12)
xlabel('Distancia al IAF','FontWeight','bold');
ylabel('TRKERR','FontWeight','bold');
grid on

figure
plot(Energy,'y');hold on
legend('Energy','Location','Best');
title('Energy Error','FontWeight','bold','FontSize',12)
xlabel('Tiempo (s)','FontWeight','bold');
ylabel('Energy','FontWeight','bold');
grid on

figure
plot(ALT_err,'y');hold on
legend('Error de Altitud','Location','Best');
title('Error de Altitud','FontWeight','bold','FontSize',12)
xlabel('Tiempo (s)','FontWeight','bold');
ylabel('Error de Altitud','FontWeight','bold');
grid on

figure
plot(Vel_err,'y');hold on
legend('Error de Velocidad','Location','Best');
title('Error de Velocidad','FontWeight','bold','FontSize',12)
xlabel('Tiempo (s)','FontWeight','bold');
ylabel('Error de Velocidad','FontWeight','bold');
grid on

figure
plot(throttle,'y');hold on
legend('Acelerador tanto por 1','Location','Best');
title('Acelerador','FontWeight','bold','FontSize',12)
xlabel('Tiempo (s)','FontWeight','bold');
ylabel('Error de Velocidad','FontWeight','bold');
grid on

figure
plot(Pitch,'y');hold on;
plot(Dpitch,'c');hold on;
plot(Dpitch.time,Dpitch.data+0.05,'r');hold on
plot(Dpitch.time,Dpitch.data-0.05,'r');hold on
legend('Pitch','Pitch requerido','Limite de diseño','Location','Best');
title('Pitch','FontWeight','bold','FontSize',12)
xlabel('Tiempo (s)','FontWeight','bold');
ylabel('Error de Velocidad','FontWeight','bold');
grid on

figure
plot(elevators,'y');hold on
legend('elevadores','Location','Best');
title('Deflexión de los elevadores en tanto por 1','FontWeight','bold','FontSize',12)
xlabel('Tiempo (s)','FontWeight','bold');
ylabel('Error de Velocidad','FontWeight','bold');
grid on