<?xml version="1.0"?>

<!--
***********************************************************************************
c172p-detailed, May 2015... Nov. 2016 
Extra weight and drag due to bush wheels, floats and aircraft with 180 hp engine
***********************************************************************************
 -->

<system name="bushkits">

    <channel name="extra-weight">
        <switch name="extra-weight-left-26-in">
            <default value="0"/>
            <test logic="AND" value="10">
                bushkit EQ 1
            </test>
            <output>/fdm/jsbsim/inertia/pointmass-weight-lbs[5]</output>
        </switch>

        <switch name="extra-weight-right-26-in">
            <default value="0"/>
            <test logic="AND" value="10">
                bushkit EQ 1
            </test>
            <output>/fdm/jsbsim/inertia/pointmass-weight-lbs[6]</output>
        </switch>

        <switch name="extra-weight-left-36-in">
            <default value="0"/>
            <test logic="AND" value="15">
                bushkit EQ 2
            </test>
            <output>/fdm/jsbsim/inertia/pointmass-weight-lbs[7]</output>
        </switch>

        <switch name="extra-weight-right-36-in">
            <default value="0"/>
            <test logic="AND" value="15">
                bushkit EQ 2
            </test>
            <output>/fdm/jsbsim/inertia/pointmass-weight-lbs[8]</output>
        </switch>

        <switch name="extra-weight-left-ski">
            <default value="0"/>
            <test logic="AND" value="18">
                bushkit EQ 5
            </test>
            <output>/fdm/jsbsim/inertia/pointmass-weight-lbs[13]</output>
        </switch>

        <switch name="extra-weight-right-ski">
            <default value="0"/>
            <test logic="AND" value="18">
                bushkit EQ 5
            </test>
            <output>/fdm/jsbsim/inertia/pointmass-weight-lbs[14]</output>
        </switch>

        <switch name="extra-weight-180hp">
        <!-- Aircraft **Basic Empty Weight** difference. 160 hp: 1467 lbs, 180 hp: 1642 lbs -->
            <default value="0"/>
            <test logic="AND" value="175">
                /controls/engines/active-engine EQ 1
            </test>
            <output>/fdm/jsbsim/inertia/pointmass-weight-lbs[15]</output>
        </switch>

        <fcs_function name="extra-weight-left-float">
            <function>
                <product>
                    <eq>
                        <property>bushkit</property>
                        <value>3</value>
                    </eq>
                    <sum>
                        <value>66.0</value>
                        <property>left-pontoon/leaked-water-lbs</property>
                    </sum>
                </product>
            </function>
            <output>inertia/pointmass-weight-lbs[9]</output>
        </fcs_function>

        <fcs_function name="extra-weight-right-float">
            <function>
                <product>
                    <eq>
                        <property>bushkit</property>
                        <value>3</value>
                    </eq>
                    <sum>
                        <value>66.0</value>
                        <property>right-pontoon/leaked-water-lbs</property>
                    </sum>
                </product>
            </function>
            <output>inertia/pointmass-weight-lbs[10]</output>
        </fcs_function>

        <fcs_function name="extra-weight-left-amphibious">
            <function>
                <product>
                    <eq>
                        <property>bushkit</property>
                        <value>4</value>
                    </eq>
                    <sum>
                        <value>138.0</value>
                        <property>left-pontoon/leaked-water-lbs</property>
                    </sum>
                </product>
            </function>
            <output>inertia/pointmass-weight-lbs[11]</output>
        </fcs_function>

        <fcs_function name="extra-weight-right-amphibious">
            <function>
                <product>
                    <eq>
                        <property>bushkit</property>
                        <value>4</value>
                    </eq>
                    <sum>
                        <value>138.0</value>
                        <property>right-pontoon/leaked-water-lbs</property>
                    </sum>
                </product>
            </function>
            <output>inertia/pointmass-weight-lbs[12]</output>
        </fcs_function>
    </channel>

    <channel name="extra-drag">
        <switch name="drag-26-in">
            <default value="1"/>
            <test logic="AND" value="1.05">
                bushkit EQ 1
            </test>
            <output>gear/drag-coef[1]</output>
        </switch>

        <switch name="drag-36-in">
            <default value="1"/>
            <test logic="AND" value="1.07">
                bushkit EQ 2
            </test>
            <output>gear/drag-coef[2]</output>
        </switch>

        <switch name="drag-Floats">
            <default value="1"/>
            <test logic="AND" value="1.20">
                bushkit EQ 3
            </test>
            <output>gear/drag-coef[3]</output>
        </switch>

        <switch name="drag-Amphibious">
            <default value="1"/>
            <test logic="AND" value="1.22">
                bushkit EQ 4
            </test>
            <output>gear/drag-coef[4]</output>
        </switch>

        <switch name="drag-Ski">
            <default value="1"/>
            <test logic="AND" value="1.20"> <!-- 36 inch wheel plus ski  -->
                bushkit EQ 5
            </test>
            <output>gear/drag-coef[5]</output>
        </switch>
    </channel>

    <channel name="JSBSim hydrodynamics integration">
        <fcs_function name="tmp/floats-enabled-norm">
            <function>
                <product>
                    <value>1.0</value>
                    <le>
                        <value>3.0</value>
                        <property>bushkit</property>
                    </le>
                    <le>
                        <property>bushkit</property>
                        <value>4.0</value>
                    </le>
                    <not>
                        <and>
                            <property>contact/unit[13]/solid</property>
                            <property>contact/unit[14]/solid</property>
                            <property>contact/unit[15]/solid</property>
                            <property>contact/unit[16]/solid</property>
                            <property>contact/unit[17]/solid</property>
                            <property>contact/unit[18]/solid</property>
                        </and>
                    </not>
                </product>
            </function>
            <output>floats-enabled-norm</output>
        </fcs_function>

        <!-- FIXME: The delayed activation isn't pretty but, apparently,
                    the solid property isn't reliable at the begining. -->
        <switch name="left-pontoon/contact-z-pos-in[0]">
            <default value="38.5"/>
            <test logic="AND" value="-50.0">
                simulation/sim-time-sec GT 0.25
                bushkit GE 3
                bushkit LE 4
                contact/unit[13]/solid NE 0
            </test>
            <output>contact/unit[13]/z-position</output>
        </switch>

        <switch name="left-pontoon/contact-z-pos-in[1]">
            <default value="38.5"/>
            <test logic="AND" value="-44.2">
                simulation/sim-time-sec GT 0.25
                bushkit GE 3
                bushkit LE 4
                contact/unit[15]/solid NE 0
            </test>
            <output>contact/unit[15]/z-position</output>
        </switch>

        <switch name="left-pontoon/contact-z-pos-in[2]">
            <default value="38.5"/>
            <test logic="AND" value="-24.5">
                simulation/sim-time-sec GT 0.25
                bushkit GE 3
                bushkit LE 4
                contact/unit[17]/solid NE 0
            </test>
            <output>contact/unit[17]/z-position</output>
        </switch>

        <switch name="right-pontoon/contact-z-pos-in[0]">
            <default value="38.5"/>
            <test logic="AND" value="-50.0">
                simulation/sim-time-sec GT 0.25
                bushkit GE 3
                bushkit LE 4
                contact/unit[14]/solid NE 0
            </test>
            <output>contact/unit[14]/z-position</output>
        </switch>

        <switch name="right-pontoon/contact-z-pos-in[1]">
            <default value="38.5"/>
            <test logic="AND" value="-44.2">
                bushkit GE 3
                bushkit LE 4
                contact/unit[16]/solid NE 0
            </test>
            <output>contact/unit[16]/z-position</output>
        </switch>

        <switch name="right-pontoon/contact-z-pos-in[2]">
            <default value="38.5"/>
            <test logic="AND" value="-24.5">
                simulation/sim-time-sec GT 0.25
                bushkit GE 3
                bushkit LE 4
                contact/unit[18]/solid NE 0
            </test>
            <output>contact/unit[18]/z-position</output>
        </switch>

        <fcs_function name="left-pontoon/leakage-lbs_sec">
            <function>
                <product>
                    <property>hydro/active-norm</property>
                    <switch>
                        <property>pontoon-damage/left-pontoon</property>
                        <value>0.0</value>
                        <value>250.0</value>
                        <value>5.0</value>
                    </switch>
                </product>
            </function>
        </fcs_function>

        <!-- No water leakage as long as pontoon is not damaged or broken -->
        <switch name="left-pontoon/leaked-water-windup-trigger">
            <default value="-1"/>

            <test logic="AND" value="0">
                pontoon-damage/left-pontoon GT 0
            </test>
        </switch>

        <!-- If trigger is negative then the integrator is reset to 0 -->
        <pid name="left-pontoon/leaked-water-lbs">
            <input>left-pontoon/leakage-lbs_sec</input>
            <trigger>left-pontoon/leaked-water-windup-trigger</trigger>
            <kp>0.0</kp>
            <ki>1.0</ki>
            <kd>0.0</kd>
            <clipto>
                <min>0.0</min>
                <max>2500.0</max> <!-- FIXME: Too much? Or too little? -->
            </clipto>
        </pid>

        <fcs_function name="right-pontoon/leakage-lbs_sec">
            <function>
                <product>
                    <property>hydro/active-norm</property>
                    <switch>
                        <property>pontoon-damage/right-pontoon</property>
                        <value>0.0</value>
                        <value>250.0</value>
                        <value>5.0</value>
                    </switch>
                </product>
            </function>
        </fcs_function>

        <!-- No water leakage as long as pontoon is not damaged or broken -->
        <switch name="right-pontoon/leaked-water-windup-trigger">
            <default value="-1"/>

            <test logic="AND" value="0">
                pontoon-damage/right-pontoon GT 0
            </test>
        </switch>

        <!-- If trigger is negative then the integrator is reset to 0 -->
        <pid name="right-pontoon/leaked-water-lbs">
            <input>right-pontoon/leakage-lbs_sec</input>
            <trigger>right-pontoon/leaked-water-windup-trigger</trigger>
            <kp>0.0</kp>
            <ki>1.0</ki>
            <kd>0.0</kd>
            <clipto>
                <min>0.0</min>
                <max>2500.0</max> <!-- FIXME: Too much? Or too little? -->
            </clipto>
        </pid>
    </channel>

</system>