From 48a5a19b975c24c2db39805dc9d6bd8fff9a331d Mon Sep 17 00:00:00 2001
From: Joshua Davidson <joshuadavidson2000@gmail.com>
Date: Fri, 9 Mar 2018 11:10:13 -0500
Subject: [PATCH] A3XX: Improve Autobrake system

---
 A320-main.xml          |  5 +++++
 Nasal/brakes.nas       | 11 +++++------
 Systems/a320-fcs.xml   | 20 ++++++++++++++++++++
 Systems/hydraulics.xml |  4 ++--
 revision.txt           |  2 +-
 5 files changed, 33 insertions(+), 9 deletions(-)

diff --git a/A320-main.xml b/A320-main.xml
index 68eaf22f..8b3c955c 100644
--- a/A320-main.xml
+++ b/A320-main.xml
@@ -494,6 +494,11 @@
 			<start type="bool">0</start>
 			<bleed type="bool">0</bleed>
 		</APU>
+		<autobrake>
+			<active type="bool">0</active>
+			<brake-left type="double">0</brake-left>
+			<brake-right type="double">0</brake-right>
+		</autobrake>
 		<electric>
 			<avionics-switch type="bool">true</avionics-switch>
 		</electric>
diff --git a/Nasal/brakes.nas b/Nasal/brakes.nas
index c9ffc22a..a9f320df 100644
--- a/Nasal/brakes.nas
+++ b/Nasal/brakes.nas
@@ -21,13 +21,9 @@ var autobrake_init = func {
 	setprop("/controls/autobrake/mode", 0);
 }
 
-# Override FG's generic brake, so we can use toe brakes to disconnect autobrake
+# Override FG's generic brake
 controls.applyBrakes = func(v, which = 0) {
 	if (getprop("/systems/acconfig/autoconfig-running") != 1) {
-		wow0 = getprop("/gear/gear[0]/wow");
-		if (getprop("/controls/autobrake/mode") != 0 and wow0 == 1 and getprop("/controls/autobrake/active") == 1) {
-			arm_autobrake(0);
-		}
 		if (which <= 0) {
 			interpolate("/controls/gear/brake-left", v, 0.5);
 		}
@@ -64,7 +60,7 @@ var arm_autobrake = func(mode) {
 	}
 }
 
-# Autobrake enable if armed
+# Autobrake loop
 var absChk = maketimer(0.2, func {
 	thr1 = getprop("/controls/engines/engine[0]/throttle");
 	thr2 = getprop("/controls/engines/engine[1]/throttle");
@@ -82,4 +78,7 @@ var absChk = maketimer(0.2, func {
 	if (getprop("/controls/autobrake/mode") == 3 and getprop("/controls/gear/gear-down") == 0) {
 		arm_autobrake(0);
 	}
+	if (getprop("/controls/autobrake/mode") != 0 and wow0 == 1 and getprop("/controls/autobrake/active") == 1 and (getprop("/controls/gear/brake-left") > 0.05 or getprop("/controls/gear/brake-right") > 0.05)) {
+		arm_autobrake(0);
+	}
 });
diff --git a/Systems/a320-fcs.xml b/Systems/a320-fcs.xml
index 71bb48a6..e312e00b 100644
--- a/Systems/a320-fcs.xml
+++ b/Systems/a320-fcs.xml
@@ -824,5 +824,25 @@
 		</actuator>
 	
 	</channel>
+	
+	<channel name="Brakes">
+		
+		<switch name="rubbish/left-brake-cmd-norm">
+			<default value="/controls/gear/brake-left"/>
+			<test value="/controls/autobrake/brake-left">
+				/controls/autobrake/active EQ 1
+			</test>
+			<output>fcs/left-brake-cmd-norm</output>
+		</switch>
+		
+		<switch name="rubbish/right-brake-cmd-norm">
+			<default value="/controls/gear/brake-right"/>
+			<test value="/controls/autobrake/brake-right">
+				/controls/autobrake/active EQ 1
+			</test>
+			<output>fcs/right-brake-cmd-norm</output>
+		</switch>
+	
+	</channel>
 
 </system>
diff --git a/Systems/hydraulics.xml b/Systems/hydraulics.xml
index db526172..e1a32059 100644
--- a/Systems/hydraulics.xml
+++ b/Systems/hydraulics.xml
@@ -1573,8 +1573,8 @@
 			<property>/controls/autobrake/decel-rate</property>
 		</reference>
 		<output>
-			<property>/controls/gear/brake-left</property>
-			<property>/controls/gear/brake-right</property>
+			<property>/controls/autobrake/brake-left</property>
+			<property>/controls/autobrake/brake-right</property>
 		</output>
 		<config>
 			<Kp>0.45</Kp>
diff --git a/revision.txt b/revision.txt
index a84f1c38..afd4e579 100644
--- a/revision.txt
+++ b/revision.txt
@@ -1 +1 @@
-4315
\ No newline at end of file
+4316
\ No newline at end of file