<?xml version="1.0"?>

<!--
	This is implementation of AP-28L1 autopilot
	installed on AN-24B
-->

<PropertyList>

<!-- ********************************************************
	 ************* TARGET AND ERRORS CALCULATION ************
	 ******************************************************** -->

	<!-- ***** ROLL ***** -->

	<filter> <!-- target bank calculation -->
		<type>gain</type>
		<gain>1</gain>
		<input>
			<condition>
				<property>/an24/AP-28l1/internal/horizon-mode</property>
			</condition>
			<value>0</value>
		</input>
		<input>/an24/AP-28l1/switches/roll-control</input>
		<output>/an24/AP-28l1/internal/target-bank</output>
	</filter>

	<filter> <!-- bank error -->
		<type>gain</type>
		<gain>1</gain>
		<enable>
			<condition>
				<property>/an24/AP-28l1/internal/powered</property>
			</condition>
		</enable>	
		<input>/an24/AP-28l1/internal/target-bank</input>
		<reference>/instrumentation/agd-r[1]/indicated-roll-deg</reference>
		<output>/an24/AP-28l1/internal/roll-error-deg</output>
	</filter>

	<filter> <!-- heading error in ZK mode -->
		<type>gain</type>
		<gain>1</gain>
		<enable>
			<condition>
				<property>/an24/AP-28l1/internal/powered</property>
			</condition>
		</enable>	
		<input>/instrumentation/zk2/heading-bug</input>
		<reference>/instrumentation/gpk-52/indicated-heading-deg</reference>
		<output>/an24/AP-28l1/coefficients/heading-error-deg</output>
		<period>
			<min>-180</min>
			<max>180</max>
		</period>
		<min>-10</min>
		<max>10</max>
	</filter>

	<filter> <!-- GPK-52 heading error -->
		<type>gain</type>
		<gain>-1</gain>
		<enable>
			<condition>
				<property>/an24/AP-28l1/internal/powered</property>
			</condition>
		</enable>	
		<input>/an24/AP-28l1/internal/target-heading-gpk-52</input>
		<reference>/instrumentation/gpk-52/indicated-heading-deg</reference>
		<output>/an24/AP-28l1/internal/heading-error-gpk-52</output>
		<period>
			<min>-180</min>
			<max>180</max>
		</period>
	</filter>

	<filter> <!-- GIK-1 heading error -->
		<type>gain</type>
		<gain>-1</gain>
		<enable>
			<condition>
				<property>/an24/AP-28l1/internal/powered</property>
			</condition>
		</enable>	
		<input>/an24/AP-28l1/internal/target-heading-gik-1</input>
		<reference>/instrumentation/gik-1/indicated-heading</reference>
		<output>/an24/AP-28l1/internal/heading-error-gik-1</output>
		<period>
			<min>-180</min>
			<max>180</max>
		</period>
	</filter>

	<!-- ***** PITCH ***** -->

	<filter> <!-- pitch error -->
		<type>gain</type>
		<gain>1</gain>
		<enable>
			<condition>
				<property>/an24/AP-28l1/internal/powered</property>
			</condition>
		</enable>	
		<input>/instrumentation/agd-r[1]/indicated-pitch-deg</input>
		<reference>/an24/AP-28l1/internal/target-pitch</reference>
		<output>/an24/AP-28l1/internal/pitch-error-deg</output>
	</filter>

	<filter> <!-- altitude error in mm of water -->
		<type>gain</type>
		<gain>345.324166512</gain>
		<enable>
			<condition>
				<property>/an24/AP-28l1/internal/powered</property>
			</condition>
		</enable>	
		<input>/systems/static/pressure-inhg</input>
		<reference>/an24/AP-28l1/internal/target-pressure-inhg</reference>
		<output>/an24/AP-28l1/internal/altitude-error-mm</output>
	</filter>

