<?xml version="1.0" encoding="iso-8859-1"?>

<!-- Mirage 2000 FBW Autopilot -->
<!-- Copyright (c) 2020 Josh Davidson (OCtal450) -->

<PropertyList>
	
	<!-- From old Autopilot to make nasal work -->
	<filter>
		<name>NAV Heading Deflection</name>
		<debug>false</debug>
		<type>noise-spike</type>
		<input>
			<condition>
				<equals>
					<property>autopilot/settings/nav-source</property>
					<value>NAV1</value>
				</equals>
			</condition>
			<property>instrumentation/nav[0]/heading-needle-deflection</property>
			<scale>3.0</scale>
		</input>
		<input>
			<condition>
				<equals>
					<property>autopilot/settings/nav-source</property>
					<value>NAV2</value>
				</equals>
			</condition>
			<property>instrumentation/nav[1]/heading-needle-deflection</property>
			<scale>3.0</scale>
		</input>
		<input>
			<condition>
				<equals>
					<property>autopilot/settings/nav-source</property>
					<value>FMS</value>
				</equals>
			</condition>
			<property>instrumentation/gps/wp/wp[1]/course-error-nm</property>
			<scale>9.0</scale>
		</input>
		<output>/autopilot/internal/cdi</output>
		<max-rate-of-change>20</max-rate-of-change>
	</filter>
	
	<!-- Roll Axis -->
	<filter>
		<name>Max Roll</name>
		<debug>false</debug>
		<type>gain</type>
		<gain>1.0</gain>
		<input>
			<expression>
				<table>
					<property>/velocities/airspeed-kt</property>
					<entry><ind>130</ind><dep>30</dep></entry>
					<entry><ind>600</ind><dep>60</dep></entry>
				</table>
			</expression>
		</input>
		<output>/fdm/jsbsim/fbw/ap/max-roll</output>
	</filter>
	
	<filter>
		<name>Heading Error Deg</name>
		<debug>false</debug>
		<type>gain</type>
		<gain>1.0</gain>
		<input>
			<condition>
				<and>
					<equals>
						<property>/autopilot/locks/heading</property>
						<value>HDG</value>
					</equals>
					<not-equals>
						<property>/instrumentation/efis/mfd/true-north</property>
						<value>1</value>
					</not-equals>
				</and>
			</condition>
			<property>/autopilot/settings/heading-bug-deg</property>
			<offset>
				<property>/orientation/heading-magnetic-deg</property>
				<scale>-1.0</scale>
			</offset>
		</input>
		<input>
			<condition>
				<and>
					<equals>
						<property>/autopilot/locks/heading</property>
						<value>HDG</value>
					</equals>
					<equals>
						<property>/instrumentation/efis/mfd/true-north</property>
						<value>1</value>
					</equals>
				</and>
			</condition>
			<property>/autopilot/settings/heading-bug-deg</property>
			<offset>
				<property>/orientation/heading-deg</property>
				<scale>-1.0</scale>
			</offset>
		</input>
		<input>
			<condition>
				<and>
					<equals>
						<property>/autopilot/locks/heading</property>
						<value>LNAV</value>
					</equals>
					<equals>
						<property>/autopilot/settings/nav-source</property>
						<value>TACAN</value>
					</equals>
				</and>
			</condition>
			<property>/instrumentation/tacan/indicated-bearing-true-deg</property>
			<offset>
				<property>/orientation/heading-deg</property>
				<scale>-1.0</scale>
			</offset>
		</input>
		<input>
			<condition>
				<and>
					<equals>
						<property>/autopilot/locks/heading</property>
						<value>LNAV</value>
					</equals>
					<equals>
						<property>/autopilot/settings/nav-source</property>
						<value>FMS</value>
					</equals>
					<equals>
						<property>/autopilot/route-manager/active</property>
						<value>1</value>
					</equals>
				</and>
			</condition>
			<expression>
				<sum>
					<property>/autopilot/route-manager/wp[0]/true-bearing-deg</property>
					<difference>
						<property>/orientation/track-magnetic-deg</property>
						<property>/orientation/heading-magnetic-deg</property>
					</difference>
					<product>
						<property>/orientation/heading-deg</property>
						<value>-1</value>
					</product>
				</sum>
			</expression>
		</input>
		<input>
			<condition>
				<and>
					<equals>
						<property>/autopilot/locks/heading</property>
						<value>LNAV</value>
					</equals>
					<equals>
						<property>/autopilot/settings/nav-source</property>
						<value>FMS</value>
					</equals>
					<not-equals>
						<property>/autopilot/route-manager/active</property>
						<value>0</value>
					</not-equals>
				</and>
			</condition>
			<value>0</value>
		</input>
		<input>0</input>
		<output>/fdm/jsbsim/fbw/ap/heading-error-deg</output>
		<period>
			<min>-180</min>
			<max>180</max>
		</period>
	</filter>
	
	<pi-simple-controller>
		<name>IT-CONTROLLER: HDG/LNAV</name>
		<debug>false</debug>
		<enable>
			<condition>
				<or>
					<equals>
						<property>/autopilot/locks/heading</property>
						<value>HDG</value>
					</equals>
					<equals>
						<property>/autopilot/locks/heading</property>
						<value>LNAV</value>
					</equals>
				</or>
			</condition>
		</enable>
		<input>/fdm/jsbsim/fbw/ap/heading-error-deg</input>
		<reference>0.0</reference>
		<output>/autopilot/settings/target-roll-deg</output>
		<config>
			<Kp>
				<expression>
					<table>
						<property>/velocities/airspeed-kt</property>
						<entry><ind>130</ind><dep>-1.8</dep></entry>
						<entry><ind>600</ind><dep>-3.7</dep></entry>
						<entry><ind>950</ind><dep>-4.1</dep></entry>
					</table>
				</expression>
			</Kp>
			<Ki>
				<expression>
					<table>
						<property>/velocities/airspeed-kt</property>
						<entry><ind>260</ind><dep>-0.005</dep></entry>
						<entry><ind>850</ind><dep>-0.010</dep></entry>
					</table>
				</expression>
			</Ki>
			<min>
				<property>/fdm/jsbsim/fbw/ap/max-roll</property>
				<scale>-1</scale>
			</min>
			<max>/fdm/jsbsim/fbw/ap/max-roll</max>
		</config>
	</pi-simple-controller>
	
	<filter>
		<name>NAV Error Deg</name>
		<debug>false</debug>
		<type>gain</type>
		<gain>1.0</gain>
		<input>
			<condition>
				<and>
					<or>
						<equals>
							<property>/autopilot/locks/heading</property>
							<value>VOR</value>
						</equals>
						<equals>
							<property>/autopilot/locks/heading</property>
							<value>LOC</value>
						</equals>
					</or>
					<equals>
						<property>/autopilot/settings/nav-source</property>
						<value>NAV1</value>
					</equals>
				</and>
			</condition>
			<property>/instrumentation/nav[0]/radials/target-auto-hdg-deg</property>
			<offset>
				<property>/orientation/heading-deg</property>
				<scale>-1.0</scale>
			</offset>
		</input>
		<input>
			<condition>
				<and>
					<or>
						<equals>
							<property>/autopilot/locks/heading</property>
							<value>VOR</value>
						</equals>
						<equals>
							<property>/autopilot/locks/heading</property>
							<value>LOC</value>
						</equals>
					</or>
					<equals>
						<property>/autopilot/settings/nav-source</property>
						<value>NAV2</value>
					</equals>
				</and>
			</condition>
			<property>/instrumentation/nav[1]/radials/target-auto-hdg-deg</property>
			<offset>
				<property>/orientation/heading-deg</property>
				<scale>-1.0</scale>
			</offset>
		</input>
		<input>0</input>
		<output>/fdm/jsbsim/fbw/ap/nav-error-deg</output>
		<period>
			<min>-180</min>
			<max>180</max>
		</period>
	</filter>
	
	<pi-simple-controller>
		<name>IT-CONTROLLER: NAV</name>
		<debug>false</debug>
		<enable>
			<condition>
				<or>
					<equals>
						<property>/autopilot/locks/heading</property>
						<value>VOR</value>
					</equals>
					<equals>
						<property>/autopilot/locks/heading</property>
						<value>LOC</value>
					</equals>
				</or>
			</condition>
		</enable>
		<input>/fdm/jsbsim/fbw/ap/nav-error-deg</input>
		<reference>0</reference>
		<output>/autopilot/settings/target-roll-deg</output>
		<config>
			<Kp>
				<expression>
					<table>
						<property>/velocities/airspeed-kt</property>
						<entry><ind>130</ind><dep>-2.7</dep></entry>
						<entry><ind>600</ind><dep>-5.5</dep></entry>
						<entry><ind>950</ind><dep>-6.1</dep></entry>
					</table>
				</expression>
			</Kp>
			<Ki>
				<expression>
					<table>
						<property>/velocities/airspeed-kt</property>
						<entry><ind>260</ind><dep>-0.0075</dep></entry>
						<entry><ind>850</ind><dep>-0.0150</dep></entry>
					</table>
				</expression>
			</Ki>
			<min>
				<property>/fdm/jsbsim/fbw/ap/max-roll</property>
				<scale>-1</scale>
			</min>
			<max>/fdm/jsbsim/fbw/ap/max-roll</max>
		</config>
	</pi-simple-controller>
	
	<!-- Pitch Axis -->
	<filter>
		<name>System Command: Pitch Target Inactive Sync</name>
		<type>gain</type>
		<gain>1.0</gain>
		<enable>
			<condition>
				<and>
					<not-equals>
						<property>/autopilot/locks/altitude</property>
						<value>ALT</value>
					</not-equals>
					<not-equals>
						<property>/autopilot/locks/altitude</property>
						<value>VS</value>
					</not-equals>
					<not-equals>
						<property>/autopilot/locks/altitude</property>
						<value>GS</value>
					</not-equals>
					<not-equals>
						<property>/autopilot/locks/altitude</property>
						<value>TF</value>
					</not-equals>
				</and>
			</condition>
		</enable>
		<input>/orientation/pitch-deg</input>
		<output>/fdm/jsbsim/fbw/ap/target-pitch-deg</output>
		<min>-15</min>
		<max>30</max>
	</filter>
 
	<pid-controller>
		<name>IT-CONTROLLER: FPM Hold</name>
		<debug>false</debug>
		<enable>
			<condition>
				<or>
					<equals>
						<property>/autopilot/locks/altitude</property>
						<value>ALT</value>
					</equals>
					<equals>
						<property>/autopilot/locks/altitude</property>
						<value>VS</value>
					</equals>
					<equals>
						<property>/autopilot/locks/altitude</property>
						<value>GS</value>
					</equals>
				</or>
			</condition>
		</enable>
		<input>/instrumentation/gps/indicated-vertical-speed</input>
		<reference>
			<condition>
				<equals>
					<property>/autopilot/locks/altitude</property>
					<value>ALT</value>
				</equals>
			</condition>
			<expression>
				<min>
					<max>
						<product>
							<difference>
								<property>/instrumentation/altimeter/indicated-altitude-ft</property>
								<property>/autopilot/settings/target-altitude-ft</property>
								<value>1.0</value>
							</difference>
							<table>
								<property>/velocities/airspeed-kt</property>
								<entry><ind>130</ind><dep>-7</dep></entry>
								<entry><ind>950</ind><dep>-2</dep></entry>
							</table>
						</product>
						<value>-500</value>
					</max>
					<value>500</value>
				</min>
			</expression>
		</reference>
		<reference>
			<condition>
				<and>
					<equals>
						<property>/autopilot/locks/altitude</property>
						<value>GS</value>
					</equals>
					<equals>
						<property>/autopilot/settings/nav-source</property>
						<value>NAV1</value>
					</equals>
				</and>
			</condition>
			<expression>
				<min>
					<max>
						<property>/instrumentation/nav[0]/gs-rate-of-climb-fpm</property>
						<value>-1500</value>
					</max>
					<value>0</value>
				</min>
			</expression>
		</reference>
		<reference>
			<condition>
				<and>
					<equals>
						<property>/autopilot/locks/altitude</property>
						<value>GS</value>
					</equals>
					<equals>
						<property>/autopilot/settings/nav-source</property>
						<value>NAV2</value>
					</equals>
				</and>
			</condition>
			<expression>
				<min>
					<max>
						<property>/instrumentation/nav[1]/gs-rate-of-climb-fpm</property>
						<value>-1500</value>
					</max>
					<value>0</value>
				</min>
			</expression>
		</reference>
		<reference>
			<condition>
				<equals>
					<property>/autopilot/locks/altitude</property>
					<value>VS</value>
				</equals>
			</condition>
			<property>/autopilot/settings/vertical-speed-fpm</property>
		</reference>
		<reference>/autopilot/settings/vertical-speed-fpm</reference>
		<output>/fdm/jsbsim/fbw/ap/target-pitch-deg</output>
		<config>
			<Kp>
				<expression>
					<table>
						<property>/velocities/airspeed-kt</property>
						<entry><ind>130</ind><dep>0.0029</dep></entry>
						<entry><ind>600</ind><dep>0.0009</dep></entry>
						<entry><ind>950</ind><dep>0.0003</dep></entry>
					</table>
				</expression>
			</Kp>
			<Ti>2.0</Ti>
			<Td>0.0001</Td>
			<u_min>
				<expression>
					<max>
						<dif>
							<property>/orientation/pitch-deg</property>
							<value>5</value>
						</dif>
						<value>-15</value>
					</max>
				</expression>
			</u_min>
			<u_max>
				<expression>
					<min>
						<sum>
							<property>/orientation/pitch-deg</property>
							<value>5</value>
						</sum>
						<value>30</value>
					</min>
				</expression>
			</u_max>
		</config>
	</pid-controller>
	
	<filter>
		<name>TFS Ground Altitude Damp</name>
		<type>exponential</type>
		<input>/instrumentation/tfs/ground-altitude-ft</input>
		<output>/fdm/jsbsim/fbw/ap/ground-altitude-ft-damp</output>
		<filter-time>0.12</filter-time>
	</filter>
	
	<filter>
		<name>TF Error Computer</name>
		<type>gain</type>
		<gain>1.0</gain>
		<input>
			<expression>
				<difference>
					<property>/position/altitude-ft</property>
					<sum>
						<property>/fdm/jsbsim/fbw/ap/ground-altitude-ft-damp</property>
						<property>/autopilot/settings/target-altitude-ft</property>
					</sum>
				</difference>
			</expression>
		</input>
		<output>/fdm/jsbsim/fbw/ap/tf-error</output>
	</filter>
	
	<filter>
		<name>System Command: Vert Speed to Pitch Absolute Computer</name>
		<type>gain</type>
		<gain>1.0</gain>
		<input>
			<condition>
				<equals>
					<property>/autopilot/locks/altitude</property>
					<value>TF</value>
				</equals>
			</condition>
			<expression>
				<sum>
					<product> <!-- Fix for roll -->
						<property>/orientation/alpha-deg</property>
						<cos>
							<div>
								<property>/orientation/roll-deg</property>
								<value>57.2957795131</value>
							</div>
						</cos>
					</product>
					<product>
						<asin>
							<div>
								<min>
									<max>
										<product>
											<property>/fdm/jsbsim/fbw/ap/tf-error</property>
											<table>
												<property>/velocities/airspeed-kt</property>
												<entry><ind>130</ind><dep>-14.0</dep></entry>
												<entry><ind>950</ind><dep> -4.0</dep></entry>
											</table>
											<table>
												<abs><property>/fdm/jsbsim/fbw/ap/tf-error</property></abs>
												<entry><ind>   0</ind><dep>1.0</dep></entry>
												<entry><ind> 250</ind><dep>1.5</dep></entry>
												<entry><ind>1000</ind><dep>2.5</dep></entry>
											</table>
										</product>
										<value>-5000</value>
									</max>
									<table>
										<property>/instrumentation/airspeed-indicator/indicated-speed-kt</property>
										<entry><ind>200</ind><dep>10000</dep></entry>
										<entry><ind>600</ind><dep>25000</dep></entry>
									</table>
								</min>
								<product>
									<sum> <!-- Calculate True Airspeed -->
										<property>/velocities/airspeed-kt</property>
										<product>
											<property>/position/altitude-ft</property>
											<value>0.005</value>
										</product>
									</sum>
									<value>101.0696354508</value>
								</product>
							</div>
						</asin>
						<value>57.2957795131</value>
					</product>
				</sum>
			</expression>
		</input>
		<input>/orientation/pitch-deg</input>
		<output>/fdm/jsbsim/fbw/ap/target-pitch-deg-tf</output>
		<min>-15</min>
		<max>30</max>
	</filter>
	
	<!-- Command to FBW Roll and Pitch -->
	<logic>
		<input>
			<equals>
				<property>/autopilot/locks/AP-status</property>
				<value>AP1</value>
			</equals>
		</input>
		<output>/fdm/jsbsim/fbw/ap/roll-enabled</output>
		<output>/fdm/jsbsim/fbw/ap/pitch-enabled</output>
	</logic>
	
	<filter>
		<name>System Command: Roll Rate</name>
		<debug>false</debug>
		<type>gain</type>
		<gain>-0.0436332</gain> <!-- -2.5 * Deg2Rad -->
		<input>/orientation/roll-deg</input>
		<reference>/autopilot/settings/target-roll-deg</reference>
		<output>/fdm/jsbsim/fbw/ap/roll-cmd</output>
		<min>-0.349066</min>
		<max>0.349066</max>
	</filter>
	
	<filter>
		<name>System Command: Pitch Rate</name>
		<debug>false</debug>
		<type>gain</type>
		<gain>-1.5</gain>
		<input>/orientation/pitch-deg</input>
		<reference>
			<condition>
				<equals>
					<property>/autopilot/locks/altitude</property>
					<value>TF</value>
				</equals>
			</condition>
			<property>/fdm/jsbsim/fbw/ap/target-pitch-deg-tf</property>
		</reference>
		<reference>
			<condition>
				<or>
					<equals>
						<property>/autopilot/locks/altitude</property>
						<value>ALT</value>
					</equals>
					<equals>
						<property>/autopilot/locks/altitude</property>
						<value>VS</value>
					</equals>
					<equals>
						<property>/autopilot/locks/altitude</property>
						<value>GS</value>
					</equals>
				</or>
			</condition>
			<property>/fdm/jsbsim/fbw/ap/target-pitch-deg</property>
		</reference>
		<reference>/autopilot/settings/target-pitch-deg</reference>
		<output>/fdm/jsbsim/fbw/ap/target-pitch-rate</output>
		<min>
			<condition>
				<equals>
					<property>/autopilot/locks/altitude</property>
					<value>TF</value>
				</equals>
			</condition>
			<value>-20</value>
		</min>
		<min>-10</min>
		<max>
			<condition>
				<equals>
					<property>/autopilot/locks/altitude</property>
					<value>TF</value>
				</equals>
			</condition>
			<value>20</value>
		</max>
		<max>10</max>
	</filter>
	
	<pi-simple-controller>
		<name>System Command: G Accel</name>
		<debug>false</debug>
		<enable>
			<condition>
				<equals>
					<property>/autopilot/locks/AP-status</property>
					<value>AP1</value>
				</equals>
			</condition>
		</enable>
		<input>/fdm/jsbsim/fbw/ap/target-pitch-rate</input>
		<reference>/orientation/pitch-rate-degps</reference>
		<output>/fdm/jsbsim/fbw/ap/target-g-cmd</output>
		<config>
			<Kp>
				<expression>
					<table>
						<property>/velocities/airspeed-kt</property>
						<entry><ind>130</ind><dep>-0.24</dep></entry>
						<entry><ind>600</ind><dep>-0.40</dep></entry>
						<entry><ind>950</ind><dep>-0.47</dep></entry>
					</table>
				</expression>
			</Kp>
			<Ki>
				<expression>
					<table>
						<property>/velocities/airspeed-kt</property>
						<entry><ind>130</ind><dep>-0.040</dep></entry>
						<entry><ind>600</ind><dep>-0.067</dep></entry>
						<entry><ind>950</ind><dep>-0.079</dep></entry>
					</table>
				</expression>
			</Ki>
			<!--Kp>/it-fbw/tune/kp</Kp>
			<Ki>/it-fbw/tune/ki</Ki-->
			<min>
				<condition>
					<equals>
						<property>/autopilot/locks/altitude</property>
						<value>TF</value>
					</equals>
				</condition>
				<value>-2.5</value> <!-- -1.5G -->
			</min>
			<min>-1.0</min> <!-- 0.0G -->
			<max>
				<condition>
					<equals>
						<property>/autopilot/locks/altitude</property>
						<value>TF</value>
					</equals>
				</condition>
				<value>4.0</value> <!-- 5.0G -->
			</max>
			<max>1.5</max> <!-- 2.5G -->
		</config>
	</pi-simple-controller>
	
	<filter>
		<name>System Command: G Accel Target</name>
		<type>gain</type>
		<gain>1.0</gain>
		<input>
			<expression>
				<sum>
					<property>/fdm/jsbsim/fbw/ap/target-g-cmd</property>
					<div>
						<value>1</value>
						<cos>
							<deg2rad><property>/orientation/roll-deg</property></deg2rad>
						</cos>
					</div>
				</sum>
			</expression>
		</input>
		<output>/fdm/jsbsim/fbw/ap/g-cmd</output>
		<min>
			<condition>
				<equals>
					<property>/autopilot/locks/altitude</property>
					<value>TF</value>
				</equals>
			</condition>
			<value>-1.5</value>
		</min>
		<min>0.0</min>
		<max>
			<condition>
				<equals>
					<property>/autopilot/locks/altitude</property>
					<value>TF</value>
				</equals>
			</condition>
			<value>5.0</value>
		</max>
		<max>2.5</max>
	</filter>
	
	<filter>
		<name>System Command: Trim Killer</name>
		<type>gain</type>
		<gain>1.0</gain>
		<enable>
			<condition>
				<equals>
					<property>/autopilot/locks/AP-status</property>
					<value>AP1</value>
				</equals>
			</condition>
		</enable>
		<input>0</input>
		<output>/controls/flight/aileron-trim</output>
		<output>/controls/flight/elevator-trim</output>
		<output>/controls/flight/rudder-trim</output>
	</filter>
	
	<!-- System Command Autothrottle -->
	<pid-controller>
		<name>IT-CONTROLLER: Speed Knots</name>
		<debug>false</debug>
		<enable>
			<condition>
				<equals>
					<property>/autopilot/locks/speed</property>
					<value>SPD</value>
				</equals>
			</condition>
		</enable>
		<input>/instrumentation/airspeed-indicator/indicated-speed-kt</input>
		<reference>/autopilot/settings/target-speed-kt</reference>
		<output>/controls/engines/engine[0]/throttle</output>
		<config>
			<Kp>0.075</Kp>
			<Ti>5.0</Ti>
			<Td>0.001</Td>
			<u_min>0.0</u_min>
			<u_max>
				<condition>
					<property>/fdm/jsbsim/fbw/ap/athr-tuningmode</property>
				</condition>
				<value>1.0</value>
			</u_max>
			<u_max>0.8999</u_max> <!-- No afterburner -->
		</config>
	</pid-controller>

</PropertyList>
