<?xml version="1.0"?>
<!--
    Usable with FlightGear
    Copyright Gerard Robin  31-03-2009
    Under License:  GPLv2
-->

<system name="Landing-Gear">



    <property value="1">systems/gear/gear-constant-value-coeff-rain</property>
    <property value="1">systems/gear/gear-constant-value-coeff-snow</property>
    <property value="1">systems/gear/anim-constant</property>
    <!--<property value="1">systems/gear/additionnal-friction</property>-->
    <property>systems/gear/terrain-bump_timer-kinemat-out</property>
    <property>systems/gear/gear[1]/tyre-smoke</property>
    <property>systems/gear/gear[2]/tyre-smoke</property>
    <!--<property value="0">gear/NWS/engaged</property>-->
    <property>/environment/terrain-hight</property>
    <property>/environment/terrain-undefined</property>
    <property value="1">systems/gear/additionnal-friction-k</property>

    <channel name="Landing Gear Main">

            <switch name="Gear Switch">
                <default value="/controls/gear/gear-down"/>
                <test  logic="AND" value="0">
                    systems/crash/switch  == 1
                </test>
                <test  logic="AND" value="0">
                    /environment/terrain-solid == 0
                    /environment/terrain-undefined == 0
                    position/h-agl-ft  LT 8
                </test>
                <output>gear/gear-cp-cmd-norm</output>
            </switch>

            <kinematic name="Gear Control">
                <input>gear/gear-cp-cmd-norm</input>
                <traverse>
                    <setting>
                        <position> 0 </position>
                        <time>     0 </time>
                    </setting>
                    <setting>
                        <position> 1 </position>
                        <time>     5 </time>
                    </setting>
                </traverse>
                <output>gear/unit[0]/pos-norm</output>
                <output>gear/unit[1]/pos-norm</output>
                <output>gear/unit[2]/pos-norm</output>
                <output>contact/unit[15]/pos-norm</output>
                <output>contact/unit[16]/pos-norm</output>
                <output>contact/unit[17]/pos-norm</output>
            </kinematic>

            <!--<actuator name="Gear Nose Actuator">
                <input>gear/gear-cmd-norm</input>
                <rate_limit>0.2</rate_limit>
                <output>gear/unit[0]/pos-norm</output>
            </actuator>
            <actuator name="Gear Left Actuator">
                <input>gear/gear-cmd-norm</input>
                <rate_limit>0.2</rate_limit>
                <output>gear/unit[1]/pos-norm</output>
            </actuator>
            <actuator name="Gear Right Actuator">
                <input>gear/gear-cmd-norm</input>
                <rate_limit>0.2</rate_limit>
                <output>gear/unit[2]/pos-norm</output>
            </actuator>-->

            <!--<switch name="Control Damage">
                <default value="fcs/cutoff-switch"/>
                <test  logic="AND" value="0">
                    /controls/gear/gear-down  == 0
                    fcs/cutoff-switch == 1
                </test>
                <output>fcs/cutoff-switch</output>
            </switch>-->

            <kinematic name="Gear Surface Position">
                <input>/controls/gear/gear-down</input>
                <traverse>
                    <setting>
                        <position>0</position>
                        <time>0</time>
                    </setting>
                    <setting>
                        <position>1</position>
                        <time>5</time>
                    </setting>
                </traverse>
                <output>/surface-positions/gear-pos-norm</output>
            </kinematic>


            <scheduled_gain name="systems/NWS/steer-cmd">
                <input>fcs/rudder-cmd-norm</input>
                <table>
                    <independentVar lookup="row">gear/NWS/engaged</independentVar>
                    <tableData>
                        0     -0.01
                        1     -1.0
                    </tableData>
                </table>
                <output>fcs/steer-pos-norm</output>
            </scheduled_gain>

            <scheduled_gain name="Scheduled Steer Cmd Deg">
                <input>fcs/steer-pos-norm</input>
                <table>
                    <independentVar lookup="column">gear/NWS/engaged</independentVar>
                    <independentVar lookup="row" >gear/unit[0]/wheel-speed-fps</independentVar>
                    <tableData>
                                        0       1
                        30.0       15      25
                        40.0        5       25
                        45.0        2       25
                    </tableData>
                </table>
                <output>fcs/steer-pos-deg</output>
            </scheduled_gain>

            <!--both following could  be nicely included in a specific static/rolling friction with contact point-->
            <switch name="Drag Terrain Factor Switch">
                <default value="0"/>
                <test  logic="AND" value="1">
                    /environment/terrain-solid == 1
                    /position/altitude-agl-ft lt 1
                    gear/gear-pos-norm == 0
                </test>
            </switch>
            <switch name="Drag Gear In Water Switch">
                <default value="0"/>
                <test  logic="AND" value="1">
                    /environment/terrain-solid == 0
                    /position/altitude-agl-ft lt 1
                    /surface-positions/gear-pos-norm == 1
                </test>
            </switch>
        </channel>

        <channel name="Animations specific">
    <!--<documentation>Only for 3D model animation</documentation>-->
            <switch  name="Wheel Rotation Factor" >
                <default value="0"/>
                <test  logic="AND" value="1">
                    /position/altitude-agl-ft lt 11
                    gear/unit[0]/pos-norm == 1
                </test>
            </switch>

    <!--<documentation>Only for 3D model animation</documentation>-->
            <pure_gain name="Wheel Rotation">
                <input>velocities/u-fps</input>
                <gain>fcs/wheel-rotation-factor</gain>
            </pure_gain>

           <!-- <pure_gain name="Rudder Steering Cmd">
                <input>fcs/rudder-cmd-norm</input>
                <gain>1</gain>
                <output>fcs/steer-cmd-norm</output>
            </pure_gain>-->




            <!-- traitement de l'animation pour compression du train -->

            <!--<switch  name="systems/gear/gear[0]/compression" >
                <default value=""/>
                <test  logic="AND" value="gear/unit[0]/compression-ft">
                    gear/unit[0]/pos-norm == 1
                </test>
            </switch>-->


            <summer name="systems/contact/gear-mlg/compression-ft">
                <input>-systems/gear/anim-constant</input>
                <input>contact/unit[16]/compression-ft</input>
                <clipto>
                    <min>-1</min>
                    <max>0</max>
                </clipto>
            </summer>

            <switch  name="systems/gear/gear[1]/compression" >
                <default value="gear/unit[1]/compression-ft"/>
                <test  logic="AND" value="systems/contact/gear-mlg/compression-ft">
                    gear/unit[1]/compression-ft  == 0
                </test>
                <test  logic="AND" value="-1">
                    gear/unit[1]/pos-norm != 1
                </test>
            </switch>

            <summer name="systems/contact/gear-mrg/compression-ft">
                <input>-systems/gear/anim-constant</input>
                <input>contact/unit[17]/compression-ft</input>
                <clipto>
                    <min>-1</min>
                    <max>0</max>
                </clipto>
            </summer>

            <switch  name="systems/gear/gear[2]/compression" >
                <default value="gear/unit[2]/compression-ft"/>
                <test  logic="AND" value="systems/contact/gear-mrg/compression-ft">
                    gear/unit[2]/compression-ft  == 0
                </test>
                <test  logic="AND" value="-1">
                    gear/unit[2]/pos-norm != 1
                </test>
            </switch>

        <!--effets de  fumée-->
       <!-- landing gear smoke-->

        <summer name="systems/gear/gear[0]/wheel-speed-diff">
            <input>velocities/u-fps</input>
            <input>-gear/unit[0]/wheel-speed-fps</input>
        </summer>
	
	
	<kinematic name="systems/gear/gear[0]/tyre-smoke-timer">
            <input>systems/gear/gear[0]/tyre-smoke-on</input>
            <traverse>
                <setting>
                    <position>0</position>
                    <time>0</time>
                </setting>
                <setting>
                    <position>1</position>
                    <time>2.5</time>
                </setting>
            </traverse>
            <output>systems/gear/gear[0]/tyre-smoke</output>
        </kinematic>
	
	
        <switch name="systems/gear/gear[0]/tyre-smoke-on-sw">
            <default value="systems/gear/gear[0]/tyre-smoke-on"/>
            <test  logic="AND" value="1">
                systems/gear/gear[0]/wheel-speed-diff gt 0.2
                gear/unit[0]/WOW == 1
            </test>
            <test  logic="AND" value="0">
                systems/gear/gear[0]/tyre-smoke-on == 1
                systems/gear/gear[0]/tyre-smoke == 1
            </test>
            <output>systems/gear/gear[0]/tyre-smoke-on</output>
        </switch>

        


        <summer name="systems/gear/gear[1]/wheel-speed-diff">
            <input>velocities/u-fps</input>
            <input>-gear/unit[1]/wheel-speed-fps</input>
        </summer>

        <switch name="systems/gear/gear[1]/tyre-smoke-on-sw">
            <default value="systems/gear/gear[1]/tyre-smoke-on"/>
            <test  logic="AND" value="1">
                systems/gear/gear[1]/wheel-speed-diff gt 0.2
                gear/unit[1]/WOW == 1
            </test>
            <test  logic="AND" value="0">
                systems/gear/gear[1]/tyre-smoke-on == 1
                systems/gear/gear[1]/tyre-smoke == 1
            </test>
            <output>systems/gear/gear[1]/tyre-smoke-on</output>
        </switch>

        <kinematic name="systems/gear/gear[1]/tyre-smoke-timer">
            <input>systems/gear/gear[1]/tyre-smoke-on</input>
            <traverse>
                <setting>
                    <position>0</position>
                    <time>0</time>
                </setting>
                <setting>
                    <position>1</position>
                    <time>2.5</time>
                </setting>
            </traverse>
            <output>systems/gear/gear[1]/tyre-smoke</output>
        </kinematic>

        <summer name="systems/gear/gear[2]/wheel-speed-diff">
            <input>velocities/u-fps</input>
            <input>-gear/unit[2]/wheel-speed-fps</input>
        </summer>

        <switch name="systems/gear/gear[2]/tyre-smoke-on-sw">
            <default value="systems/gear/gear[2]/tyre-smoke-on"/>
            <test  logic="AND" value="1">
                systems/gear/gear[2]/wheel-speed-diff gt 0.2
                gear/unit[2]/WOW == 1
            </test>
            <test  logic="AND" value="0">
                systems/gear/gear[2]/tyre-smoke-on == 1
                systems/gear/gear[2]/tyre-smoke == 1
            </test>
            <output>systems/gear/gear[2]/tyre-smoke-on</output>
        </switch>


        <kinematic name="systems/gear/gear[2]/tyre-smoke-timer">
            <input>systems/gear/gear[2]/tyre-smoke-on</input>
            <traverse>
                <setting>
                    <position>0</position>
                    <time>0</time>
                </setting>
                <setting>
                    <position>1</position>
                    <time>2.5</time>
                </setting>
            </traverse>
            <output>systems/gear/gear[2]/tyre-smoke</output>
            </kinematic>



    </channel>
    <!--copied from  <ground_reactions>-->
    <property value="0.8">systems/gear/gear[0]/static-friction-coeff-base</property>
    <property value="0.8">systems/gear/gear[1]/static-friction-coeff-base</property>
    <property value="0.8">systems/gear/gear[2]/static-friction-coeff-base</property>

    <channel name="Additionnal Static Friction duetoTerrain">
        <scheduled_gain name="systems/gear/terrain-static-friction-coeff-rain">
            <input>gear/gear-cmd-norm</input>
            <table>
                <independentVar>/environment/metar/rain-norm</independentVar>
                <tableData>
                    0.0000	1.0000
                    1.0000	0.40000
                </tableData>
            </table>
        </scheduled_gain>

        <scheduled_gain name="systems/gear/terrain-static-friction-coeff-snow">
            <input>gear/gear-cmd-norm</input>
            <table>
                <independentVar>/environment/metar/snow-norm</independentVar>
                <tableData>
                    0.0000	1.0000
                    1.0000	0.20000
                </tableData>
            </table>
        </scheduled_gain>

        <fcs_function name="fcs/gear-n-static_friction_coeff">
            <function>
                <product>
                    <property>/environment/terrain-friction-factor</property>
                    <property>systems/gear/gear[0]/static-friction-coeff-base</property>
                    <property>systems/gear/terrain-static-friction-coeff-rain</property>
                    <property>systems/gear/terrain-static-friction-coeff-snow</property>
                    <property>systems/gear/additionnal-friction-k</property>
                </product>
            </function>
            <output>gear/unit[0]/static_friction_coeff</output>
        </fcs_function>

        <fcs_function name="fcs/gear-l-static_friction_coeff">
            <function>
                <product>
                    <property>/environment/terrain-friction-factor</property>
                    <property>systems/gear/gear[1]/static-friction-coeff-base</property>
                    <property>systems/gear/terrain-static-friction-coeff-rain</property>
                    <property>systems/gear/terrain-static-friction-coeff-snow</property>
                    <property>systems/gear/additionnal-friction-k</property>
                </product>
            </function>
            <output>gear/unit[1]/static_friction_coeff</output>
        </fcs_function>

        <fcs_function name="fcs/gear-r-static_friction_coeff">
            <function>
                <product>
                    <property>/environment/terrain-friction-factor</property>
                    <property>systems/gear/gear[2]/static-friction-coeff-base</property>
                    <property>systems/gear/terrain-static-friction-coeff-rain</property>
                    <property>systems/gear/terrain-static-friction-coeff-snow</property>
                    <property>systems/gear/additionnal-friction-k</property>
                </product>
            </function>
            <output>gear/unit[2]/static_friction_coeff</output>
        </fcs_function>
    </channel>


    <!--These values given with a supposed to be perfect terrain ==> with terrain-friction-factor = 1-->
    <!--copied from  <ground_reactions>-->
    <property value="0.5">systems/gear/gear[0]/dynamic-friction-coeff-base</property>
    <property value="0.5">systems/gear/gear[1]/dynamic-friction-coeff-base</property>
    <property value="0.5">systems/gear/gear[2]/dynamic-friction-coeff-base</property>

    <channel name="Additionnal Dynamic Friction duetoTerrain">

        <fcs_function name="fcs/gear-n-dynamic_friction_coeff">
            <function>
                <product>
                    <property>systems/gear/gear[0]/dynamic-friction-coeff-base</property>
                    <product>
                        <property>/environment/terrain-friction-factor</property>
                        <property>systems/gear/terrain-static-friction-coeff-rain</property>
                        <property>systems/gear/terrain-static-friction-coeff-snow</property>
                        <property>systems/gear/additionnal-friction-k</property>
                    </product>
                </product>
            </function>
            <output>gear/unit[0]/dynamic_friction_coeff</output>
        </fcs_function>

        <fcs_function name="fcs/gear-l-dynamic_friction_coeff">
            <function>
                <product>
                    <property>systems/gear/gear[1]/dynamic-friction-coeff-base</property>
                    <product>
                        <property>/environment/terrain-friction-factor</property>
                        <property>systems/gear/terrain-static-friction-coeff-rain</property>
                        <property>systems/gear/terrain-static-friction-coeff-snow</property>
                        <property>systems/gear/additionnal-friction-k</property>
                    </product>
                </product>
            </function>
            <output>gear/unit[1]/dynamic_friction_coeff</output>
        </fcs_function>

        <fcs_function name="fcs/gear-r-dynamic_friction_coeff">
            <function>
                <product>
                    <property>systems/gear/gear[2]/dynamic-friction-coeff-base</property>
                    <product>
                        <property>/environment/terrain-friction-factor</property>
                        <property>systems/gear/terrain-static-friction-coeff-rain</property>
                        <property>systems/gear/terrain-static-friction-coeff-snow</property>
                        <property>systems/gear/additionnal-friction-k</property>
                    </product>
                </product>
            </function>
            <output>gear/unit[2]/dynamic_friction_coeff</output>
        </fcs_function>
    </channel>
    <!--These values given with a supposed to be perfect terrain ==> with terrain-rolling-friction = 0 -->
    <!--copied from  <ground_reactions>-->
    <property value="0.02">systems/gear/gear[0]/rolling-friction-coeff-base</property>
    <property value="0.02">systems/gear/gear[1]/rolling-friction-coeff-base</property>
    <property value="0.02">systems/gear/gear[2]/rolling-friction-coeff-base</property>

    <channel name="Additionnal Rolling Friction duetoTerrain">
        <scheduled_gain name="systems/gear/terrain-rolling-friction-coeff-rain">
            <input>gear/gear-cmd-norm</input>
            <table>
                <independentVar>/environment/metar/rain-norm</independentVar>
                <tableData>
                    0.0000	1.0000
                    1.0000	1.0000
                </tableData>
            </table>
        </scheduled_gain>

        <scheduled_gain name="systems/gear/terrain-rolling-friction-coeff-snow">
            <input>gear/gear-cmd-norm</input>
            <table>
                <independentVar>/environment/metar/snow-norm</independentVar>
                <tableData>
                    0.0000	1.0000
                    1.0000	1.2000
                </tableData>
            </table>
        </scheduled_gain>
        <!--here a guess , we take in account the gear rolling-friction modified by the terrain rolling-friction-->
        <fcs_function name="fcs/gear-n-rolling_friction_coeff">
            <function>
                <sum>
                    <product>
                        <property>/environment/terrain-rolling-friction</property>
                        <property>systems/gear/terrain-rolling-friction-coeff-rain</property>
                        <property>systems/gear/terrain-rolling-friction-coeff-snow</property>
                    </product>
                    <property>systems/gear/gear[0]/rolling-friction-coeff-base</property>
                </sum>
            </function>
            <output>gear/unit[0]/rolling_friction_coeff</output>
        </fcs_function>

        <fcs_function name="fcs/gear-l-rolling_friction_coeff">
            <function>
                <sum>
                    <product>
                        <property>/environment/terrain-rolling-friction</property>
                        <property>systems/gear/terrain-rolling-friction-coeff-rain</property>
                        <property>systems/gear/terrain-rolling-friction-coeff-snow</property>
                    </product>
                    <property>systems/gear/gear[1]/rolling-friction-coeff-base</property>
                </sum>
            </function>
            <output>gear/unit[1]/rolling_friction_coeff</output>
        </fcs_function>

        <fcs_function name="fcs/gear-r-rolling_friction_coeff">
            <function>
                <sum>
                    <product>
                        <property>/environment/terrain-rolling-friction</property>
                        <property>systems/gear/terrain-rolling-friction-coeff-rain</property>
                        <property>systems/gear/terrain-rolling-friction-coeff-snow</property>
                    </product>
                    <property>systems/gear/gear[2]/rolling-friction-coeff-base</property>
                </sum>
            </function>
            <output>gear/unit[2]/rolling_friction_coeff</output>
        </fcs_function>

    </channel>
    <!--position copied from  <ground_reactions>-->
    <property value="-81.6">systems/gear/gear[0]/z-position</property>
    <property value="-65.75">systems/gear/gear[1]/z-position</property>
    <property value="-65.75">systems/gear/gear[2]/z-position</property>

    <channel name="Additionnal bumpiness  duetoTerrain">
       <!-- NOTE that channel is not realistic only for animation-->

       <switch name="systems/gear/terrain-bump_timer">
           <default value="0"/>
           <test logic="AND" value="1">
               /environment/terrain-bumpiness gt 0.05
           </test>
       </switch>

       <switch name="terrain-bump_timer-sw">
           <default value="systems/gear/terrain-bump_timer-kinemat-in"/>
           <test logic="AND" value="0">
               systems/gear/terrain-bump_timer-kinemat-in == 1
               systems/gear/terrain-bump_timer-kinemat-out == 1
           </test>
           <test logic="AND" value="1">
               systems/gear/terrain-bump_timer-kinemat-out == 0
               systems/gear/terrain-bump_timer == 0
           </test>
           <output>systems/gear/terrain-bump_timer-kinemat-in</output>
       </switch>

       <kinematic name="terrain-bump_timer-norm">
           <input>systems/gear/terrain-bump_timer-kinemat-in</input>
           <traverse>
               <setting>
                   <position>0</position>
                   <time>0</time>
               </setting>
               <setting>
                   <position>1</position>
                   <time>5</time>
               </setting>
           </traverse>
           <output>systems/gear/terrain-bump_timer-kinemat-out</output>
        </kinematic>


            <!-- <scheduled_gain name="systems/gear/gear[0]/z-position-coeff">
                <input>gear/unit[0]/pos-norm</input>
                <table>
                    <independentVar>/environment/terrain-load-resistance</independentVar>
                    <tableData>
                        100000	1.0000
                        100 	0.6
                    </tableData>
                </table>
            </scheduled_gain>-->

            <!--<scheduled_gain name="systems/gear/gear[0]/bump-coeff">
                <input>gear/unit[0]/pos-norm</input>
                <table>
                    <independentVar>/environment/terrain-bumpiness</independentVar>
                    <tableData>
                        0.1	0
                        1       0.2
                    </tableData>
                </table>
            </scheduled_gain>

            <scheduled_gain name="systems/gear/gear[1]/bump-coeff">
                <input>gear/unit[1]/pos-norm</input>
                <table>
                    <independentVar>/environment/terrain-bumpiness</independentVar>
                    <tableData>
                        0.1	0
                        1       0.15
                    </tableData>
                </table>
            </scheduled_gain>

            <scheduled_gain name="systems/gear/gear[2]/bump-coeff">
                <input>gear/unit[2]/pos-norm</input>
                <table>
                    <independentVar>/environment/terrain-bumpiness</independentVar>
                    <tableData>
                        0.1	0
                        1       0.15
                    </tableData>
                </table>
            </scheduled_gain>-->


            <!--<fcs_function name="gear-n-terrain-bump-Z">
                <function>
                    <product>
                        <property>gear/unit[0]/compression-ft</property>
                        <property>forces/fbz-gear-lbs</property>
                        <property>systems/gear/gear[1]/bump-coeff</property>
                        <property>fcs/gear-n-rolling</property>
                    </product>
                </function>
                <output>external_reactions/nose-gear-bump/magnitude</output>
            </fcs_function>

            <fcs_function name="gear-l-terrain-bump-Z">
                <function>
                    <product>
                        <property>gear/unit[1]/compression-ft</property>
                        <property>forces/fbz-gear-lbs</property>
                        <property>systems/gear/gear[1]/bump-coeff</property>
                        <property>fcs/gear-l-rolling</property>
                    </product>
                </function>
                <output>external_reactions/left-gear-bump/magnitude</output>
            </fcs_function>

            <fcs_function name="gear-r-terrain-bump-Z">
                <function>
                    <product>
                        <property>gear/unit[2]/compression-ft</property>
                        <property>forces/fbz-gear-lbs</property>
                        <property>systems/gear/gear[2]/bump-coeff</property>
                        <property>fcs/gear-r-rolling</property>
                    </product>
                </function>
                <output>external_reactions/right-gear-bump/magnitude</output>
            </fcs_function>-->
    </channel>

</system>
