<!-- this is a default Walkera V120D02S frame equiped with Lisa/S 0.1 -->
<!-- Using Lisa/S V1.0 as it is software compatible. -->

<!--
     Applicable configuration:
     airframe="airframes/tudelft/IMAV2013/walkera_V120D02S.xml"
     radio="radios/cockpitSX.xml"
     telemetry="telemetry/default_rotorcraft.xml"
     flight_plan="flight_plans/rotorcraft_basic.xml"
     settings="settings/rotorcraft_basic.xml settings/control/rotorcraft_guidance.xml settings/control/stabilization_att_int.xml"
-->

<airframe name="walkera_v120d02s">

  <servos driver="Pwm">
    <servo name="CIC_FRONT" no="5" min="2000" neutral="1550" max="900"/>
    <servo name="CIC_RIGHT" no="0" min="900" neutral="1550" max="2000"/>
    <servo name="CIC_LEFT" no="1" min="900" neutral="1550" max="2000"/>
    <servo name="TAIL" no="2" min="1000" neutral="1400" max="1800"/>
    <servo name="GAS" no="3" min="900" neutral="980" max="2200"/>
  </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>

  <command_laws>
    <let var="hoverpower" value=".75*MAX_PPRZ"/>
    <let var="hoverstick" value=".35*MAX_PPRZ"/>
    <let var="halfway" value="(@THRUST >= ($hoverstick) ? 1 : 0)"/>
    <let var="collective" value="@THRUST * 0.4 + (MAX_PPRZ*0.01)"/>
    <let var="gas" value="($hoverpower) + $halfway * (((@THRUST) - ($hoverstick)) * (MAX_PPRZ - ($hoverpower))) / (MAX_PPRZ - ($hoverstick))  + (1 - $halfway) * ((@THRUST - ($hoverstick)) * ($hoverpower)) / ($hoverstick)"/>

    <set servo="GAS" value="$gas"/>
    <set servo="CIC_LEFT" value="((@PITCH*0.5*0.5)-(@ROLL*0.5*0.7))+($collective)"/>
    <set servo="CIC_RIGHT" value="((-@PITCH*0.5*0.5)-(@ROLL*0.5*0.7))-($collective)"/>
    <set servo="CIC_FRONT" value="-@PITCH*0.5*1.0+($collective)"/>
    <set servo="TAIL" value="@YAW"/>
  </command_laws>

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

    <define name="ACCEL_X_NEUTRAL" value="0"/>
    <define name="ACCEL_Y_NEUTRAL" value="0"/>
    <define name="ACCEL_Z_NEUTRAL" value="0"/>

    <!-- From delft MAVLab (TODO) -->
    <define name="MAG_X_NEUTRAL" value="-638"/>
    <define name="MAG_Y_NEUTRAL" value="-101"/>
    <define name="MAG_Z_NEUTRAL" value="-307"/>
    <define name="MAG_X_SENS" value="3.61215021824" integer="16"/>
    <define name="MAG_Y_SENS" value="4.23407732206" integer="16"/>
    <define name="MAG_Z_SENS" value="4.28237277804" integer="16"/>
  </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_DIRECT"/>
 </section>

 <section name="BAT">
   <define name="MILLIAMP_AT_FULL_THROTTLE" value="10"/>
   <define name="CATASTROPHIC_BAT_LEVEL" value="4.3" unit="V"/>
 </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="90." unit="deg/s"/>
    <define name="DEADBAND_R" value="250"/>
    <define name="DEADBAND_A" value="250"/>

    <!-- reference -->
    <define name="REF_OMEGA_P" value="3000" 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="3000" 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="3000" unit="deg/s"/>
    <define name="REF_ZETA_R" value="0.85"/>
    <define name="REF_MAX_R" value="300." unit="deg/s"/>
    <define name="REF_MAX_RDOT" value="RadOfDeg(7000.)"/>

    <!-- feedback -->
    <define name="PHI_PGAIN" value="3052"/>
    <define name="PHI_DGAIN" value="108"/>
    <define name="PHI_IGAIN" value="14"/>

    <define name="THETA_PGAIN" value="3052"/>
    <define name="THETA_DGAIN" value="108"/>
    <define name="THETA_IGAIN" value="14"/>

    <define name="PSI_PGAIN" value="1099"/>
    <define name="PSI_DGAIN" value="201"/>
    <define name="PSI_IGAIN" value="19"/>

    <!-- feedforward -->
    <define name="PHI_DDGAIN" value="0"/>
    <define name="THETA_DDGAIN" value="0"/>
    <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="INS" prefix="INS_">
  </section>

 <section name="GUIDANCE_V" prefix="GUIDANCE_V_">
   <define name="HOVER_KP" value="281"/>
   <define name="HOVER_KD" value="66"/>
   <define name="HOVER_KI" value="0"/>
   <define name="GUIDANCE_V_NOMINAL_HOVER_THROTTLE" value="0.9"/>
  </section>

  <section name="AHRS" prefix="AHRS_">
    <define name="H_X" value=" 0.47577"/>
    <define name="H_Y" value=" 0.11811"/>
    <define name="H_Z" value=" 0.87161"/>
  </section>

 <section name="GUIDANCE_H" prefix="GUIDANCE_H_">
   <define name="PGAIN" value="16"/>
   <define name="DGAIN" value="35"/>
   <define name="IGAIN" value="6"/>
 </section>

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

  <firmware name="rotorcraft">
    <target name="ap" board="lisa_s_1.0">
      <module name="radio_control" type="superbitrf_rc">
        <define name="RADIO_TRANSMITTER_ID" value="2008496626"/> <!-- TUDelft Dx6i: TX 4 -->
        <define name="RADIO_TRANSMITTER_CHAN" value="6"/>
        <define name="RADIO_TRANSMITTER_PROTOCOL" value="0x01"/>
        <define name="RADIO_KILL_SWITCH" value="RADIO_FLAP"/>
      </module>

      <configure name="AHRS_PROPAGATE_FREQUENCY" value="500"/>
    </target>

    <module name="actuators" type="pwm">
      <define name="SERVO_HZ" value="50"/>
      <define name="USE_SERVOS_1AND2"/>
    </module>

    <module name="telemetry" type="superbitrf"/>
    <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"/>
  </firmware>
</airframe>
