<!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:          one (two) Spektrum sats   http://wiki.paparazziuav.org/wiki/Subsystem/radio_control#Spektrum
-->

<airframe name="Hooper Gen AP">

  <firmware name="rotorcraft">
    <autopilot name="rotorcraft_autopilot.xml"/>

    <configure name="AHRS_ALIGNER_LED" value="2"/>
    <define name="IMU_MPU9250_READ_MAG" value="0"/>
    <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"/>
    </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="mag"           type="hmc58xx">
      <define name="MODULE_HMC58XX_UPDATE_AHRS"/>
      <configure name="MAG_HMC58XX_I2C_DEV" value="i2c2"/>
      <define name="HMC58XX_CHAN_X" value="1"/>
      <define name="HMC58XX_CHAN_Y" value="0"/>
    </module>
    <module name="gps"           type="ublox">
      <configure name="GPS_BAUD" value="B115200"/>
    </module>
    <module name="stabilization" type="int_quat"/>
    <module name="ahrs"          type="int_cmpl_quat"/>
    <module name="ins"           />

    <module name="send_imu_mag_current"/>
    <!--module name="geo_mag"/-->
    <module name="air_data"/>
    <module name="stabilization" type="rate"/>

    <!--define name="KILL_ON_GROUND_DETECT" value="TRUE"/-->
    <module name="bat_checker">
      <define name="BAT_CHECKER_GPIO" value="GPIOA,GPIO0"/>
      <define name="USE_PWM5" value="0"/>
    </module>
  </firmware>

  <servos driver="Pwm">
    <servo name="FL" no="0" min="1000" neutral="1200" max="2000"/>
    <servo name="FR" no="1" min="1000" neutral="1200" max="2000"/>
    <servo name="BR" no="2" min="1000" neutral="1200" max="2000"/>
    <servo name="BL" no="3" min="1000" neutral="1200" 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_">
    <!-- front left (CW), front right (CCW), back right (CW), back left (CCW) -->
    <define name="TYPE" value="QUAD_X"/>
  </section>

  <command_laws>
    <call fun="motor_mixing_run(autopilot_get_motors_on(),FALSE,values)"/>
    <set servo="FL" value="motor_mixing.commands[MOTOR_FRONT_LEFT]"/>
    <set servo="FR" value="motor_mixing.commands[MOTOR_FRONT_RIGHT]"/>
    <set servo="BR" value="motor_mixing.commands[MOTOR_BACK_RIGHT]"/>
    <set servo="BL" value="motor_mixing.commands[MOTOR_BACK_LEFT]"/>
  </command_laws>

  <section name="IMU" prefix="IMU_">

    <define name="ACCEL_X_NEUTRAL" value="-55"/>
    <define name="ACCEL_Y_NEUTRAL" value="3"/>
    <define name="ACCEL_Z_NEUTRAL" value="-141"/>
    <define name="ACCEL_X_SENS" value="2.44645099697" integer="16"/>
    <define name="ACCEL_Y_SENS" value="2.44618333167" integer="16"/>
    <define name="ACCEL_Z_SENS" value="2.43722535075" integer="16"/>


    <!-- MPU MAG -->
    <!--define name="MAG_X_NEUTRAL" value="-326"/>
    <define name="MAG_Y_NEUTRAL" value="-82"/>
    <define name="MAG_Z_NEUTRAL" value="-448"/>
    <define name="MAG_X_SENS" value="8.93784052264" integer="16"/>
    <define name="MAG_Y_SENS" value="8.76983337164" integer="16"/>
    <define name="MAG_Z_SENS" value="8.15871475442" integer="16"/-->

    <!-- HMC MAG -->
    <define name="MAG_X_SIGN" value="-1"/>
    <define name="MAG_Y_SIGN" value="1"/>
    <define name="MAG_Z_SIGN" value="1"/>
    <define name="MAG_X_NEUTRAL" value="-86"/>
    <define name="MAG_Y_NEUTRAL" value="41"/>
    <define name="MAG_Z_NEUTRAL" value="-32"/>
    <define name="MAG_X_SENS" value="3.73940141026" integer="16"/>
    <define name="MAG_Y_SENS" value="3.81423665322" integer="16"/>
    <define name="MAG_Z_SENS" value="4.15369264623" integer="16"/>

    <!--define name= "MAG_X_CURRENT_COEF" value="0.0350248861409"/>
    <define name= "MAG_Y_CURRENT_COEF" value="-0.0118884242797"/>
    <define name= "MAG_Z_CURRENT_COEF" value="0.0176235525201"/-->

    <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 if loaded -->
    <!-- Toulouse -->
    <define name="H_X" value="0.513081"/>
    <define name="H_Y" value="-0.00242783"/>
    <define name="H_Z" value="0.858336"/>
  </section>

  <section name="STABILIZATION_RATE" prefix="STABILIZATION_RATE_">
    <!-- setpoints -->
    <define name="SP_MAX_P" value="300" unit="deg/s"/>
    <define name="SP_MAX_Q" value="300" unit="deg/s"/>
    <define name="SP_MAX_R" value="240" unit="deg/s"/>
    <define name="DEADBAND_P" value="20"/>
    <define name="DEADBAND_Q" value="20"/>
    <define name="DEADBAND_R" value="200"/>

    <!-- feedback -->
    <define name="GAIN_P" value="1000"/>
    <define name="GAIN_Q" value="1000"/>
    <define name="GAIN_R" value="800"/>

    <define name="IGAIN_P" value="75"/>
    <define name="IGAIN_Q" value="75"/>
    <define name="IGAIN_R" value="50"/>
  </section>


  <section name="STABILIZATION_ATTITUDE" prefix="STABILIZATION_ATTITUDE_">
    <!-- setpoints -->
    <define name="SP_MAX_PHI"     value="45." unit="deg"/>
    <define name="SP_MAX_THETA"   value="45." unit="deg"/>
    <define name="SP_MAX_R"       value="200." 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="250." unit="deg/s"/>
    <define name="REF_MAX_RDOT" value="RadOfDeg(1800.)"/>

    <!-- feedback -->
    <define name="PHI_PGAIN"  value="500"/>
    <define name="PHI_DGAIN"  value="260"/>
    <define name="PHI_IGAIN"  value="100"/>

    <define name="THETA_PGAIN"  value="500"/>
    <define name="THETA_DGAIN"  value="260"/>
    <define name="THETA_IGAIN"  value="100"/>

    <define name="PSI_PGAIN"  value="500"/>
    <define name="PSI_DGAIN"  value="300"/>
    <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="STABILIZATION_ATTITUDE_INDI" prefix="STABILIZATION_INDI_">
    <!-- control effectiveness -->
    <define name="G1_P" value="0.05"/>
    <define name="G1_Q" value="0.025"/>
    <define name="G1_R" value="0.0022"/>
    <define name="G2_R" value="0.20"/>

    <define name="FILTER_ROLL_RATE" value="TRUE"/>
    <define name="FILTER_PITCH_RATE" value="TRUE"/>
    <define name="FILTER_YAW_RATE" value="FALSE"/>

    <!-- reference acceleration for attitude control -->
    <define name="REF_ERR_P" value="170.0"/>
    <define name="REF_ERR_Q" value="600.0"/>
    <define name="REF_ERR_R" value="600.0"/>
    <define name="REF_RATE_P" value="14.3"/>
    <define name="REF_RATE_Q" value="28.0"/>
    <define name="REF_RATE_R" value="28.0"/>

    <!-- second order filter parameters -->
    <define name="FILT_OMEGA" value="20.0"/>
    <define name="FILT_ZETA" value="0.55"/>
    <define name="FILT_OMEGA_R" value="20.0"/>
    <define name="FILT_ZETA_R" value="0.55"/>

    <!-- first order actuator dynamics -->
    <define name="ACT_DYN_P" value="0.06"/>
    <define name="ACT_DYN_Q" value="0.06"/>
    <define name="ACT_DYN_R" value="0.06"/>

    <!-- Adaptive Learning Rate -->
    <define name="USE_ADAPTIVE" value="FALSE"/>
    <define name="ADAPTIVE_MU" value="0.0001"/>
  </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.5"/>
    <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="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_teensyfly_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">
    <define name="MODE_MANUAL" value="AP_MODE_ATTITUDE_DIRECT"/> <!-- for compilation -->
    <define name="MODE_AUTO1"  value="AP_MODE_ATTITUDE_Z_HOLD"/> <!-- for compilation -->
    <define name="MODE_AUTO2"  value="AP_MODE_NAV"/>
  </section>

  <section name="BAT">
    <define name="CATASTROPHIC_BAT_LEVEL" value="9.3" unit="V"/>
    <define name="CRITIC_BAT_LEVEL" value="9.8" unit="V"/>
    <define name="LOW_BAT_LEVEL" value="10.5" unit="V"/>
    <define name="MAX_BAT_LEVEL" value="12.4" unit="V"/>
    <define name="MILLIAMP_AT_FULL_THROTTLE" value="20000" unit="mA"/>
    <define name="MILLIAMP_AT_IDLE_THROTTLE" value="1000" value="mA"/>
    <define name="CURRENT_ESTIMATION_NONLINEARITY" value="1.0"/>
  </section>

  <section name="GCS">
    <define name="ALT_SHIFT_PLUS_PLUS" value="5"/>
    <define name="ALT_SHIFT_PLUS"      value="1"/>
    <define name="ALT_SHIFT_MINUS"     value="-1"/>
    <define name="AC_ICON"             value="quadrotor_x"/>
  </section>

</airframe>
