From d3a35b41c9b26015453e947b8034d534367b2f9f Mon Sep 17 00:00:00 2001
From: Joshua Davidson <joshuadavidson2000@gmail.com>
Date: Mon, 15 Oct 2018 16:29:00 -0400
Subject: [PATCH] Control: Rework trim system, fix bugs

---
 A320-main.xml                         |  1 -
 Models/FlightDeck/a320.flightdeck.xml | 22 ++++++--------
 Systems/a320-fcs.xml                  | 18 ++++++++++--
 Systems/libraries.xml                 | 42 ---------------------------
 revision.txt                          |  2 +-
 5 files changed, 25 insertions(+), 60 deletions(-)

diff --git a/A320-main.xml b/A320-main.xml
index aa156e93..5d988963 100644
--- a/A320-main.xml
+++ b/A320-main.xml
@@ -582,7 +582,6 @@
 			<aileron-drives-tiller type="bool">0</aileron-drives-tiller>
 			<aileron-input>0</aileron-input>
 			<elevator-input>0</elevator-input>
-			<elevator-trim-fdm>0.0</elevator-trim-fdm>
 			<flap-pos type="int">0</flap-pos>
 			<rudder-trim-fdm>0.0</rudder-trim-fdm>
 			<speedbrake-arm>0</speedbrake-arm>
diff --git a/Models/FlightDeck/a320.flightdeck.xml b/Models/FlightDeck/a320.flightdeck.xml
index ccfc6d0c..601abcca 100644
--- a/Models/FlightDeck/a320.flightdeck.xml
+++ b/Models/FlightDeck/a320.flightdeck.xml
@@ -4895,11 +4895,9 @@
 			<button>3</button>
 			<repeatable type="bool">true</repeatable>
 			<binding>
-				<command>nasal</command>
-				<script>
-					var trim = getprop("/controls/flight/elevator-trim");
-					setprop("/controls/flight/elevator-trim", trim - 0.01);
-				</script>
+				<command>property-adjust</command>
+				<property>/controls/flight/elevator-trim</property>
+				<step>0.01</step>
 			</binding>
 		</action>
 		<action>
@@ -4907,11 +4905,9 @@
 			<button>4</button>
 			<repeatable type="bool">true</repeatable>
 			<binding>
-				<command>nasal</command>
-				<script>
-					var trim = getprop("/controls/flight/elevator-trim");
-					setprop("/controls/flight/elevator-trim", trim + 0.01);
-				</script>
+				<command>property-adjust</command>
+				<property>/controls/flight/elevator-trim</property>
+				<step>-0.01</step>
 			</binding>
 		</action>
 	</animation>
@@ -4920,8 +4916,8 @@
 		<type>rotate</type>
 		<object-name>trim_2_lts</object-name>
 		<property>fdm/jsbsim/hydraulics/elevator-trim/cmd-deg</property>
-		<factor>-15.55</factor>
-		<offset-deg>-0.9</offset-deg>
+		<factor>15.55</factor>
+		<offset-deg>1.4</offset-deg>
 		<axis>
 			<object-name>trim.axis</object-name>
 		</axis>
@@ -4931,7 +4927,7 @@
 		<type>rotate</type>
 		<object-name>trim_1</object-name>
 		<property>controls/flight/elevator-trim</property>
-		<factor>-1000</factor>
+		<factor>1000</factor>
 		<axis>
 			<object-name>trim.axis</object-name>
 		</axis>
diff --git a/Systems/a320-fcs.xml b/Systems/a320-fcs.xml
index c8c14742..4ab41bdb 100644
--- a/Systems/a320-fcs.xml
+++ b/Systems/a320-fcs.xml
@@ -1482,9 +1482,21 @@
 			</range>
 		</aerosurface_scale>
 		
+		<switch name="hydraulics/elevator-trim/rate">
+			<default value="0.01"/>
+			<test value="0.02">
+				/systems/hydraulic/green-psi ge 1500
+			</test>
+		</switch>
+		
+		<actuator name="hydraulics/elevator-trim/actuator">
+			<input>/controls/flight/elevator-trim</input>
+			<rate_limit>hydraulics/elevator-trim/rate</rate_limit>
+		</actuator>
+		
 		<lag_filter name="hydraulics/elevator-trim/lag">
-			<input>/controls/flight/elevator-trim-fdm</input>
-			<c1>0.5</c1>
+			<input>hydraulics/elevator-trim/actuator</input>
+			<c1>10.5</c1>
 		</lag_filter>
 		
 		<aerosurface_scale name="hydraulics/elevator-trim/final-deg">
@@ -1638,7 +1650,7 @@
 		</aerosurface_scale>
 		
 		<aerosurface_scale name="hydraulics/rudder/trim-norm">
-			<input>/controls/flight/rudder-trim-fdm</input>
+			<input>/controls/flight/rudder-trim</input>
 			<range>
 				<min>-0.8</min>
 				<max>0.8</max>
diff --git a/Systems/libraries.xml b/Systems/libraries.xml
index 06e6f042..9b835098 100644
--- a/Systems/libraries.xml
+++ b/Systems/libraries.xml
@@ -681,48 +681,6 @@
 		<output>/controls/flight/auto-coordination</output>
 	</filter>
 	
-	<filter>
-		<debug>false</debug>
-		<type>noise-spike</type>
-		<enable>
-			<condition>
-				<greater-than-equals>
-					<property>/systems/hydraulic/green-psi</property>
-					<value>1500</value>
-				</greater-than-equals>
-			</condition>
-		</enable>
-		<input>/controls/flight/elevator-trim</input>
-		<output>/controls/flight/elevator-trim-fdm</output>
-		<max-rate-of-change>0.1</max-rate-of-change>
-	</filter>
-	
-	<filter>
-		<debug>false</debug>
-		<type>noise-spike</type>
-		<enable>
-			<condition>
-				<or>
-					<greater-than-equals>
-						<property>/systems/hydraulic/green-psi</property>
-						<value>1500</value>
-					</greater-than-equals>
-					<greater-than-equals>
-						<property>/systems/hydraulic/blue-psi</property>
-						<value>1500</value>
-					</greater-than-equals>
-					<greater-than-equals>
-						<property>/systems/hydraulic/yellow-psi</property>
-						<value>1500</value>
-					</greater-than-equals>
-				</or>
-			</condition>
-		</enable>
-		<input>/controls/flight/rudder-trim</input>
-		<output>/controls/flight/rudder-trim-fdm</output>
-		<max-rate-of-change>0.5</max-rate-of-change>
-	</filter>
-	
 	<!-- Braking -->
 	
 	<filter>
diff --git a/revision.txt b/revision.txt
index 707965dd..37fd5e10 100644
--- a/revision.txt
+++ b/revision.txt
@@ -1 +1 @@
-4673
\ No newline at end of file
+4674
\ No newline at end of file