<!DOCTYPE airframe SYSTEM "../airframe.dtd">

<!-- Minion Airframe 
  aileron, elevator, rudder, flaps (optional)
     TWOG - 4S compliant
     UGEAR 2.0
     Xbee modem in API mode
     Airspeed Sensor - with Airspeed warning
-->

<airframe name="Pinot Ummarine Lite V2">

<!-- Servo Configuration -->
<servos>
  <servo name="THROTTLE"    no="0" min="1100" neutral="1100" max="1900"/>
  <servo name="AILERON_RIGHT"    no="1" min="1900" neutral="1500" max="1100"/>
  <servo name="AILERON_LEFT"  no="2" min="1900" neutral="1500" max="1100"/>
  <servo name="ELEVATOR"     no="3" min="1100" neutral="1500" max="1900"/>
  <servo name="RUDDER"     no="4" min="1100" neutral="1500" max="1900"/>
  <servo name="FLAP"    no="5" min="1900" neutral="1900" max="1100"/>
  <servo name="KNOB"    no="6" min="1100" neutral="1500" max="1900"/>
</servos>


<!-- Servo Command Structure -->
<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"/>
  <axis name="FLAP"  failsafe_value="0"/>
  <axis name="CROW"  failsafe_value="0"/>
</commands>

<!-- RC Command Structure -->
<rc_commands>
  <set command="THROTTLE" value="@THROTTLE"/>
  <set command="ROLL"     value="@ROLL"/>
  <set command="PITCH"    value="@PITCH"/>
  <set command="YAW"  value="@YAW"/>
  <set command="FLAP"      value="@FLAP"/>
  <set command="CROW"  value="@KNOB"/>
</rc_commands>

<!-- Define RC commands to Servo in Auto Mode -->
<auto_rc_commands>
  <set command="YAW"   value="@YAW"/>
  <set command="FLAP"      value="@FLAP"/>
  <set command="CROW"  value="@KNOB"/>
</auto_rc_commands>

<!-- Define Mixing Parameters -->
<section name="MIXER">
  <define name="AILERON_AILERON_RATE" value="0.9"/>
  <define name="AILERON_CROW_RATE" value="0.6"/>
  <define name="FLAP_CROW_RATE" value="1.0"/>
  <define name="ELEV_CROW_RATE" value="0.35"/>
  <define name="ELEV_ELEV_RATE" value="0.8"/>
</section>


<!-- Define RC commands to Servos in Manual -->  
<command_laws>
  <let var="aileron" value="@ROLL * AILERON_AILERON_RATE"/>
  <let var="crow" value="@CROW * AILERON_CROW_RATE"/>
  <let var="crowf" value="@CROW * FLAP_CROW_RATE"/>
  <let var="elevf" value="@CROW * ELEV_CROW_RATE"/>
  <let var="elevator" value="@PITCH * ELEV_ELEV_RATE"/>
        
  <set servo="THROTTLE"     value="@THROTTLE"/>
  <set servo="AILERON_RIGHT"    value="$aileron + $crow"/>  <!--flip signs if necessary -->
  <set servo="AILERON_LEFT"    value=" - $aileron + $crow"/>
  <set servo="ELEVATOR"      value="$elevator - $elevf"/>
  <set servo="RUDDER"      value="@YAW"/>
  <set servo="FLAP"      value="$crowf"/>
  <set servo="KNOB"      value="@FLAP"/>
  
</command_laws>

<!-- Define Max Roll and Pitch setpoints in Auto1 -->
<section name="AUTO1" prefix="AUTO1_">
  <define name="MAX_ROLL" value="0.6"/>
  <define name="MAX_PITCH" value="0.6"/>
</section>

<!-- Define Roll, Pitch Neutrals -->
<section name="INS" prefix="INS_">
  <define name="ROLL_NEUTRAL_DEFAULT" value="0" unit="deg"/>
  <define name="PITCH_NEUTRAL_DEFAULT" value="0" unit="deg"/>
</section>

<!-- Define current estimator and Battery Level Warnings -->
<section name="BAT">
  <define name="MILLIAMP_AT_FULL_THROTTLE" value="50000"/>
  <define name="CURRENT_ESTIMATION_NONLINEARITY" value="1.3"/>
  <define name="CATASTROPHIC_BAT_LEVEL" value="13.5" unit="V"/>
  <define name="LOW_BAT_LEVEL" value="14.1" unit="V"/>
   <define name="CRITIC_BAT_LEVEL" value="13.8" unit="V"/>
  <define name="MAX_BAT_LEVEL" value="16.5" unit="V" />
   <!--<define name="VoltageOfAdc(adc)" value="(0.029*adc)"/>-->