<!-- ********************************************************
	 ************* ROLL CONTROL *****************************
	 ******************************************************** -->

	<filter> <!-- aileron steering machine -->
		<type>gain</type>
		<gain>1</gain>
		<enable>
			<condition>
				<and>
					<property>/an24/AP-28l1/internal/powered</property>
					<property>/an24/AP-28l1/internal/engaged</property>
					<equals>
						<property>/an24/AP-28l1/internal/roll-mode</property>
						<value>0</value>
					</equals>
				</and>
			</condition>
		</enable>	
		<input>
			<expression>
				<product>
					<difference>
						<product>
							<property>/an24/AP-28l1/internal/roll-error-deg</property>
							<property>/an24/AP-28l1/coefficients/roll-angle</property>
						</product>
						<product>
							<property>/orientation/roll-rate-degps</property>
							<property>/an24/AP-28l1/coefficients/roll-rate</property>
						</product>
					</difference>
					<value>0.041666667</value>
				</product>
			</expression>
		</input>
		<output>/controls/flight/aileron</output>
	</filter>

	<filter> <!-- aileron steering machine heading error -->
		<type>gain</type>
		<gain>1</gain>
		<enable>
			<condition>
				<and>
					<property>/an24/AP-28l1/internal/powered</property>
					<property>/an24/AP-28l1/internal/engaged</property>
					<equals>
						<property>/an24/AP-28l1/internal/roll-mode</property>
						<value>1</value>
					</equals>
				</and>
			</condition>
		</enable>	
		<input>
			<expression>
				<product>
					<sum>
						<difference>
							<product>
								<property>/an24/AP-28l1/internal/roll-error-deg</property>
								<property>/an24/AP-28l1/coefficients/roll-angle</property>
							</product>
							<product>
								<property>/orientation/roll-rate-degps</property>
								<property>/an24/AP-28l1/coefficients/roll-rate</property>
							</product>
						</difference>
						<product>
							<property>/an24/AP-28l1/coefficients/heading-error-deg</property>
							<property>/an24/AP-28l1/coefficients/heading-error</property>
						</product>
					</sum>
					<value>0.041666667</value>
				</product>
			</expression>
		</input>
		<output>/controls/flight/aileron</output>
	</filter>

