<?xml version="1.0"?>

<!-- McDonnell Douglas DC-10 Honeywell TSCP700-4B APU -->
<!-- Copyright (c) 2026 Josh Davidson (Octal450) -->

<system name="DC-10: APU">
	
	<channel name="APU Logic">
		
		<switch name="/systems/apu/dc-fuel-pump">
			<default value="0"/>
			<test value="1">
				/controls/apu/master ge 1
			</test>
		</switch>
		
		<switch name="/systems/apu/throttle-cmd-norm">
			<default value="0"/> <!-- Idle N1 -->
			<test value="0"> <!-- Force Idle N1 -->
				/controls/apu/master eq 0
			</test>
			<test logic="AND" value="1"> <!-- Max N1 -->
				<test logic="OR">
					/systems/electrical/relay/apu-ac-gen-1/contact-pos eq 1
					/systems/electrical/relay/apu-ac-gen-2/contact-pos eq 1
					/systems/electrical/relay/apu-ac-gen-3/contact-pos eq 1
				</test>
				/engines/engine[0]/state ne 3
			</test>
			<output>fcs/throttle-pos-norm[3]</output>
		</switch>
	
	</channel>
	
	<channel name="APU Start/Stop"> <!-- Do NOT execrate! -->
		
		<switch name="/systems/apu/door-cmd">
			<default value="0"/>
			<test value="/systems/apu/door-cmd">
				/systems/electrical/bus/dc-bat lt 24
			</test>
			<test logic="OR" value="1">
				/controls/apu/master ge 1
				/engines/engine[3]/n2-actual ge 2
			</test>
		</switch>
		
		<actuator name="/systems/apu/door-norm">
			<input>/systems/apu/door-cmd</input>
			<rate_limit>0.5</rate_limit> <!-- 2 seconds? Guess -->
		</actuator>
		
		<switch name="/systems/apu/cutoff-cmd">
			<default value="1"/>
			<test logic="AND" value="0">
				/controls/apu/master ge 1
				/engines/engine[3]/n2-actual ge 10
			</test>
		</switch>
		
		<switch name="/systems/apu/cutoff">
			<default value="1"/>
			<test logic="AND" value="0">
				/controls/apu/master ge 1
				/systems/acconfig/autoconfig-running eq 1
			</test>
			<test logic="AND" value="0">
				/systems/apu/cutoff-cmd eq 0
				propulsion/tank[10]/contents-lbs ge 1
				/systems/apu/door-norm eq 1
			</test>
		</switch>
		
		<switch name="/systems/apu/starter-avail">
			<default value="0"/>
			<test logic="AND" value="1">
				/systems/electrical/bus/dc-bat ge 24
				propulsion/tank[10]/contents-lbs ge 1
				/systems/apu/door-norm eq 1
			</test>
		</switch>
		
		<pure_gain name="/systems/apu/starter-run-start">
			<input>/controls/apu/master</input>
			<gain>1.0</gain>
			<clipto>
				<min>1</min>
				<max>2</max>
			</clipto>
		</pure_gain>
		
		<actuator name="/systems/apu/starter-run-start-timer">
			<input>/systems/apu/starter-run-start</input>
			<rate_limit sense="incr">0.5</rate_limit> <!-- 2 seconds -->
			<rate_limit sense="decr">10000</rate_limit> <!-- Instant -->
		</actuator>
		
		<switch name="/systems/apu/starter-latch">
			<default value="/systems/apu/starter-latch"/>
			<test value="0">
				/controls/apu/master eq 0
			</test>
			<test logic="AND" value="1">
				/controls/apu/master eq 2
				/systems/apu/starter-run-start-timer eq 2
				/systems/apu/starter-avail eq 1
			</test>
		</switch>
		
		<switch name="/systems/apu/starter">
			<default value="0"/>
			<test logic="AND" value="1">
				<test logic="OR">
					/controls/apu/master eq 2
					/systems/apu/starter-latch eq 1
				</test>
				/systems/apu/starter-avail eq 1
			</test>
		</switch>
		
		<switch name="/systems/apu/state">
			<default value="0"/>
			<test logic="AND" value="3">
				/controls/apu/master ge 1
				/systems/apu/cutoff eq 0
				/engines/engine[3]/n2-actual ge 95
			</test>
			<test logic="AND" value="2">
				/controls/apu/master ge 1
				/engines/engine[3]/n2-actual ge 16
				/systems/apu/cutoff eq 0
				/systems/apu/starter eq 1
			</test>
			<test logic="AND" value="1">
				/controls/apu/master ge 1
				/systems/apu/starter eq 1
			</test>
			<output>/engines/engine[3]/state</output>
		</switch>
	
	</channel>
	
	<channel name="APU Parameters" execrate="2">
		
		<lag_filter name="/systems/apu/n1-actual">
			<input>/engines/engine[3]/n1</input>
			<c1>2.25</c1>
			<output>/engines/engine[3]/n1-actual</output>
		</lag_filter>
		
		<fcs_function name="/systems/apu/egt-fake-n2"> <!-- Blend N1 and N2 -->
			<function>
				<sum>
					<property>/engines/engine[3]/n2</property>
					<table>
						<independentVar lookup="row">/engines/engine[3]/n1</independentVar>
						<tableData>
							 92   0
							100  10
						</tableData>
					</table>
				</sum>
			</function>
		</fcs_function>
		
		<fcs_function name="/systems/apu/egt-cmd">
			<function>
				<sum>
					<property>propulsion/tat-c</property>
					<table>
						<independentVar lookup="row">/systems/apu/egt-fake-n2</independentVar>
						<independentVar lookup="column">atmosphere/density-altitude</independentVar>
						<independentVar lookup="table">/controls/engines/engine[3]/cutoff</independentVar>
						<tableData breakPoint="0">
							       0    50000
							 16    0    0
							 40  460  355
							 50  505  400
							 55  535  425
							 65  580  460
							 75  545  440
							 90  490  385
							100  363  272
							110  457  376
						</tableData>
						<tableData breakPoint="1">
							       0    50000
							 20    0    0
							100  363  272
							110  457  376
						</tableData>
					</table>
				</sum>
			</function>
		</fcs_function>
		
		<lag_filter name="/systems/apu/egt-actual">
			<input>/systems/apu/egt-cmd</input>
			<c1>0.35</c1>
			<output>/engines/engine[3]/egt-actual</output>
		</lag_filter>
		
		<lag_filter name="/systems/apu/n2-actual">
			<input>/engines/engine[3]/n2</input>
			<c1>2.25</c1>
			<output>/engines/engine[3]/n2-actual</output>
		</lag_filter>
		
		<lag_filter name="/systems/apu/ff-actual">
			<input>/engines/engine[3]/fuel-flow_pph</input>
			<c1>3.0</c1>
			<output>/engines/engine[3]/ff-actual</output>
		</lag_filter>
		
		<fcs_function name="/engines/engine[3]/oil-qty">
			<function>
				<sum>
					<property>/engines/engine[3]/oil-qty-input</property>
					<table>
						<independentVar lookup="row">/engines/engine[3]/n2-actual</independentVar>
						<tableData>
							20  0.0
							55 -1.5
						</tableData>
					</table>
				</sum>
			</function>
		</fcs_function>
	
	</channel>

</system>