</section>

<!-- Define Airspeed Warnings for GCS (optional) -->
<section name="AIRSPEED_WARNING">
  <define name="WARNING_AIRSPEED" value="12"/>
  <define name="CRITIC_AIRSPEED"  value="10"/>
  <define name="CATASTROPHIC_AIRSPEED"  value="8"/>
</section>

<section name="MISC">
  <define name="NOMINAL_AIRSPEED"   value="16."   unit="m/s"/> <!-- GCS only -->
  <define name="CARROT"       value="5."   unit="s"/>   <!-- GCS only -->
  <define name="KILL_MODE_DISTANCE"   value="(1.5*MAX_DIST_FROM_HOME)"/>
  <define name="CONTROL_RATE"     value="60"   unit="Hz"/>
  <define name="XBEE_INIT"     value="&quot;ATRN5\rATTT80\r&quot;"/>  
  <!-- ATRN - random delay for retransmits -->
  <!-- ATTT - streaming limit before a delay is forced -->
  <define name="ALT_KALMAN_ENABLED"   value="TRUE"/>
  <define name="DEFAULT_CIRCLE_RADIUS"   value="150."/>
  <define name="DEFAULT_LANDING_CIRCLE_RADIUS" value="150"/>
  <define name="RC_LOST_MODE" value="AP_MODE_AUTO2"/>
</section>

<!-- Vertical Control done with Throttle and Pitch-->
<section name="VERTICAL CONTROL" prefix="V_CTL_">
  <define name="POWER_CTL_BAT_NOMINAL"   value="23.2" unit="volt"/>
  <define name="ALTITUDE_PGAIN"     value="0.136" unit="(m/s)/m"/>   <!-- outer loop proportional gain -->
  <define name="ALTITUDE_MAX_CLIMB"   value="3." unit="m/s"/>  <!-- outer loop saturation -->

  <!-- auto throttle inner loop -->
  <define name="AUTO_THROTTLE_NOMINAL_CRUISE_THROTTLE"   value="0.42" unit="%"/>
  <define name="AUTO_THROTTLE_MIN_CRUISE_THROTTLE"   value="0.20" unit="%"/>
  <define name="AUTO_THROTTLE_MAX_CRUISE_THROTTLE"   value="0.70" unit="%"/>
  <define name="AUTO_THROTTLE_LOITER_TRIM"     value="1500" unit="pprz_t"/>
  <define name="AUTO_THROTTLE_DASH_TRIM"       value="-4000" unit="pprz_t"/>
  <define name="AUTO_THROTTLE_CLIMB_THROTTLE_INCREMENT"   value="0.05"   unit="%/(m/s)"/>
  <define name="AUTO_THROTTLE_PGAIN"       value="0.010"/>
  <define name="AUTO_THROTTLE_IGAIN"       value="0.01"/>
  <define name="AUTO_THROTTLE_DGAIN"       value="0.00"/>
  <define name="AUTO_THROTTLE_PITCH_OF_VZ_PGAIN"     value="0.142"/>
  <define name="AUTO_THROTTLE_PITCH_OF_VZ_DGAIN"     value="0.00"/>
  <define name="THROTTLE_SLEW_LIMITER"       value="2"   unit="s"/>

  <!-- auto pitch inner loop -->
  <define name="AUTO_PITCH_PGAIN"       value="0.1"/>
  <define name="AUTO_PITCH_IGAIN"       value="0.025"/>
  <define name="AUTO_PITCH_MAX_PITCH"       value="0.31415"   unit="rad"/>
  <define name="AUTO_PITCH_MIN_PITCH"       value="-0.31415"   unit="rad"/>

    <!-- landing control loop -->
    <define name="LANDING_THROTTLE_PGAIN"           value="600.0"/>
    <define name="LANDING_THROTTLE_IGAIN"           value="10.0"/>
    <define name="LANDING_THROTTLE_MAX"           value="0.65"/>
    <define name="LANDING_DESIRED_SPEED"           value="18" unit="m/s"/>
    <define name="LANDING_PITCH_PGAIN"           value="0.1"/>
    <define name="LANDING_PITCH_IGAIN"           value="0.1"/>
    <define name="LANDING_PITCH_LIMITS"           value="0.2" unit="rad"/>
    <define name="LANDING_PITCH_FLARE"           value="0.06"/>
    <define name="LANDING_ALT_THROTTLE_KILL"           value="15" unit="m"/>
    <define name="LANDING_ALT_FLARE"           value="5" unit="m"/>