<!-- ********************************************************
	 ************* PITCH CONTROL ****************************
	 ******************************************************** -->

	 <filter> <!-- altitude correction module -->
		<type>gain</type>
		<gain>1</gain>
		<enable>
			<condition>
				<and>
					<property>/an24/AP-28l1/internal/powered</property>
					<property>/an24/AP-28l1/internal/engaged</property>
					<equals>
						<property>/an24/AP-28l1/switches/pitch</property>
						<value>1</value>
					</equals>
				</and>
			</condition>
		</enable>	
		<input>
			<condition>
				<property>/an24/AP-28l1/internal/kv</property>
			</condition>
			<expression>
				<product>
					<property>/an24/AP-28l1/internal/altitude-error-mm</property>
					<property>/an24/AP-28l1/coefficients/pitch-altitude</property>
					<value>-1</value>
				</product>
			</expression>
		</input>
		<input>
			<value>0</value>
		</input>
		<output>/an24/AP-28l1/internal/elevator-alt-correction</output>
	</filter>

	<filter> <!-- elevator steering machine -->
		<type>gain</type>
		<gain>1</gain>
		<enable>
			<condition>
				<and>
					<property>/an24/AP-28l1/internal/powered</property>
					<property>/an24/AP-28l1/internal/engaged</property>
					<equals>
						<property>/an24/AP-28l1/switches/pitch</property>
						<value>1</value>
					</equals>
				</and>
			</condition>
		</enable>	
		<input>
			<expression>
				<product>
					<sum>
						<product>
							<property>/an24/AP-28l1/internal/pitch-error-deg</property>
							<property>/an24/AP-28l1/coefficients/pitch-angle</property>
						</product>
						<product>
							<property>/orientation/pitch-rate-degps</property>
							<property>/an24/AP-28l1/coefficients/pitch-rate</property>
						</product>
						<product>
							<abs><property>/instrumentation/agd-r[1]/indicated-roll-deg</property></abs>
							<property>/an24/AP-28l1/coefficients/pitch-roll-compensate</property>
						</product>
						<property>/an24/AP-28l1/internal/elevator-alt-correction</property>
					</sum>
					<value>0.0333333</value>
				</product>
			</expression>
		</input>
		<output>/controls/flight/elevator</output>
	</filter>

	<!-- ***** AUTOTRIM ***** -->

	<logic>
		<enable>
			<condition>
				<and>
					<property>/an24/AP-28l1/internal/powered</property>
					<property>/an24/AP-28l1/internal/engaged</property>
					<property>/an24/AP-28l1/switches/autotrim</property>
					<equals>
						<property>/an24/AP-28l1/switches/pitch</property>
						<value>1</value>
					</equals>
				</and>
			</condition>
		</enable>
		<input>
			<or>
				<greater-than>
					<property>/controls/flight/elevator</property>
					<value>0.01</value>
				</greater-than>
				<less-than>
					<property>/controls/flight/elevator</property>
					<value>-0.01</value>
				</less-than>
			</or>
		</input>
		<output>/an24/AP-28l1/internal/elevator-cmd-gr02</output>
	</logic>

	<filter>
		<type>moving-average</type>
		<samples>
			<expression>
				<product>
					<value>3</value>
					<property>/sim/model-hz</property>
				</product>
			</expression>
		</samples>
		<input>/an24/AP-28l1/internal/elevator-cmd-gr02</input>
		<output>/an24/AP-28l1/internal/elevator-avg-3sec</output> <!-- average for 3 seconds -->
	</filter>

	<logic>
		<input>
			<greater-than>
				<property>/an24/AP-28l1/internal/elevator-avg-3sec</property>
				<value>0.99</value>
			</greater-than>
		</input>
		<output>/an24/AP-28l1/internal/elevator-autotrim-feel</output>
	</logic>

	<filter> <!-- autotrim control -->
		<type>gain</type>
		<gain>1</gain>
		<enable>
			<condition>
				<and>
					<property>/an24/AP-28l1/internal/powered</property>
					<property>/an24/AP-28l1/internal/engaged</property>
					<property>/an24/AP-28l1/switches/autotrim</property>
					<property>/an24/AP-28l1/internal/elevator-autotrim-feel</property>
					<equals>
						<property>/an24/AP-28l1/switches/pitch</property>
						<value>1</value>
					</equals>
				</and>
			</condition>
		</enable>
		<input>
			<condition>
				<greater-than>
					<property>/controls/flight/elevator</property>
					<value>0</value>
				</greater-than>
			</condition>
			<expression>
				<sum>
					<property>/controls/flight/elevator-trim</property>
					<product>
						<value>0.01</value>
						<property>/fdm/jsbsim/simulation/dt</property>
					</product>
				</sum>
			</expression>
		</input>
		<input>
			<expression>
				<sum>
					<property>/controls/flight/elevator-trim</property>
					<product>
						<value>-0.01</value>
						<property>/fdm/jsbsim/simulation/dt</property>
					</product>
				</sum>
			</expression>
		</input>
		<output>/controls/flight/elevator-trim</output>
	</filter>

