<?xml version="1.0"?>

<!-- S-TEC Thirty Autopilot System -->
<!-- 2019 Bea Wolf (D-ECHO) -->
<!-- based on S-TEC Fifty-Five X by Joshua Davidson (Octal450) -->

<!-- Reference: S-TEC Twenty | Thirty | Thirtly Alt Pilot's Operating Handbook https://genesys-aerosystems.com/sites/default/files/files/System%20Twenty_Thirty_%20ThirtyALT.pdf -->

<PropertyList>
	
	<!-- NAV Switching -->
	
	
	<filter>
		<name>NAV NEEDLE DEFLECTION</name>
		<type>gain</type>
		<gain>1.0</gain>
		<input>
			<property>/instrumentation/nav[0]/heading-needle-deflection</property>
		</input>
		<output>/stec30/nav/nav-needle</output>
	</filter>
	
	<logic>
		<name>NAV IN RANGE</name>
		<input>
			<or>
				<and>
					<equals>
						<property>/stec30/input/nav-selector</property>
						<value>0</value>
					</equals>
					<equals>
						<property>/instrumentation/nav[0]/in-range</property>
						<value>1</value>
					</equals>
				</and>
				<and>
					<equals>
						<property>/stec30/input/nav-selector</property>
						<value>1</value>
					</equals>
					<equals>
						<property>/instrumentation/nav[1]/in-range</property>
						<value>1</value>
					</equals>
				</and>
				<and>
					<equals>
						<property>/stec30/input/nav-selector</property>
						<value>2</value>
					</equals>
					<equals>
						<property>/stec30/custom-nav/in-range-1</property>
						<value>1</value>
					</equals>
				</and>
				<and>
					<equals>
						<property>/stec30/input/nav-selector</property>
						<value>3</value>
					</equals>
					<equals>
						<property>/stec30/custom-nav/in-range-2</property>
						<value>1</value>
					</equals>
				</and>
			</or>
		</input>
		<output>/stec30/nav/in-range</output>
	</logic>
	
	<logic>
		<name>NAV IS LOC</name>
		<input>
			<or>
				<and>
					<equals>
						<property>/stec30/input/nav-selector</property>
						<value>0</value>
					</equals>
					<equals>
						<property>/instrumentation/nav[0]/nav-loc</property>
						<value>1</value>
					</equals>
				</and>
				<and>
					<equals>
						<property>/stec30/input/nav-selector</property>
						<value>1</value>
					</equals>
					<equals>
						<property>/instrumentation/nav[1]/nav-loc</property>
						<value>1</value>
					</equals>
				</and>
			</or>
		</input>
		<output>/stec30/nav/is-loc</output>
	</logic>
	
	<filter>
		<name>NAV COURSE</name>
		<type>gain</type>
		<gain>1.0</gain>
		<input>
			<property>/instrumentation/nav[0]/radials/selected-deg</property>
		</input>
		<output>/stec30/nav/nav-course</output>
	</filter>
	
	<filter>
		<name>NAV COURSE RECIPROCAL STAGE 1</name>
		<debug>false</debug>
		<type>gain</type>
		<gain>1.0</gain>
		<input>
			<expression>
				<sum>
					<property>/stec30/nav/nav-course</property>
					<value>180</value>
				</sum>
			</expression>
		</input>
		<output>/stec30/nav/nav-course-reciprocal-cmd</output>
	</filter>
	
	<filter>
		<name>NAV COURSE RECIPROCAL STATE 2</name>
		<debug>false</debug>
		<type>gain</type>
		<gain>1.0</gain>
		<input>
			<condition>
				<greater-than-equals>
					<property>/stec30/nav/nav-course-reciprocal-cmd</property>
					<value>360.5</value>
				</greater-than-equals>
			</condition>
			<expression>
				<dif>
					<property>/stec30/nav/nav-course-reciprocal-cmd</property>
					<value>360</value>
				</dif>
			</expression>
		</input>
		<input>
			<condition>
				<less-than>
					<property>/stec30/nav/nav-course-reciprocal-cmd</property>
					<value>0.5</value>
				</less-than>
			</condition>
			<expression>
				<sum>
					<property>/stec30/nav/nav-course-reciprocal-cmd</property>
					<value>360</value>
				</sum>
			</expression>
		</input>
		<input>/stec30/nav/nav-course-reciprocal-cmd</input>
		<output>/stec30/nav/nav-course-reciprocal</output>
	</filter>
	
	<filter>
		<name>NAV COURSE ERROR</name>
		<type>gain</type>
		<gain>1.0</gain>
		<input>
			<condition>
				<or>
					<equals>
						<property>/stec30/output/roll</property>
						<value>3</value>
					</equals>
					<equals>
						<property>/stec30/output/roll</property>
						<value>4</value>
					</equals>
				</or>
			</condition>
			<property>/stec30/nav/nav-course</property>
			<offset>
				<property>/instrumentation/heading-indicator/indicated-heading-deg</property>
				<scale>-1.0</scale>
			</offset>
		</input>
		<input>0</input>
		<output>/stec30/nav/nav-course-error</output>
		<period>
			<min>-180</min>
			<max>180</max>
		</period>
	</filter>
	
	<!-- Roll Axis -->
	
	<filter>
		<name>TARGET INTERCEPT ANGLE P</name>
		<type>gain</type>
		<gain>12.5</gain>
		<input>
			<expression>
				<product>
					<property>/stec30/nav/nav-needle</property>
					<property>/stec30/internal/nav-gain</property>
				</product>
			</expression>
		</input>
		<output>/stec30/internal/intercept-angle-p</output>
		<min>-45</min>
		<max>45</max>
	</filter>
	
	<filter>
		<name>TARGET INTERCEPT ANGLE I</name> <!-- Integrate to cancel steady state error only if stabilized and captured -->
		<type>integrator</type>
		<gain>0.05</gain>
		<input>
			<condition>
				<and>
					<or>
						<equals>
							<property>/stec30/output/roll</property>
							<value>3</value>
						</equals>
						<equals>
							<property>/stec30/output/roll</property>
							<value>4</value>
						</equals>
					</or>
					<less-than-equals>
						<property>/stec30/nav/nav-course-error</property>
						<value>10</value>
					</less-than-equals>
					<greater-than-equals>
						<property>/stec30/nav/nav-course-error</property>
						<value>-10</value>
					</greater-than-equals>
					<less-than-equals>
						<property>/stec30/nav/nav-needle</property>
						<value>1</value>
					</less-than-equals>
					<greater-than-equals>
						<property>/stec30/nav/nav-needle</property>
						<value>-1</value>
					</greater-than-equals>
				</and>
			</condition>
			<expression>
				<product>
					<property>/stec30/nav/nav-needle</property>
					<property>/stec30/internal/nav-gain</property>
				</product>
			</expression>
		</input>
		<input>
			<expression>
				<product>
					<value>-75.0</value>
					<property>/stec30/internal/intercept-angle-i</property>
				</product>
			</expression>
		</input>
		<output>/stec30/internal/intercept-angle-i</output>
		<u_min>-5</u_min>
		<u_max>5</u_max>
	</filter>
	
	<filter>
		<name>TARGET INTERCEPT ANGLE D</name>
		<type>derivative</type>
		<input>
			<expression>
				<product>
					<property>/stec30/nav/nav-needle</property>
					<property>/stec30/internal/nav-gain</property>
				</product>
			</expression>
		</input>
		<reference>0</reference>
		<output>/stec30/internal/intercept-angle-d</output>
		<filter-time>45</filter-time>
		<min>-45</min>
		<max>45</max>
	</filter>
	
	<filter>
		<name>TARGET INTERCEPT ANGLE</name> <!-- Sum the above 3 controllers to make a manual PID controller -->
		<debug>false</debug>
		<type>gain</type>
		<gain>1.0</gain>
		<input>
			<expression>
				<sum>
					<property>/stec30/internal/intercept-angle-p</property>
					<property>/stec30/internal/intercept-angle-i</property>
					<property>/stec30/internal/intercept-angle-d</property>
				</sum>
			</expression>
		</input>
		<output>/stec30/internal/intercept-angle</output>
		<min>-45</min>
		<max>45</max>
	</filter>
	
	<filter>
		<name>HDG/NAV ERROR DEG</name>
		<debug>false</debug>
		<type>gain</type>
		<gain>1.0</gain>
		<input>
			<condition>
				<equals>
					<property>/stec30/output/roll</property>
					<value>2</value>
				</equals>
			</condition>
			<property>/stec30/input/hdg</property>
			<offset>
				<property>/instrumentation/heading-indicator/indicated-heading-deg</property>
				<scale>-1.0</scale>
			</offset>
		</input>
		<input>
			<condition>
				<or>
					<equals>
						<property>/stec30/output/roll</property>
						<value>3</value>
					</equals>
					<equals>
						<property>/stec30/output/roll</property>
						<value>4</value>
					</equals>
				</or>
			</condition>
			<expression>
				<sum>
					<property>/stec30/nav/nav-course</property>
					<property>/stec30/internal/intercept-angle</property>
				</sum>
			</expression>
			<offset>
				<property>/instrumentation/heading-indicator/indicated-heading-deg</property>
				<scale>-1.0</scale>
			</offset>
		</input>
		<input>0</input>
		<output>/stec30/internal/heading-error-deg</output>
		<period>
			<min>-180</min>
			<max>180</max>
		</period>
	</filter>
	
	<filter>
		<name>TARGET TURN RATE</name>
		<type>gain</type>
		<gain>1.0</gain><!--
		<input>
			<condition>
				<equals>
					<property>/stec30/output/roll</property>
					<value>1</value>
				</equals>
			</condition>
			<value>0</value>
		</input>-->
		<input>
			<condition>
				<greater-than>
					<property>/stec30/output/roll</property>
					<value>0</value>
				</greater-than>
			</condition>
			<expression>
				<table>
					<property>/stec30/internal/heading-error-deg</property>
					<entry><ind>-30</ind><dep>-1.0</dep></entry>
					<entry><ind> -5</ind><dep>-0.2</dep></entry>
					<entry><ind>  5</ind><dep> 0.2</dep></entry>
					<entry><ind> 30</ind><dep> 1.0</dep></entry>
				</table>
			</expression>
		</input>
		<input>0</input>
		<output>/stec30/internal/target-turn-rate</output>
		<min>/stec30/internal/min-turn-rate</min>
		<max>/stec30/internal/max-turn-rate</max>
	</filter>
	
	<predict-simple>
		<name>TURN RATE PREDICTOR</name>
		<debug>false</debug>
		<input>/instrumentation/turn-indicator/indicated-turn-rate</input>
		<output>/stec30/internal/predicted-turn-rate</output>
		<seconds>2.0</seconds>
		<filter-gain>0.0</filter-gain>
	</predict-simple>
	
	<filter>
		<name>IT-CONTROLLER: TARGET TURN RATE FILTER</name>
		<debug>false</debug>
		<type>noise-spike</type>
		<input>
			<condition>
				<greater-than>
					<property>/stec30/output/roll</property>
					<value>1</value>
				</greater-than>
			</condition>
			<property>/stec30/internal/target-turn-rate</property>
		</input>
		<input>/stec30/internal/predicted-turn-rate</input>
		<output>/stec30/internal/turn-rate-cmd</output>
		<max-rate-of-change>
			<condition>
				<greater-than>
					<property>/stec30/output/roll</property>
					<value>1</value>
				</greater-than>
			</condition>
			<value>0.2</value>
		</max-rate-of-change>
		<max-rate-of-change>10</max-rate-of-change>
	</filter>
	
	<pi-simple-controller>
		<name>IT-CONTROLLER: ROLL PI</name>
		<debug>false</debug>
		<enable>
			<condition>
				<and>
					<greater-than>
						<property>/stec30/output/roll</property>
						<value>1</value>
					</greater-than>
					<equals>
						<property>/stec30/internal/servo-roll-power</property>
						<value>1</value>
					</equals>
				</and>
			</condition>
		</enable>
		<input>/stec30/internal/turn-rate-cmd</input>
		<reference>/stec30/internal/predicted-turn-rate</reference>
		<output>/stec30/internal/aileron-pi</output>
		<config>
			<Kp>-0.07</Kp>
			<Ki>-0.021</Ki>
			<min>-0.25</min>
			<max>0.25</max>
		</config>
	</pi-simple-controller>
	
	<filter>
		<name>IT-CONTROLLER: ROLL D</name>
		<enable>
			<condition>
				<and>
					<greater-than>
						<property>/stec30/output/roll</property>
						<value>1</value>
					</greater-than>
					<equals>
						<property>/stec30/internal/servo-roll-power</property>
						<value>1</value>
					</equals>
				</and>
			</condition>
		</enable>
		<type>derivative</type>
		<input>/stec30/internal/predicted-turn-rate</input>
		<reference>/stec30/internal/turn-rate-cmd</reference>
		<output>/stec30/internal/aileron-d</output>
		<filter-time>-0.0196</filter-time>
		<min>-0.25</min>
		<max>0.25</max>
	</filter>
	
	<pi-simple-controller>
		<name>IT-CONTROLLER: ROLL PI</name>
		<debug>false</debug>
		<enable>
			<condition>
				<and>
					<equals>
						<property>/stec30/output/roll</property>
						<value>1</value>
					</equals>
					<equals>
						<property>/stec30/internal/servo-roll-power</property>
						<value>1</value>
					</equals>
				</and>
			</condition>
		</enable>
		<input>
			<value>0</value>
		</input>
		<reference>/orientation/roll-deg</reference>
		<output>/stec30/internal/aileron-pi</output>
		<config>
			<Kp>-0.07</Kp>
			<Ki>-0.021</Ki>
			<min>-0.25</min>
			<max>0.25</max>
		</config>
	</pi-simple-controller>
	<filter>
		<name>IT-CONTROLLER: ROLL D</name>
		<enable>
			<condition>
				<and>
					<equals>
						<property>/stec30/output/roll</property>
						<value>1</value>
					</equals>
					<equals>
						<property>/stec30/internal/servo-roll-power</property>
						<value>1</value>
					</equals>
				</and>
			</condition>
		</enable>
		<type>derivative</type>
		<input>/orientation/roll-deg</input>
		<reference>
			<value>0</value>
		</reference>
		<output>/stec30/internal/aileron-d</output>
		<filter-time>-0.0196</filter-time>
		<min>-0.25</min>
		<max>0.25</max>
	</filter>
	
	<filter>
		<name>IT-CONTROLLER: ROLL OUTPUT INTERNAL</name>
		<debug>false</debug>
		<type>noise-spike</type>
		<input>
			<condition>
				<and>
					<greater-than>
						<property>/stec30/output/roll</property>
						<value>0</value>
					</greater-than>
					<equals>
						<property>/stec30/internal/servo-roll-power</property>
						<value>1</value>
					</equals>
				</and>
			</condition>
			<expression>
				<product>
					<sum>
						<property>/stec30/internal/aileron-pi</property>
						<property>/stec30/internal/aileron-d</property>
					</sum>
					<property>/stec30/settings/gain-roll</property>
				</product>
			</expression>
		</input>
		<input>0</input>
		<output>/stec30/internal/aileron</output>
		<max-rate-of-change>
			<condition>
				<and>
					<greater-than>
						<property>/stec30/output/roll</property>
						<value>0</value>
					</greater-than>
					<equals>
						<property>/stec30/internal/servo-roll-power</property>
						<value>1</value>
					</equals>
				</and>
			</condition>
			<value>0.5</value>
		</max-rate-of-change>
		<max-rate-of-change>10</max-rate-of-change>
	</filter>
	
	<filter>
		<name>IT-CONTROLLER: ROLL OUTPUT GENERIC</name>
		<debug>false</debug>
		<type>noise-spike</type>
		<enable>
			<condition>
				<and>
					<greater-than>
						<property>/stec30/output/roll</property>
						<value>0</value>
					</greater-than>
					<equals>
						<property>/stec30/internal/servo-roll-power</property>
						<value>1</value>
					</equals>
				</and>
			</condition>
		</enable>
		<input>
			<expression>
				<product>
					<sum>
						<property>/stec30/internal/aileron-pi</property>
						<property>/stec30/internal/aileron-d</property>
					</sum>
					<property>/stec30/settings/gain-roll</property>
				</product>
			</expression>
		</input>
		<output>/controls/flight/aileron</output>
		<max-rate-of-change>0.5</max-rate-of-change>
	</filter>
	
	<!-- Pitch Axis -->
	
	<filter>
		<name>STATIC PRESSURE 20FT DELTA</name>
		<type>gain</type>
		<gain>1.0</gain>
		<input>
			<expression>
				<table>
					<property>/stec30/input/alt</property>
					<entry><ind>32.148</ind><dep>0.022600</dep></entry> <!-- -2000 -->
					<entry><ind>31.018</ind><dep>0.021940</dep></entry> <!-- -1000 -->
					<entry><ind>29.921</ind><dep>0.021300</dep></entry> <!-- 0 -->
					<entry><ind>28.856</ind><dep>0.020700</dep></entry> <!-- 1000 -->
					<entry><ind>27.821</ind><dep>0.020080</dep></entry> <!-- 2000 -->
					<entry><ind>26.817</ind><dep>0.019500</dep></entry> <!-- 3000 -->
					<entry><ind>25.842</ind><dep>0.018920</dep></entry> <!-- 4000 -->
					<entry><ind>24.896</ind><dep>0.018360</dep></entry> <!-- 5000 -->
					<entry><ind>23.978</ind><dep>0.017800</dep></entry> <!-- 6000 -->
					<entry><ind>23.088</ind><dep>0.017260</dep></entry> <!-- 7000 -->
					<entry><ind>22.225</ind><dep>0.016740</dep></entry> <!-- 8000 -->
					<entry><ind>21.388</ind><dep>0.016220</dep></entry> <!-- 9000 -->
					<entry><ind>20.577</ind><dep>0.014764</dep></entry> <!-- 10000 -->
					<entry><ind>16.886</ind><dep>0.012544</dep></entry> <!-- 15000 -->
					<entry><ind>13.750</ind><dep>0.010584</dep></entry> <!-- 20000 -->
					<entry><ind>11.104</ind><dep>0.008874</dep></entry> <!-- 25000 -->
					<entry><ind>8.8855</ind><dep>0.007379</dep></entry> <!-- 30000 -->
					<entry><ind>8.1475</ind><dep>0.006831</dep></entry> <!-- 32000 -->
				</table>
			</expression>
		</input>
		<output>/stec30/internal/static-20ft-delta</output>
	</filter>
	
	<filter>
		<name>PRESSURE RATE COMPUTER</name>
		<debug>false</debug>
		<type>derivative</type>
		<input>/systems/static[0]/pressure-inhg</input>
		<output>/stec30/internal/pressure-rate-raw</output>
		<filter-time>1.0</filter-time>
	</filter>

	<filter>
		<name>PRESSURE RATE LAG</name>
		<debug>false</debug>
		<type>double-exponential</type>
		<input>/stec30/internal/pressure-rate-raw</input>
		<output>/stec30/internal/pressure-rate</output>
		<filter-time>0.08</filter-time>
	</filter>
	
	<predict-simple>
		<name>VERTICAL SPEED PREDICTOR</name>
		<debug>false</debug>
		<input>
			<expression> <!-- Pressure Rate to Vertical Speed FPM -->
				<product>
					<property>/stec30/internal/pressure-rate</property>
					<value>-58000</value>
				</product>
			</expression>
		</input>
		<output>/stec30/internal/predicted-vertical-speed</output>
		<seconds>2.0</seconds>
		<filter-gain>0.1</filter-gain>
	</predict-simple>
	
	<filter>
		<name>ALTITUDE CAPTURE/HOLD</name>
		<debug>false</debug>
		<type>gain</type>
		<gain>3625</gain>
		<input>/systems/static[0]/pressure-inhg</input>
		<reference>
			<expression>
				<sum>
					<property>/stec30/input/alt</property>
					<property>/stec30/input/alt-offset</property>	
				</sum>
			</expression>
		</reference>
		<output>/stec30/internal/alt-vs</output>
		<min>-400</min>
		<max>400</max>
	</filter>
	
	<pi-simple-controller>
		<name>IT-CONTROLLER: ACCEL PI</name>
		<debug>false</debug>
		<enable>
			<condition>
				<and>
					<greater-than>
						<property>/stec30/output/pitch</property>
						<value>0</value>
					</greater-than>
					<equals>
						<property>/stec30/internal/servo-pitch-power</property>
						<value>1</value>
					</equals>
				</and>
			</condition>
		</enable>
		<input>/stec30/internal/vs-slow</input>
		<reference>/stec30/internal/predicted-vertical-speed</reference>
		<output>/stec30/internal/target-accel</output>
		<config>
			<Kp>-0.00015</Kp>
			<Ki>-0.000011</Ki>
			<min>-0.3</min>
			<max>0.3</max>
		</config>
	</pi-simple-controller>
	
	<filter>
		<name>INTERNAL G ACCEL</name>
		<type>double-exponential</type>
		<input>
			<expression>
				<difference>
					<property>/accelerations/pilot-g</property>
					<product>
						<abs><property>/instrumentation/turn-indicator/indicated-turn-rate</property></abs>
						<value>0.05</value>
					</product>
				</difference>
			</expression>
		</input>
		<output>/stec30/internal/g-damped</output>
		<filter-time>0.02</filter-time>
	</filter>
	
	<filter>
		<name>TARGET ACCEL</name>
		<type>gain</type>
		<gain>1.0</gain>
		<input>
			<condition>
				<and>
					<greater-than>
						<property>/stec30/output/pitch</property>
						<value>0</value>
					</greater-than>
					<equals>
						<property>/stec30/internal/servo-pitch-power</property>
						<value>1</value>
					</equals>
				</and>
			</condition>
			<expression>
				<sum>
					<property>/stec30/internal/target-accel</property>
					<value>1</value>
				</sum>
			</expression>
		</input>
		<input>/stec30/internal/g-damped</input>
		<output>/stec30/internal/accel-cmd</output>
	</filter>
	
	<pi-simple-controller>
		<name>IT-CONTROLLER: PITCH</name>
		<debug>false</debug>
		<enable>
			<condition>
				<and>
					<greater-than>
						<property>/stec30/output/pitch</property>
						<value>0</value>
					</greater-than>
					<equals>
						<property>/stec30/internal/servo-pitch-power</property>
						<value>1</value>
					</equals>
				</and>
			</condition>
		</enable>
		<input>/stec30/internal/accel-cmd</input>
		<reference>/stec30/internal/g-damped</reference>
		<output>/stec30/internal/elevator-pi</output>
		<config>
			<Kp>0.16</Kp>
			<Ki>0.42</Ki>
			<min>-0.25</min>
			<max>0.25</max>
		</config>
	</pi-simple-controller>
	
	<filter>
		<name>IT-CONTROLLER: PITCH OUTPUT INTERNAL</name>
		<debug>false</debug>
		<type>noise-spike</type>
		<input>
			<condition>
				<and>
					<greater-than>
						<property>/stec30/output/pitch</property>
						<value>0</value>
					</greater-than>
					<equals>
						<property>/stec30/internal/servo-pitch-power</property>
						<value>1</value>
					</equals>
				</and>
			</condition>
			<expression>
				<product>
					<property>/stec30/internal/elevator-pi</property>
					<property>/stec30/settings/gain-pitch</property>
				</product>
			</expression>
		</input>
		<input>0</input>
		<output>/stec30/internal/elevator</output>
		<max-rate-of-change>
			<condition>
				<and>
					<greater-than>
						<property>/stec30/output/pitch</property>
						<value>0</value>
					</greater-than>
					<equals>
						<property>/stec30/internal/servo-pitch-power</property>
						<value>1</value>
					</equals>
				</and>
			</condition>
			<value>0.5</value>
		</max-rate-of-change>
		<max-rate-of-change>10</max-rate-of-change>
	</filter>
	
	<filter>
		<name>IT-CONTROLLER: PITCH OUTPUT GENERIC</name>
		<debug>false</debug>
		<type>noise-spike</type>
		<enable>
			<condition>
				<and>
					<greater-than>
						<property>/stec30/output/pitch</property>
						<value>0</value>
					</greater-than>
					<equals>
						<property>/stec30/internal/servo-pitch-power</property>
						<value>1</value>
					</equals>
				</and>
			</condition>
		</enable>
		<input>
			<expression>
				<product>
					<property>/stec30/internal/elevator-pi</property>
					<property>/stec30/settings/gain-pitch</property>
				</product>
			</expression>
		</input>
		<output>/controls/flight/elevator</output>
		<max-rate-of-change>0.5</max-rate-of-change>
	</filter>

</PropertyList>
