<?xml version="1.0" encoding="UTF-8" ?>
<!--
    CRJ700-ap-rules.xml - Bombardier CRJ700 series autopilot configuration

    This file is included in CRJ700-main.xml as <property-rule>, e.g.
    property rules in this file will run at frame rate, which is lower than FDM
    rate.
    What to put in this file:
    - Things that depend on an often changing input or multiple inputs but
      output is not used as input for AP controllers, e.g. annunciators

    see also:
    - CRJ700-autopilot.xml for rules that run at FDM rate (higher precision)
    - Nasal/autopilot.nas for handling of MCP switches, logic etc.
-->
<PropertyList>
    <logic>
        <name>Autopilot engaged</name>
        <input>
            <and>
                <property>autopilot/autoflight-serviceable</property>
                <property>controls/autoflight/autopilot/engage</property>
                <greater-than>
                    <property>systems/DC/outputs/afcs-l</property>
                    <value>15</value>
                </greater-than>
            </and>
        </input>
        <output>autopilot/internal/autoflight-engaged</output>
    </logic>
    <logic>
        <name>Flight director engaged</name>
        <input>
            <and>
                <property>autopilot/flight-director-serviceable</property>
                <property>controls/autoflight/flight-director/engage</property>
                <greater-than>
                    <property>systems/DC/outputs/afcs-l</property>
                    <value>15</value>
                </greater-than>
            </and>
        </input>
        <output>autopilot/internal/flight-director-engaged</output>
    </logic>
    <logic>
        <name>Autothrottle engaged</name>
        <input>
            <and>
                <property>sim/config/allow-autothrottle</property>
                <property>controls/autoflight/autothrottle-engage</property>
                <greater-than>
                    <property>systems/DC/outputs/afcs-l</property>
                    <value>15</value>
                </greater-than>
            </and>
        </input>
        <output>autopilot/internal/autothrottle-engaged</output>
    </logic>
    <!-- output properties not directly used in AP controllers -->
    <filter>
        <name>Altitude preselect deviation</name>
        <type>gain</type>
        <debug type="bool">false</debug>
        <gain>1</gain>
        <input>
            <property>controls/autoflight/altitude-select</property>
            <offset>
                <property>instrumentation/altimeter[0]/indicated-altitude-ft</property>
                <scale>-1</scale>
            </offset>
        </input>
        <output>autopilot/internal/alts-error-ft</output>
    </filter>
    <logic>
        <name>Altitude selected alert</name>
        <input>
            <and>
                <not-equals>
                    <property>controls/autoflight/vert-mode</property>
                    <value>3</value>
                </not-equals>
                <less-than>
                    <property>autopilot/internal/alts-error-ft</property>
                    <value>900</value>
                </less-than>
                <greater-than>
                    <property>autopilot/internal/alts-error-ft</property>
                    <value>-900</value>
                </greater-than>
            </and>
        </input>
        <output>autopilot/internal/alts-alert</output>
    </logic>
    <logic>
        <name>Altitude capture</name>
        <input>
            <and>
                <less-than>
                    <property>autopilot/internal/alts-error-ft</property>
                    <value>200</value>
                </less-than>
                <greater-than>
                    <property>autopilot/internal/alts-error-ft</property>
                    <value>-200</value>
                </greater-than>
            </and>
        </input>
        <output>autopilot/internal/alts-capture</output>
    </logic>
    <logic>
        <name>MDA alert</name>
        <input>
            <less-than-equals>
                <property>instrumentation/altimeter/indicated-altitude-ft</property>
                <property>instrumentation/adc/reference/mda</property>
            </less-than-equals>
        </input>
        <output>autopilot/annunciators/mda-alert</output>
    </logic>
    
    <!-- Instrumentation stuff not used in AP controllers -->
    <filter>
        <name>FD pitch indication</name>
        <type>gain</type>
        <debug type="bool">false</debug>
        <gain>1</gain>
        <input>
            <property>instrumentation/attitude-indicator/indicated-pitch-deg</property>
            <offset>
                <property>autopilot/internal/target-pitch-deg</property>
                <scale>-1</scale>
            </offset>
        </input>
        <output>instrumentation/efis/pfd[0]/fd-pitch-deg</output>
        <period>
            <min>-30</min>
            <max>30</max>
        </period>
    </filter>
    <filter>
        <name>FD roll indication</name>
        <type>gain</type>
        <debug type="bool">false</debug>
        <gain>1</gain>
        <input>
            <property>autopilot/internal/target-roll-deg</property>
        </input>
        <output>instrumentation/efis/pfd[0]/fd-roll-deg</output>
    </filter>
    <filter>
        <name>VOR1 heading error computer/normalizer (instrument)</name>
        <type>gain</type>
        <debug type="bool">false</debug>
        <gain>1</gain>
        <input>
            <property>instrumentation/nav[0]/heading-deg</property>
            <offset>
                <property>instrumentation/heading-indicator[0]/indicated-heading-deg</property>
                <scale>-1</scale>
            </offset>
        </input>
        <output>instrumentation/nav[0]/heading-error-deg</output>
        <period>
            <min>-180</min>
            <max>180</max>
        </period>
    </filter>
    <filter>
        <name>VOR2 heading error computer/normalizer (instrument)</name>
        <type>gain</type>
        <debug type="bool">false</debug>
        <gain>1</gain>
        <input>
            <property>instrumentation/nav[1]/heading-deg</property>
            <offset>
                <property>instrumentation/heading-indicator[0]/indicated-heading-deg</property>
                <scale>-1</scale>
            </offset>
        </input>
        <output>instrumentation/nav[1]/heading-error-deg</output>
        <period>
            <min>-180</min>
            <max>180</max>
        </period>
    </filter>
    <filter>
        <name>ADF1 bearing error computer/normalizer</name>
        <type>gain</type>
        <debug type="bool">false</debug>
        <gain>1</gain>
        <input>
            <property>instrumentation/adf[0]/indicated-bearing-deg</property>
            <offset>
                <property>instrumentation/heading-indicator[0]/indicated-heading-deg</property>
                <scale>-1</scale>
            </offset>
        </input>
        <output>instrumentation/adf[0]/indicated-bearing-error-deg</output>
        <period>
            <min>-180</min>
            <max>180</max>
        </period>
    </filter>
    <filter>
        <name>ADF2 bearing error computer/normalizer</name>
        <type>gain</type>
        <debug type="bool">false</debug>
        <gain>1</gain>
        <input>
            <property>instrumentation/adf[1]/indicated-bearing-deg</property>
            <offset>
                <property>instrumentation/heading-indicator[0]/indicated-heading-deg</property>
                <scale>-1</scale>
            </offset>
        </input>
        <output>instrumentation/adf[1]/indicated-bearing-error-deg</output>
        <period>
            <min>-180</min>
            <max>180</max>
        </period>
    </filter>

    <filter>
        <name>VNAV altitude difference</name>
        <debug type="bool">false</debug>
        <type>gain</type>
        <gain>1</gain>
        <input>
            <expression>
                <dif>
                    <property>instrumentation/altimeter/indicated-altitude-ft</property>
                    <property>autopilot/route-manager/vnav/target-altitude-ft</property>
                </dif>
            </expression>
        </input>
        <output>autopilot/internal/vnav-difference-ft</output>
    </filter>

    <logic>
        <name>VOR1 captured</name>
        <input>
            <and>
                <greater-than>
                    <property>autopilot/internal/nav1-radial-deviation-deg</property>
                    <value>-5</value>
                </greater-than>
                <less-than>
                    <property>autopilot/internal/nav1-radial-deviation-deg</property>
                    <value>5</value>
                </less-than>
            </and>
        </input>
        <output>autopilot/internal/vor1-captured</output>
    </logic>
    <logic>
        <name>VOR2 captured</name>
        <input>
            <and>
                <greater-than>
                    <property>autopilot/internal/nav2-radial-deviation-deg</property>
                    <value>-5</value>
                </greater-than>
                <less-than>
                    <property>autopilot/internal/nav2-radial-deviation-deg</property>
                    <value>5</value>
                </less-than>
            </and>
        </input>
        <output>autopilot/internal/vor2-captured</output>
    </logic>
    <logic>
        <name>FMS1 captured</name>
        <input>
            <and>
                <greater-than>
                    <property>autopilot/internal/true-heading-error-deg</property>
                    <value>-5</value>
                </greater-than>
                <less-than>
                    <property>autopilot/internal/true-heading-error-deg</property>
                    <value>5</value>
                </less-than>
            </and>
        </input>
        <output>autopilot/internal/fms1-captured</output>
    </logic>
</PropertyList>