<!-- ********************************************************
	 ************* YAW CONTROL ******************************
	 ******************************************************** -->

	<filter> <!-- yaw damper module -->
		<type>gain</type>
		<gain>-1</gain>
		<enable>
			<condition>
				<property>/an24/AP-28l1/internal/powered</property>
			</condition>
		</enable>	
		<input>
			<expression>
				<product>
					<property>/an24/AP-28l1/coefficients/yaw-rate</property>
					<div>
						<product>
							<property>/an24/AP-28l1/coefficients/yaw-rate-t-constant</property>
							<property>/an24/AP-28l1/internal/yaw-accel-der</property>
						</product>
						<sum>
							<product>
								<property>/an24/AP-28l1/coefficients/yaw-rate-t-constant</property>
								<property>/an24/AP-28l1/internal/yaw-accel-der</property>
							</product>
							<value>1</value>
						</sum>
					</div>
					<property>orientation/yaw-rate-degps</property>
				</product>
			</expression>
		</input>
		<output>/an24/AP-28l1/internal/rudder-yaw-damper</output>
		<min>-3</min>
		<max>3</max>
	</filter>

	<filter> <!-- GPK-52 module -->
		<type>gain</type>
		<gain>-1</gain>
		<enable>
			<condition>
				<property>/an24/AP-28l1/internal/powered</property>
			</condition>
		</enable>	
		<input>
			<condition>
				<equals>
					<property>/an24/AP-28l1/internal/yaw-mode</property>
					<value>1</value>
				</equals>
			</condition>
			<expression>
				<product>
					<property>/an24/AP-28l1/coefficients/yaw-angle</property>
					<property>/an24/AP-28l1/internal/heading-error-gpk-52</property>
				</product>
			</expression>
		</input>
		<input>0</input>
		<output>/an24/AP-28l1/internal/rudder-gpk-52</output>
	</filter>

	<filter> <!-- GIK-1 module -->
		<type>gain</type>
		<gain>-1</gain>
		<enable>
			<condition>
				<property>/an24/AP-28l1/internal/powered</property>
			</condition>
		</enable>	
		<input>
			<condition>
				<equals>
					<property>/an24/AP-28l1/internal/yaw-mode</property>
					<value>2</value>
				</equals>
			</condition>
			<expression>
				<product>
					<property>/an24/AP-28l1/coefficients/yaw-angle</property>
					<property>/an24/AP-28l1/internal/heading-error-gik-1</property>
				</product>
			</expression>
		</input>
		<input>0</input>
		<output>/an24/AP-28l1/internal/rudder-gik-1</output>
	</filter>

	<filter> <!-- GIK-1 integrator module -->
		<type>gain</type>
		<gain>1</gain>
		<enable>
			<condition>
				<and>
					<property>/an24/AP-28l1/internal/powered</property>
					<equals>
						<property>/an24/AP-28l1/internal/yaw-mode</property>
						<value>2</value>
					</equals>
					<or>
						<greater-than>
							<property>/an24/AP-28l1/internal/heading-error-gik-1</property>
							<value>0.7</value>
						</greater-than>
						<less-than>
							<property>/an24/AP-28l1/internal/heading-error-gik-1</property>
							<value>-0.7</value>
						</less-than>
					</or>
				</and>
			</condition>
		</enable>	
		<input>
			<expression>
				<difference>
					<property>/an24/AP-28l1/internal/rudder-gik-1-integrator</property>
					<product>
						<div>
							<abs><property>/an24/AP-28l1/internal/heading-error-gik-1</property></abs>
							<property>/an24/AP-28l1/internal/heading-error-gik-1</property>
						</div>
						<value>1</value>
						<property>/fdm/jsbsim/simulation/dt</property>
					</product>
				</difference>
			</expression>
		</input>
		<output>/an24/AP-28l1/internal/rudder-gik-1-integrator</output>
	</filter>

	 <filter> <!-- rudder steering machine -->
		<type>gain</type>
		<gain>1</gain>
		<enable>
			<condition>
				<and>
					<property>/an24/AP-28l1/internal/powered</property>
					<property>/an24/AP-28l1/internal/engaged</property>
				</and>
			</condition>
		</enable>	
		<input>
			<expression>
				<product>
					<sum>
						<property>/an24/AP-28l1/internal/rudder-yaw-damper</property>
						<property>/an24/AP-28l1/internal/rudder-gpk-52</property>
						<property>/an24/AP-28l1/internal/rudder-gik-1</property>
						<property>/an24/AP-28l1/internal/rudder-gik-1-integrator</property>
					</sum>
					<value>0.04</value>
				</product>
			</expression>
		</input>
		<output>/controls/flight/rudder</output>
	</filter>

