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

<!-- this is a quadrotor frame in X-configuration equiped with
     * Autopilot:   Lisa-m 
     * Actuators:   HobbyKing Avro Slim 20A
     * GPS:         Ublox                  http://wiki.paparazziuav.org/wiki/Subsystem/gps
     * RC:          one Spektrum sat       http://wiki.paparazziuav.org/wiki/Subsystem/radio_control#Spektrum
-->

<airframe name="Quadrotor MavTec">

  <firmware name="rotorcraft">
    <target name="ap" board="lisa_m_2.0">
      <subsystem name="radio_control" type="spektrum">
        <define name="RADIO_KILL_SWITCH" value="RADIO_GEAR"/>
        <define name="RADIO_MODE" value="RADIO_FLAP"/>
        <!--define name="USE_KILL_SWITCH_FOR_MOTOR_ARMING" value="1"/-->
      </subsystem>
      <configure name="NO_LUFTBOOT" value="1"/>
      <!-- MPU6000 is configured to output data at 2kHz, but polled at 512Hz PERIODIC_FREQUENCY -->
    </target>

    <target name="nps" board="pc">
      <subsystem name="fdm" type="jsbsim"/>
      <subsystem name="radio_control" type="ppm"/>
    </target>

    <subsystem name="motor_mixing"/>
    <subsystem name="actuators"     type="pwm">
      <define name="SERVO_HZ" value="400"/>
    </subsystem>
    <subsystem name="telemetry"     type="transparent"/>
    <subsystem name="imu"           type="aspirin_v2.2"/>
    <subsystem name="gps"           type="ublox"/>
    <subsystem name="stabilization" type="int_quat"/>
    <subsystem name="ahrs"          type="int_cmpl_quat">
      <define name="AHRS_GRAVITY_HEURISTIC_FACTOR" value="30"/>
    </subsystem>
    <subsystem name="ins"/>
  </firmware>

  <firmware name="test_progs">
  </firmware>

  <modules>
  </modules>

  <servos driver="Pwm">
    <servo name="MOTOR_1" no="0" min="1000" neutral="1100" max="1900"/>
    <servo name="MOTOR_2" no="1" min="1000" neutral="1100" max="1900"/>
    <servo name="MOTOR_3" no="2" min="1000" neutral="1100" max="1900"/>
    <servo name="MOTOR_4" no="3" min="1000" neutral="1100" max="1900"/>
  </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="4"/>
    <define name="SCALE" value="256"/>
    <!-- order (and rotation direction) : NE (CW), SE (CCW), SW (CW), NW (CCW) -->
    <define name="ROLL_COEF"   value="{ -256, -256,  256,  256 }"/>
    <define name="PITCH_COEF"  value="{  256, -256, -256,  256 }"/>
    <define name="YAW_COEF"    value="{ -256,  256, -256,  256 }"/>
    <define name="THRUST_COEF" value="{  256,  256,  256,  256 }"/>
  </section>

  <command_laws>
    <call fun="motor_mixing_run(autopilot_motors_on,FALSE,values)"/>
    <set servo="MOTOR_1"  value="motor_mixing.commands[0]"/>
    <set servo="MOTOR_2"  value="motor_mixing.commands[1]"/>
    <set servo="MOTOR_3"  value="motor_mixing.commands[2]"/>
    <set servo="MOTOR_4"  value="motor_mixing.commands[3]"/>
  </command_laws>

  <section name="IMU" prefix="IMU_">
    <define name="ACCEL_X_NEUTRAL" value="-9"/>
    <define name="ACCEL_Y_NEUTRAL" value="24"/>
    <define name="ACCEL_Z_NEUTRAL" value="65"/>
    <define name="ACCEL_X_SENS" value="4.90393762843" integer="16"/>
    <define name="ACCEL_Y_SENS" value="4.919330721" integer="16"/>
    <define name="ACCEL_Z_SENS" value="4.85197383186" integer="16"/>

    <!-- replace this with your own calibration -->
    <define name="MAG_X_NEUTRAL" value="-88"/>
    <define name="MAG_Y_NEUTRAL" value="42"/>
    <define name="MAG_Z_NEUTRAL" value="44"/>
    <define name="MAG_X_SENS" value="3.55840131062" integer="16"/>
    <define name="MAG_Y_SENS" value="3.59312782171" integer="16"/>
    <define name="MAG_Z_SENS" value="4.16199901926" 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="270." unit="deg"/>
  </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"/>
    <define name="USE_GPS_HEADING" value="0"/>
  </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="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"/>

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

 <section name="INS" prefix="INS_">
    <!-- Use GPS altitude measurments and set the R gain -->
    <!-- define name="USE_GPS_ALT" value="1"/ -->
    <!-- define name="VFF_R_GPS" value="0.01"/ -->
  </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="180." unit="deg/s"/>
    <define name="REF_MAX_RDOT" value="RadOfDeg(1800.)"/>
    <!-- feedback -->
    <define name="PHI_PGAIN" value="900"/>
    <define name="PHI_DGAIN" value="200"/>
    <define name="PHI_IGAIN" value="200"/>
    <define name="THETA_PGAIN" value="900"/>
    <define name="THETA_DGAIN" value="200"/>
    <define name="THETA_IGAIN" value="200"/>
    <define name="PSI_PGAIN" value="900"/>
    <define name="PSI_DGAIN" value="200"/>
    <define name="PSI_IGAIN" value="10"/>
    <!-- feedforward -->
    <define name="PHI_DDGAIN" value="75"/>
    <define name="THETA_DDGAIN" value="75"/>
    <define name="PSI_DDGAIN" value="75"/>
  </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="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="FALSE"/>
  </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="PGAIN" value="50"/>
    <define name="DGAIN" value="100"/>
    <define name="AGAIN" value="70"/>
    <define name="IGAIN" value="20"/>
  </section>

  <section name="SIMULATOR" prefix="NPS_">
    <define name="ACTUATOR_NAMES" value="{&quot;ne_motor&quot;, &quot;se_motor&quot;, &quot;sw_motor&quot;, &quot;nw_motor&quot;}"/>
    <define name="JSBSIM_MODEL" value="&quot;simple_x_quad&quot;"/>
    <define name="SENSORS_PARAMS" value="&quot;nps_sensors_params_default.h&quot;"/>
  </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">
    <define name="CATASTROPHIC_BAT_LEVEL" value="9.3" unit="V"/>
    <define name="CRITIC_BAT_LEVEL" value="9.6" unit="V"/>
    <define name="LOW_BAT_LEVEL" value="10.1" unit="V"/>
    <define name="MAX_BAT_LEVEL" value="12.4" unit="V"/>
  </section>

</airframe>
