From 876d7caa830e39d83fc980ca6bbbc904c9689c10 Mon Sep 17 00:00:00 2001
From: Joshua Davidson <joshuadavidson2000@gmail.com>
Date: Wed, 27 Dec 2017 14:34:50 -0500
Subject: [PATCH] A32X: Improve lift table, and add Alpha Floor function

---
 Models/Instruments/PFD/PFD.nas | 49 +++++++++++++++++++++------------
 Nasal/FMGC.nas                 |  1 -
 Nasal/fadec-common.nas         | 50 ++++++++++++++++++++--------------
 Systems/a320-aerodynamics.xml  |  6 ++--
 4 files changed, 65 insertions(+), 41 deletions(-)

diff --git a/Models/Instruments/PFD/PFD.nas b/Models/Instruments/PFD/PFD.nas
index 02e80f32..e1fba257 100644
--- a/Models/Instruments/PFD/PFD.nas
+++ b/Models/Instruments/PFD/PFD.nas
@@ -209,7 +209,8 @@ var canvas_PFD_base = {
 		wow2 = getprop("/gear/gear[2]/wow");
 		
 		# FMA MAN TOGA MCT FLX THR
-		if (athr == 1 and (state1 == "TOGA" or state1 == "MCT" or state1 == "MAN THR" or state2 == "TOGA" or state2 == "MCT" or state2 == "MAN THR") and getprop("/systems/thrust/eng-out") != 1) {
+		if (athr == 1 and (state1 == "TOGA" or state1 == "MCT" or state1 == "MAN THR" or state2 == "TOGA" or state2 == "MCT" or state2 == "MAN THR") and getprop("/systems/thrust/eng-out") != 1 and getprop("/systems/thrust/alpha-floor") != 1 and 
+		getprop("/systems/thrust/toga-lk") != 1) {
 			me["FMA_man"].show();
 			me["FMA_manmode"].show();
 			if (state1 == "TOGA" or state2 == "TOGA") {
@@ -245,7 +246,7 @@ var canvas_PFD_base = {
 				me["FMA_man_box"].setColor(0.7333,0.3803,0);
 			}
 		} else if (athr == 1 and (state1 == "TOGA" or (state1 == "MCT" and getprop("/controls/engines/thrust-limit") == "FLX") or (state1 == "MAN THR" and thr1 >= 0.83) or state2 == "TOGA" or (state2 == "MCT" and 
-		getprop("/controls/engines/thrust-limit") == "FLX") or (state2 == "MAN THR" and thr2 >= 0.83)) and getprop("/systems/thrust/eng-out") == 1) {
+		getprop("/controls/engines/thrust-limit") == "FLX") or (state2 == "MAN THR" and thr2 >= 0.83)) and getprop("/systems/thrust/eng-out") == 1 and getprop("/systems/thrust/alpha-floor") != 1 and getprop("/systems/thrust/toga-lk") != 1) {
 			me["FMA_man"].show();
 			me["FMA_manmode"].show();
 			if (state1 == "TOGA" or state2 == "TOGA") {
@@ -293,27 +294,41 @@ var canvas_PFD_base = {
 		}
 	
 		# FMA A/THR
-		if (athr == 1 and getprop("/systems/thrust/eng-out") != 1 and (state1 == "MAN" or state1 == "CL") and (state2 == "MAN" or state2 == "CL")) {
-			me["FMA_thrust"].show();
-			if (getprop("/modes/pfd/fma/throttle-mode-box") == 1 and throttle_mode != " ") {
-				me["FMA_thrust_box"].show();
-			} else {
-				me["FMA_thrust_box"].hide();
-			}
-		} else if (athr == 1 and getprop("/systems/thrust/eng-out") == 1 and (state1 == "MAN" or state1 == "CL" or (state1 == "MAN THR" and thr1 < 0.83) or (state1 == "MCT" and getprop("/controls/engines/thrust-limit") != "FLX")) and 
-		(state2 == "MAN" or state2 == "CL" or (state2 == "MAN THR" and thr2 < 0.83) or (state2 == "MCT" and getprop("/controls/engines/thrust-limit") != "FLX"))) {
-			me["FMA_thrust"].show();
-			if (getprop("/modes/pfd/fma/throttle-mode-box") == 1 and throttle_mode != " ") {
-				me["FMA_thrust_box"].show();
+		if (getprop("/systems/thrust/alpha-floor") != 1 and getprop("/systems/thrust/toga-lk") != 1) {
+			if (athr == 1 and getprop("/systems/thrust/eng-out") != 1 and (state1 == "MAN" or state1 == "CL") and (state2 == "MAN" or state2 == "CL")) {
+				me["FMA_thrust"].show();
+				if (getprop("/modes/pfd/fma/throttle-mode-box") == 1 and throttle_mode != " ") {
+					me["FMA_thrust_box"].show();
+				} else {
+					me["FMA_thrust_box"].hide();
+				}
+			} else if (athr == 1 and getprop("/systems/thrust/eng-out") == 1 and (state1 == "MAN" or state1 == "CL" or (state1 == "MAN THR" and thr1 < 0.83) or (state1 == "MCT" and getprop("/controls/engines/thrust-limit") != "FLX")) and 
+			(state2 == "MAN" or state2 == "CL" or (state2 == "MAN THR" and thr2 < 0.83) or (state2 == "MCT" and getprop("/controls/engines/thrust-limit") != "FLX"))) {
+				me["FMA_thrust"].show();
+				if (getprop("/modes/pfd/fma/throttle-mode-box") == 1 and throttle_mode != " ") {
+					me["FMA_thrust_box"].show();
+				} else {
+					me["FMA_thrust_box"].hide();
+				}
 			} else {
+				me["FMA_thrust"].hide();
 				me["FMA_thrust_box"].hide();
 			}
 		} else {
-			me["FMA_thrust"].hide();
-			me["FMA_thrust_box"].hide();
+			me["FMA_thrust"].show();
+			me["FMA_thrust_box"].show();
 		}
 		
-		me["FMA_thrust"].setText(sprintf("%s", throttle_mode));
+		if (getprop("/systems/thrust/alpha-floor") == 1) {
+			me["FMA_thrust"].setText("A.FLOOR");
+			me["FMA_thrust_box"].setColor(0.7333,0.3803,0);
+		} else if (getprop("/systems/thrust/toga-lk") == 1) {
+			me["FMA_thrust"].setText("TOGA LK");
+			me["FMA_thrust_box"].setColor(0.7333,0.3803,0);
+		} else {
+			me["FMA_thrust"].setText(sprintf("%s", throttle_mode));
+			me["FMA_thrust_box"].setColor(0.8078,0.8039,0.8078);
+		}
 		
 		# FMA Pitch Roll Common
 		me["FMA_combined"].setText(sprintf("%s", pitch_mode));
diff --git a/Nasal/FMGC.nas b/Nasal/FMGC.nas
index 04e7509a..b6e93d57 100644
--- a/Nasal/FMGC.nas
+++ b/Nasal/FMGC.nas
@@ -11,7 +11,6 @@
 
 setprop("/FMGC/internal/maxspeed", 0);
 setprop("/FMGC/internal/minspeed", 0);
-setprop("/FMGC/internal/alpha-prot-speed", 0);
 setprop("/position/gear-agl-ft", 0);
 setprop("/FMGC/internal/mng-spd", 157);
 setprop("/FMGC/internal/mng-spd-cmd", 157);
diff --git a/Nasal/fadec-common.nas b/Nasal/fadec-common.nas
index 609ec6ee..179a28ab 100644
--- a/Nasal/fadec-common.nas
+++ b/Nasal/fadec-common.nas
@@ -45,9 +45,10 @@ setlistener("/sim/signals/fdm-initialized", func {
 	var n1mct = getprop("/systems/thrust/n1/mct-lim");
 	var n1flx = getprop("/systems/thrust/n1/flx-lim");
 	var n1clb = getprop("/systems/thrust/n1/clb-lim");
-	var ias = getprop("/velocities/airspeed-kt");
+	var alpha = getprop("/fdm/jsbsim/aero/alpha-deg");
 	var flaps = getprop("/controls/flight/flap-pos");
-	var alphaProtSpd = getprop("/FMGC/internal/alpha-prot-speed");
+	var alphaProt = 0;
+	var togaLock = 0;
 	var gs = getprop("/velocities/groundspeed-kt");
 	thrust_lim.start();
 	thrustt.start();
@@ -296,24 +297,33 @@ var thrust_loop = func {
 		}
 	}
 	
-#	ias = getprop("/instrumentation/airspeed-indicator/indicated-speed-kt");
-#	flaps = getprop("/controls/flight/flap-pos");
-#	alphaProtSpd = getprop("/FMGC/internal/alpha-prot-speed");
-#	togaLockSpd = alphaProtSpd + 3;
-#	if (getprop("/gear/gear[1]/wow") == 0 and getprop("/gear/gear[2]/wow") == 0 and getprop("/it-fbw/law") == 0) {
-#		if (ias < alphaProtSpd) {
-#			setprop("/systems/thrust/alpha-floor", 1);
-#			setprop("/systems/thrust/toga-lk", 0);
-#			setprop("/it-autoflight/input/athr", 1);
-#		} else if (getprop("/systems/thrust/alpha-floor") == 1 and ias > togaLockSpd) {
-#			setprop("/systems/thrust/alpha-floor", 0);
-#			setprop("/it-autoflight/input/athr", 1);
-#			setprop("/systems/thrust/toga-lk", 1);
-#		}
-#	} else {
-#		setprop("/systems/thrust/alpha-floor", 0);
-#		setprop("/systems/thrust/toga-lk", 0);
-#	}
+	alpha = getprop("/fdm/jsbsim/aero/alpha-deg");
+	flaps = getprop("/controls/flight/flap-pos");
+	if (flaps == 0) {
+		alphaProt = 9.5;
+	} else if (flaps == 1 or flaps == 2 or flaps == 3) {
+		alphaProt = 15.0;
+	} else if (flaps == 4) {
+		alphaProt = 14.0;
+	} else if (flaps == 5) {
+		alphaProt = 13.0;
+	}
+	togaLock = alphaProt - 1;
+	if (getprop("/gear/gear[1]/wow") == 0 and getprop("/gear/gear[2]/wow") == 0 and getprop("/it-fbw/law") == 0 and (getprop("/systems/thrust/eng-out") == 0 or (getprop("/systems/thrust/eng-out") == 1 and flaps == 0)) and getprop("/systems/fadec/n1mode1") == 0 
+	and getprop("/systems/fadec/n1mode2") == 0) {
+		if (alpha > alphaProt and getprop("/position/gear-agl-ft") >= 100) {
+			setprop("/systems/thrust/alpha-floor", 1);
+			setprop("/systems/thrust/toga-lk", 0);
+			setprop("/it-autoflight/input/athr", 1);
+		} else if (getprop("/systems/thrust/alpha-floor") == 1 and alpha < togaLock) {
+			setprop("/systems/thrust/alpha-floor", 0);
+			setprop("/it-autoflight/input/athr", 1);
+			setprop("/systems/thrust/toga-lk", 1);
+		}
+	} else {
+		setprop("/systems/thrust/alpha-floor", 0);
+		setprop("/systems/thrust/toga-lk", 0);
+	}
 }
 
 # Timers
diff --git a/Systems/a320-aerodynamics.xml b/Systems/a320-aerodynamics.xml
index 798233ed..ba86df31 100644
--- a/Systems/a320-aerodynamics.xml
+++ b/Systems/a320-aerodynamics.xml
@@ -28,9 +28,9 @@
 						-0.64 -1.0312
 						-0.25 -0.9379
 						 0.00  0.1494
-						 0.20  1.6072
-						 0.60  1.4305
-						 0.88  0.8415
+						 0.25  1.6072
+						 0.60  1.0305
+						 0.88  0.6415
 						 1.05  0.2294
 						 1.22  0.0544
 						 1.57  0.0000