<?xml version="1.0" encoding="utf-8"?>

<Gauge Name="RealEngine - Automixture Control">

<!--
**********************************************************

Goal of the gauge:

Enables setting of Fuel-Air Mixture Ratio (MR).

Adjusts Mixture lever position (>K:MIXTURE1_SET) such that Mixture ratio is at setpoint value 
as defined by variable (>L:RE1_SetMR_TargetMR,part) (see below in "Effect Calls" section).

0 = Manual mixture control.
]0-1[ = Mixture Ratio Setpoint (part). MR setpoint can be defined externally, e.g. max power mixture is 0.083.
1 = Full Rich 			Sets mixture to full rich.
2 = Cutoff				Sets mixture to full lean.

Mixture lever position to reach a given MR setpoint is determined from lookup-table listing

* Mixture lever position (A:GENERAL ENG MIXTURE LEVER POSITION:1,percent)

vs

* Variable "A" = Rho * MR = (A:AMBIENT DENSITY,kilogram per cubic meter) * (A:RECIP MIXTURE RATIO:1,percent)

	Rho*MR is a relative measure of how fuel flow into the carburetor changes with varying mixture lever position, 
	probably controlled by a table in the aircraft.cfg (TBC).

For most aircraft the relation between Mixture lever position and "A" is probably the same I'd guess (TBC!!)
(because acft designer didn't change default table in aircraft.cfg (?) ), 
but there are probably some acft where that table has been changed. 

If mixture is not properly adjusted to the desired setpoint by the gauge you have two options:
a) Disable the gauge (by commenting out // in the panel.cfg for example) and live without automatic mixture control.
b) Or run tests of the aircraft yourself, and change the lookup table below. It's not very difficult, 
but you'll need some experience with xml programming/ data analysis to edit the lookup table below.
Perform as follows:
Sit at any airport and measure the following parameters with the display gauge that is in the RE folder:
(A:GENERAL ENG MIXTURE LEVER POSITION:1,percent)
(A:RECIP MIXTURE RATIO:1,percent)
(A:AMBIENT DENSITY,kilogram per cubic meter)
Change throttle lever position and note down the Mixture ratio that you obtain for several throttle lever positions between 0 and 100% (about 10 - 15 positions).
No need to change (A:AMBIENT DENSITY,kilogram per cubic meter). One atmospheric condition is enough.
Multiply the obtained (A:RECIP MIXTURE RATIO:1,percent) x (A:AMBIENT DENSITY,kilogram per cubic meter) values to obtain Values "A".
Plot the MIXTURE LEVER POSITION against value "A" in a graph, and extract a few significant data pairs that well describe the curve.
Enter these data pairs into the lookup table below.
If this sounds complicated better leave it, and completely turn off the automixture control gauge.
But I believe most aircraft should work well with the default lookup table, and not need any fiddling.

teson1, 2012

**********************************************************
-->

<Update Frequency="18"/> 

<Size X="1" Y="1"/>


<Element>
<Select>
<Value> 

<!-- ********************************************* EFFECT CALLS ************************************************ -->

	(L:RE_AutoMixCP_TargetMR,part) 

	(>L:RE1_SetMR_TargetMR,part) 
	
</Value>
</Select>
</Element>

<!-- ******************************************** Lookup table MIXTURE LEVER POSITION vs "A" ******************************************************* -->

<Macro Name="A_MixLeverPos_LookupData">	
<!-- Number of data pairs in lookup table --> 4 	(>L:RE1_SetMR_LUTNumberDataPairs,enum) 
<!-- "A" value data (Unit: "kg/m3 * %") -->	
	0.0 6.0 9.9 10.6 (L:RE1_SetMR_LUTNumberDataPairs,enum) @1 case (>L:RE1_SetMR_An,number)  
<!-- Lever position data (%) -->	
	0.0 21.0 64.0 100.0 (L:RE1_SetMR_LUTNumberDataPairs,enum) @1 case (>L:RE1_SetMR_Ln,percent) 

<!-- Notes:
	"A" values have to start with the value "0.0" (i.e. for MR=0%)
-->
</Macro>

<!-- ******************************************** CALCULATIONS ******************************************************* -->

<Element>
<Select>
<Value>

	<!-- Show module is loaded -->

	1 (>L:RE1_SetMR_Loaded,bool) 
	
	<!-- Determine real mix lever position -->
	
	-2 2 (A:GENERAL ENG MIXTURE LEVER POSITION:1,percent) (L:RE1_SetMR_PreviousMixLeverSetTo,percent) - rng ! 
	if{ (A:GENERAL ENG MIXTURE LEVER POSITION:1,percent) (>L:RE1_SetMR_MixLvrRealPosition,percent) } 

<!-- Determine operating mode by externally setting value of variable (L:RE1_SetMR_TargetMR,part).
	0 = Manual mixture control.
	]0-1[ = Mixture Ratio Setpoint (part). MR setpoint can be defined externally, e.g. max power mixture is 0.083
	1 = Emergency Rich ER. 	Sets mixture to full rich.
-->

	(L:RE1_SetMR_TargetMR,part) 0 &gt; ! 
	(A:GENERAL ENG MIXTURE LEVER POSITION:1,percent) 5 &lt; || 
	if{ g0 } 
	els{ 
		(L:RE1_SetMR_TargetMR,part) 1 &lt; 
		if{ (L:RE1_SetMR_TargetMR,part) (>L:RE1_SetMR_MRsetpoint,part) } 
		els{ 
			(L:RE1_SetMR_TargetMR,part) 1 ==  
			if{ (>K:MIXTURE1_RICH) } 
			els{ 
				(L:RE1_SetMR_TargetMR,part) 2 ==  
				if{ (>K:MIXTURE1_LEAN) } 
			} 
			g0 
		} 
	} 

<!-- Determine Mixture Lever position required to reach Mixture Ratio setpoint -->

<!-- 1. Calculate A value of MR setpoint : A_setpoint -->

	(L:RE1_SetMR_MRsetpoint,percent) (A:AMBIENT DENSITY,kilogram per cubic meter) * 
	0 max 
	(>L:RE1_SetMR_Asetpoint,number) 

<!-- 2. Loop through A values of lookup table until interval containing A_setpoint is reached. -->

	:1

	@A_MixLeverPos_LookupData( (L:RE1_SetMR_LoopCounter,enum) ) 

	(L:RE1_SetMR_An,number) (>L:RE1_SetMR_An_1,number) 
	(L:RE1_SetMR_Ln,percent) (>L:RE1_SetMR_Ln_1,percent) 

	(L:RE1_SetMR_LoopCounter,enum) ++ (>L:RE1_SetMR_LoopCounter,enum) 

	@A_MixLeverPos_LookupData( (L:RE1_SetMR_LoopCounter,enum) ) 

	(L:RE1_SetMR_An,number) (L:RE1_SetMR_Asetpoint,number) &gt; 
	if{ g1 } 

<!-- 3. Calculate Mix lever position required. -->
<!-- 	Interpolate lever position from lookup data: 
		Lever position L = (Ln - Ln_1)/(An - An_1) * (A-An) + Ln 
-->

	(L:RE1_SetMR_Ln,percent) (L:RE1_SetMR_Ln_1,percent) - 
	(L:RE1_SetMR_An,number) (L:RE1_SetMR_An_1,number) - / 
	d (>L:RE1_SetMR_DataSlope,number)

	(L:RE1_SetMR_Asetpoint,number) (L:RE1_SetMR_An,number) - * 
	(L:RE1_SetMR_Ln,percent) + 

	0 max 
	100 min 

	(>L:RE1_SetMR_MixLeverSetTo,percent) 

<!-- Set mixture lever to setpoint -->

	(L:RE1_SetMR_MixLeverSetTo,percent) (A:GENERAL ENG MIXTURE LEVER POSITION:1,percent) - 
	(A:AMBIENT DENSITY,kilogram per cubic meter) / (L:RE1_SetMR_DataSlope,number) / 
	sp0 
	
	-0.05 0.05 l0 rng ! 
	if{ 		
		(L:RE1_SetMR_MixLeverSetTo,part) 16383 * (>K:MIXTURE1_SET) 
	} 

<!-- Clean up -->

	0 (>L:RE1_SetMR_LoopCounter,enum)

	(L:RE1_SetMR_TargetMR,part) 			(>L:RE1_SetMR_PreviousTargetMR,part) 
	(L:RE1_SetMR_MixLeverSetTo,percent) 	(>L:RE1_SetMR_PreviousMixLeverSetTo,percent)

	:0 

</Value>
</Select>
</Element>

</Gauge>
