<?xml version="1.0"?>
<system name="Dragonfly Instruments">
	
	<channel name="Engine Controls">
		
		<switch>
			<default value="0"/>
			<test value="1" logic="AND">
				/controls/engines/engine/left-magneto-switch eq 1
				/controls/engines/engine/right-magneto-switch eq 0
			</test>
			<test value="2" logic="AND">
				/controls/engines/engine/left-magneto-switch eq 0
				/controls/engines/engine/right-magneto-switch eq 1
			</test>
			<test value="3" logic="AND">
				/controls/engines/engine/left-magneto-switch eq 1
				/controls/engines/engine/right-magneto-switch eq 1
			</test>
			<output>/controls/engines/engine/magnetos</output>
		</switch>
		
		<!--	The Rotax 582 does not have a mixture control. Mixture is automatically controlled by a "HAC - High Altitude Compensation" system. This is - very basically - simulated here: -->
		<fcs_function>
			<function>
				<table>
					<independentVar>atmosphere/pressure-altitude</independentVar>
					<tableData>
						0	1
						30000	0.36
					</tableData>
				</table>
			</function>
			<output>fcs/mixture-cmd-norm</output>
			<output>fcs/mixture-pos-norm</output>
		</fcs_function>
	
	</channel>
	
	<channel name="Switches">
		<lag_filter>
			<input>/controls/electric/master-switch</input>
			<c1>15</c1>
			<output>/controls/electric/master-switch-pos</output>
		</lag_filter>
		<lag_filter>
			<input>/controls/engines/engine[0]/left-magneto-switch</input>
			<c1>15</c1>
			<output>/controls/engines/engine[0]/left-magneto-switch-pos</output>
		</lag_filter>
		<lag_filter>
			<input>/controls/engines/engine[0]/right-magneto-switch</input>
			<c1>15</c1>
			<output>/controls/engines/engine[0]/right-magneto-switch-pos</output>
		</lag_filter>
		<lag_filter>
			<input>/controls/switches/starter</input>
			<c1>15</c1>
			<output>/controls/switches/starter-pos</output>
		</lag_filter>
	</channel>
	
	<!-- Engine Temperatures Simulation based on DR400 (Ecoflyer) -->
	<channel name="Engine Temperature">
		<!-- Concept:
				Engine temperature is dependent on ambient temperature and manifold pressure
				Engine is cooled by oil which is cooled by coolant as well as air
		-->
		<fcs_function>
			<function>
				<difference>
					<sum>
						<product>
							<property>propulsion/engine[0]/map-inhg</property>
							<value>1.2</value>
						</product>
						<value>59</value>
					</sum>
					<property>propulsion/engine[0]/engine-temp-c</property>
				</difference>
			</function>
			<output>propulsion/engine[0]/engine-heating</output>
		</fcs_function>		
		<summer>
			<input>propulsion/engine[0]/engine-heating</input>
			<input>-propulsion/engine[0]/oil-heating</input>
			<output>propulsion/engine[0]/delta-et-running</output>
		</summer>
		<switch name="propulsion/engine[0]/delta-et">
			<default value="0"/>
			<test value="propulsion/engine[0]/delta-et-running">
				propulsion/engine[0]/set-running eq 1
			</test>
			<test value="-0.2">
				propulsion/engine[0]/engine-temp-c gt /environment/temperature-degc
				propulsion/engine[0]/set-running eq 0
			</test>
			<test value="0.2">
				propulsion/engine[0]/engine-temp-c le /environment/temperature-degc
				propulsion/engine[0]/set-running eq 0
			</test>
		</switch>
		<integrator>
			<input>propulsion/engine[0]/delta-et</input>
			<c1>1</c1>
			<output>propulsion/engine[0]/engine-temp-c</output>
		</integrator>
		
	</channel>
	
	<property value="1.0">propulsion/engine[0]/coolant-level-norm</property>
	
	<channel name="Coolant Temperature">
		<!-- Coolant is
				heated by:	oil
				cooled by:	air
		-->
		<fcs_function>
			<function>
				<sum>
					<product>
						<property>velocities/vc-kts</property>
						<value>-0.1</value>
					</product>
					<value>-1.0</value>
				</sum>
			</function>
			<output>propulsion/engine[0]/coolant-cooling</output>
		</fcs_function>
		<fcs_function>
			<function>
				<product>
					<property>propulsion/engine[0]/oil-cooling</property>
					<property>propulsion/engine[0]/coolant-level-norm</property>
					<value>-1.5</value>
				</product>
			</function>
			<output>propulsion/engine[0]/coolant-heating</output>
		</fcs_function>			
		<summer>
			<input>propulsion/engine[0]/coolant-heating</input>
			<input>propulsion/engine[0]/coolant-cooling</input>
			<output>propulsion/engine[0]/delta-ct-running</output>
		</summer>
		<switch name="propulsion/engine[0]/delta-ct">
			<default value="0"/>
			<test value="propulsion/engine[0]/delta-ct-running">
				propulsion/engine[0]/set-running eq 1
			</test>
			<test value="-0.2">
				propulsion/engine[0]/coolant-temp-c gt /environment/temperature-degc
				propulsion/engine[0]/set-running eq 0
			</test>
			<test value="0.2">
				propulsion/engine[0]/coolant-temp-c le /environment/temperature-degc
				propulsion/engine[0]/set-running eq 0
			</test>
		</switch>
		<integrator>
			<input>propulsion/engine[0]/delta-ct</input>
			<c1>1</c1>
			<output>propulsion/engine[0]/coolant-temp-c</output>
			<output>/engines/engine[0]/coolant-temp-c</output>
		</integrator>
	</channel>
	
	<channel name="Oil Temperature">
		<!-- Oil is
				heated by:	engine
				cooled by:	coolant
		-->
		<fcs_function>
			<function>
				<product>
					<difference>
						<property>propulsion/engine[0]/coolant-temp-c</property>
						<property>/engines/engine[0]/oil-temp-c</property>
					</difference>
					<value>0.3</value>
				</product>
			</function>
			<output>propulsion/engine[0]/oil-cooling</output>
		</fcs_function>
		<fcs_function>
			<function>
				<product>
					<difference>
						<property>propulsion/engine[0]/engine-temp-c</property>
						<property>/engines/engine[0]/oil-temp-c</property>
					</difference>
					<value>0.9</value>
				</product>
			</function>
			<output>propulsion/engine[0]/oil-heating</output>
		</fcs_function>
		<summer name="propulsion/engine[0]/delta-ot-running">
			<input>propulsion/engine[0]/oil-heating</input>
			<input>propulsion/engine[0]/oil-cooling</input>
		</summer>
		<switch name="propulsion/engine[0]/delta-ot">
			<default value="0"/>
			<test value="propulsion/engine[0]/delta-ot-running">
				/engines/engine[0]/oil-temp-c lt propulsion/engine[0]/engine-temp-c
				propulsion/engine[0]/set-running eq 1
			</test>
			<test value="-0.5">
				/engines/engine[0]/oil-temp-c gt /environment/temperature-degc
				propulsion/engine[0]/set-running eq 0
			</test>
			<test value="0.5">
				/engines/engine[0]/oil-temp-c le /environment/temperature-degc
				propulsion/engine[0]/set-running eq 0
			</test>
		</switch>
		<integrator>
			<input>propulsion/engine[0]/delta-ot</input>
			<c1>1</c1>
			<output>propulsion/engine[0]/oil-temp-c</output>
			<output>/engines/engine[0]/oil-temp-c</output>
		</integrator>
	</channel>
	
	<channel name="Engine Instruments">
		<fcs_function name="systems/indicators/coolant-raw">
			<function>
				<table>
					<independentVar lookup="row">/engines/engine[0]/coolant-temp-c</independentVar>
					<independentVar lookup="column">/systems/electrical/outputs/eis</independentVar>
					<tableData>
							0	8.9	9
						0	-10	-10	0
						60	-10	-10	0
						120	-10	-10	78.85
					</tableData>
				</table>
			</function>
		</fcs_function>
		<lag_filter>
			<input>systems/indicators/coolant-raw</input>
			<c1>12.0</c1>
			<output>systems/indicators/coolant-needle</output>
		</lag_filter>
	</channel>    
	
	<channel name="Effects">
		<pure_gain>
			<input>propulsion/tank[0]/pct-full</input>
			<gain>0.01</gain>
			<output>/consumables/fuel/tank[0]/level-norm-untied</output>
		</pure_gain>
		<fcs_function>
			<function>
				<atan>
					<quotient>
						<property>accelerations/Nx</property>
						<property>accelerations/Nz</property>
					</quotient>
				</atan>
			</function>
			<output>/sim/model/fuel-tank-effect/pitch-rad</output>
		</fcs_function>
		<fcs_function>
			<function>
				<product>
					<atan>
						<quotient>
							<property>accelerations/Ny</property>
							<property>accelerations/Nz</property>
						</quotient>
					</atan>
					<value>-1</value>
				</product>
			</function>
			<output>/sim/model/fuel-tank-effect/roll-rad</output>
		</fcs_function>
		<fcs_function>
			<function>
				<sum>
					<property>velocities/vc-kts</property>
					<product>
						<property>propulsion/engine[0]/prop-induced-velocity_fps</property>
						<value>0.592483801</value>
					</product>
				</sum>
			</function>
			<output>/sim/model/wind-speed-stage1</output>
		</fcs_function>
		<lag_filter>
			<input>/sim/model/wind-speed-stage1</input>
			<c1>3</c1>
			<output>/sim/model/wind-speed-stage2</output>
		</lag_filter>
		<integrator>
			<input>/sim/model/wind-speed-stage2</input>
			<c1>1</c1>
			<output>/sim/model/wind-integrator</output>
		</integrator>
	</channel>
</system>
