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

<!--
/home/microuav/paparazzi/sw/tools/bluegiga_usb_dongle/bluegiga_usb_driver  /dev/ttyACM2  00:07:80:2d:d6:d9  4242  4252
-->

<airframe name="GeniusDD">
  <description>BSlinger Genius Micro Helicopter with BlueGiga
  </description>

  <firmware name="rotorcraft">
    <target name="ap" board="lisa_s_1.0">
      <define name="AHRS_GRAVITY_HEURISTIC_FACTOR" value="0"/>
      <module name="radio_control" type="datalink">
        <define name="USE_PERSISTENT_SETTINGS" value="TRUE"/>
      </module>
      <configure name="AHRS_PROPAGATE_FREQUENCY" value="500"/>
    </target>
    <define name="AHRS_GRAVITY_HEURISTIC_FACTOR" value="0"/>

    <module name="actuators" type="pwm">
      <define name="TIM5_SERVO_HZ" value="2000"/>
      <define name="SERVO_HZ" value="300"/>
      <define name="USE_SERVOS_1AND2"/>
    </module>

    <configure name="USE_MAGNETOMETER" value="1"/>
    <module name="telemetry" type="bluegiga"/>
    <module name="imu" type="lisa_s_v1.0"/>
    <module name="gps" type="ublox"/>
    <module name="stabilization" type="int_quat"/>
    <module name="ahrs" type="int_cmpl_quat"/>
    <module name="ins"/>
<!--
    <module name="stabilization" type="heli_indi"/>
    <module name="rpm_sensor" type="eagletree"/>
    <module name="ahrs" type="int_cmpl_quat_notch"/>-->
  </firmware>

 <modules main_freq="512">
    <!--<load name="geo_mag.xml"/>-->
    <load name="air_data.xml"/>
    <load name="gps_ubx_ucenter.xml"/>
    <load name="heli_swashplate_mixing.xml"/>
    <load name="heli_throttle_curve.xml"/>
  </modules>

  <servos driver="Pwm">
    <servo name="THROTTLE" no="1" min="1890" neutral="1890" max="1430"/>
    <servo name="FRONT" no="5" min="1700" neutral="1300" max="1000"/>
    <servo name="RIGHTBACK" no="4" min="1850" neutral="1450" max="1150"/>
    <servo name="LEFTBACK" no="0" min="1900" neutral="1550" max="1200"/>
    <servo name="TAIL" no="2" min="0" neutral="0" max="3500"/>
  </servos>

  <commands>
    <axis name="THRUST" failsafe_value="0"/>
    <axis name="ROLL" failsafe_value="0"/>
    <axis name="PITCH" failsafe_value="0"/>
    <axis name="YAW" failsafe_value="0"/>
    <axis name="FMODE" failsafe_value="-9600"/>
  </commands>

  <rc_commands>
    <set command="THRUST" value="@THROTTLE"/>
    <set command="ROLL" value="@ROLL"/>
    <set command="PITCH" value="@PITCH"/>
    <set command="YAW" value="@YAW"/>
    <set command="FMODE" value="-9600"/>
  </rc_commands>

  <section name="MIXING" prefix="SW_MIXING_">
    <!-- front left (CW), front right (CCW), back right (CW), back left (CCW) -->
    <define name="TYPE" value="H120"/>
    <define name="TRIM_ROLL" value="0"/>
    <define name="TRIM_PITCH" value="0"/>
    <define name="TRIM_COLL" value="0"/>
  </section>

  <command_laws>
    <call fun="throttle_curve_run(autopilot_get_motors_on(), values)"/>
    <call fun="swashplate_mixing_run(values)"/>
    <set servo="THROTTLE" value="throttle_curve.throttle"/>
    <set servo="FRONT" value="swashplate_mixing.commands[SW_FRONT]"/>
    <set servo="RIGHTBACK" value="swashplate_mixing.commands[SW_RIGHTBACK]"/>
    <set servo="LEFTBACK" value="swashplate_mixing.commands[SW_LEFTBACK]"/>
    <set servo="TAIL" value="@YAW + throttle_curve.throttle*2800/7000"/>
  </command_laws>

  <heli_curves>
    <curve throttle="0,4800,7200,8400,9600" collective="-1000,750,2500,4250,6000"/>
    <!--<curve throttle="9600,7200,9600" collective="-7500,0,7500"/>-->
  </heli_curves>

  <section name="MISC">
    <define name="NAV_CLIMB_VSPEED" value="2.5"/>
    <define name="NAV_DESCEND_VSPEED" value="-1.0"/>
    <define name="NO_RC_THRUST_LIMIT" value="TRUE"/>
  </section>

  <section name="IMU" prefix="IMU_">
    <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="127"/>
