<?xml version="1.0"?>

<!-- Mirage 2000 FCS/FBW -->
<!-- Licenced under GPL -->
<!-- Copyright (c) 2020 Josh Davidson (Octal450) -->
<!-- Copyright (c) 2020 Richard Harrison -->

<system name="Mirage-2000: FCS/FBW">
	
	<property value="1">fbw/enable</property>
	<property value="0">fbw/mode</property> <!-- 0: Air/Air (default), 1: Charges -->
	<property value="0">fbw/man-slat</property> <!-- Debugging -->
	<property value="0">fbw/man-yaw</property> <!-- Debugging -->
	<property value="0">fbw/roll/pid</property>
	<property value="0">fbw/pitch/q-demand</property>
	<property value="0">fbw/pitch/q-pi</property>
	<property value="0">fbw/pitch/q-d</property>
	<property value="0">fbw/yaw/beta-d</property>
	<property value="1">fbw/pitch/instrumented-g</property>
	
	<property value="0">gear/gear-emergency-extension</property>
	<property value="1">/controls/flight/nws-enabled</property>
	
	<!-- Autopilot stuff -->
	<property value="0">fbw/ap/roll-enabled</property>
	<property value="0">fbw/ap/pitch-enabled</property>
	<property value="0">fbw/ap/roll-cmd</property>
	<property value="0">fbw/ap/g-cmd</property>
	
	<!-- Default gains for tuning -->
	<property value="0">fbw/roll/p-gain</property>
	<property value="0">fbw/roll/d-gain</property>
	<property value="0">fbw/pitch/g-p-gain</property>
	<property value="0">fbw/pitch/g-i-gain</property>
	<property value="0">fbw/pitch/g-d-gain</property>
	<property value="0">fbw/pitch/q-p-gain</property>
	<property value="0">fbw/pitch/q-i-gain</property>
	<property value="0">fbw/pitch/q-d-gain</property>
	<property value="0">fbw/yaw/yd-p-gain</property>
	<property value="0">fbw/yaw/beta-p-gain</property>
	<property value="0">fbw/yaw/beta-d-gain</property>
	<property value="140">fcs/rudder-rate</property>

	<channel name="Libraries">
		
		<switch name="position/wow">
			<default value="0"/>
			<test logic="OR" value="1">
				gear/unit[0]/WOW eq 1
				gear/unit[1]/WOW eq 1
				gear/unit[2]/WOW eq 1
			</test>
		</switch>
		
		<switch name="fcs/electric-avail">
			<default value="0"/>
			<test logic="OR" value="1">
				/systems/electrical/volts gt 0
			</test>
		</switch>
		
		<switch name="fcs/hydraulic-avail">
			<default value="0"/>
			<test logic="OR" value="1">
				systems/hydraulics/number1-system-available eq 1
				systems/hydraulics/number2-system-available eq 1
			</test>
		</switch>
		
		<switch name="fbw/gain-switch">
			<default value="0"/>
			<test logic="AND" value="1">
				position/wow eq 1
				fbw/enable eq 1
				fcs/electric-avail eq 1
				fcs/hydraulic-avail eq 1
			</test>
			<test logic="OR" value="2">
				fbw/enable eq 0
				fcs/electric-avail ne 1
				fcs/hydraulic-avail ne 1
			</test>
		</switch>
		
		<fcs_function name="fbw/roll/p-gain">
			<function>
				<table>
					<independentVar lookup="row">velocities/vc-kts</independentVar>
					<independentVar lookup="column">fbw/gain-switch</independentVar>
					<tableData>
						     0     1     2
						130 -0.84 -0.22  0.00
						200 -0.70 -0.22  0.00
						350 -0.40 -0.22  0.00
						600 -0.25 -0.22  0.00
						950 -0.15 -0.22  0.00
					</tableData>
				</table>
			</function>
		</fcs_function>
		
		<fcs_function name="fbw/roll/d-gain">
			<function>
				<table>
					<independentVar lookup="row">velocities/vc-kts</independentVar>
					<independentVar lookup="column">fbw/gain-switch</independentVar>
					<tableData>
						     0      1
						200 -0.050  0.000
						350 -0.040  0.000
						600 -0.020  0.000
						950 -0.005  0.000
					</tableData>
				</table>
			</function>
		</fcs_function>
		
		<fcs_function name="fbw/pitch/g-p-gain">
			<function>
				<table>
					<independentVar lookup="row">velocities/vc-kts</independentVar>
					<tableData>
						130 -0.240
						200 -0.150
						350 -0.090
						600 -0.060
						950 -0.025
					</tableData>
				</table>
			</function>
		</fcs_function>
		
		<fcs_function name="fbw/pitch/g-i-gain">
			<function>
				<table>
					<independentVar lookup="row">velocities/vc-kts</independentVar>
					<independentVar lookup="column">fbw/gain-switch</independentVar>
					<tableData>
						     0      1
						130 -0.150  0.000
						200 -0.100  0.000
						350 -0.065  0.000
						600 -0.060  0.000
						950 -0.042  0.000
					</tableData>
				</table>
			</function>
		</fcs_function>
		
		<fcs_function name="fbw/pitch/q-p-gain">
			<function>
				<table>
					<independentVar lookup="row">velocities/vc-kts</independentVar>
					<independentVar lookup="column">fbw/gain-switch</independentVar>
					<tableData>
						     0     1
						130  7.4  4.5
						200  6.0  4.5
						350  3.0  4.5
						600  1.7  4.5
						950  1.0  4.5
					</tableData>
				</table>
			</function>
		</fcs_function>
		
		<fcs_function name="fbw/pitch/q-i-gain">
			<function>
				<table>
					<independentVar lookup="row">velocities/vc-kts</independentVar>
					<independentVar lookup="column">fbw/gain-switch</independentVar>
					<tableData>
						     0    1
						130  8.9  0.0
						200  8.0  0.0
						350  6.0  0.0
						600  4.0  0.0
						950  3.0  0.0
					</tableData>
				</table>
			</function>
		</fcs_function>
		
		<fcs_function name="fbw/pitch/q-d-gain">
			<function>
				<table>
					<independentVar lookup="row">velocities/vc-kts</independentVar>
					<independentVar lookup="column">fbw/gain-switch</independentVar>
					<tableData>
						     0     1
						130  0.15  0.00
						200  0.10  0.00
						350  0.08  0.00
						600  0.03  0.00
						950  0.01  0.00
					</tableData>
				</table>
			</function>
		</fcs_function>
		
		<fcs_function name="fbw/yaw/yd-p-gain">
			<function>
				<table>
					<independentVar lookup="row">velocities/vc-kts</independentVar>
					<independentVar lookup="column">fbw/gain-switch</independentVar>
					<tableData>
						     0    1
						130 -2.1  0.0
						200 -1.8  0.0
						350 -1.0  0.0
						600 -0.6  0.0
						950 -0.2  0.0
					</tableData>
				</table>
			</function>
		</fcs_function>
		
		<fcs_function name="fbw/yaw/beta-p-gain">
			<function>
				<table>
					<independentVar lookup="row">velocities/vc-kts</independentVar>
					<independentVar lookup="column">fbw/gain-switch</independentVar>
					<tableData>
						     0    1
						130  8.4  0.0
						200  7.0  0.0
						350  5.0  0.0
						600  2.0  0.0
						950  1.0  0.0
					</tableData>
				</table>
			</function>
		</fcs_function>

		<fcs_function name="fbw/yaw/beta-d-gain">
			<function>
				<table>
					<independentVar lookup="row">velocities/vc-kts</independentVar>
					<independentVar lookup="column">fbw/gain-switch</independentVar>
					<tableData>
						     0    1
						130  4.5  0.0
						200  4.0  0.0
						350  3.0  0.0
						600  1.0  0.0
						950  0.5  0.0
					</tableData>
				</table>
			</function>
		</fcs_function>
	
	</channel>
	
	<channel name="FBW: Roll">
		
		<fcs_function name="fbw/roll/aileron-nul">
			<function>
				<table>
					<independentVar lookup="row">/controls/flight/aileron</independentVar>
					<tableData>
						-1.00 -1.0
						-0.02  0.0
						 0.00  0.0
						 0.02  0.0
						 1.00  1.0
					</tableData>
				</table>
			</function>
		</fcs_function>
		
		<fcs_function name="fbw/roll/aileron-modified"> <!-- Make a nice curve, or its too sensitive -->
			<function>
				<product>
					<pow>
						<product>
							<abs>
								<property>fbw/roll/aileron-nul</property>
							</abs>
							<value>100</value>
						</product>
						<value>2</value>
					</pow>
					<ifthen> <!-- Cause negative aileron by power don't work -->
						<ge>
							<property>fbw/roll/aileron-nul</property>
							<value>0</value>
						</ge>
						<value>1</value>
						<value>-1</value>
					</ifthen>
				</product>
			</function>
		</fcs_function>
		
		<fcs_function name="fbw/roll/rate-stick"> <!-- Air/Air: 270 deg/4.71239 rad, Charges: 150 deg/2.61799 rad per sec -->
			<function>
				<table>
					<independentVar lookup="row">fbw/roll/aileron-modified</independentVar>
					<independentVar lookup="column">fbw/mode</independentVar>
					<independentVar lookup="table">fbw/pitch/instrumented-g</independentVar>
					<tableData breakPoint="2"> <!-- breakPoint is guess -->
						        0        1
						-10000 -4.71239 -2.61799
						     0  0.00000  0.00000
						 10000  4.71239  2.61799
					</tableData>
					<tableData breakPoint="5"> <!-- breakPoint is guess -->
						        0        1
						-10000 -4.71239 -1.48353
						     0  0.00000  0.00000
						 10000  4.71239  1.48353
					</tableData>
				</table>
			</function>
		</fcs_function>
		
		<lag_filter name="fbw/roll/rate-stick-lag">
			<input>fbw/roll/rate-stick</input>
			<c1>11.3</c1>
		</lag_filter>
		
		<switch name="fbw/roll/rad-hold">
			<default value="0"/>
			<test logic="AND" value="1">
				fbw/roll/rate-stick-lag lt 0.005
				fbw/roll/rate-stick-lag gt -0.005
				position/wow eq 0
				fbw/ap/roll-enabled eq 0
				velocities/p-rad_sec lt 0.10472
				velocities/p-rad_sec gt -0.10472
			</test>
		</switch>
		
		<fcs_function name="fbw/roll/rad">
			<function>
				<ifthen>
					<eq>
						<property>fbw/roll/rad-hold</property>
						<value>0</value>
					</eq>
					<property>attitude/roll-rad</property>
					<property>fbw/roll/rad</property>
				</ifthen>
			</function>
		</fcs_function>
		
		<summer name="fbw/roll/rad-error">
			<input>attitude/roll-rad</input>
			<input>-fbw/roll/rad</input>
		</summer>
		
		<pure_gain name="fbw/roll/rad-rate">
			<input>fbw/roll/rad-error</input>
			<gain>-2.5</gain>
			<clipto>
				<min>-0.0872665</min>
				<max>0.0872665</max>
			</clipto>
		</pure_gain>
		
		<summer name="fbw/roll/rate-command">
			<input>fbw/roll/rate-stick-lag</input>
			<input>fbw/roll/rad-rate</input>
		</summer>
		
		<switch name="fbw/roll/rate-demand">
			<default value="fbw/roll/rate-command"/>
			<test value="fbw/ap/roll-cmd">
				fbw/ap/roll-enabled eq 1
			</test>
		</switch>
		
		<summer name="fbw/roll/rate-error">
			<input>velocities/p-rad_sec</input>
			<input>-fbw/roll/rate-demand</input>
		</summer>
		
		<pid name="fbw/roll/pid-v">
			<input>fbw/roll/rate-error</input>
			<kp>fbw/roll/p-gain</kp>
			<ki>0.0</ki>
			<kd>fbw/roll/d-gain</kd>
			<clipto>
				<min>-1.0</min>
				<max>1.0</max>
			</clipto>
			<output>fbw/roll/pid</output>
		</pid>
		
		<pure_gain name="fbw/roll/p-g">
			<input>fbw/roll/rate-error</input>
			<gain>fbw/roll/p-gain</gain>
			<clipto>
				<min>-1.0</min>
				<max>1.0</max>
			</clipto>
		</pure_gain>
		
		<switch name="fbw/roll-output">
			<default value="fbw/roll/pid"/>
			<test value="fbw/roll/p-g">
				fbw/gain-switch gt 0
			</test>
		</switch>
		
		<pure_gain name="fbw/roll-inner-output"> <!-- Change to switch once the inner elevon roll control can be disabled -->
			<input>fbw/roll-output</input>
			<gain>1.0</gain>
		</pure_gain>
	
	</channel>
	
	<channel name="FBW: Pitch">
		
		<fcs_function name="fbw/pitch/elevator-nul">
			<function>
				<table>
					<independentVar lookup="row">/controls/flight/elevator</independentVar>
					<tableData>
						-1.00 -1.0
						-0.02  0.0
						 0.00  0.0
						 0.02  0.0
						 1.00  1.0
					</tableData>
				</table>
			</function>
		</fcs_function>
		
		<fcs_function name="fbw/pitch/elevator-trim-nul"> <!-- This way, the trim can be reset and not affect the G when the pilot tries to center it -->
			<function>
				<table>
					<independentVar lookup="row">/controls/flight/elevator-trim</independentVar>
					<tableData>
						-1.00 -1.0
						-0.02  0.0
						 0.00  0.0
						 0.02  0.0
						 1.00  1.0
					</tableData>
				</table>
			</function>
		</fcs_function>
		
		<summer name="fcs/elevator-sum">
			<input>fbw/pitch/elevator-nul</input>
			<input>fbw/pitch/elevator-trim-nul</input>
			<clipto>
				<min>-1.0</min>
				<max>1.0</max>
			</clipto>
		</summer>
		
		<fcs_function name="fbw/pitch/deadzone">
			<function>
				<table>
					<independentVar lookup="row">fbw/roll/aileron-nul</independentVar>
					<tableData>
						-1.0  0.15
						-0.5  0.00
						 0.5  0.00
						 1.0  0.15
					</tableData>
				</table>
			</function>
		</fcs_function>
		
		<fcs_function name="fbw/pitch/alpha-modified">
			<function>
				<ifthen>
					<lt>
						<property>/instrumentation/airspeed-indicator/indicated-speed-kt</property>
						<value>100</value>
					</lt>
					<sum>
						<property>aero/alpha-deg</property> <!-- Should really be indicated -->
						<value>2</value>
					</sum>
					<property>aero/alpha-deg</property> <!-- Should really be indicated -->
				</ifthen>
			</function>
		</fcs_function>
		
		<switch name="fbw/pitch/alpha-switched">
			<default value="fbw/pitch/alpha-modified"/>
			<test value="0">
				position/wow eq 1
			</test>
		</switch>
		
		<lag_filter name="fbw/pitch/alpha-lag">
			<input>fbw/pitch/alpha-switched</input>
			<c1>10</c1>
		</lag_filter>
		
		<fcs_function name="fbw/pitch/alpha-pitch-feed">
			<function>
				<table>
					<independentVar lookup="row">fbw/pitch/alpha-lag</independentVar>
					<tableData>
						26  0.0
						34  1.0
					</tableData>
				</table>
			</function>
		</fcs_function>
		
		<fcs_function name="fbw/pitch/elevator-modified-min">
			<function>
				<table>
					<independentVar lookup="row">fbw/pitch/alpha-lag</independentVar>
					<tableData>
						17 -1.00
						27 -0.85
						29  0.00
					</tableData>
				</table>
			</function>
		</fcs_function>
		
		<fcs_function name="fbw/pitch/elevator-modified">
			<function>
				<product>
					<quotient>
						<property>fbw/pitch/elevator-nul</property>
						<pow>
							<value>1</value>
							<property>fbw/pitch/elevator-nul</property>
						</pow>
					</quotient>
					<product>
						<max>
							<value>0</value>
							<difference>
								<abs>
									<property>fbw/pitch/elevator-nul</property>
								</abs>
								<property>fbw/pitch/deadzone</property>
							</difference>
						</max>
						<quotient>
							<value>1.0</value>
							<difference>
								<value>1.0</value>
								<property>fbw/pitch/deadzone</property>
							</difference>
						</quotient>
					</product>
				</product>
			</function>
			<clipto>
				<min>fbw/pitch/elevator-modified-min</min>
				<max>1.0</max>
			</clipto>
		</fcs_function>
		
		<summer name="fbw/pitch/g-command-input">
			<input>fbw/pitch/elevator-modified</input>
			<input>fbw/pitch/alpha-pitch-feed</input>
		</summer>
		
		<switch name="fbw/pitch/g-max">
			<default value="10.5"/>
			<test logic="AND" value="7.5">
				fbw/mode eq 1
			</test>
		</switch>
		
		<fcs_function name="fbw/pitch/g-command">
			<function>
				<sum>
					<product> <!-- Reduces stick sensitivity at low speeds -->
						<difference>
							<sum>
								<table>
									<independentVar lookup="row">fbw/pitch/g-command-input</independentVar>
									<independentVar lookup="column">fbw/mode</independentVar>
									<tableData>
										        0    1
										-1.000 10.5  7.5
										-0.900  8.5  5.5 <!-- A slight null zone for the elastic stop -->
										-0.850  8.5  5.5
										 0.000  1.0  1.0
										 1.000 -3.5 -3.5
									</tableData>
								</table>
								<product> <!-- Equals +/-1.5G -->
									<property>fbw/pitch/elevator-trim-nul</property>
									<value>-1.5</value>
								</product>
							</sum>
							<value>1.0</value>
						</difference>
						<table>
							<independentVar lookup="row">velocities/mach</independentVar>
							<tableData>
								0.2  0.25
								0.3  0.50
								0.5  1.00
							</tableData>
						</table>
					</product>
					<value>1.0</value>
				</sum>
			</function>
			<clipto>
				<min>-3.5</min>
				<max>fbw/pitch/g-max</max>
			</clipto>
		</fcs_function>
		
		<lag_filter name="fbw/pitch/g-command-lag">
			<input>fbw/pitch/g-command</input>
			<c1>8.1</c1>
		</lag_filter>
		
		<switch name="fbw/pitch/g-demand">
			<default value="fbw/pitch/g-command-lag"/>
			<test value="fbw/ap/g-cmd">
				fbw/ap/pitch-enabled eq 1
			</test>
		</switch>
		
		<pure_gain name="fbw/pitch/instrumented-g">
			<input>/accelerations/pilot-g</input>
			<gain>1.0</gain>
			<output>/instrumentation/g-meter/instrumented-g</output>
		</pure_gain>
		
		<summer name="fbw/pitch/g-error">
			<input>fbw/pitch/instrumented-g</input>
			<input>-fbw/pitch/g-demand</input>
		</summer>
		
		<switch name="fbw/pitch/g-trigger">
			<default value="0"/>
			<test value="-1">
				fbw/gain-switch gt 0
			</test>
			<test logic="OR" value="1">
				fbw/pitch/q-demand le -0.999
				fbw/pitch/q-demand ge 0.999
			</test>
		</switch>
		
		<pid name="fbw/pitch/g-pi-v">
			<input>fbw/pitch/g-error</input>
			<kp>fbw/pitch/g-p-gain</kp>
			<ki>fbw/pitch/g-i-gain</ki>
			<kd>fbw/pitch/g-d-gain</kd>
			<trigger>fbw/pitch/g-trigger</trigger>
			<clipto>
				<min>-1.0</min>
				<max>1.0</max>
			</clipto>
			<output>fbw/pitch/q-demand</output>
		</pid>
		
		<fcs_function name="fbw/pitch/q-error">
			<function>
				<difference>
					<property>velocities/q-rad_sec</property>
					<product>
						<property>fbw/pitch/q-demand</property>
						<value>0.523599</value>
						<!-- For tuning/testing only -->
						<!--property>fbw/pitch/elevator-nul</property>
						<value>-0.523599</value-->
					</product>
				</difference>
			</function>
		</fcs_function>
		
		<switch name="fbw/pitch/q-trigger">
			<default value="0"/>
			<test value="-1">
				fbw/gain-switch gt 0
			</test>
			<test logic="OR" value="1">
				fbw/pitch/q-pi le -0.999
				fbw/pitch/q-pi ge 0.639
			</test>
		</switch>
		
		<pid name="fbw/pitch/q-pi-v"> <!-- Driver -->
			<input>fbw/pitch/q-error</input>
			<kp>fbw/pitch/q-p-gain</kp>
			<ki>fbw/pitch/q-i-gain</ki>
			<kd>0.0</kd>
			<trigger>fbw/pitch/q-trigger</trigger>
			<clipto>
				<min>-1.0</min>
				<max>0.64</max>
			</clipto>
			<output>fbw/pitch/q-pi</output>
		</pid>
		
		<pid name="fbw/pitch/q-d-v"> <!-- Damper -->
			<input>velocities/q-rad_sec</input>
			<kp>0.0</kp>
			<ki>0.0</ki>
			<kd>fbw/pitch/q-d-gain</kd>
			<clipto>
				<min>-1.0</min>
				<max>0.64</max>
			</clipto>
			<output>fbw/pitch/q-d</output>
		</pid>
		
		<summer name="fbw/pitch-output">
			<input>fbw/pitch/q-pi</input>
			<input>fbw/pitch/q-d</input>
			<clipto>
				<min>-1.0</min>
				<max>0.64</max>
			</clipto>
		</summer>
	
	</channel>
	
	<channel name="FBW: Yaw">
		
		<fcs_function name="fbw/yaw/rudder-nul">
			<function>
				<table>
					<independentVar lookup="row">/controls/flight/rudder</independentVar>
					<tableData>
						-1.00 -1.0
						-0.02  0.0
						 0.00  0.0
						 0.02  0.0
						 1.00  1.0
					</tableData>
				</table>
			</function>
		</fcs_function>
		
		<fcs_function name="fbw/yaw/rudder-trim-nul"> <!-- This way, the trim can be reset and not affect the Yaw when the pilot tries to center it -->
			<function>
				<table>
					<independentVar lookup="row">/controls/flight/rudder-trim</independentVar>
					<tableData>
						-1.00 -1.0
						-0.02  0.0
						 0.00  0.0
						 0.02  0.0
						 1.00  1.0
					</tableData>
				</table>
			</function>
		</fcs_function>
		
		<summer name="fcs/rudder-sum">
			<input>fbw/yaw/rudder-nul</input>
			<input>fbw/yaw/rudder-trim-nul</input>
			<clipto>
				<min>-1.0</min>
				<max>1.0</max>
			</clipto>
		</summer>
		
		<fcs_function name="fbw/yaw/rudder-min">
			<function>
				<table>
					<independentVar lookup="row">fbw/pitch/alpha-lag</independentVar>
					<independentVar lookup="column">attitude/roll-rad</independentVar>
					<tableData>
						    0.000000  0.174533
						20 -1.0      -1.0
						28 -1.0       0.0
					</tableData>
				</table>
			</function>
		</fcs_function>
		
		<fcs_function name="fbw/yaw/rudder-max">
			<function>
				<table>
					<independentVar lookup="row">fbw/pitch/alpha-lag</independentVar>
					<independentVar lookup="column">attitude/roll-rad</independentVar>
					<tableData>
						   -0.174533  0.000000
						20  1.0       1.0
						28  0.0       1.0
					</tableData>
				</table>
			</function>
		</fcs_function>
		
		<pure_gain name="fbw/yaw/rudder-cmd">
			<input>fcs/rudder-sum</input>
			<gain>1.0</gain>
			<clipto>
				<min>fbw/yaw/rudder-min</min>
				<max>fbw/yaw/rudder-max</max>
			</clipto>
		</pure_gain>
		
		<switch name="fbw/yaw/gear">
			<default value="0"/>
			<test logic="OR" value="1">
				gear/gear-pos-norm eq 1
			</test>
		</switch>
		
		<fcs_function name="fbw/yaw/l-f">
			<function>
				<product>
					<table>
						<independentVar lookup="row">velocities/r-rad_sec</independentVar>
						<independentVar lookup="column">attitude/roll-rad</independentVar>
						<tableData>
							           -3.141590 -2.356190 -0.785398  0.000000  0.785398  2.356190  3.141590
							-0.2094390  1.0       0.0       0.0       1.0       0.0       0.0       1.0
							-0.1396260  0.4       0.0       0.0       0.4       0.0       0.0       0.4
							-0.1047195  0.0       0.0       0.0       0.0       0.0       0.0       0.0
							 0.0000000  0.0       0.0       0.0       0.0       0.0       0.0       0.0
							 0.1047195  0.0       0.0       0.0       0.0       0.0       0.0       0.0
							 0.1396260 -0.4       0.0       0.0      -0.4       0.0       0.0      -0.4
							 0.2094390 -1.0       0.0       0.0      -1.0       0.0       0.0      -1.0
						</tableData>
					</table>
					<table>
						<independentVar lookup="row">velocities/vc-kts</independentVar>
						<tableData>
							320  0.950
							600  0.450
							950  0.150
						</tableData>
					</table>
				</product>
			</function>
		</fcs_function>
		
		<washout_filter name="fbw/yaw/r-washout">
			<input>velocities/r-rad_sec</input>
			<c1>0.5</c1>
		</washout_filter>
		
		<pure_gain name="fbw/yaw/yd-f">
			<input>fbw/yaw/r-washout</input>
			<gain>fbw/yaw/yd-p-gain</gain>
			<clipto>
				<min>-0.5</min>
				<max>0.5</max>
			</clipto>
		</pure_gain>
		
		<fcs_function name="fbw/yaw/rudder-fixed">
			<function>
				<sum>
					<property>fbw/yaw/rudder-nul</property>
					<product>
						<property>fbw/yaw/rudder-trim-nul</property>
						<value>0.5</value>
					</product>
				</sum>
			</function>
			<clipto>
				<min>-1.0</min>
				<max>1.0</max>
			</clipto>
		</fcs_function>
		
		<lag_filter name="fbw/yaw/rudder-lag">
			<input>fbw/yaw/rudder-fixed</input>
			<c1>3.8</c1>
		</lag_filter>
		
		<fcs_function name="fbw/yaw/beta-cmd">
			<function>
				<table>
					<independentVar lookup="row">fbw/yaw/rudder-lag</independentVar>
					<independentVar lookup="column">fbw/yaw/gear</independentVar>
					<tableData>
						      0         1
						-1.0  0.261799  0.349066
						 0.0  0.000000  0.000000
						 1.0 -0.261799 -0.349066
					</tableData>
				</table>
			</function>
		</fcs_function>
		
		<summer name="fbw/yaw/beta-error">
			<input>aero/beta-rad</input>
			<input>-fbw/yaw/beta-cmd</input>
		</summer>
		
		<pure_gain name="fbw/yaw/beta-p">
			<input>fbw/yaw/beta-error</input>
			<gain>fbw/yaw/beta-p-gain</gain>
			<clipto>
				<min>-1.0</min>
				<max>1.0</max>
			</clipto>
		</pure_gain>
		
		<pid name="fbw/yaw/beta-d-v">
			<input>aero/beta-rad</input>
			<kp>0.0</kp>
			<ki>0.0</ki>
			<kd>fbw/yaw/beta-d-gain</kd>
			<clipto>
				<min>-1.0</min>
				<max>1.0</max>
			</clipto>
			<output>fbw/yaw/beta-d</output>
		</pid>
		
		<summer name="fbw/yaw/beta-pd">
			<input>fbw/yaw/beta-p</input>
			<input>fbw/yaw/beta-d</input>
			<clipto>
				<min>-1.0</min>
				<max>1.0</max>
			</clipto>
		</summer>
		
		<summer name="fbw/yaw/active">
			<input>fbw/yaw/l-f</input>
			<input>fbw/yaw/yd-f</input>
			<input>fbw/yaw/beta-pd</input>
		</summer>
		
		<switch name="fbw/yaw-output">
			<default value="fbw/yaw/active"/>
			<test value="fcs/rudder-sum">
				position/wow eq 1
			</test>
			<clipto>
				<min>fbw/yaw/rudder-min</min>
				<max>fbw/yaw/rudder-max</max>
			</clipto>
		</switch>
	
	</channel>
	
	<channel name="FBW: Auto slats based on alpha">
		
		<fcs_function name="fbw/slat/fully-extended-max-mn">
			<function>
				<table>
					<independentVar lookup="row">position/h-sl-ft</independentVar>
					<tableData>
						    0  0.5105
						28500  0.8516
						35000  0.8595
						40000  0.8643
					</tableData>
				</table>
			</function>
		</fcs_function>
		
		<fcs_function name="fbw/slat/partially-extended-max-mn">
			<function>
				<table>
					<independentVar lookup="row">position/h-sl-ft</independentVar>
					<tableData>
						    0  0.5806
						28500  0.8654
						35000  0.8702
						40000  0.8765
					</tableData>
				</table>
			</function>
		</fcs_function>
		
		<fcs_function name="fbw/slat/extension-aoa">
			<function>
				<table>
					<independentVar lookup="row">velocities/mach</independentVar>
					<tableData>
						0.3800  9.32
						0.4000  9.16
						0.4500  9.16
						0.5000  9.09
						0.5500  9.05
						0.6100  9.01
						0.6600  8.95
						0.7100  8.89
						0.7600  8.83
						0.8100  8.81
						0.8700  8.78
					</tableData>
				</table>
			</function>
		</fcs_function>
		
		<fcs_function name="fbw/slat/retraction-aoa">
			<function>
				<table>
					<independentVar lookup="row">velocities/mach</independentVar>
					<tableData>
						0.3800  6.16
						0.4000  6.00
						0.4500  6.00
						0.5000  5.93
						0.5500  5.88
						0.6100  5.85
						0.6600  5.78
						0.7100  5.73
						0.7600  5.67
						0.8100  5.64
						0.8700  5.62
					</tableData>
				</table>
			</function>
		</fcs_function>
		
		<fcs_function name="fbw/slat/extension-calc">
			<function>
				<table>
					<independentVar lookup="row">aero/alpha-deg</independentVar>
					<independentVar lookup="column">velocities/mach</independentVar>
					<tableData>
						    0      0.45   0.5    0.8    0.9    1
						 0  0.000  0.000  0.000  0.000  0.000  0.000
						 8  0.000  0.000  0.178  0.178  0.000  0.000
						10  0.245  0.245  0.315  0.315  0.000  0.000
						20  1.000  1.000  1.000  1.000  0.000  0.000
						25  1.000  1.000  1.000  1.000  0.000  0.000
					</tableData>
				</table>
			</function>
		</fcs_function>
		
		<switch name="fbw/slat/active">
			<default value="fbw/slat/active"/>
			<test logic="OR" value="0">
				aero/alpha-deg lt fbw/slat/retraction-aoa
				velocities/mach gt fbw/slat/partially-extended-max-mn
				gear/gear-pos-norm gt 0
			</test>
			<test logic="AND" value="1">
				aero/alpha-deg gt fbw/slat/extension-aoa
				velocities/mach le fbw/slat/fully-extended-max-mn
				gear/gear-pos-norm le 0.3
			</test>
		</switch>
		
		<fcs_function name="fbw/slat/auto-cmd"> <!-- Prevent manual slat from going less than auto -->
			<function>
				<max>
					<property>fbw/slat/extension-calc</property>
					<property>/controls/flight/flaps</property>
				</max>
			</function>
		</fcs_function>
		
		<switch name="fbw/slat-output">
			<default value="fbw/slat/extension-calc"/>
			<test logic="OR" value="/controls/flight/flaps">
				gear/gear-pos-norm gt 0.7
				fbw/man-slat gt 0
				fbw/enable ne 1
				/controls/flight/flaps gt 0
				fbw/slat/active le 0
			</test>
		</switch>
	
	</channel>
	
	<channel name="Elevons">
		
		<fcs_function name="fcs/elevon-droop">
			<function>
				<table>
					<independentVar lookup="row">velocities/vc-kts</independentVar>
					<tableData>
						62  16
						74   0
					</tableData>
				</table>
			</function>
		</fcs_function>
		
		<switch name="fcs/roll-input">
			<default value="fbw/roll-output"/>
			<test value="fbw/roll/aileron-nul">
				fbw/enable eq 0
			</test>
			<clipto>
				<min>-1.0</min>
				<max>1.0</max>
			</clipto>
		</switch>
		
		<switch name="fcs/roll-inner-input">
			<default value="fbw/roll-inner-output"/>
			<test value="fbw/roll/aileron-nul">
				fbw/enable eq 0
			</test>
			<clipto>
				<min>-1.0</min>
				<max>1.0</max>
			</clipto>
		</switch>
		
		<switch name="fcs/pitch-input">
			<default value="fbw/pitch-output"/>
			<test value="fcs/elevator-sum">
				fbw/enable eq 0
			</test>
			<clipto>
				<min>-1.0</min>
				<max>1.0</max>
			</clipto>
		</switch>
		
		<pure_gain name="fcs/roll-pos-deg">
			<input>fcs/roll-input</input>
			<gain>25.0</gain>
		</pure_gain>
		
		<pure_gain name="fcs/roll-inner-pos-deg">
			<input>fcs/roll-inner-input</input>
			<gain>25.0</gain>
		</pure_gain>
		
		<pure_gain name="fcs/pitch-pos-deg">
			<input>fcs/pitch-input</input>
			<gain>25.0</gain>
			<clipto>
				<min>-25.0</min>
				<max>16.0</max>
			</clipto>
		</pure_gain>
		
		<fcs_function name="fcs/elevon-1L-cmd-deg">
			<function>
				<ifthen>
					<eq>
						<property>systems/hydraulics/number2-system-available</property>
						<value>1</value>
					</eq>
					<sum>
						<property>fcs/roll-pos-deg</property>
						<property>fcs/pitch-pos-deg</property>
					</sum>
					<property>fcs/elevon-droop</property>
				</ifthen>
			</function>
			<clipto>
				<min>-25.0</min>
				<max>16.0</max>
			</clipto>
		</fcs_function>
		
		<actuator name="fcs/elevon-1L-pos-deg">
			<input>fcs/elevon-1L-cmd-deg</input>
			<rate_limit>100</rate_limit>
			<lag>26.7</lag>
		</actuator>
		
		<fcs_function name="fcs/elevon-1R-cmd-deg">
			<function>
				<ifthen>
					<eq>
						<property>systems/hydraulics/number2-system-available</property>
						<value>1</value>
					</eq>
					<sum>
						<product>
							<property>fcs/roll-pos-deg</property>
							<value>-1</value>
						</product>
						<property>fcs/pitch-pos-deg</property>
					</sum>
					<property>fcs/elevon-droop</property>
				</ifthen>
			</function>
			<clipto>
				<min>-25.0</min>
				<max>16.0</max>
			</clipto>
		</fcs_function>
		
		<actuator name="fcs/elevon-1R-pos-deg">
			<input>fcs/elevon-1R-cmd-deg</input>
			<rate_limit>100</rate_limit>
			<lag>26.7</lag>
		</actuator>
		
		<fcs_function name="fcs/elevon-2L-cmd-deg">
			<function>
				<ifthen>
					<eq>
						<property>systems/hydraulics/number1-system-available</property>
						<value>1</value>
					</eq>
					<sum>
						<property>fcs/roll-inner-pos-deg</property>
						<property>fcs/pitch-pos-deg</property>
					</sum>
					<property>fcs/elevon-droop</property>
				</ifthen>
			</function>
			<clipto>
				<min>-25.0</min>
				<max>16.0</max>
			</clipto>
		</fcs_function>
		
		<actuator name="fcs/elevon-2L-pos-deg">
			<input>fcs/elevon-2L-cmd-deg</input>
			<rate_limit>100</rate_limit>
			<lag>26.7</lag>
		</actuator>
		
		<fcs_function name="fcs/elevon-2R-cmd-deg">
			<function>
				<ifthen>
					<eq>
						<property>systems/hydraulics/number1-system-available</property>
						<value>1</value>
					</eq>
					<sum>
						<product>
							<property>fcs/roll-inner-pos-deg</property>
							<value>-1</value>
						</product>
						<property>fcs/pitch-pos-deg</property>
					</sum>
					<property>fcs/elevon-droop</property>
				</ifthen>
			</function>
			<clipto>
				<min>-25.0</min>
				<max>16.0</max>
			</clipto>
		</fcs_function>
		
		<actuator name="fcs/elevon-2R-pos-deg">
			<input>fcs/elevon-2R-cmd-deg</input>
			<rate_limit>100</rate_limit>
			<lag>26.7</lag>
		</actuator>
	
	</channel>
	
	<channel name="Rudder">
		
		<switch name="fcs/yaw-input">
			<default value="fbw/yaw-output"/>
			<test logic="OR" value="fcs/rudder-sum">
				fbw/enable eq 0
				fbw/man-yaw eq 1
			</test>
			<clipto>
				<min>-1.0</min>
				<max>1.0</max>
			</clipto>
		</switch>
		
		<pure_gain name="fcs/yaw-input-deg">
			<input>fcs/yaw-input</input>
			<gain>25.0</gain>
		</pure_gain>
		
		<switch name="fcs/yaw-input-deg-hyd">
			<default value="0"/>
			<test value="fcs/yaw-input-deg">
				systems/hydraulics/number2-system-available eq 1
			</test>
		</switch>
		
		<actuator name="rubbish/rudder-pos-deg">
			<input>fcs/yaw-input-deg-hyd</input>
			<rate_limit>95</rate_limit>
			<lag>24.7</lag>
			<output>fcs/rudder-pos-deg</output>
		</actuator>
	
	</channel>
	
	<channel name="Nose Wheel">
		
		<switch name="fcs/dirav-enabled">
			<default value="0"/>
			<test logic="AND" value="1">
				/controls/flight/nws-enabled eq 1
				velocities/vtrue-kts le 40
				systems/hydraulics/number2-system-available eq 1
			</test>
		</switch>
		
		<fcs_function name="fcs/steer-function">
			<function>
				<product>
					<property>fcs/dirav-enabled</property>
					<table>
						<independentVar lookup="row">velocities/vtrue-kts</independentVar>
						<independentVar lookup="column">/controls/flight/rudder</independentVar>
						<tableData>
								   -1     0    1
							 0.500  00.0  0.0  00.0
							 1.000 -45.0  0.0  45.0
						</tableData>
					</table>
				</product>
			</function>
		</fcs_function>
		
		<actuator name="fcs/steer-deg">
			<input>fcs/steer-function</input>
			<rate_limit>fcs/rudder-rate</rate_limit>
			<output>fcs/steer-pos-deg</output>
		</actuator>
	
	</channel>
	
	<channel name="Slats">
		
		<switch name="fcs/slat-pos-rate">
			<default value="0.0"/>
			<test value="0.512612418">
				systems/hydraulics/number1-system-available eq 1
			</test>
		</switch>
		
		<actuator name="fcs/slat-a">
			<input>fbw/slat-output</input>
			<rate_limit>fcs/slat-pos-rate</rate_limit>
		</actuator>
		
		<pure_gain name="fcs/slat-outer">
			<input>fcs/slat-a</input>
			<gain>30.0</gain>
			<output>fcs/slat-1L-pos-deg</output>
			<output>fcs/slat-1R-pos-deg</output>
		</pure_gain>
		
		<aerosurface_scale name="fcs/slat-outer-norm">
			<input>fcs/slat-outer</input>
			<domain>
				<min>0.0</min>
				<max>30.0</max>
			</domain>
			<range>
				<min>0.0</min>
				<max>1.0</max>
			</range>
		</aerosurface_scale>
		
		<pure_gain name="fcs/slat-inner">
			<input>fcs/slat-a</input>
			<gain>17.5</gain>
			<output>fcs/slat-2L-pos-deg</output>
			<output>fcs/slat-2R-pos-deg</output>
		</pure_gain>
		
		<aerosurface_scale name="fcs/slat-inner-norm">
			<input>fcs/slat-inner</input>
			<domain>
				<min>0.0</min>
				<max>17.5</max>
			</domain>
			<range>
				<min>0.0</min>
				<max>1.0</max>
			</range>
		</aerosurface_scale>
		
		<summer name="fcs/slat-norm-sum">
			<input>fcs/slat-outer-norm</input>
			<input>fcs/slat-inner-norm</input>
		</summer>
	
	</channel>
	
	<channel name="Landing Gear">
		
		<switch name="fcs/gear-cmd-norm">
			<default value="gear/gear-pos-norm"/> <!-- hyd not avail, and emergency extension not commanded = stuck at present position -->
			<test logic="OR" value="1">
				gear/gear-emergency-extension eq 1
			</test>
			<test  logic="OR" value="gear/gear-cmd-norm">
				systems/hydraulics/number1-system-available eq 1
				systems/hydraulics/number2-system-available eq 1 <!-- secours extension -->
			</test>
		</switch>
		
		<kinematic name="rubbish/gear-pos-norm">
			<input>fcs/gear-cmd-norm</input>
			<traverse>
				<setting>
					<position>0.0</position>
					<time>0.0</time>
				</setting>
				<setting>
					<position>1.0</position>
					<time>2.5</time>
				</setting>
			</traverse>
			<output>gear/gear-pos-norm</output>
		</kinematic>
	
	</channel>
	
	<channel name="Brakes">
		
		<switch name="gear/brake-parking">
			<default value="/controls/gear/brake-parking"/>
			<test logic="OR" value="0">
				systems/hydraulics/number2-system-available eq 0
			</test>
		</switch>
		
		<switch name="gear/brake-left">
			<default value="/controls/gear/brake-left"/>
			<test logic="OR" value="0">
				systems/hydraulics/number1-system-available eq 0
				systems/hydraulics/number2-system-available eq 0
			</test>
		</switch>
		
		<switch name="gear/brake-right">
			<default value="/controls/gear/brake-right"/>
			<test logic="OR" value="0">
				systems/hydraulics/number1-system-available eq 0
				systems/hydraulics/number2-system-available eq 0
			</test>
		</switch>
		
		<summer name="fcs/left-brake-summer">
			<input>gear/brake-left</input>
			<input>gear/brake-parking</input>
			<clipto>
				<min>0</min>
				<max>1</max>
			</clipto>
		</summer>
		
		<switch name="rubbish/left-brake-cmd-norm">
			<default value="fcs/left-brake-summer"/>
			<output>fcs/left-brake-cmd-norm</output>
		</switch>
		
		<summer name="fcs/right-brake-summer">
			<input>gear/brake-right</input>
			<input>gear/brake-parking</input>
			<clipto>
				<min>0</min>
				<max>1</max>
			</clipto>
		</summer>
		
		<switch name="rubbish/right-brake-cmd-norm">
			<default value="fcs/right-brake-summer"/>
			<output>fcs/right-brake-cmd-norm</output>
		</switch>
	</channel>
	
	<channel name="Airbrake">
		
		<switch name="fcs/airbrake-upper-hyd">
			<default value="0"/>
			<test logic="OR" value="/controls/flight/speedbrake">
				systems/hydraulics/number1-system-available eq 1
			</test>
		</switch>
		
		<kinematic name="fcs/airbrake-upper-k">
			<input>fcs/airbrake-upper-hyd</input>
			<traverse>
				<setting>
					<position>0</position>
					<time>0.0</time>
				</setting>
				<setting>
					<position>1</position>
					<time>3.0</time>
				</setting>
			</traverse>
		</kinematic>
		
		<pure_gain name="fcs/airbrake-upper">
			<input>fcs/airbrake-upper-k</input>
			<gain>55.0</gain>
		</pure_gain>
		
		<aerosurface_scale name="fcs/airbrake-upper-norm">
			<input>fcs/airbrake-upper</input>
			<domain>
				<min>0.0</min>
				<max>55.0</max>
			</domain>
			<range>
				<min>0.0</min>
				<max>1.0</max>
			</range>
		</aerosurface_scale>
		
		<switch name="fcs/airbrake-lower-hyd">
			<default value="0"/>
			<test logic="OR" value="/controls/flight/speedbrake">
				systems/hydraulics/number1-system-available eq 1
			</test>
		</switch>
		
		<kinematic name="fcs/airbrake-lower-k">
			<input>fcs/airbrake-lower-hyd</input>
			<traverse>
				<setting>
					<position>0</position>
					<time>0.0</time>
				</setting>
				<setting>
					<position>1</position>
					<time>2.0</time>
				</setting>
			</traverse>
		</kinematic>
		
		<pure_gain name="fcs/airbrake-lower">
			<input>fcs/airbrake-lower-k</input>
			<gain>68.0</gain>
		</pure_gain>
		
		<aerosurface_scale name="fcs/airbrake-lower-norm">
			<input>fcs/airbrake-lower</input>
			<domain>
				<min>0.0</min>
				<max>68.0</max>
			</domain>
			<range>
				<min>0.0</min>
				<max>1.0</max>
			</range>
		</aerosurface_scale>
		
		<summer name="fcs/airbrake-norm-sum">
			<input>fcs/airbrake-upper-norm</input>
			<input>fcs/airbrake-lower-norm</input>
		</summer>
	
	</channel>

</system>
