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

<!--
Aggie Air ARK
-->

<airframe name="ARK Hexarotor 1.8">

  <firmware name="rotorcraft">

    <target name="ap" board="lisa_mx_2.1">
      <module name="radio_control" type="sbus">
        <configure name="SBUS_PORT" value="UART5"/>
      </module>
      <configure name="HAS_LUFTBOOT" value="1"/>
      <configure name="FLASH_MODE" value="DFU"/>
    </target>

    <!-- NOTE: if you want to use extra_dl module for HITL
    you have to set TELEMETRY_FREQUENCY to PERIODIC_FREQUENCY -->
    <configure name="PERIODIC_FREQUENCY" value="160"/>
    <configure name="TELEMETRY_FREQUENCY" value="160"/>

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

    <target name="hitl" board="pc">
      <module name="fdm" type="jsbsim"/>
      <module name="imu" type="nps"/>
      <module name="radio_control" type="spektrum"/>
      <configure name="INS_DEV" value="/dev/ttyUSB1"/>
      <configure name="INS_BAUD" value="B921600"/>
      <configure name="AP_DEV" value="/dev/ttyUSB2"/>
      <configure name="AP_BAUD" value="B921600"/>
    </target>

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

    <module name="telemetry"     type="transparent">
      <configure name="MODEM_PORT" value="UART3"/>
      <configure name="MODEM_BAUD" value="B57600"/>
    </module>

    <module name="stabilization" type="float_euler"/>

    <module name="ins"       type="vectornav">
      <configure name="VN_PORT" value="UART2"/>
      <configure name="VN_BAUD" value="B921600"/>
    </module>
  </firmware>

  <modules>
    <module name="sys_mon"/>
    <module name="copilot"/>
    <module name="extra_dl">
      <!-- in order to use uart1 without chibios we need to remap the peripheral-->
      <define name="REMAP_UART1" value="TRUE"/>
      <configure name="EXTRA_DL_PORT" value="UART1"/>
      <configure name="EXTRA_DL_BAUD" value="B921600"/>
    </module>
    <module name="battery_monitor.xml">
      <define name="BATMON_REV4" value="0"/>
      <define name="BATMON_CURRENT_OFFSET" value="-120"/>
      <define name="BATMON_CURRENT_SENSITIVITY" value="25.6"/>
      <define name="BATMON_TEMP_OFFSET" value="250"/>
      <define name="BATMON_TEMP_SENSITIVITY" value="10"/>
    </module>
  </modules>

  <!--These values are set for the Castle Creations HV Lite 60A ESCs-->
  <servos driver="Pwm">
    <servo name="FRONT"       no="0" min="1010" neutral="1150" max="2000"/>
    <servo name="FRONT_RIGHT" no="1" min="1010" neutral="1150" max="2000"/>
    <servo name="BACK_RIGHT"  no="2" min="1010" neutral="1150" max="2000"/>
    <servo name="BACK"        no="3" min="1010" neutral="1150" max="2000"/>
    <servo name="BACK_LEFT"   no="4" min="1010" neutral="1150" max="2000"/>
    <servo name="FRONT_LEFT"  no="5" min="1010" neutral="1150" 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="6"/>
    <define name="SCALE" value="256"/>
    <!-- hex plus A -->
    <define name="ROLL_COEF"   value="{   0, -256, -256,  0,  256,  256 }"/>
    <define name="PITCH_COEF"  value="{  256,  128, -128, -256, -128, 128 }"/>
    <define name="YAW_COEF"    value="{  128, -128, 128, -128, 128,  -128 }"/>
    <define name="THRUST_COEF" value="{  256,  256,  256,  256,  256,  256 }"/>
  </section>

  <command_laws>
    <call fun="motor_mixing_run(autopilot_get_motors_on(),FALSE,values)"/>
    <set servo="FRONT"  value="motor_mixing.commands[SERVO_FRONT]"/>
    <set servo="FRONT_RIGHT" value="motor_mixing.commands[SERVO_FRONT_RIGHT]"/>
    <set servo="BACK_RIGHT"   value="motor_mixing.commands[SERVO_BACK_RIGHT]"/>
    <set servo="BACK"    value="motor_mixing.commands[SERVO_BACK]"/>
    <set servo="BACK_LEFT"  value="motor_mixing.commands[SERVO_BACK_LEFT]"/>
    <set servo="FRONT_LEFT"   value="motor_mixing.commands[SERVO_FRONT_LEFT]"/>
  </command_laws>

  <section name="IMU" prefix="IMU_">
    <define name="BODY_TO_IMU_PHI"   value="0.0" unit="deg"/>
    <define name="BODY_TO_IMU_THETA" value="0.0" unit="deg"/>
    <define name="BODY_TO_IMU_PSI"   value="0.0" unit="deg"/>

    <define name="BODY_TO_IMU2_PHI"   value="0." unit="deg"/>
    <define name="BODY_TO_IMU2_THETA" value="0." unit="deg"/>
    <define name="BODY_TO_IMU2_PSI"   value="270." unit="deg"/>

    <define name="ACCEL_X_NEUTRAL" value="11"/>
    <define name="ACCEL_Y_NEUTRAL" value="11"/>
    <define name="ACCEL_Z_NEUTRAL" value="-25"/>

    <!-- replace this with your own calibration -->
    <define name="MAG_X_NEUTRAL" value="-179"/>
    <define name="MAG_Y_NEUTRAL" value="-21"/>
    <define name="MAG_Z_NEUTRAL" value="79"/>
    <define name="MAG_X_SENS" value="4.17334785618" integer="16"/>
    <define name="MAG_Y_SENS" value="3.98885954135" integer="16"/>
    <define name="MAG_Z_SENS" value="4.40442339014" integer="16"/>
  </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_RATE" prefix="STABILIZATION_RATE_">
    <!-- setpoints -->
    <define name="SP_MAX_P" value="10000"/>
    <define name="SP_MAX_Q" value="10000"/>
    <define name="SP_MAX_R" value="10000"/>
    <define name="DEADBAND_P" value="20"/>
    <define name="DEADBAND_Q" value="20"/>
    <define name="DEADBAND_R" value="200"/>
    <define name="REF_TAU" value="4"/>

    <!-- feedback -->
    <define name="GAIN_P" value="000"/>
    <define name="GAIN_Q" value="000"/>
    <define name="GAIN_R" value="00"/>

    <define name="IGAIN_P" value="0"/>
    <define name="IGAIN_Q" value="0"/>
    <define name="IGAIN_R" value="0"/>

    <!-- feedforward -->
    <define name="DDGAIN_P" value="300"/>
    <define name="DDGAIN_Q" value="300"/>
    <define name="DDGAIN_R" value="300"/>
  </section>



  <section name="STABILIZATION_ATTITUDE" prefix="STABILIZATION_ATTITUDE_">
    <!-- setpoints -->
    <define name="SP_MAX_PHI"     value="35." unit="deg"/>
    <define name="SP_MAX_THETA"   value="35." unit="deg"/>
    <define name="SP_MAX_R"       value="40." unit="deg/s"/>
    <define name="DEADBAND_A"     value="5"/>
    <define name="DEADBAND_E"     value="5"/>
    <define name="DEADBAND_R"     value="10"/>

    <!-- inetgrator limits -->
    <define name="MAX_SUM_ERR"   value="50"/>

    <!-- reference -->
    <define name="REF_OMEGA_P"  value="800" 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="800" 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="500" 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.)"/>

    <!-- Big fat ARK Gains -->
    <define name="PHI_PGAIN"  value="2700"/>
    <define name="PHI_DGAIN"  value="700"/>
    <define name="PHI_IGAIN"  value="5.0"/>

    <define name="THETA_PGAIN"  value="2700"/>
    <define name="THETA_DGAIN"  value="700"/>
    <define name="THETA_IGAIN"  value="5.0"/>

    <define name="PSI_PGAIN" value="4000"/>
    <define name="PSI_DGAIN" value="3500"/>
    <define name="PSI_IGAIN" value="1.5"/>

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

  <section name="GUIDANCE_V" prefix="GUIDANCE_V_">
    <define name="MIN_ERR_Z"   value="POS_BFP_OF_REAL(-10.)"/>
    <define name="MAX_ERR_Z"   value="POS_BFP_OF_REAL( 10.)"/>
    <define name="MIN_ERR_ZD"  value="SPEED_BFP_OF_REAL(-10.)"/>
    <define name="MAX_ERR_ZD"  value="SPEED_BFP_OF_REAL( 10.)"/>
    <define name="MAX_SUM_ERR" value="2000000"/>
    <define name="HOVER_KP"    value="265"/>
    <define name="HOVER_KD"    value="339"/>
    <define name="HOVER_KI"    value="52"/>
    <define name="NOMINAL_HOVER_THROTTLE" value="0.50"/>
    <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="REF_MAX_SPEED" value="8.0" unit="m/s"/>
    <define name="REF_MAX_ACCEL" value="5.66" unit="m/s2"/>
    <define name="PGAIN" value="27"/>
    <define name="DGAIN" value="21"/>
    <define name="AGAIN" value="17"/>
    <define name="IGAIN" value="16"/>
  </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="f_motor, fr_motor, br_motor, b_motor, bl_motor, fl_motor" type="string[]"/>
    <define name="JSBSIM_MODEL"    value="AGGIEAIR/aggieair_ark_hexa" type="string"/>
    <define name="SENSORS_PARAMS"  value="nps_sensors_params_default.h" type="string"/>

    <define name="JS_AXIS_THROTTLE" value="0"/>
    <define name="JS_AXIS_THROTTLE_REVERSED" value="1"/>

    <define name="JS_AXIS_ROLL" value="1"/>

    <define name="JS_AXIS_PITCH" value="2"/>
    <define name="JS_AXIS_PITCH_REVERSED" value="1"/>

    <define name="JS_AXIS_YAW" value="3"/>
    <define name="JS_AXIS_YAW_REVERSED" value="1"/>

    <define name="JS_AXIS_MODE" value="7"/>
  </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="AC_ICON"             value="hexarotor"/>
  </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">
    <!-- Flight values -->
    <!--
    <define name="CATASTROPHIC_BAT_LEVEL" value="19.2" unit="V"/>
    <define name="CRITIC_BAT_LEVEL" value="20.0" unit="V"/>
    <define name="LOW_BAT_LEVEL" value="21.0" unit="V"/>
    <define name="MAX_BAT_LEVEL" value="25.3" unit="V"/>
    -->

    <!-- Simulator values -->
    <define name="MAX_BAT_LEVEL" value="5.0" unit="V" />
    <define name="LOW_BAT_LEVEL" value="4.0" unit="V"/>
    <define name="CRITIC_BAT_LEVEL" value="3.5" unit="V"/>
    <define name="CATASTROPHIC_BAT_LEVEL" value="3.0" unit="V"/>
  </section>

</airframe>
