<system  name="FCS Supermarine Swift">
    <!-- Supermarine Swift Flight controls 
    Author: Richard Harrison (rjh@zaretto.com)
    References: None
-->
    <property value="1">gear/serviceable</property>
    <property value="0">gear/gear-asym</property>
    <property value="0">malfunctions/fcs/pitch-trim-inop</property>
    <property value="0">malfunctions/fcs/roll-trim-actuator-norm-inop</property>
    <property value="0">malfunctions/fcs/aileron-actuator-inop</property>
    <property value="0">malfunctions/fcs/rudder-actuator-inop</property>
    <property value="1">fcs/pitch-effectivity</property>
    <property value="1">fcs/rudder-effectivity</property>

    <channel name="Auxiliary">
        <pure_gain name="Pilot G">
            <input>accelerations/n-pilot-z-norm</input>
            <gain>-1</gain>
            <output>/accelerations/pilot-gdamped</output>
        </pure_gain>

        <pure_gain name="aero/alphadot-rad_sec-limited">
            <input>aero/alphadot-rad_sec</input>
            <output>aero/alphadot-rad_sec-limited</output>
            <gain>1</gain>
            <clipto>
                <min> -0.2</min>
                <max> 0.2</max>
            </clipto>
        </pure_gain>
        <pure_gain name="aero/betadot-rad_sec-limited">
            <input>aero/betadot-rad_sec</input>
            <output>aero/betadot-rad_sec-limited</output>
            <gain>1</gain>
            <clipto>
                <min> -0.1</min>
                <max> 0.1</max>
            </clipto>
        </pure_gain>

        <switch name="position/aircraft-on-ground">
            <description>Aircraft on ground</description>
            <default value="0"/>
            <test value="1" logic="OR">
                gear/unit[0]/WOW ne 0
                gear/unit[1]/WOW ne 0
                gear/unit[2]/WOW ne 0
            </test>
        </switch>
        <fcs_function name="aero/alpha-indicated-deg">
            <description>ARI nose probe; ref ADA101648 p84; alpha_true = .8122 alphaNOSEPROBE + .797deg</description>
            <function>
                <product>
                    <sum>
                        <property> aero/alpha-deg </property>
                        <value> -0.797 </value>
                    </sum>
                    <value>1.231223836</value>
                </product>
            </function>
        </fcs_function>
    </channel>

    <channel name="SoundFX">
        <fcs_function name="fcs/flap-windfx-volume">
            <function>
                <product>
                    <property>velocities/ve-kts</property>
                    <property>fcs/flap-pos-norm</property>
                </product>
            </function>
            <output>fcs/flap-windfx-volume</output>
        </fcs_function>
        <fcs_function name="fcs/gear-windfx-volume">
            <function>
                <product>
                    <property>velocities/ve-kts</property>
                    <property>fcs/gear-control</property>
                </product>
            </function>
            <output>fcs/gear-windfx-volume</output>
        </fcs_function>
    </channel>


    <channel name="Pitch">
        <summer name="fcs/elevator-pos-norm">
            <input>fcs/elevator-cmd-norm</input>
            <input>fcs/pitch-trim-cmd-norm</input>
            <clipto>
                <min> -1 </min>
                <max>  1 </max>
            </clipto>
            <output>fcs/elevator-pos-norm</output>
        </summer>

        <kinematic name="fcs/elevator-actuator-norm">
            <input>fcs/elevator-pos-norm</input>
            <traverse>
                <setting>
                    <position>-1</position>
                    <time>0.9</time>
                </setting>
                <setting>
                    <position>1</position>
                    <time>0.90</time>
                    <!-- based on 30 deg/sec -->
                </setting>
            </traverse>
        </kinematic>
        
        <fcs_function name="fcs/elevator-pos-rad">
            <description>Calculate the elevator position in radians based on the inputs</description>
            <function>
                <product>
                    <property>fcs/elevator-actuator-norm</property>
                    <v>0.723599</v>
                    <v>-1</v>
                </product>
            </function>
            <output>fcs/elevator-pos-rad</output>
        </fcs_function>
    </channel>
    <!-- aileron: +/- 12.5deg : +/- 33 deg/sec -->
    <channel name="Roll">
        <summer name="fcs/roll-trim-sum-norm-raw">
            <input>fcs/aileron-cmd-norm</input>
            <input>fcs/roll-trim-cmd-norm</input>
            <!--<input>fcs/roll-trim-sas-cmd-norm</input>
            <input>fcs/roll-rate-limiter</input>-->
            <clipto>
                <min> -1 </min>
                <max>  1 </max>
            </clipto>
        </summer>
        <fcs_function name="fcs/aileron-effectivity">
            <function>
                <table>
                    <independentVar>velocities/vc-kts</independentVar>
                    <tableData>
                          0 1.0
                        250 1.0
                        450 0.5
                        600 0.25
                    </tableData>
                </table>
            </function>
        </fcs_function>

        <fcs_function>
            <function>
                <sum>
                    <property>fcs/roll-trim-cmd-norm</property>
                    <min>
                        <max>
                            <product>
                                <sum>
                                    <p>fcs/aileron-cmd-norm</p>
                                    <p>fcs/roll-trim-cmd-norm</p>
                                </sum>
                                <p>fcs/aileron-effectivity</p>
                            </product>
                            <v>-1</v>
                        </max>
                        <v>1</v>
                    </min>
                </sum>
            </function>
            <clipto>
                <min> -1 </min>
                <max>  1 </max>
            </clipto>
            <output>fcs/aileron-sum</output>
        </fcs_function>


        <switch name="fcs/aileron-actuator-pos">
            <default value="fcs/aileron-sum"/>
            <!--<test value="fcs/aileron-actuator-pos">
                systems/hydraulics/system-pressure eq 0
            </test>-->
            <test value="fcs/aileron-actuator-pos">
                malfunctions/fcs/aileron-actuator-inop ne 0
            </test>
        </switch>
        <kinematic name="Aileron Control">
            <input>fcs/aileron-actuator-pos</input>
            <traverse>
                <setting>
                    <position>-0.35</position>
                    <time>0.90</time>
                    <!-- based on 30 deg/sec -->
                </setting>
                <setting>
                    <position>0.35</position>
                    <time>0.90</time>
                    <!-- based on 30 deg/sec -->
                </setting>
            </traverse>
            <output>fcs/aileron-pos-rad</output>
        </kinematic>

        <aerosurface_scale name="aileron normalization">
            <input>fcs/aileron-pos-rad</input>
            <domain>
                <min> -0.35 </min>
                <max>  0.35 </max>
            </domain>
            <range>
                <min> -1 </min>
                <max>  1 </max>
            </range>
            <output>fcs/aileron-pos-norm</output>
        </aerosurface_scale>
        <pure_gain name="fcs/aileron-pos-deg">
            <input>fcs/aileron-pos-rad</input>
            <gain>57.324</gain>
        </pure_gain >
        <pure_gain name="/surface-positions/aileron-pos-norm">
            <input>fcs/aileron-pos-norm</input>
            <gain>1</gain>
        </pure_gain >

    </channel>

    <channel name="Yaw">
        <switch name="fcs/yaw-damper-active">
            <default value="0.0"/>
            <test logic="AND" value="1">
                fcs/yaw-damper-enable ne 0.0
            </test>
            <output>fcs/yaw-damper-active</output>
        </switch>

        <scheduled_gain name="fcs/yaw-damper-sum">
            <input>velocities/r-aero-rad_sec</input>
            <table>
                <independentVar>velocities/vc-kts</independentVar>
                <tableData>
                    0   0
                    80  0
                    120	3.6251
                    650 2
                    1000 1
                </tableData>
            </table>
        </scheduled_gain>

        <scheduled_gain name="fcs/yaw-damper-dmd">
            <input>fcs/yaw-damper-sum</input>
            <table>
                <independentVar>aero/qbar-psf</independentVar>
                <tableData>
                    2.9900	0.0000
                    3.0000	1.0000
                </tableData>
            </table>
            <gain>fcs/yaw-damper-active</gain>
        </scheduled_gain>

        <fcs_function>
            <function>
                <product>
                    <v>-1</v>
                    <min>
                        <max>
                            <product>
                                <sum>
                                    <p>fcs/rudder-cmd-norm</p>
                                    <p>fcs/yaw-damper-dmd</p>
                                    <p>fcs/yaw-trim-cmd-norm</p>
                                </sum>
                                <p>fcs/rudder-effectivity</p>
                            </product>
                            <v>-1</v>
                        </max>
                        <v>1</v>
                    </min>
                    <sum>
                        <product>
                            <p>gear/gear-cmd-norm</p>
                            <v>0.7</v>
                        </product>
                        <v>0.3</v>
                    </sum>
                    <!--<property>fcs/pitch-effectivity</property>-->
                    <!--<v>-1</v>-->
                </product>
            </function>
            <output>fcs/rudder-sum</output>
        </fcs_function>


        <switch name="fcs/rudder-actuator-pos">
            <default value="fcs/rudder-sum"/>
            <!--<test value="fcs/rudder-actuator-pos">
                systems/hydraulics/system-pressure eq 0
            </test>-->
            <test value="fcs/rudder-actuator-pos">
                malfunctions/fcs/rudder-actuator-inop ne 0
            </test>
        </switch>
        <kinematic name="Rudder Control">
            <input>fcs/rudder-actuator-pos</input>
            <traverse>
                <setting>
                    <position>-0.1364</position>
                    <time>0.90</time>
                    <!-- based on 30 deg/sec -->
                </setting>
                <setting>
                    <position>0.1364</position>
                    <time>0.90</time>
                    <!-- based on 30 deg/sec -->
                </setting>
            </traverse>
            <output>fcs/rudder-pos-rad</output>
        </kinematic>

        <aerosurface_scale name="rudder normalization">
            <input>fcs/rudder-pos-rad</input>
            <domain>
                <min> -0.1364 </min>
                <max>  0.1364 </max>
            </domain>
            <range>
                <min> -1 </min>
                <max>  1 </max>
            </range>
            <output>fcs/rudder-pos-norm</output>
        </aerosurface_scale>
    </channel>

    <channel name="Landing Gear">
        <switch name="gear/unit[0]/locked">
            <default value="-1"/>
            <test value="0">
                <!-- unlocked. -->
                gear/gear-pos-norm gt 0
                gear/gear-pos-norm lt 1
            </test>
            <test value="1">
                gear/gear-pos-norm le 0
            </test>
            <test value="-1">
                gear/gear-pos-norm ge 1
            </test>
        </switch>
        <switch name="gear/unit[1]/locked">
            <default value="-1"/>
            <test value="0">
                <!-- unlocked. -->
                gear/gear-pos-norm gt 0
                gear/gear-pos-norm lt 1
            </test>
            <test value="1">
                gear/gear-pos-norm le 0
            </test>
            <test value="-1">
                gear/gear-pos-norm ge 1
            </test>
        </switch>
        <switch name="gear/unit[2]/locked">
            <default value="-1"/>
            <test value="0">
                <!-- unlocked. -->
                gear/gear-pos-norm gt 0
                gear/gear-pos-norm lt 1
            </test>
            <test value="1">
                gear/gear-pos-norm le 0
            </test>
            <test value="-1">
                gear/gear-pos-norm ge 1
            </test>
        </switch>
        <switch name="gear/gear-dmd-norm">
            <default value="1"/>
            <test value="1">
                <!-- always force gear down when on ground - to prevent inadvertant retraction -->
                position/aircraft-on-ground ne 0
            </test>
            <test value="gear/gear-pos-norm">
                gear/serviceable ne 1
            </test>
            <test value="gear/gear-dmd-norm">
                systems/hydraulics/system-pressure eq 0
            </test>
            <test value="gear/gear-cmd-norm">
                systems/hydraulics/system-pressure ne 0
            </test>
            <clipto>
                <min> 0 </min>
                <max>  1 </max>
            </clipto>
            <output>gear/gear-dmd-norm</output>
        </switch>
        <!-- timing taken from https://youtu.be/lFu88Ob8qH8 -->
        <kinematic name="Gear Control">
            <input>gear/gear-dmd-norm</input>
            <traverse>
                <setting>
                    <position> 0 </position>
                    <time>     0 </time>
                </setting>
                <setting>
                    <position> 1 </position>
                    <time>     3.4 </time>
                </setting>
            </traverse>
            <output>gear/gear-pos-norm</output>
        </kinematic>

    </channel>

    <channel name="Speedbrake">
        <switch name="fcs/speedbrake-dmd-norm">
            <default value="0"/>
            <test value="fcs/speedbrake-dmd-norm">
                systems/hydraulics/system-pressure eq 0
            </test>
            <test value="fcs/speedbrake-cmd-norm">
                systems/hydraulics/system-pressure ne 0
            </test>
            <clipto>
                <min> 0 </min>
                <max>  1 </max>
            </clipto>
            <output>fcs/speedbrake-dmd-norm</output>
        </switch>

        <kinematic name="Speedbrake Control">
            <input>fcs/speedbrake-dmd-norm</input>
            <traverse>
                <setting>
                    <position> 0 </position>
                    <time>     0 </time>
                </setting>
                <setting>
                    <position> 1 </position>
                    <time>     1.5 </time>
                </setting>
            </traverse>
            <output>fcs/speedbrake-pos-norm</output>
        </kinematic>
    </channel>

    <channel name="Flaps">
        <switch name="fcs/flap-total-dmd">
            <default value="0"/>
            <test value="fcs/flap-total-dmd">
                systems/hydraulics/number1-system-pressure eq 0
            </test>
            <test value="fcs/flap-cmd-norm">
                fcs/flap-cmd-norm ne 0
                systems/hydraulics/number1-system-pressure ne 0
            </test>
            <clipto>
                <min> 0 </min>
                <max>  1 </max>
            </clipto>
            <output>fcs/flap-total-dmd</output>
        </switch>

        <kinematic name="Flaps Control">
            <input>fcs/flap-total-dmd</input>
            <traverse>
                <setting>
                    <position>  0 </position>
                    <time>      0 </time>
                </setting>
                <setting>
                    <position> 50 </position>
                    <time>    1.9 </time>
                </setting>
            </traverse>
            <output>fcs/flap-pos-deg</output>
        </kinematic>

        <aerosurface_scale name="flap normalization">
            <input>fcs/flap-pos-deg</input>
            <domain>
                <min>  0 </min>
                <max> 50 </max>
            </domain>
            <range>
                <min> 0 </min>
                <max> 1 </max>
            </range>
            <output>fcs/flap-pos-norm</output>
        </aerosurface_scale>

    </channel>

</system>
