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

<airframe name="bebop2_opticflow">

  <firmware name="rotorcraft">
    <target name="ap" board="bebop2">
      <module name="stabilization" type="indi_simple"/>
    </target>
    <target name="nps" board="pc">
      <module name="fdm" type="gazebo"/>
      <module name="stabilization" type="int_quat"/>
    </target>

    <module name="telemetry" type="transparent_udp"/>
    <module name="radio_control" type="datalink"/>
    <module name="motor_mixing"/>
    <module name="actuators" type="bebop"/>
    <module name="imu" type="bebop"/>
    <module name="gps" type="datalink"/>
    <module name="ahrs" type="int_cmpl_quat">
      <configure name="USE_MAGNETOMETER" value="FALSE"/>
      <define name="AHRS_USE_GPS_HEADING" value="TRUE"/>
      <define name="AHRS_HEADING_UPDATE_GPS_MIN_SPEED" value="0"/>
    </module>
    <module name="ins" type="hff_extended"/>

    <module name="stereocam">
      <configure name="STEREO_UART" value="UART2"/>
    </module>
    
  </firmware>

  <commands>
    <axis name="PITCH" failsafe_value="0"/>
    <axis name="ROLL" failsafe_value="0"/>
    <axis name="YAW" failsafe_value="0"/>
    <axis name="THRUST" failsafe_value="6000"/>
  </commands>

  <servos driver="Default">
    <servo name="TOP_LEFT" no="0" min="2500" neutral="2500" max="12000"/>
    <servo name="TOP_RIGHT" no="1" min="2500" neutral="2500" max="12000"/>
    <servo name="BOTTOM_RIGHT" no="2" min="2500" neutral="2500" max="12000"/>
    <servo name="BOTTOM_LEFT" no="3" min="2500" neutral="2500" max="12000"/>
  </servos>

  <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="REVERSE" value="TRUE"/>
    <define name="TYPE" value="QUAD_X"/>
  </section>

  <command_laws>
    <call fun="motor_mixing_run(autopilot_get_motors_on(),FALSE,values)"/>
    <set servo="TOP_LEFT" value="motor_mixing.commands[MOTOR_FRONT_LEFT]"/>
    <set servo="TOP_RIGHT" value="motor_mixing.commands[MOTOR_FRONT_RIGHT]"/>
    <set servo="BOTTOM_RIGHT" value="motor_mixing.commands[MOTOR_BACK_RIGHT]"/>
    <set servo="BOTTOM_LEFT" value="motor_mixing.commands[MOTOR_BACK_LEFT]"/>
  </command_laws>
  
  <section name="stereocam" prefix="STEREO_">
    <define name="BODY_TO_STEREO_PHI" value="90." unit="deg"/>
    <define name="BODY_TO_STEREO_THETA" value="0." unit="deg"/>
    <define name="BODY_TO_STEREO_PSI" value="90." unit="deg"/>
  </section>

  <!-- Magnetometer still needs to be calibrated -->
  <section name="IMU" prefix="IMU_">
    <define name="MAG_X_NEUTRAL" value="0"/>
    <define name="MAG_Y_NEUTRAL" value="0"/>
    <define name="MAG_Z_NEUTRAL" value="0"/>
    <define name="MAG_X_SENS" value="7.28514789391" integer="16"/>
    <define name="MAG_Y_SENS" value="7.33022132691" integer="16"/>
    <define name="MAG_Z_SENS" value="7.57102035692" integer="16"/>
  </section>

  <!-- local magnetic field -->
  <!-- http://wiki.paparazziuav.org/wiki/Subsystem/ahrs#Local_Magnetic_Field -->
  <section name="AHRS" prefix="AHRS_">
    <!-- values used if no GPS fix, on 3D fix is update by geo_mag module -->
    <!--define name="GRAVITY_HEURISTIC_FACTOR" value="0"/-->
    <!-- Delft -->
    <define name="H_X" value="0.3892503"/>
    <define name="H_Y" value="0.0017972"/>
    <define name="H_Z" value="0.9211303"/>
  </section>

  <section name="INS" prefix="INS_">
    <define name="USE_GPS_ALT" value="1"/>
    <!-- trust GPS a lot: -->
    <define name="VFF_R_GPS" value="0.01"/> 
    <define name="VFF_VZ_R_GPS" value="0.01"/>     
  </section>


  <section name="RC_SETPOINT" prefix="STABILIZATION_ATTITUDE_">
    <!-- setpoint limits for attitude stabilization rc flight -->
    <define name="SP_MAX_PHI" value="45" unit="deg"/>
    <define name="SP_MAX_THETA" value="45" unit="deg"/>
    <define name="SP_MAX_R" value="300" unit="deg/s"/>
    <define name="DEADBAND_A" value="0"/>
    <define name="DEADBAND_E" value="0"/>
    <define name="DEADBAND_R" value="50"/>
  </section>

  <section name="ATTITUDE_REFERENCE" prefix="STABILIZATION_ATTITUDE_">
    <!-- attitude reference generation model -->
    <define name="REF_OMEGA_P" value="450" unit="deg/s"/>
    <define name="REF_ZETA_P" value="0.9"/>
    <define name="REF_MAX_P" value="600." unit="deg/s"/>
    <define name="REF_MAX_PDOT" value="RadOfDeg(8000.)"/>

    <define name="REF_OMEGA_Q" value="450" unit="deg/s"/>
    <define name="REF_ZETA_Q" value="0.9"/>
    <define name="REF_MAX_Q" value="600." unit="deg/s"/>
    <define name="REF_MAX_QDOT" value="RadOfDeg(8000.)"/>

    <define name="REF_OMEGA_R" value="450" unit="deg/s"/>
    <define name="REF_ZETA_R" value="0.9"/>
    <define name="REF_MAX_R" value="600." unit="deg/s"/>
    <define name="REF_MAX_RDOT" value="RadOfDeg(8000.)"/>
    
    <!-- feedback -->
    <define name="PHI_PGAIN" value="850"/>
    <define name="PHI_DGAIN" value="425"/>
    <define name="PHI_IGAIN" value="0"/>

    <define name="THETA_PGAIN" value="850"/>
    <define name="THETA_DGAIN" value="425"/>
    <define name="THETA_IGAIN" value="0"/>

    <define name="PSI_PGAIN" value="1000"/>
    <define name="PSI_DGAIN" value="700"/>
    <define name="PSI_IGAIN" value="0"/>

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

  <section name="STABILIZATION_ATTITUDE_INDI" prefix="STABILIZATION_INDI_">
    <!-- control effectiveness, can be obtained by switching on "adaptive" in the indi settings -->
    <define name="G1_P" value="0.100"/>
    <define name="G1_Q" value="0.050"/>
    <define name="G1_R" value="0.0010"/>
    <define name="G2_R" value="0.236"/>

    <!-- Here it is assumed that your removed the damping from your bebop2!
         The dampers do not really damp, but cause oscillation. By removing/
         fixing them, the bebop2 will fly much better-->
    <define name="FILTER_ROLL_RATE" value="FALSE"/>
    <define name="FILTER_PITCH_RATE" value="FALSE"/>
    <define name="FILTER_YAW_RATE" value="FALSE"/>

    <!-- reference acceleration for attitude control -->
    <define name="REF_ERR_P" value="600.0"/>
    <define name="REF_ERR_Q" value="600.0"/>
    <define name="REF_ERR_R" value="600.0"/>
    <define name="REF_RATE_P" value="28.0"/>
    <define name="REF_RATE_Q" value="28.0"/>
    <define name="REF_RATE_R" value="28.0"/>

    <!-- second order filter parameters -->
    <define name="FILT_CUTOFF" value="3.2"/>
    <define name="FILT_CUTOFF_R" value="3.2"/>

    <!-- first order actuator dynamics -->
    <define name="ACT_DYN_P" value="0.06"/>
    <define name="ACT_DYN_Q" value="0.06"/>
    <define name="ACT_DYN_R" value="0.06"/>

    <!-- Adaptive Learning Rate -->
    <define name="USE_ADAPTIVE" value="FALSE"/>
    <define name="ADAPTIVE_MU" value="0.0001"/>
  </section>

  <section name="GUIDANCE_V" prefix="GUIDANCE_V_">
    <define name="HOVER_KP" value="350"/>
    <define name="HOVER_KD" value="85"/>
    <define name="HOVER_KI" value="20"/>
    <define name="NOMINAL_HOVER_THROTTLE" value="0.52"/>
    <define name="ADAPT_THROTTLE_ENABLED" value="TRUE"/>
  </section>

  <section name="GUIDANCE_H" prefix="GUIDANCE_H_">
    <define name="REF_MAX_SPEED" value="2" unit="m/s"/>
    <define name="MAX_BANK" value="35" unit="deg"/>
    <define name="PGAIN" value="82"/> <!-- GPS: 120 no GPS: 0--> 
    <define name="DGAIN" value="68"/> <!-- GPS: 80 no GPS: 302-->
    <define name="IGAIN" value="10"/> <!-- GPS: 30 no GPS: 0-->
  </section>

  <section name="NAVIGATION" prefix="NAV_">
    <define name="CLIMB_VSPEED" value="1.0"/>
    <define name="DESCEND_VSPEED" value="-0.75"/>
  </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"/>

    <define name="GAZEBO_AC_NAME" value="simple_x_quad" type="string"/>    
    <define name="ACTUATOR_THRUSTS" value="1.55, 1.55, 1.55, 1.55" type="double[]"/>
    <define name="ACTUATOR_TORQUES" value="0.155, 0.155, 0.155, 0.155" type="double[]"/>
    
    <!--define name="DEBUG_STEREOCAM" value="1"/-->
    <!--define name="NO_GPS" value="1"/-->
  </section>

  <section name="AUTOPILOT">
    <define name="MODE_STARTUP" value="AP_MODE_NAV"/>
    <define name="MODE_MANUAL" value="AP_MODE_MODULE"/>
    <define name="MODE_AUTO1" value="AP_MODE_MODULE"/>
    <define name="MODE_AUTO2" value="AP_MODE_NAV"/>

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

  <section name="BAT">
    <define name="MILLIAMP_AT_FULL_THROTTLE" value="8700"/>
    <define name="CATASTROPHIC_BAT_LEVEL" value="9.9" unit="V"/>
    <define name="CRITIC_BAT_LEVEL" value="11.0" unit="V"/>
    <define name="LOW_BAT_LEVEL" value="11.1" unit="V"/>
    <define name="MAX_BAT_LEVEL" value="12.4" unit="V"/>
  </section>
</airframe>

