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

<airframe name="Blender">

  <modules main_freq="512">
    <load name="servo_switch.xml"/>
    <load name="rotorcraft_cam.xml"/>
    <!--load name="sonar_adc.xml"/-->
    <!--load name="adc_generic.xml">
      <configure name="ADC_CHANNEL_GENERIC1" value="ADC_0"/>
    </load-->
  </modules>

  <firmware name="rotorcraft">
    <define name="USE_INS_NAV_INIT"/>

    <target name="ap" board="navgo_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">
      <subsystem name="fdm"         type="jsbsim"/>
    </target>

    <subsystem name="radio_control" type="ppm"/>
    <subsystem name="telemetry"     type="transparent"/>
    <subsystem name="actuators"     type="skiron">
      <define name="SKIRON_I2C_SCL_TIME" value="25"/>
    </subsystem>
    <subsystem name="motor_mixing"/>
    <subsystem name="actuators"     type="pwm">
      <define name="USE_PWM0"/>
    </subsystem>
    <subsystem name="imu"           type="navgo"/>
    <subsystem name="gps"           type="ublox">
      <configure name="GPS_BAUD" value="B57600"/>
    </subsystem>
    <subsystem name="stabilization" type="int_euler"/>
    <subsystem name="ahrs"          type="int_cmpl_quat"/>
    <subsystem name="ins"           type="hff"/>
  </firmware>

  <section name="MIXING" prefix="MOTOR_MIXING_">
    <define name="TRIM_ROLL" value="-50"/>
    <define name="TRIM_PITCH" value="-150"/>
    <define name="TRIM_YAW" value="-100"/>
    <define name="NB_MOTOR" value="4"/>
    <define name="SCALE" value="256"/>
    <define name="ROLL_COEF"  value="{    0, -256,    0,  256}"/>
    <define name="PITCH_COEF" value="{  256,    0, -256,    0}"/>
    <define name="YAW_COEF"   value="{ -256,  256, -256,  256}"/>
    <define name="THRUST_COEF" value="{ 256,  256,  256,  256}"/>
  </section>

  <servos driver="Skiron">
    <servo name="FRONT"  no="0"    min="0" neutral="20" max="255"/>
    <servo name="RIGHT"  no="1"    min="0" neutral="20" max="255"/>
    <servo name="BACK"   no="2"    min="0" neutral="20" max="255"/>
    <servo name="LEFT"   no="3"    min="0" neutral="20" max="255"/>
  </servos>

  <servos driver="Pwm">
    <servo name="DROP" no="0" min="1000" neutral="1500" max="2000"/>
  </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>
    <call fun="motor_mixing_run(autopilot_motors_on,FALSE,values)"/>
    <set servo="FRONT" value="motor_mixing.commands[0]"/>
    <set servo="BACK"  value="motor_mixing.commands[1]"/>
    <set servo="RIGHT" value="motor_mixing.commands[2]"/>
    <set servo="LEFT"  value="motor_mixing.commands[3]"/>
  </command_laws>

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

    <define name="GYRO_P_NEUTRAL" value="10"/>
    <define name="GYRO_Q_NEUTRAL" value="-32"/>
    <define name="GYRO_R_NEUTRAL" value="11"/>

    <!-- SENS ITG3200  1/14.375 (deg/s)/LSB, rate frac 12bit => 1/14.375 * pi / 180 * 2^12 -->
    <define name="GYRO_P_SENS" value="4.97312" integer="16"/>
    <define name="GYRO_Q_SENS" value="4.97312" integer="16"/>
    <define name="GYRO_R_SENS" value="4.97312" integer="16"/>

    <define name="ACCEL_X_NEUTRAL" value="9"/>
    <define name="ACCEL_Y_NEUTRAL" value="14"/>
    <define name="ACCEL_Z_NEUTRAL" value="-16"/>

    <!-- SENS ADXL345 16G 31.2 mg/LSB, accel frac 10bit => 31.2 * 2^10 / 1000 = 31.9488-->
    <define name="ACCEL_X_SENS" value="38.5866088465" integer="16"/>
    <define name="ACCEL_Y_SENS" value="38.7212932023" integer="16"/>
    <define name="ACCEL_Z_SENS" value="39.403098907" integer="16"/>

    <!-- Calib Toulouse -->
    <!--define name="MAG_X_NEUTRAL" value="80"/>
    <define name="MAG_Y_NEUTRAL" value="-271"/>
    <define name="MAG_Z_NEUTRAL" value="112"/>
    <define name="MAG_X_SENS" value="4.44131219218" integer="16"/>
    <define name="MAG_Y_SENS" value="4.56234629213" integer="16"/>
    <define name="MAG_Z_SENS" value="5.298653926" integer="16"/-->

    <!-- Calid IMAV -->
    <define name="MAG_X_NEUTRAL" value="308"/>
    <define name="MAG_Y_NEUTRAL" value="-395"/>
    <define name="MAG_Z_NEUTRAL" value="156"/>
    <define name="MAG_X_SENS" value="4.04127878147" integer="16"/>
    <define name="MAG_Y_SENS" value="3.99918458069" integer="16"/>
    <define name="MAG_Z_SENS" value="4.79859595184" integer="16"/>

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

  </section>

  <!-- Magnetic field for Toulouse (declination -16°) -->
  <!--section name="AHRS" prefix="AHRS_">
    <define name="PROPAGATE_LOW_PASS_RATES" value="1"/>
    <define name="H_X" value=" 0.513081"/>
    <define name="H_Y" value="-0.00242783"/>
    <define name="H_Z" value=" 0.858336"/>
  </section-->

  <!-- Magnetic field for Gifhorn (declination 2°) -->
  <section name="AHRS" prefix="AHRS_">
    <define name="PROPAGATE_LOW_PASS_RATES" value="1"/>
    <define name="H_X" value="0.3794131"/>
    <define name="H_Y" value="0.0141005"/>
    <define name="H_Z" value="0.9251199"/>
  </section>

  <section name="STABILIZATION_RATE" prefix="STABILIZATION_RATE_">

    <define name="SP_MAX_P" value="10000"/>
    <define name="SP_MAX_Q" value="10000"/>
    <define name="SP_MAX_R" value="10000"/>

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

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

    <!-- 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="90." unit="deg/s"/>
    <define name="REF_MAX_RDOT" value="RadOfDeg(900.)"/>

    <!-- feedback -->
    <define name="PHI_PGAIN"  value="1800"/>
    <define name="PHI_DGAIN"  value="240"/>
    <define name="PHI_IGAIN"  value="200"/>

    <define name="THETA_PGAIN"  value="1800"/>
    <define name="THETA_DGAIN"  value="240"/>
    <define name="THETA_IGAIN"  value="200"/>

    <define name="PSI_PGAIN"  value="1000"/>
    <define name="PSI_DGAIN"  value="350"/>
    <define name="PSI_IGAIN"  value="20"/>

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

    <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="130"/>
   <define name="HOVER_KD"     value="80"/>
   <define name="HOVER_KI"     value="0"/>
   <!-- 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="50"/>
   <define name="DGAIN" value="80"/>
   <define name="IGAIN" value="20"/>
   <define name="NGAIN" value="0"/>
   <!-- feedforward -->
   <define name="AGAIN" value="0"/>
 </section>

 <section name="BAT">
   <define name="CATASTROPHIC_BAT_LEVEL" value="9.3" 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_NAV"/>
 </section>

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

 <section name="CAM" prefix="ROTORCRAFT_CAM_">
    <define name="DEFAULT_MODE" value="ROTORCRAFT_CAM_MODE_MANUAL"/>
 </section>

  <section name="SERVO_SWITCH">
    <define name="SERVO_SWITCH_SERVO" value="DROP"/>
    <define name="SERVO_SWITCH_ON_VALUE" value="SERVO_DROP_MIN"/>
    <define name="SERVO_SWITCH_OFF_VALUE" value="SERVO_DROP_MAX"/>
    <define name="DropOpen()" value="ServoSwitchOn()"/>
    <define name="DropClose()" value="ServoSwitchOff()"/>
  </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>

</airframe>
