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

<airframe name="ardrone2_indi">
  <description>?
  </description>

  <firmware name="rotorcraft">
    <target name="ap" board="ardrone2">
      <define name="AUTOPILOT_DISABLE_AHRS_KILL"/>
    </target>

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

    <define name="USE_SONAR" value="FALSE"/>

    <!-- Subsystem section -->
    <module name="telemetry" type="transparent_udp"/>
    <module name="radio_control" type="datalink"/>
    <module name="motor_mixing"/>
    <module name="actuators" type="ardrone2"/>
    <module name="imu" type="ardrone2"/>
    <module name="gps" type="ublox"/>
    <module name="stabilization" type="indi_simple"/>
    <module name="ahrs" type="int_cmpl_quat">
      <configure name="USE_MAGNETOMETER" value="TRUE"/>
      <define name="AHRS_USE_GPS_HEADING" value="FALSE"/>
    </module>
    <module name="ins" type="extended"/>
  </firmware>

  <modules main_freq="512">
    <module name="bat_voltage_ardrone2"/>
    <module name="gps" type="ubx_ucenter"/>
    <module name="send_imu_mag_current"/>
    <module name="air_data"/>
    <module name="logger_file"/>
  </modules>

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

  <servos driver="Default">
    <servo name="TOP_LEFT" no="0" min="0" neutral="1" max="500"/>
    <servo name="TOP_RIGHT" no="1" min="0" neutral="1" max="500"/>
    <servo name="BOTTOM_RIGHT" no="2" min="0" neutral="1" max="500"/>
    <servo name="BOTTOM_LEFT" no="3" min="0" neutral="1" max="500"/>
  </servos>

  <section name="MIXING" prefix="MOTOR_MIXING_">
    <define name="TRIM_ROLL" value="0"/>
    <define name="TRIM_PITCH" value="0"/>
    <define name="TRIM_YAW" value="0"/>

    <!-- Time cross layout (X), with order NW (CW), NE (CCW), SE (CW), SW (CCW) -->
    <define name="TYPE" value="QUAD_X"/>
  </section>

  <command_laws>
    <call fun="motor_mixing_run(autopilot_get_motors_on(),FALSE,values)"/>
    <set servo="TOP_LEFT" value="motor_mixing.commands[MOTOR_FRONT_LEFT]"/>
    <set servo="TOP_RIGHT" value="motor_mixing.commands[MOTOR_FRONT_RIGHT]"/>
    <set servo="BOTTOM_RIGHT" value="motor_mixing.commands[MOTOR_BACK_RIGHT]"/>
    <set servo="BOTTOM_LEFT" value="motor_mixing.commands[MOTOR_BACK_LEFT]"/>
  </command_laws>

  <section name="IMU" prefix="IMU_">
    <!-- Accelero -->
    <define name="ACCEL_X_NEUTRAL" value="2048"/>
    <define name="ACCEL_Y_NEUTRAL" value="2048"/>
    <define name="ACCEL_Z_NEUTRAL" value="2048"/>

    <!-- Magneto calibration -->
    <define name="MAG_X_NEUTRAL" value="0"/>
    <define name="MAG_Y_NEUTRAL" value="0"/>
    <define name="MAG_Z_NEUTRAL" value="-180"/>
    <define name="MAG_X_SENS" value="16." integer="16"/>
    <define name="MAG_Y_SENS" value="16." integer="16"/>
    <define name="MAG_Z_SENS" value="16." integer="16"/>

    <!-- Magneto current calibration -->
    <define name="MAG_X_CURRENT_COEF" value="0.0"/>
    <define name="MAG_Y_CURRENT_COEF" value="0.0"/>
    <define name="MAG_Z_CURRENT_COEF" value="0.0"/>

    <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>

  <!-- local magnetic field -->
  <!-- http://wiki.paparazziuav.org/wiki/Subsystem/ahrs#Local_Magnetic_Field -->
  <section name="AHRS" prefix="AHRS_">
    <!-- Delft -->
    <define name="H_X" value="0.3892503"/>
    <define name="H_Y" value="0.0017972"/>
    <define name="H_Z" value="0.9211303"/>
  </section>

  <section name="INS" prefix="INS_">
    <define name="SONAR_MAX_RANGE" value="2.2"/>
    <define name="SONAR_UPDATE_ON_AGL" value="TRUE"/>
  </section>


  <section name="RC_SETPOINT" prefix="STABILIZATION_ATTITUDE_">
    <!-- setpoint limits for attitude stabilization rc flight -->
    <define name="SP_MAX_PHI" value="45" unit="deg"/>
    <define name="SP_MAX_THETA" value="45" unit="deg"/>
    <define name="SP_MAX_R" value="180" unit="deg/s"/>
    <define name="DEADBAND_A" value="0"/>
    <define name="DEADBAND_E" value="0"/>
    <define name="DEADBAND_R" value="250"/>
  </section>

  <section name="ATTITUDE_REFERENCE" prefix="STABILIZATION_ATTITUDE_">
    <!-- attitude reference generation model -->
    <define name="REF_OMEGA_P" value="450" unit="deg/s"/>
    <define name="REF_ZETA_P" value="0.9"/>
    <define name="REF_MAX_P" value="600." unit="deg/s"/>
    <define name="REF_MAX_PDOT" value="RadOfDeg(8000.)"/>

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

    <define name="REF_OMEGA_R" value="450" unit="deg/s"/>
    <define name="REF_ZETA_R" value="0.9"/>
    <define name="REF_MAX_R" value="600." unit="deg/s"/>
    <define name="REF_MAX_RDOT" value="RadOfDeg(8000.)"/>
  </section>

  <section name="STABILIZATION_ATTITUDE_INDI" prefix="STABILIZATION_INDI_">
    <!-- control effectiveness -->
    <define name="G1_P" value="0.032"/>
    <define name="G1_Q" value="0.025"/>
    <define name="G1_R" value="0.0032"/>
    <define name="G2_R" value="0.16"/>

    <!-- reference acceleration for attitude control -->
    <define name="REF_ERR_P" value="380.0"/>
    <define name="REF_ERR_Q" value="380.0"/>
    <define name="REF_ERR_R" value="250.0"/>
    <define name="REF_RATE_P" value="21.6"/>
    <define name="REF_RATE_Q" value="21.6"/>
    <define name="REF_RATE_R" value="21.0"/>

    <!-- second order filter parameters -->
    <define name="FILT_CUTOFF" value="3.2"/>
    <define name="FILT_CUTOFF_R" value="3.2"/>

    <!-- 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="TRUE"/>
    <define name="ADAPTIVE_MU" value="0.0001"/>
  </section>

  <section name="GUIDANCE_V" prefix="GUIDANCE_V_">
    <define name="HOVER_KP" value="283"/>
    <define name="HOVER_KD" value="82"/>
    <define name="HOVER_KI" value="13"/>
    <define name="NOMINAL_HOVER_THROTTLE" value="0.655"/>
    <define name="ADAPT_THROTTLE_ENABLED" value="FALSE"/>
  </section>

  <section name="GUIDANCE_H" prefix="GUIDANCE_H_">
    <!-- Good weather -->
    <define name="MAX_BANK" value="20" unit="deg"/>
    <!-- Bad weather -->
    <!-- define name="MAX_BANK" value="32" unit="deg"/ -->
    <define name="PGAIN" value="79"/>
    <define name="DGAIN" value="100"/>
    <define name="IGAIN" value="30"/>
  </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="simple_ardrone2" type="string"/>
    <define name="JSBSIM_INIT" value="reset00" type="string"/>
    <define name="SENSORS_PARAMS" value="nps_sensors_params_ardrone2.h" type="string"/>
  </section>

  <section name="AUTOPILOT">
    <define name="MODE_STARTUP" value="AP_MODE_NAV"/>
    <define name="MODE_MANUAL" value="AP_MODE_ATTITUDE_DIRECT"/>
    <define name="MODE_AUTO1" value="AP_MODE_ATTITUDE_Z_HOLD"/>
    <define name="MODE_AUTO2" value="AP_MODE_NAV"/>

    <define name="NO_RC_THRUST_LIMIT" value="TRUE"/>
  </section>

  <section name="BAT">
    <define name="MILLIAMP_AT_FULL_THROTTLE" value="8700"/>
    <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="9.7" unit="V"/>
    <define name="MAX_BAT_LEVEL" value="12.4" unit="V"/>
  </section>
</airframe>
