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

<!-- this is a quadrotor frame with the OpenPilot CC3D board:
    * Autopilot:   OpenPilot CC3D         https://www.openpilot.org/product/coptercontrol/
-->

<airframe name="QuadCC3D">

  <firmware name="rotorcraft">
    <target name="ap" board="cc3d">
    </target>

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

    <module name="radio_control" type="spektrum">
      <define name="RADIO_MODE" value="RADIO_AUX1"/>
      <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="stabilization" type="int_quat"/>
    <module name="imu"           type="mpu6000"/>
    <module name="ahrs"          type="int_cmpl_quat">
      <define name="AHRS_GRAVITY_HEURISTIC_FACTOR" value="30"/>
    </module>
    <module name="ins"/>

    <!-- CC3D board does not have a mag -->
    <configure name="USE_MAGNETOMETER" value="FALSE"/>
    <!-- compress trig tables so it fits in board -->
    <define name="PPRZ_TRIG_INT_COMPR_FLASH"/>
    <define name="PPRZ_TRIG_INT_COMPR_LOW"/>
  </firmware>

  <firmware name="test_progs">
    <target name="test_sys_time_timer" board="cc3d"/>
    <target name="test_sys_time_usleep" board="cc3d"/>
    <target name="test_telemetry" board="cc3d"/>
    <target name="test_imu" board="cc3d">
      <module name="imu" type="mpu6000"/>
    </target>
    <target name="test_ahrs" board="cc3d">
      <module name="imu" type="mpu6000"/>
      <module name="ahrs" type="int_cmpl_quat"/>
      <configure name="USE_MAGNETOMETER" value="FALSE"/>
    </target>
    <target name="test_actuators_pwm" board="cc3d"/>
  </firmware>

  <servos driver="Pwm">
    <servo name="FL"   no="0" min="1000" neutral="1100" max="2000"/>
    <servo name="FR"   no="1" min="1000" neutral="1100" max="2000"/>
    <servo name="BR"   no="2" min="1000" neutral="1100" max="2000"/>
    <servo name="BL"   no="3" min="1000" neutral="1100" max="2000"/>
  </servos>

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

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

  <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="MAG_X_NEUTRAL" value="22"/>
    <define name="MAG_Y_NEUTRAL" value="-314"/>
    <define name="MAG_Z_NEUTRAL" value="13"/>
    <define name="MAG_X_SENS" value="3.68160078446" integer="16"/>
    <define name="MAG_Y_SENS" value="3.6093630842" integer="16"/>
    <define name="MAG_Z_SENS" value="3.48306842466" integer="16"/>

    <define name="BODY_TO_IMU_PHI"   value="180." unit="deg"/>
    <define name="BODY_TO_IMU_THETA" value="0." unit="deg"/>
    <define name="BODY_TO_IMU_PSI"   value="-90." unit="deg"/>
  </section>

  <section name="AHRS" prefix="AHRS_">
    <define name="H_X" value="0.3770441"/>
    <define name="H_Y" value="0.0193986"/>
    <define name="H_Z" value="0.9259921"/>
  </section>

  <section name="INS" prefix="INS_">
  </section>



  <section name="STABILIZATION_ATTITUDE" prefix="STABILIZATION_ATTITUDE_">
    <!-- setpoints -->
    <define name="SP_MAX_PHI"     value="60." unit="deg"/>
    <define name="SP_MAX_THETA"   value="60." 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="1000"/>
    <define name="PHI_DGAIN"  value="400"/>
    <define name="PHI_IGAIN"  value="200"/>

    <define name="THETA_PGAIN"  value="1000"/>
    <define name="THETA_DGAIN"  value="400"/>
    <define name="THETA_IGAIN"  value="200"/>

    <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="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="APPROX_FORCE_BY_THRUST" value="TRUE"/>
    <define name="MAX_BANK" value="30" 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="simple_x_quad_ccw" 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"/>
    <define name="MODE_AUTO1"  value="AP_MODE_ATTITUDE_Z_HOLD"/>
    <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.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>