<define name="MAG_Y_NEUTRAL" value="7"/>
<define name="MAG_Z_NEUTRAL" value="-96"/>
<define name="MAG_X_SENS" value="3.54947831816" integer="16"/>
<define name="MAG_Y_SENS" value="3.86771086071" integer="16"/>
<define name="MAG_Z_SENS" value="4.03381407698" integer="16"/>

    <define name="BODY_TO_IMU_PHI" value="90.0" unit="deg"/>
    <define name="BODY_TO_IMU_THETA" value="15.0" unit="deg"/>
    <define name="BODY_TO_IMU_PSI" value="90.0" unit="deg"/>
  </section>

  <section name="AHRS" prefix="AHRS_">
    <!-- values used if no GPS fix, on 3D fix is update by geo_mag module -->
    <define name="H_X" value="0.3770441"/>
    <define name="H_Y" value="0.0193986"/>
    <define name="H_Z" value="0.9259921"/>
  </section>

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

    <!-- feedback -->
    <define name="GAIN_P" value="400"/>
    <define name="GAIN_Q" value="400"/>
    <define name="GAIN_R" value="350"/>

    <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="65." unit="deg"/>
    <define name="SP_MAX_THETA" value="65." unit="deg"/>
    <define name="SP_MAX_R" value="250." unit="deg/s"/>
    <define name="DEADBAND_R" value="200"/>

    <!-- 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="300." unit="deg/s"/>
    <define name="REF_MAX_PDOT" value="RadOfDeg(7000.)"/>
    <define name="REF_OMEGA_Q" value="800" unit="deg/s"/>
    <define name="REF_ZETA_Q" value="0.85"/>
    <define name="REF_MAX_Q" value="300." 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.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="3003"/>
    <define name="PHI_DGAIN" value="333"/>
    <define name="PHI_IGAIN" value="200"/>
    <define name="THETA_PGAIN" value="2172"/>
    <define name="THETA_DGAIN" value="329"/>
    <define name="THETA_IGAIN" value="200"/>
    <define name="PSI_PGAIN" value="2074"/>
    <define name="PSI_DGAIN" value="1479"/>
    <define name="PSI_IGAIN" value="10"/>

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

  <section name="INS" prefix="INS_">
  </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="35" 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="AUTOPILOT">
    <define name="MODE_MANUAL" value="AP_MODE_ATTITUDE_DIRECT"/>
    <define name="MODE_AUTO1" value="AP_MODE_HOVER_Z_HOLD"/>
    <define name="MODE_AUTO2" value="AP_MODE_HOVER_DIRECT"/>

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

 <section name="BAT">
   <define name="MILLIAMP_AT_FULL_THROTTLE" value="14000"/>
   <define name="CATASTROPHIC_BAT_LEVEL" value="2.8" unit="V"/>
   <define name="CRITIC_BAT_LEVEL" value="3.0" unit="V"/>
    <define name="LOW_BAT_LEVEL" value="3.2" unit="V"/>
    <define name="MAX_BAT_LEVEL" value="4.1" unit="V"/>
 </section>

</airframe>
