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

<airframe name="Miniquad CHNI">

  <firmware name="rotorcraft">
    <define name="USE_INS_NAV_INIT"/>
    <!--define name="GUIDANCE_H_USE_REF"/-->

    <target name="ap" board="hbmini_1.0">
      <define name="FAILSAFE_GROUND_DETECT"/>
      <define name="USE_GPS_ACC4R"/>
      <define name="ACTUATORS_START_DELAY" value="3"/>
    </target>
    <target name="nps" board="pc">
      <module name="fdm"         type="jsbsim"/>
    </target>

    <module name="radio_control" type="ppm"/>
    <module name="telemetry"     type="transparent"/>
    <module name="actuators"     type="mkk">
      <configure name="ACTUATORS_MKK_I2C_SCL_TIME" value="25"/>
    </module>
    <module name="motor_mixing"/>
    <module name="imu"           type="hbmini"/>
    <module name="gps"           type="ublox"/>
    <module name="stabilization" type="int_euler"/>
    <module name="ahrs"          type="float_cmpl_rmat"/>
    <module name="ins"           type="hff"/>
  </firmware>

  <modules main_freq="512">
    <module name="gps" type="ubx_ucenter"/>
  </modules>

  <section name="ACTUATORS_MKK" prefix="ACTUATORS_MKK_">
    <define name="NB" value="4"/>
    <!-- NW, SE, NE, SW -->
    <define name="ADDR" value="{ 0x52, 0x54, 0x56, 0x58 }"/>
  </section>

  <servos driver="Mkk">
    <servo name="NW"   no="0"    min="0" neutral="10" max="100"/>
    <servo name="SE"   no="1"    min="0" neutral="10" max="100"/>
    <servo name="NE"   no="2"    min="0" neutral="10" max="100"/>
    <servo name="SW"   no="3"    min="0" neutral="10" max="100"/>
  </servos>

  <commands>
    <axis name="PITCH"  failsafe_value="0"/>
    <axis name="ROLL"   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="135"/>
    <define name="TRIM_PITCH" value="-200"/>
    <define name="TRIM_YAW" value="-300"/>
    <define name="NB_MOTOR" value="4"/>
    <define name="SCALE" value="256"/>
    <define name="ROLL_COEF"  value="{  256, -256, -256,  256}"/>
    <define name="PITCH_COEF" value="{  256, -256,  256, -256}"/>
    <define name="YAW_COEF"   value="{  256,  256, -256, -256}"/>
    <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="NW"   value="motor_mixing.commands[0]"/>
    <set servo="SE"   value="motor_mixing.commands[1]"/>
    <set servo="NE"   value="motor_mixing.commands[2]"/>
    <set servo="SW"   value="motor_mixing.commands[3]"/>
  </command_laws>


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

    <define name="GYRO_P_NEUTRAL" value="21460"/>
    <define name="GYRO_Q_NEUTRAL" value="21490"/>
    <define name="GYRO_R_NEUTRAL" value="21830"/>

    <define name="GYRO_P_SENS" value="5.79" integer="16"/>
    <define name="GYRO_Q_SENS" value="5.89" integer="16"/>
    <define name="GYRO_R_SENS" value="4.56" integer="16"/>


    <define name="ACCEL_X_NEUTRAL" value="26480"/>
    <define name="ACCEL_Y_NEUTRAL" value="26380"/>
    <define name="ACCEL_Z_NEUTRAL" value="25836"/>
    <define name="ACCEL_X_SENS" value="1.94" integer="16"/>
    <define name="ACCEL_Y_SENS" value="1.94" integer="16"/>
    <define name="ACCEL_Z_SENS" value="1.99" integer="16"/>


    <define name="MAG_X_NEUTRAL" value="-108"/>
    <define name="MAG_Y_NEUTRAL" value="57"/>
    <define name="MAG_Z_NEUTRAL" value="-95"/>
    <define name="MAG_X_SENS" value="3.6472944636" integer="16"/>
    <define name="MAG_Y_SENS" value="3.09086502241" integer="16"/>
    <define name="MAG_Z_SENS" value="3.74963511486" integer="16"/>

    <define name="BODY_TO_IMU_PHI" value="-2.5" unit="deg"/>
    <define name="BODY_TO_IMU_THETA" value="-2.0" unit="deg"/>
    <define name="BODY_TO_IMU_PSI" value="0." unit="deg"/>

  </section>

  <section name="AHRS" prefix="AHRS_">
    <define name="H_X" value="0.366765"/>
    <define name="H_Y" value="0.013312"/>
    <define name="H_Z" value="0.930219"/>
  </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="220." unit="deg/s"/>
    <define name="DEADBAND_R"     value="250"/>

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

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

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

    <!-- feedback -->
    <define name="PHI_PGAIN"  value="880"/>
    <define name="PHI_DGAIN"  value="400"/>
    <define name="PHI_IGAIN"  value="300"/>

    <define name="THETA_PGAIN"  value="880"/>
    <define name="THETA_DGAIN"  value="400"/>
    <define name="THETA_IGAIN"  value="300"/>

    <define name="PSI_PGAIN"  value="600"/>
    <define name="PSI_DGAIN"  value="320"/>
    <define name="PSI_IGAIN"  value="150"/>

    <!-- feedforward -->
    <define name="PHI_DDGAIN"   value="100"/>
    <define name="THETA_DDGAIN" value="100"/>
    <define name="PSI_DDGAIN"   value="0"/>

    <define name="PHI_AGAIN"   value="0"/>
    <define name="THETA_AGAIN" value="0"/>
    <define name="PSI_AGAIN"   value="0"/>
  </section>

  <section name="GUIDANCE_V"   prefix="GUIDANCE_V_">

    <define name="REF_MIN_ZDD"  value="-0.5*9.81"/>
    <define name="REF_MAX_ZDD"  value=" 0.5*9.81"/>
    <define name="REF_MIN_ZD"   value="-1.5"/>
    <define name="REF_MAX_ZD"   value=" 1.5"/>
    <define name="HOVER_KP"     value="70"/>
    <define name="HOVER_KD"     value="100"/>
    <define name="HOVER_KI"     value="9"/>
    <!-- SPEED_BFP_OF_REAL(1.5) / (MAX_PPRZ/2) -->
    <!-- SPEED_BFP_OF_REAL(1.5) * 20% -->
    <define name="ADAPT_NOISE_FACTOR" value="1.5"/>
  </section>

  <section name="GUIDANCE_H" prefix="GUIDANCE_H_">
    <define name="MAX_BANK" value="30" unit="deg"/>
    <define name="PGAIN" value="80"/>
    <define name="DGAIN" value="140"/>
    <define name="IGAIN" value="10"/>
    <define name="NGAIN" value="0"/>
    <!-- feedforward -->
    <define name="AGAIN" value="0"/>
  </section>

  <section name="BAT">
    <define name="CATASTROPHIC_BAT_LEVEL" value="6.0" unit="V"/>
    <define name="CRITIC_BAT_LEVEL" value="6.5" unit="V"/>
    <define name="LOW_BAT_LEVEL" value="7.0" unit="V"/>
    <define name="MAX_BAT_LEVEL" value="8.4" unit="V"/>
  </section>


  <section name="AUTOPILOT">
    <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_ATTITUDE_Z_HOLD"/-->
    <define name="MODE_AUTO2"  value="AP_MODE_HOVER_Z_HOLD"/>
  </section>

  <section name="FMS">
    <define name="BOOZ_FMS_TIMEOUT" value="0"/>
  </section>

  <section name="MISC">
    <define name="DEFAULT_CIRCLE_RADIUS" value="10."/>
  </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"/>
  </section>

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

</airframe>