<!-- ********************************************************
	 ************* LOGIC SECTION ****************************
	 ******************************************************** -->

	<logic> <!-- armed mode logic (lamp) -->
		<input>
			<and>
				<property>/an24/AP-28l1/internal/powered</property>
				<property>/an24/instrumentation/agd-r</property><!-- NOT "agd-r[1]"! -->
				<greater-than>
					<property>/sim/time/elapsed-sec</property>
					<property>/an24/AP-28l1/internal/power-on-time</property>
				</greater-than>
				<greater-than>
					<property>/controls/flight/rudder</property>
					<value>-0.1</value>
				</greater-than>
				<less-than>
					<property>/controls/flight/rudder</property>
					<value>0.1</value>
				</less-than>
				<not><property>/an24/AP-28l1/internal/engaged</property></not>
			</and>
		</input>
		<output>/an24/AP-28l1/internal/armed</output>
	</logic>

	<logic> <!-- KV mode logic (lamp) -->
		<input>
			<and>
				<property>/an24/AP-28l1/internal/powered</property>
				<property>/an24/AP-28l1/internal/engaged</property>
				<property>/an24/AP-28l1/internal/kv</property>
			</and>
		</input>
		<output>/an24/AP-28l1/lamps/kv</output>
	</logic>

	<filter> <!-- select roll mode to due switches and horizon mode -->
		<type>gain</type>
		<gain>1</gain>
		<input>
			<condition>
				<or>
					<equals>
						<property>/an24/AP-28l1/switches/roll-mode</property>
						<value>0</value>
					</equals>
					<equals>
						<property>/an24/AP-28l1/switches/roll-mode</property>
						<value>-1</value>
					</equals>
				</or>
			</condition>
			<value>0</value>
		</input>
		<input>
			<condition>
				<and>
					<equals>
						<property>/an24/AP-28l1/switches/roll-mode</property>
						<value>1</value>
					</equals>
					<not><property>/an24/AP-28l1/internal/horizon-mode</property></not>
				</and>
			</condition>
			<value>1</value>
		</input>
		<output>/an24/AP-28l1/internal/roll-mode</output>
	</filter>

	<filter> <!-- pitch control from control stand -->
		<type>gain</type>
		<gain>1</gain>
		<enable>
			<condition>
				<not><property>/an24/AP-28l1/internal/horizon-mode</property></not>
			</condition>
		</enable>
		<input>
			<condition>
				<not>
					<equals>
						<property>an24/AP-28l1/switches/up-down-r</property>
						<value>0</value>
					</equals>
				</not>
			</condition>
			<expression>
				<difference>
					<property>/an24/AP-28l1/internal/target-pitch</property>
					<product>
						<property>an24/AP-28l1/switches/up-down-r</property>
						<value>0.7</value>
						<property>/fdm/jsbsim/simulation/dt</property>
					</product>
				</difference>
			</expression>
		</input>
		<input>
			<expression>
				<difference>
					<property>/an24/AP-28l1/internal/target-pitch</property>
					<product>
						<property>an24/AP-28l1/switches/up-down-l</property>
						<value>0.7</value>
						<property>/fdm/jsbsim/simulation/dt</property>
					</product>
				</difference>
			</expression>
		</input>
		<output>/an24/AP-28l1/internal/target-pitch</output>
	</filter>

	<filter> <!-- adjust target pitch in KV mode to smooth transition into pitch control mode -->
		<type>gain</type>
		<gain>1</gain>
		<enable>
			<condition>
				<property>/an24/AP-28l1/internal/kv</property>
			</condition>
		</enable>
		<input>
			<expression>
				<sum>
					<property>/an24/AP-28l1/internal/target-pitch</property>
					<product>
						<property>/an24/AP-28l1/internal/pitch-error-deg</property>
						<value>0.1</value>
						<property>/fdm/jsbsim/simulation/dt</property>
					</product>
				</sum>
			</expression>
		</input>
		<output>/an24/AP-28l1/internal/target-pitch</output>
	</filter>

	<!-- calculate derivate for yaw rate -->
	<predict-simple>
		<seconds>1</seconds>
		<filter-gain>0</filter-gain>
		<input>orientation/yaw-rate-degps</input>
		<output>/an24/AP-28l1/internal/yaw-rate-1sec</output>
	</predict-simple>

	<filter>
		<type>gain</type>
		<input>
			<property>/an24/AP-28l1/internal/yaw-rate-1sec</property>
			<offset>
				<property>orientation/yaw-rate-degps</property>
				<scale>-1.0</scale>
			</offset>
		</input>
		<output>/an24/AP-28l1/internal/yaw-accel-degps2</output>
		<gain>1.0</gain>
	</filter>

	<filter>
		<type>gain</type>
		<gain>1.0</gain>
		<input>
			<condition>
				<equals>
					<property>/orientation/yaw-rate-degps</property>
					<value>0</value>
				</equals>
			</condition>
			<value>65000</value>
		</input>
		<input>
			<expression>
				<div>
					<property>/an24/AP-28l1/internal/yaw-accel-degps2</property>
					<property>/orientation/yaw-rate-degps</property>
				</div>
			</expression>
		</input>
		<output>/an24/AP-28l1/internal/yaw-accel-der</output>
	</filter>

	<filter> <!-- select yaw mode to due switches and horizon mode -->
		<type>gain</type>
		<gain>1</gain>
		<input>
			<condition>
				<or>
					<property>/an24/AP-28l1/internal/horizon-mode</property>
					<and>
						<not>
							<equals>
								<property>/an24/AP-28l1/switches/roll-control</property>
								<value>0</value>
							</equals>
						</not>
						<not>
							<equals>
								<property>/an24/AP-28l1/switches/roll-mode</property>
								<value>1</value>
							</equals>
						</not>
					</and>
					<equals>
						<property>/an24/AP-28l1/switches/roll-mode</property>
						<value>1</value>
					</equals>
				</or>
			</condition>
			<value>0</value>
		</input>
		<input>
			<condition>
				<and>
					<equals>
						<property>/an24/AP-28l1/switches/roll-mode</property>
						<value>-1</value>
					</equals>
					<equals>
						<property>/an24/AP-28l1/switches/roll-control</property>
						<value>0</value>
					</equals>
					<not><property>/an24/AP-28l1/internal/horizon-mode</property></not>
				</and>
			</condition>
			<value>1</value>
		</input>
		<input>
			<value>2</value>
		</input>
		<output>/an24/AP-28l1/internal/yaw-mode</output>
	</filter>

	<!-- logic for pull-push lamps -->
	<logic>
		<enable>
			<condition>
				<and>
					<property>/an24/AP-28l1/internal/powered</property>
					<property>/an24/AP-28l1/internal/engaged</property>
					<property>/an24/AP-28l1/switches/autotrim</property>
					<equals>
						<property>/an24/AP-28l1/switches/pitch</property>
						<value>1</value>
					</equals>
				</and>
			</condition>
		</enable>
		<input>
			<less-than>
				<property>/controls/flight/elevator</property>
				<value>-0.01</value>
			</less-than>
		</input>
		<output>/an24/AP-28l1/lamps/pull</output>
	</logic>

	<logic>
		<enable>
			<condition>
				<and>
					<property>/an24/AP-28l1/internal/powered</property>
					<property>/an24/AP-28l1/internal/engaged</property>
					<property>/an24/AP-28l1/switches/autotrim</property>
					<equals>
						<property>/an24/AP-28l1/switches/pitch</property>
						<value>1</value>
					</equals>
				</and>
			</condition>
		</enable>
		<input>
			<greater-than>
				<property>/controls/flight/elevator</property>
				<value>0.01</value>
			</greater-than>
		</input>
		<output>/an24/AP-28l1/lamps/push</output>
	</logic>

	<filter> <!-- integrator for GPK-52 -->
		<type>gain</type>
		<gain>1</gain>
		<enable>
			<condition>
				<property>/instrumentation/gpk-52/on</property>
			</condition>
		</enable>
		<input>
			<expression>
				<sum>
					<property>instrumentation/gpk-52/offset-deg</property>
					<product>
						<property>instrumentation/gpk-52/left-right-slew</property>
						<value>5</value>
						<property>/fdm/jsbsim/simulation/dt</property>
					</product>
				</sum>
			</expression>
		</input>
		<output>instrumentation/gpk-52/offset-deg</output>
	</filter>
	
</PropertyList>
