<?xml version="1.0"?>

<!--
	Artifical horizon. For the MiG-15bis, but can be used in every JSB craft.
	Author: Slavutinsky Victor
-->

<system>

	<fileheader>
		<author> Slavutinsky Victor </author>
		<filecreationdate> 2010-10-03 </filecreationdate>
		<version>$Revision: 1.00 $</version>
		<description> Artifical horizon </description>
	</fileheader>

<!--	That must be aded to set file

	<property value="0">systems/arthorizon/on</property>
	<property value="1">systems/arthorizon/serviceable</property>
	<property value="0">systems/arthorizon/button-command</property>
	<property value="0">systems/arthorizon/button-pos</property>
	<property value="0">systems/arthorizon/offset</property>

	<property value="0">systems/arthorizon/roll-indicated-rad</property>
	<property value="0">systems/arthorizon/roll-zero-rad</property>
	<property value="0">systems/arthorizon/roll-stored-rad</property>
	<property value="0">systems/arthorizon/pitch-indicated-rad</property>
	<property value="0">systems/arthorizon/pitch-zero-rad</property>
	<property value="0">systems/arthorizon/pitch-stored-rad</property>

-->
	<channel name="Arthorizon">

		<!-- Button-->

		<kinematic name="calculations/arthorizon/button-pos">
			<input>systems/arthorizon/button-command</input>
			<traverse>
			<setting>
				<position>0</position>
				<time>0</time>
			</setting>
			<setting>
				<position>1</position>
				<time>0.2</time>
			</setting>
			</traverse>
			<output>systems/arthorizon/button-pos</output>
		</kinematic>

		<switch name="calculations/arthorizon/click">
			<default value="systems/sounds/click/on"/>
			<test logic="AND" value="1">
				systems/arthorizon/button-pos ge 0.25
				systems/arthorizon/button-pos le 0.75
				systems/sounds/click/on == 0
			</test>
			<output>systems/sounds/click/on</output>
		</switch>

		<!-- Roll of arthorizon-->

		<!--Drift if switched on and roll faster than 10deg/sec or switched on and button is pressed-->

		<!-- 10deg\sec=10*pi/180rad-->

		<switch name="calculations/arthorizon/roll-great-drift">
			<default value="0"/>
			<test logic="OR" value="1">
				velocities/p-rad_sec ge 0.1745 
				velocities/p-rad_sec le -0.1745
			</test>
			<output>systems/arthorizon/roll-great-drift</output>
		</switch>

		<!-- If button pressed then set zero to current angle with speed=half_of_delta/sec -->

		<fcs_function name="calculations/arthorizon/roll-zero-return-rad">
		<function>
			<product>
				<sum>
					<product>
						<property>systems/arthorizon/roll-zero-rad</property>
						<value>-1.0</value>
					</product>
					<property>attitude/roll-rad</property>
				</sum>
				<value>0.5</value><!--half per second -->
			</product>
		</function>
			<output>systems/arthorizon/roll-zero-return-rad</output>
		</fcs_function>

		<!-- If not then drift with 1/40 of current angle speed, -1 because zero plused -->

		<pure_gain name="calculations/arthorizon/roll-zero-drift-rad">
			<input>velocities/p-rad_sec</input>
			<gain>-0.025</gain>
			<output>systems/arthorizon/roll-zero-drift-rad</output>
		</pure_gain>

		<!-- If switched off at all then goes to make it remain in stored position after switch on -->

		<fcs_function name="calculations/arthorizon/roll-zero-stored-rad">
		<function>
			<sum>
				<product>
					<property>systems/arthorizon/roll-zero-rad</property>
					<value>-1.0</value>
				</product>
				<product>
					<property>systems/arthorizon/roll-stored-rad</property>
					<value>-1.0</value>
				</product>
				<property>attitude/roll-rad</property>
			</sum>
		</function>
			<output>systems/arthorizon/roll-zero-stored-rad</output>
		</fcs_function>

		<!-- If no power then moves to remain in stored position
			else it no button pressed then drift else return to current "zero"-->

		<switch name="calculations/arthorizon/roll-zero-input-rad">
			<default value="0"/>
			<test logic="OR" value="systems/arthorizon/roll-zero-stored-rad">
				systems/arthorizon/on == 0
				systems/arthorizon/serviceable == 0
			</test>
			<test logic="AND" value="systems/arthorizon/roll-zero-drift-rad">
				systems/arthorizon/on == 1
				systems/arthorizon/serviceable == 1
				systems/arthorizon/button-pos lt 0.9
			</test>
			<test logic="AND" value="systems/arthorizon/roll-zero-return-rad">
				systems/arthorizon/on == 1
				systems/arthorizon/serviceable == 1
				systems/arthorizon/button-pos ge 0.9
			</test>
			<output>systems/arthorizon/roll-zero-input-rad</output>
		</switch>

		<!-- Proportional, Integrational and Derivative response. If kp=0, ki=1 and kd=0 then
		 input rate of 1 increases the integrator output value of 1*input per second. -->

		<pid name="calculations/arthorizon/roll-zero-rad">
			<input>systems/arthorizon/roll-zero-input-rad</input>
			<kp>0.0</kp>
			<ki>1.0</ki>
			<kd>0.0</kd>
			<output>systems/arthorizon/roll-zero-rad</output>
		</pid>

		<!-- Summar roll -->

		<summer name="calculations/arthorizon/roll-resulted-rad">
			<input>attitude/roll-rad</input>
			<input>-systems/arthorizon/roll-zero-rad</input>
			<output>systems/arthorizon/roll-resulted-rad</output>
		</summer>

		<!-- If switched on then shown value stored -->

		<switch name="calculations/arthorizon/roll-stored-rad">
			<default value="0"/>
			<test logic="OR" value="systems/arthorizon/roll-stored-rad">
				systems/arthorizon/on == 0
				systems/arthorizon/serviceable == 0
			</test>
			<test logic="AND" value="systems/arthorizon/roll-resulted-rad">
				systems/arthorizon/on == 1
				systems/arthorizon/serviceable == 1
			</test>
			<output>systems/arthorizon/roll-stored-rad</output>
		</switch>

		<!-- If switched off or broken then stored value indicated -->

		<switch name="calculations/arthorizon/roll-indicated-rad">
			<default value="0"/>
			<test logic="OR" value="systems/arthorizon/roll-stored-rad">
				systems/arthorizon/on == 0
				systems/arthorizon/serviceable == 0
			</test>
			<test logic="AND" value="systems/arthorizon/roll-resulted-rad">
				systems/arthorizon/on == 1
				systems/arthorizon/serviceable == 1
			</test>
			<output>systems/arthorizon/roll-indicated-rad</output>
		</switch>

		<!-- Final output in degrees -->

		<fcs_function name="calculations/roll-indicated-deg">
		<function>
			<product>
				<property>systems/arthorizon/roll-indicated-rad</property>
				<value>57.295779</value><!--rad to deg 180/pi -->
			</product>
		</function>
			<output>systems/arthorizon/roll-indicated-deg</output>
		</fcs_function>

		<!-- Pitch of arthorizon-->

		<!--Drift if switched on and pitch faster than 10deg/sec or switched on and button is pressed-->

		<!-- 10deg\sec=10*pi/180rad-->

		<switch name="calculations/arthorizon/pitch-great-drift">
			<default value="0"/>
			<test logic="OR" value="1">
				velocities/q-rad_sec ge 0.1745 
				velocities/q-rad_sec le -0.1745
			</test>
			<output>systems/arthorizon/pitch-great-drift</output>
		</switch>

		<!-- If button pressed then set zero to current angle with speed=half_of_delta/sec -->

		<fcs_function name="calculations/arthorizon/pitch-zero-return-rad">
		<function>
			<product>
				<sum>
					<product>
						<property>systems/arthorizon/pitch-zero-rad</property>
						<value>-1.0</value>
					</product>
					<property>attitude/pitch-rad</property>
				</sum>
				<value>0.5</value><!--half per second -->
			</product>
		</function>
			<output>systems/arthorizon/pitch-zero-return-rad</output>
		</fcs_function>

		<!-- If not then drift with 1/40 of current angle speed, -1 because zero plused -->

		<pure_gain name="calculations/arthorizon/pitch-zero-drift-rad">
			<input>velocities/q-rad_sec</input>
			<gain>-0.025</gain>
			<output>systems/arthorizon/pitch-zero-drift-rad</output>
		</pure_gain>

		<!-- If switched off at all then goes to make it remain in stored position after switch on -->

		<fcs_function name="calculations/arthorizon/pitch-zero-stored-rad">
		<function>
			<sum>
				<product>
					<property>systems/arthorizon/pitch-zero-rad</property>
					<value>-1.0</value>
				</product>
				<product>
					<property>systems/arthorizon/pitch-stored-rad</property>
					<value>-1.0</value>
				</product>
				<property>attitude/pitch-rad</property>
			</sum>
		</function>
			<output>systems/arthorizon/pitch-zero-stored-rad</output>
		</fcs_function>

		<!-- If no power then moves to remain in stored position
			else it no button pressed then drift else return to current "zero"-->

		<switch name="calculations/arthorizon/pitch-zero-input-rad">
			<default value="0"/>
			<test logic="OR" value="systems/arthorizon/pitch-zero-stored-rad">
				systems/arthorizon/on == 0
				systems/arthorizon/serviceable == 0
			</test>
			<test logic="AND" value="systems/arthorizon/pitch-zero-drift-rad">
				systems/arthorizon/on == 1
				systems/arthorizon/serviceable == 1
				systems/arthorizon/button-pos lt 0.9
			</test>
			<test logic="AND" value="systems/arthorizon/pitch-zero-return-rad">
				systems/arthorizon/on == 1
				systems/arthorizon/serviceable == 1
				systems/arthorizon/button-pos ge 0.9
			</test>
			<output>systems/arthorizon/pitch-zero-input-rad</output>
		</switch>

		<!-- Proportional, Integrational and Derivative response. If kp=0, ki=1 and kd=0 then
		 input rate of 1 increases the integrator output value of 1*input per second. -->

		<pid name="calculations/arthorizon/pitch-zero-rad">
			<input>systems/arthorizon/pitch-zero-input-rad</input>
			<kp>0.0</kp>
			<ki>1.0</ki>
			<kd>0.0</kd>
			<output>systems/arthorizon/pitch-zero-rad</output>
		</pid>

		<!-- Summar pitch -->

		<summer name="calculations/arthorizon/pitch-resulted-rad">
			<input>attitude/pitch-rad</input>
			<input>-systems/arthorizon/pitch-zero-rad</input>
			<output>systems/arthorizon/pitch-resulted-rad</output>
		</summer>

		<!-- If switched on then shown value stored -->

		<switch name="calculations/arthorizon/pitch-stored-rad">
			<default value="0"/>
			<test logic="OR" value="systems/arthorizon/pitch-stored-rad">
				systems/arthorizon/on == 0
				systems/arthorizon/serviceable == 0
			</test>
			<test logic="AND" value="systems/arthorizon/pitch-resulted-rad">
				systems/arthorizon/on == 1
				systems/arthorizon/serviceable == 1
			</test>
			<output>systems/arthorizon/pitch-stored-rad</output>
		</switch>

		<!-- If switched off or broken then stored value indicated -->

		<switch name="calculations/arthorizon/pitch-indicated-rad">
			<default value="0"/>
			<test logic="OR" value="systems/arthorizon/pitch-stored-rad">
				systems/arthorizon/on == 0
				systems/arthorizon/serviceable == 0
			</test>
			<test logic="AND" value="systems/arthorizon/pitch-resulted-rad">
				systems/arthorizon/on == 1
				systems/arthorizon/serviceable == 1
			</test>
			<output>systems/arthorizon/pitch-indicated-rad</output>
		</switch>

		<!-- Final output in degrees -->

		<fcs_function name="calculations/pitch-indicated-deg">
		<function>
			<product>
				<property>systems/arthorizon/pitch-indicated-rad</property>
				<value>57.295779</value><!--rad to deg 180/pi -->
			</product>
		</function>
			<output>systems/arthorizon/pitch-indicated-deg</output>
		</fcs_function>

	</channel>

</system>