</section>

<section name="HORIZONTAL CONTROL" prefix="H_CTL_">
  <define name="COURSE_PGAIN"   value="1.135"/>
  <define name="COURSE_DGAIN"   value="0.35"/>

  <define name="ROLL_MAX_SETPOINT"   value="0.6"   unit="rad"/>
  <define name="PITCH_MAX_SETPOINT"   value="0.35"   unit="rad"/>
  <define name="PITCH_MIN_SETPOINT"   value="-0.2"   unit="rad"/>

  <define name="PITCH_PGAIN"     value="12900."/>
  <define name="PITCH_DGAIN"     value="1.5"/>
  <define name="ROLL_ATTITUDE_GAIN"   value="9000"/>
  <define name="ROLL_RATE_GAIN"     value="1600"/>

  <define name="ELEVATOR_OF_ROLL"   value="1250"/>
  <define name="ROLL_SLEW"     value="0.1"/>
</section>

<section name="AGGRESSIVE" prefix="AGR_">
  <define name="BLEND_START"     value="20"/>  <!-- 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.75"/>  <!-- Throttle for Aggressive Climb -->
  <define name="CLIMB_PITCH"     value="0.13"/>  <!-- Pitch for Aggressive Climb -->
  <define name="DESCENT_THROTTLE"   value="0.10"/>  <!-- Throttle for Aggressive Decent -->
  <define name="DESCENT_PITCH"     value="-0.17"/>  <!-- Pitch for Aggressive Decent in radians-->
  <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>

<!-- Launcher Takeoff Configuration -->
<section name="Takeoff" prefix="Takeoff_">
  <define name="Pitch" value="0.23" unit="rad"/>
  <define name="Height" value="70" unit="m"/>
  <define name="Speed" value="8" unit="m/s"/>
  <define name="Distance" value="30" unit="m"/>
  <define name="MinSpeed" value="5" unit="m/s"/>
</section>

<!-- Skid Landing Configuration -->
<section name="Landing" prefix="Landing_">
    <define name="AFHeight" value="50" unit="m"/>
    <define name="FinalHeight" value="50" unit="m"/>
    <define name="FinalStageTime" value="10" unit="s"/>
</section>

<!-- Define Kill Position -->
<section name="FAILSAFE" prefix="FAILSAFE_">
    <define name="DELAY_WITHOUT_GPS" value="5" unit="s"/>
    <define name="DEFAULT_THROTTLE" value="0" unit="%"/>
    <define name="DEFAULT_ROLL" value="0.3" unit="rad"/>
    <define name="DEFAULT_PITCH" value="0.5" unit="rad"/>
</section>

  <section name="SIMULATOR" prefix="NPS_">
    <define name="JSBSIM_LAUNCHSPEED" value="25"/>
    <define name="JSBSIM_MODEL" value="AGGIEAIR/minion" type="string"/>
    <define name="SENSORS_PARAMS" value="nps_sensors_params_invariant.h" type="string"/>
    <define name="JS_AXIS_THROTTLE" value="0"/>
    <define name="JS_AXIS_THROTTLE_REVERSED" value="1"/>

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

    <define name="JS_AXIS_PITCH" value="2"/>
    <define name="JS_AXIS_PITCH_REVERSED" value="1"/>

    <define name="JS_AXIS_YAW" value="3"/>
    <define name="JS_AXIS_YAW_REVERSED" value="1"/>

    <define name="JS_AXIS_FLAPS" value="5"/>
    <define name="JS_AXIS_MODE" value="7"/>

    <define name="JSBSIM_ROLL_TRIM" value="0.008"/>
    <define name="JSBSIM_PITCH_TRIM" value="0.0375"/>
    <define name="JSBSIM_YAW_TRIM" value="0.001"/>
  </section>

  <section name="IMU" prefix="IMU_">
    <define name="BODY_TO_IMU_PHI"   value="0." unit="deg"/>
    <define name="BODY_TO_IMU_THETA" value="0." unit="deg"/>
    <define name="BODY_TO_IMU_PSI"   value="0." unit="deg"/>

    <!-- Dummy Mag values for NPS -
         replace with your own calibration -->
    <define name="MAG_X_NEUTRAL" value="0"/>
    <define name="MAG_Y_NEUTRAL" value="0"/>
    <define name="MAG_Z_NEUTRAL" value="0"/>
  </section>

