<!DOCTYPE airframe SYSTEM "airframe.dtd">

<!-- Mentor Multiplex (http://www.multiplex-rc.de/)
     Twog v1 board (http://paparazzi.enac.fr/wiki/Twog_v1)
     Tilted infrared sensor
     XBee modem in transparent mode.
     Radio PPM encoder
-->

<airframe name="Mentor Twog AkaModell">

  <firmware name="fixedwing">
    <target name="ap" 			board="twog_1.0"/>
    <target name="sim" 			board="pc"/>
    <target name="jsbsim" 		board="pc"/>

    <define name="AGR_CLIMB"/>
    <define name="LOITER_TRIM"/>
    <define name="WIND_INFO"/>

    <subsystem name="radio_control" type="ppm"/>
    <!--<define name="TELEMETRY_MODE_FBW" value="1"/>-->
    <subsystem name="telemetry" type="transparent"/>
    <subsystem name="control"/>
    <subsystem name="ahrs" type="infrared"/>
    <subsystem name="gps" type="ublox"/>
    <subsystem name="navigation" type="extra"/>
    <subsystem name="ins" type="alt_float"/>
  </firmware>

  <firmware name="setup">
    <target name="tunnel"          board="twog_1.0" />
    <target name="usb_tunnel_0"    board="twog_1.0" />
  </firmware>

  <modules main_freq="60">
    <load name="servo_switch.xml">
      <define name="SERVO_SWITCH_ON_VALUE"  value="1100"/>
      <define name="SERVO_SWITCH_OFF_VALUE" value="1900"/>
      <define name="SERVO_SWITCH_SERVO"     value="SERVO_SWITCH"/>
    </load>
    <load name="sys_mon.xml"/>
    <load name="infrared_adc.xml"/>
  </modules>

<!-- commands section -->
  <commands>
    <axis name="THROTTLE" failsafe_value="0"/>
    <axis name="ROLL" failsafe_value="0"/>
    <axis name="PITCH" failsafe_value="0"/>
    <axis name="YAW" failsafe_value="0"/>
  </commands>

  <servos>
    <servo name="AILERON_RIGHT" no="0" min="1000" neutral="1500" max="2000"/>
    <servo name="ELEVATOR" no="2" min="2000" neutral="1500" max="1000"/>
    <servo name="THROTTLE" no="3" min="1000" neutral="1000" max="2000"/>
    <servo name="RUDDER" no="4" min="2000" neutral="1500" max="1000"/>
    <servo name="AILERON_LEFT" no="6" min="1000" neutral="1500" max="2000"/>
    <servo name="SWITCH" no="7" min="1000" neutral="1500" max="2000"/>
  </servos>

  <rc_commands>
    <set command="ROLL" value="@ROLL"/>
    <set command="PITCH" value="@PITCH"/>
    <set command="THROTTLE" value="@THROTTLE"/>
    <set command="YAW" value="@YAW"/>
  </rc_commands>

  <auto_rc_commands>
   <!-- <set command="YAW" value="@YAW"/> -->
  </auto_rc_commands>

  <section name="MIXER">
     <define name="AILERON_DIFF" value="0.66"/>
     <define name="COMBI_SWITCH" value="0.3"/>
  </section>

  <command_laws>
    <set servo="THROTTLE" value="@THROTTLE"/>
    <set servo="ELEVATOR" value="@PITCH"/>
    <set servo="RUDDER" value="@YAW + @ROLL*COMBI_SWITCH"/>

    <let var="roll" value="@ROLL"/>
    <set servo="AILERON_LEFT" value="($roll > 0 ? AILERON_DIFF : 1) * $roll"/>
    <set servo="AILERON_RIGHT" value="($roll > 0 ? 1 : AILERON_DIFF) * $roll"/>
  </command_laws>


  <section name="AUTO1" prefix="AUTO1_">
    <define name="MAX_ROLL" value="55" unit="deg"/>
    <define name="MAX_PITCH" value="35" unit="deg"/>
  </section>


  <section name="INFRARED" prefix="IR_">
    <define name="ADC_IR1_NEUTRAL" value="511"/>
    <define name="ADC_IR2_NEUTRAL" value="516"/>
    <define name="ADC_TOP_NEUTRAL" value="512"/>

    <define name="LATERAL_CORRECTION" value="0.35"/>
    <define name="LONGITUDINAL_CORRECTION" value="0.35"/>
    <define name="VERTICAL_CORRECTION" value="1.0"/>

    <define name="HORIZ_SENSOR_TILTED" value="1"/>

    <define name="IR1_SIGN" value="1"/>
    <define name="IR2_SIGN" value="-1"/>
    <define name="TOP_SIGN" value="-1"/>

    <define name="ROLL_NEUTRAL_DEFAULT" value="0." unit="deg"/>
    <define name="PITCH_NEUTRAL_DEFAULT" value="11.4591560364" unit="deg"/>

    <define name="CORRECTION_UP" value="1."/>
    <define name="CORRECTION_DOWN" value="1."/>
    <define name="CORRECTION_LEFT" value="1."/>
    <define name="CORRECTION_RIGHT" value="1."/>
  </section>


  <section name="BAT">
    <define name="MILLIAMP_AT_FULL_THROTTLE" value="20000" unit="mA"/>
    <define name="CATASTROPHIC_BAT_LEVEL" value="12" unit="V"/>
    <define name="CRITIC_BAT_LEVEL" value="12.5" unit="V"/>
    <define name="LOW_BAT_LEVEL" value="13.5" unit="V"/>
    <define name="MAX_BAT_LEVEL" value="16" unit="V"/>
  </section>

  <section name="MISC">
    <define name="MINIMUM_AIRSPEED" value="12." unit="m/s"/>
    <define name="NOMINAL_AIRSPEED" value="15." unit="m/s"/>
    <define name="MAXIMUM_AIRSPEED" value="20." unit="m/s"/>
    <define name="CARROT" value="5." unit="s"/>
    <define name="KILL_MODE_DISTANCE" value="(1.5*MAX_DIST_FROM_HOME)"/>
    <define name="CONTROL_FREQUENCY" value="60" unit="Hz"/>
    <define name="ALT_KALMAN_ENABLED" value="TRUE"/>

    <define name="TRIGGER_DELAY" value="1."/>
    <define name="DEFAULT_CIRCLE_RADIUS" value="100."/>
  </section>

  <section name="VERTICAL CONTROL" prefix="V_CTL_">
    <define name="POWER_CTL_BAT_NOMINAL" value="14.8" unit="volt"/>
    <!-- outer loop proportional gain -->
    <define name="ALTITUDE_PGAIN" value="0.04" unit="(m/s)/m"/>
    <!-- outer loop saturation -->
    <define name="ALTITUDE_MAX_CLIMB" value="2.5" unit="m/s"/>

    <!-- auto throttle inner loop -->
    <define name="AUTO_THROTTLE_NOMINAL_CRUISE_THROTTLE" value="0.55" unit="%"/>
    <define name="AUTO_THROTTLE_MIN_CRUISE_THROTTLE" value="0.30" unit="%"/>
    <define name="AUTO_THROTTLE_MAX_CRUISE_THROTTLE" value="0.80" unit="%"/>
    <define name="AUTO_THROTTLE_LOITER_TRIM" value="1500" unit="pprz_t"/>
    <define name="AUTO_THROTTLE_DASH_TRIM" value="-1000" unit="pprz_t"/>
    <define name="AUTO_THROTTLE_CLIMB_THROTTLE_INCREMENT" value="0.1" unit="%/(m/s)"/>
    <define name="AUTO_THROTTLE_PGAIN" value="0.02"/>
    <define name="AUTO_THROTTLE_IGAIN" value="0.1"/>
    <define name="AUTO_THROTTLE_PITCH_OF_VZ_PGAIN" value="0.5" unit="rad/(m/s)"/>

    <!-- auto pitch inner loop -->
    <define name="AUTO_PITCH_PGAIN" value="0.05"/>
    <define name="AUTO_PITCH_IGAIN" value="0.075"/>
    <define name="AUTO_PITCH_MAX_PITCH" value="0.35" unit="rad"/>
    <define name="AUTO_PITCH_MIN_PITCH" value="-0.35" unit="rad"/>

   <define name="THROTTLE_SLEW" value="0.25"/>
  </section>

  <section name="HORIZONTAL CONTROL" prefix="H_CTL_">
    <define name="COURSE_PGAIN" value="1.1"/>

    <define name="ROLL_MAX_SETPOINT"  value="50" unit="deg"/>
    <define name="PITCH_MAX_SETPOINT" value="30" unit="deg"/>
    <define name="PITCH_MIN_SETPOINT" value="-25" unit="deg"/>

    <define name="PITCH_PGAIN" value="7000."/>
    <define name="PITCH_DGAIN" value="1.5"/>

    <define name="AILERON_OF_THROTTLE" value="0.0"/>
    <define name="ELEVATOR_OF_ROLL" value="1250"/>

    <define name="ROLL_ATTITUDE_GAIN" value="7500."/>
    <!-- <define name="ROLL_RATE_GAIN" value="1500"/> -->
  </section>

  <section name="NAV">
    <define name="NAV_GLIDE_PITCH_TRIM" value="0"/>
  </section>

  <section name="AGGRESSIVE" prefix="AGR_">
    <define name="BLEND_START" value="25"/><!-- Altitude Error to Initiate Aggressive Climb CANNOT BE ZERO!!-->
    <define name="BLEND_END" value="10"/><!-- Altitude Error to Blend Aggressive to Regular Climb Modes  CANNOT BE ZERO!!-->
    <define name="CLIMB_THROTTLE" value="0.8"/><!-- Gaz for Aggressive Climb -->
    <define name="CLIMB_PITCH" value="0.3"/><!-- Pitch for Aggressive Climb -->
    <define name="DESCENT_THROTTLE" value="0.1"/><!-- Gaz for Aggressive Decent -->
    <define name="DESCENT_PITCH" value="-0.15"/><!-- Pitch for Aggressive Decent -->
    <define name="CLIMB_NAV_RATIO" value="0.8"/><!-- Percent Navigation for Altitude Error Equal to Start Altitude -->
    <define name="DESCENT_NAV_RATIO" value="1.0"/>
    </section>

  <section name="FAILSAFE" prefix="FAILSAFE_">
    <define name="DELAY_WITHOUT_GPS" value="1" unit="s"/>
    <define name="DEFAULT_THROTTLE" value="0.3" unit="%"/>
    <define name="DEFAULT_ROLL" value="0.3" unit="rad"/>
    <define name="DEFAULT_PITCH" value="0.5" unit="rad"/>
    <define name="HOME_RADIUS" value="70" unit="m"/>
  </section>

  <section name="SIMU">
    <define name="JSBSIM_MODEL" value="&quot;Malolo1&quot;"/>
    <define name="JSBSIM_IR_ROLL_NEUTRAL" value="0." unit="deg"/>
    <define name="JSBSIM_IR_PITCH_NEUTRAL" value="0." unit="deg"/>
  </section>

</airframe>
