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

<!-- this is a quadrotor frame in X-configuration equiped with
     * Autopilot:   ELLE0 1.2 with STM32F4    http://wiki.paparazziuav.org/wiki/ELLE0
     * IMU:         MPU9250 & MS5611          http://wiki.paparazziuav.org/wiki/ELLE0#IMU
     * Actuators:   PWM motor controllers     http://wiki.paparazziuav.org/wiki/Subsystem/actuators#PWM
     * GPS:         Ublox                     http://wiki.paparazziuav.org/wiki/Subsystem/gps
     * RC:          two Spektrum sats         http://wiki.paparazziuav.org/wiki/Subsystem/radio_control#Spektrum
-->

<airframe name="RacerPEX Quad Elle0 v1_2">

  <firmware name="rotorcraft">

    <configure name="RADIO_CONTROL_LED" value="none"/>
    <configure name="GPS_LED" value="3"/>
    <define name="LOW_NOISE_THRESHOLD" value="3500"/>
    <define name="LOW_NOISE_TIME"      value="10"/>

    <target name="ap" board="elle0_1.2">
    </target>

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

    <module name="radio_control" type="spektrum">
      <define name="RADIO_MODE"        value="RADIO_FLAP"/>
      <define name="RADIO_KILL_SWITCH" value="RADIO_GEAR"/>
        <configure name="USE_SECONDARY_SPEKTRUM_RECEIVER" value="1"/>
    </module>

    <module name="motor_mixing"/>
    <module name="actuators"     type="pwm">
      <define name="SERVO_HZ" value="400"/>
    </module>

    <module name="telemetry"     type="transparent"/>
    <module name="imu"           type="elle0"/>
    <module name="gps"           type="ublox"/>
    <module name="stabilization" type="int_quat"/>
    <module name="ahrs"          type="int_cmpl_quat"/>
    <module name="ins"           type="hff"/>

    <module name="send_imu_mag_current"/>
    <module name="gps" type="ubx_ucenter"/>
    <module name="geo_mag"/>
    <module name="air_data"/>
    <module name="nav" type="survey_rectangle_rotorcraft">
      <define name="RECTANGLE_SURVEY_DEFAULT_SWEEP" value="10"/>
    </module>

    <!--define name="KILL_ON_GROUND_DETECT" value="TRUE"/-->
  </firmware>


  <servos driver="Pwm">
    <servo name="FRONT_LEFT"  no="0" min="1000" neutral="1100" max="2000"/>
    <servo name="FRONT_RIGHT" no="1" min="1000" neutral="1100" max="2000"/>
    <servo name="BACK_RIGHT"  no="2" min="1000" neutral="1100" max="2000"/>
    <servo name="BACK_LEFT"   no="3" min="1000" neutral="1100" max="2000"/>
  </servos>

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

  <section name="MIXING" prefix="MOTOR_MIXING_">
    <define name="TRIM_ROLL"  value="0"/>
    <define name="TRIM_PITCH" value="0"/>
    <define name="TRIM_YAW"   value="0"/>
    <define name="NB_MOTOR"   value="4"/>
    <define name="SCALE"      value="256"/>
    <!-- order (and rotation direction) : FL(CW), FR(CCW), BR(CW), BL(CCW) -->
    <define name="ROLL_COEF"   value="{  256, -256, -256,  256 }"/>
    <define name="PITCH_COEF"  value="{  256,  256, -256, -256 }"/>
    <define name="YAW_COEF"    value="{ -144,  144, -144,  144 }"/>
    <define name="THRUST_COEF" value="{  256,  256,  256,  256 }"/>
  </section>

  <command_laws>
    <call fun="motor_mixing_run(autopilot_get_motors_on(),FALSE,values)"/>
    <set servo="FRONT_LEFT"  value="motor_mixing.commands[0]"/>
    <set servo="FRONT_RIGHT" value="motor_mixing.commands[1]"/>
    <set servo="BACK_RIGHT"  value="motor_mixing.commands[2]"/>
    <set servo="BACK_LEFT"   value="motor_mixing.commands[3]"/>
  </command_laws>

  <section name="IMU" prefix="IMU_">
    <!-- Accelerometer calibration -->
    <define name="ACCEL_X_NEUTRAL" value="-104"/>
    <define name="ACCEL_Y_NEUTRAL" value="-32"/>
    <define name="ACCEL_Z_NEUTRAL" value="-10"/>
    <define name="ACCEL_X_SENS" value="2.44861361621" integer="16"/>
    <define name="ACCEL_Y_SENS" value="2.45042493913" integer="16"/>
    <define name="ACCEL_Z_SENS" value="2.42812924191" integer="16"/>

    <!-- Magnetometer calibration -->
    <define name="MAG_X_NEUTRAL" value="236"/>
    <define name="MAG_Y_NEUTRAL" value="-177"/>
    <define name="MAG_Z_NEUTRAL" value="-204"/>
    <define name="MAG_X_SENS" value="9.44875845387" integer="16"/>
    <define name="MAG_Y_SENS" value="8.37596334381" integer="16"/>
    <define name="MAG_Z_SENS" value="6.32684865126" integer="16"/>

    <!-- Magnetometer current calibration -->
    <define name= "MAG_X_CURRENT_COEF" value="-0.00171135592158"/>
    <define name= "MAG_Y_CURRENT_COEF" value="0.000627013357834"/>
    <define name= "MAG_Z_CURRENT_COEF" value="0.00187410090891"/>

    <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="AHRS" prefix="AHRS_">
    <!-- values used if no GPS fix, on 3D fix is update by geo_mag module -->
    <define name="H_X" value="0.36852472"/>
    <define name="H_Y" value="0.10300471"/>
    <define name="H_Z" value="0.9238937"/>
  </section>



  <section name="STABILIZATION_ATTITUDE" prefix="STABILIZATION_ATTITUDE_">
    <!-- setpoints -->
    <define name="SP_MAX_PHI"     value="30." unit="deg"/>
    <define name="SP_MAX_THETA"   value="30." unit="deg"/>
    <define name="SP_MAX_R"       value="90." unit="deg/s"/>
    <define name="DEADBAND_A"     value="0"/>
    <define name="DEADBAND_E"     value="0"/>
    <define name="DEADBAND_R"     value="250"/>

    <!-- reference -->
    <define name="REF_OMEGA_P"  value="400" unit="deg/s"/>
    <define name="REF_ZETA_P"   value="0.85"/>
    <define name="REF_MAX_P"    value="400." unit="deg/s"/>
    <define name="REF_MAX_PDOT" value="RadOfDeg(8000.)"/>

    <define name="REF_OMEGA_Q"  value="400" unit="deg/s"/>
    <define name="REF_ZETA_Q"   value="0.85"/>
    <define name="REF_MAX_Q"    value="400." unit="deg/s"/>
    <define name="REF_MAX_QDOT" value="RadOfDeg(8000.)"/>

    <define name="REF_OMEGA_R"  value="250" unit="deg/s"/>
    <define name="REF_ZETA_R"   value="0.85"/>
    <define name="REF_MAX_R"    value="180." unit="deg/s"/>
    <define name="REF_MAX_RDOT" value="RadOfDeg(1800.)"/>

    <!-- feedback -->
    <define name="PHI_PGAIN"  value="1246"/>
    <define name="PHI_DGAIN"  value="495"/>
    <define name="PHI_IGAIN"  value="200"/>

    <define name="THETA_PGAIN"  value="1246"/>
    <define name="THETA_DGAIN"  value="495"/>
    <define name="THETA_IGAIN"  value="200"/>

    <define name="PSI_PGAIN"  value="1622"/>
    <define name="PSI_DGAIN"  value="1241"/>
    <define name="PSI_IGAIN"  value="10"/>

    <!-- feedforward -->
    <define name="PHI_DDGAIN"   value="300"/>
    <define name="THETA_DDGAIN" value="300"/>
    <define name="PSI_DDGAIN"   value="300"/>
  </section>

  <section name="GUIDANCE_V" prefix="GUIDANCE_V_">
    <define name="HOVER_KP"    value="150"/>
    <define name="HOVER_KD"    value="80"/>
    <define name="HOVER_KI"    value="20"/>
    <define name="NOMINAL_HOVER_THROTTLE" value="0.53"/>
    <define name="ADAPT_THROTTLE_ENABLED" value="TRUE"/>
  </section>

  <section name="GUIDANCE_H" prefix="GUIDANCE_H_">
    <define name="MAX_BANK" value="20" unit="deg"/>
    <define name="USE_SPEED_REF" value="TRUE"/>
    <define name="PGAIN" value="50"/>
    <define name="DGAIN" value="100"/>
    <define name="AGAIN" value="70"/>
    <define name="IGAIN" value="20"/>
  </section>

  <section name="NAVIGATION" prefix="NAV_">
    <define name="CLIMB_VSPEED"   value="1.5" />
    <define name="DESCEND_VSPEED" value="-1.0" />
  </section>

  <section name="SIMULATOR" prefix="NPS_">
    <define name="ACTUATOR_NAMES"  value="nw_motor, ne_motor, se_motor, sw_motor" type="string[]"/>
    <define name="JSBSIM_MODEL"    value="HOOPERFLY/hooperfly_racerpex_quad" type="string"/>
    <define name="SENSORS_PARAMS"  value="nps_sensors_params_default.h" type="string"/>
    <!-- mode switch on joystick channel 5 (axis numbering starting at zero) -->
    <define name="JS_AXIS_MODE"    value="4"/>
  </section>

  <section name="AUTOPILOT">
    <!-- TOGGLE STATES -->
    <define name="MODE_STARTUP" value="AP_MODE_NAV"/>
    <define name="MODE_MANUAL"  value="AP_MODE_ATTITUDE_DIRECT"/>
    <define name="MODE_AUTO1"   value="AP_MODE_HOVER_Z_HOLD"/>
    <define name="MODE_AUTO2"   value="AP_MODE_NAV"/>
  </section>

  <section name="BAT">
    <define name="MILLIAMP_AT_IDLE_THROTTLE" value="8000" />
    <define name="MILLIAMP_AT_FULL_THROTTLE" value="80000"/>
    <define name="CATASTROPHIC_BAT_LEVEL"    value="9.3"  unit="V"/>
    <define name="CRITIC_BAT_LEVEL"          value="9.6"  unit="V"/>
    <define name="LOW_BAT_LEVEL"             value="10.1" unit="V"/>
    <define name="MAX_BAT_LEVEL"             value="12.4" unit="V"/>
  </section>

  <section name="GCS">
    <define name="ICONS_THEME"         value="flat_theme"/>
    <define name="ALT_SHIFT_PLUS_PLUS" value="30"/>
    <define name="ALT_SHIFT_PLUS"      value="10"/>
    <define name="ALT_SHIFT_MINUS"     value="-10"/>
    <define name="SPEECH_NAME"         value="Racer PEX Quad Elle0"/>
    <define name="AC_ICON"             value="quadrotor_x"/>
  </section>

</airframe>