<!-- **************************************************************** -->
  <firmware name="fixedwing">
    <target name="ap"       board="umarim_lite_2.0"/>
    <module name="radio_control" type="ppm"/>

    <!-- NOTE: if you want to use extra_dl module for HITL
    you have to set TELEMETRY_FREQUENCY to CONTROL_FREQUENCY -->
    <configure name="PERIODIC_FREQUENCY" value="100"/>
    <define name="CONTROL_FREQUENCY" value="100"/>
    <configure name="TELEMETRY_FREQUENCY" value="100"/>
    <define name="SERVO_HZ" value="100"/>

    <target name="nps" board="pc">
      <module name="fdm" type="jsbsim"/>
    </target>

    <target name="hitl" board="pc">
      <module name="fdm" type="jsbsim"/>
      <configure name="INS_DEV" value="/dev/ttyUSB1"/>
      <configure name="INS_BAUD" value="B921600"/>
      <configure name="AP_DEV" value="/dev/ttyUSB2"/>
      <configure name="AP_BAUD" value="B921600"/>
    </target>

    <module name="control"/>
    <module name="navigation"/>

    <module name="telemetry"   type="transparent">
      <configure name="MODEM_PORT"        value="UART1"/>
      <configure name="MODEM_BAUD"        value="B57600"/>
    </module>

    <module name="ins"       type="vectornav">
      <configure name="VN_PORT" value="UART0"/>
      <configure name="VN_BAUD" value="B230400"/>
    </module>

    <define name="POLY_OSAM_HALF_SWEEP_ENABLED" value="FALSE"/>
    <define name="GEOFENCE_DATALINK_LOST_TIME" value="45"/>

<!-- Extra Definitions -->
    <define name="AGR_CLIMB" />
    <define name="LOITER_TRIM" />
    <define name="ALT_KALMAN" />
    <define name="SixCellBatt"/>
    <define name="SYS_TIME_LED" value="1"/>
    <define name="TUNE_AGRESSIVE_CLIMB"/>
    <define name="WIND_INFO"/>
    <define name="WIND_INFO_RET"/>
  </firmware>

<!-- Modules -->
  <modules>
    <module name="nav" type="line"/>
    <module name="nav" type="flower"/>
    <module name="nav" type="survey_poly_osam"/>
    <module name="nav" type="launcher"/>
    <module name="nav" type="skid_landing"/>
    <module name="extra_dl">
      <configure name="EXTRA_DL_PORT" value="usb_serial"/>
      <configure name="EXTRA_DL_BAUD" value="B921600"/>
    </module>

    <!-- Airspeed Sensor -->
    <!--TODO: better to replace with air-data module and airspeed_ms45xx_i2c module-->
    <module name="airspeed_adc">
      <configure name="ADC_AIRSPEED" value="ADC_3"/>
      <configure name="AIRSPEED_PIN" value="ADC_3"/>
      <define name="AIRSPEED_ADC_SCALE" value="4.4083"/>
      <define name="AIRSPEED_ADC_BIAS" value="740"/>
      <define name="USE_AIRSPEED_ADC" value="TRUE"/>
    </module>
    <!-- Angle of Attack and Sideslip Angle -->
    <!-- TODO: using AoA module only, not sure if we ever use sideslip anymore -->
    <module name="AOA_adc">  
      <configure name="ADC_AOA" value="ADC_2"/>
      <define name="AOA_OFFSET" value="0.0"/>   
      <define name="AOA_FILTER" value="0.0"/>
      <define name="USE_AOA" value="TRUE"/>
    </module>

    <!-- I2C Payload -->
    <!--load name="aggiecap2_i2c.xml">
      <define name="I2C_SLAVE_ADDR" value="0x30"/>
      <define name="I2C_BUF_LEN" value="256"/>
      <define name="USE_I2C0_SLAVE"/>
    </load-->

    <!-- System Monitor -->
    <module name="sys_mon"/>
  </modules>

</airframe>
