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

<!--
    Use settings/tuning_ins.xml
    Use telemetry/default_fixedwing_imu.xml

     Reely Condor
     TWOG v1.0 Board (http://wiki.paparazziuav.org/wiki/TWOG)
     Sparkfun Razor IMU with Premerlani Code
     wi.232 Radiomodems
-->


<airframe name="Reely Condor CHNI">

  <servos>
    <servo name="THROTTLE"   no="7" min="1200" neutral="1200" max="2000"/>
    <servo name="AILERON" no="4" min="1000" neutral="1500" max="2000"/>
    <servo name="ELEVATOR" no="3" max="2000" neutral="1500" min="1000"/>
    <servo name="CAM" no="2" max="2000" neutral="1500" min="1000"/>
    <servo name="TILT" no="6" max="2000" neutral="1501" min="1000"/>
  </servos>

  <commands>
    <axis name="THROTTLE" failsafe_value="0"/>
    <axis name="ROLL" failsafe_value="0"/>
    <axis name="PITCH" failsafe_value="0"/>
  </commands>

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

  <command_laws>
    <set servo="THROTTLE" value="@THROTTLE"/>
    <set servo="AILERON" value="@ROLL"/>
    <set servo="ELEVATOR" value="@PITCH"/>
  </command_laws>

  <section name="IMU" prefix="IMU_">
    <define name="GYRO_P_NEUTRAL" value="512"/>
    <define name="GYRO_Q_NEUTRAL" value="512"/>
    <define name="GYRO_R_NEUTRAL" value="512"/>

    <define name="GYRO_P_SENS" value="0.017" integer="16"/>
    <define name="GYRO_Q_SENS" value="0.017" integer="16"/>
    <define name="GYRO_R_SENS" value="0.017" integer="16"/>

    <define name="GYRO_P_SIGN" value="1" />
    <define name="GYRO_Q_SIGN" value="1" />
    <define name="GYRO_R_SIGN" value="-1" />

    <define name="ACCEL_X_SENS" value="0.1" integer="16"/>
    <define name="ACCEL_Y_SENS" value="0.1" integer="16"/>
    <define name="ACCEL_Z_SENS" value="0.1" integer="16"/>

    <define name="ACCEL_X_NEUTRAL" value="512"/>
    <define name="ACCEL_Y_NEUTRAL" value="512"/>
    <define name="ACCEL_Z_NEUTRAL" value="512"/>

    <define name="ACCEL_X_SIGN" value="1"/>
    <define name="ACCEL_Y_SIGN" value="-1"/>
    <define name="ACCEL_Z_SIGN" value="1"/>

<!-- this should not be needed if no mag is used -->
    <define name="MAG_X_NEUTRAL" value="512"/>
    <define name="MAG_Y_NEUTRAL" value="512"/>
    <define name="MAG_Z_NEUTRAL" value="512"/>

    <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" />
  </section>

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

  <!-- settings for the Analog IMU -->

  <section name="INS" prefix="INS_">
    <define name="ROLL_NEUTRAL_DEFAULT" value="0" unit="deg"/>
    <define name="PITCH_NEUTRAL_DEFAULT" value="0" unit="deg"/>
  </section>

  <!-- 3S LiPo with 1050mAh, connected to an 20A ESC -->
  <section name="BAT">
   <define name="MILLIAMP_AT_FULL_THROTTLE" value="20000" unit="mA" />
   <define name="VOLTAGE_ADC_A" value="0.02456533604651162791"/>
   <define name="VOLTAGE_ADC_B" value="0.24024993023255813953"/>
   <define name="VoltageOfAdc(adc)" value ="(VOLTAGE_ADC_A * adc + VOLTAGE_ADC_B)"/>
   <define name="CATASTROPHIC_BAT_LEVEL" value="9.0" unit="V"/>
   <define name="CRITIC_BAT_LEVEL" value="9.3" unit="V"/>
   <define name="LOW_BAT_LEVEL" value="9.5" unit="V"/>
   <define name="MAX_BAT_LEVEL" value="12.4" unit="V"/>
  </section>

  <section name="SIMU">
    <define name="YAW_RESPONSE_FACTOR" value="0.7"/>
  </section>

  <section name="MISC">
    <define name="TELEMETRY_MODE_FBW" value="1"/>
    <define name="NOMINAL_AIRSPEED" value="12.5" 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="TRIGGER_DELAY" value="1."/>
    <define name="DEFAULT_CIRCLE_RADIUS" value="50."/>
  </section>

  <section name="VERTICAL CONTROL" prefix="V_CTL_">

    <define name="POWER_CTL_BAT_NOMINAL" value="11.1" unit="volt"/>
    <!-- outer loop proportional gain -->
    <define name="ALTITUDE_PGAIN" value="0.07"/>
    <!-- outer loop saturation -->
    <define name="ALTITUDE_MAX_CLIMB" value="2."/>

    <!-- auto throttle inner loop -->
    <define name="AUTO_THROTTLE_NOMINAL_CRUISE_THROTTLE" value="0.55"/>
    <define name="AUTO_THROTTLE_MIN_CRUISE_THROTTLE" value="0.30"/>
    <define name="AUTO_THROTTLE_MAX_CRUISE_THROTTLE" value="0.80"/>
    <define name="AUTO_THROTTLE_LOITER_TRIM" value="1500"/>
    <define name="AUTO_THROTTLE_DASH_TRIM" value="-500"/>
    <define name="AUTO_THROTTLE_CLIMB_THROTTLE_INCREMENT" value="0.15" unit="%/(m/s)"/>
    <define name="AUTO_THROTTLE_PGAIN" value="0.025"/>
    <define name="AUTO_THROTTLE_IGAIN" value="0.1"/>
    <define name="AUTO_THROTTLE_PITCH_OF_VZ_PGAIN" value="0.05"/>

    <!-- 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"/>
    <define name="AUTO_PITCH_MIN_PITCH" value="-0.35"/>

   <define name="THROTTLE_SLEW" value="0.05"/>

  </section>

  <section name="HORIZONTAL CONTROL" prefix="H_CTL_">
    <define name="COURSE_PGAIN" value="0.85"/>
    <define name="ROLL_MAX_SETPOINT" value="0.6" unit="rad"/>
    <define name="PITCH_MAX_SETPOINT" value="0.5" unit="rad"/>
    <define name="PITCH_MIN_SETPOINT" value="-0.5" unit="rad"/>


    <define name="ROLL_PGAIN" value="12000."/>
    <define name="AILERON_OF_THROTTLE" value="0.0"/>
    <define name="PITCH_PGAIN" value="9000."/>
    <define name="PITCH_DGAIN" value="1.5"/>
    <define name="ELEVATOR_OF_ROLL" value="1250"/>

  </section>

  <section name="NAV">
    <define name="NAV_GLIDE_PITCH_TRIM" value="0"/>
  </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.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.25"/><!-- 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="100" unit="m"/>
  </section>

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

    <define name="LOITER_TRIM"/>

    <module name="radio_control"     type="ppm"/>
    <!-- Communication -->
    <module name="telemetry"     type="transparent">
      <configure name="MODEM_BAUD"      value="B9600"/>
    </module>
    <!-- Actuators are automatically chosen according to the board-->
    <module name="control"/>
    <!-- Sensors -->
    <module name="imu"       type="analog">
      <configure name="GYRO_P" value="ADC_0"/>
      <configure name="GYRO_Q" value="ADC_1"/>
      <configure name="GYRO_R" value="ADC_2"/>
      <configure name="ACCEL_X" value="ADC_5"/>
      <configure name="ACCEL_Y" value="ADC_6"/>
      <configure name="ACCEL_Z" value="ADC_7"/>
      <configure name="ADC_ACCEL_NB_SAMPLES" value="32"/>
    </module>

    <module name="ahrs"  type="float_dcm"/>
    <module name="gps"       type="ublox"/>
    <module name="navigation"/>
    <module name="ins" type="alt_float"/>
  </firmware>

  <modules>
    <module name="nav" type="line"/>
  </modules>

</airframe>
