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

<airframe name="Quadrotor KroozSD Pwm">

  <firmware name="rotorcraft">
    <target name="ap" board="krooz_sd">
    </target>

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

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

    <module name="radio_control" type="ppm"/>
    <module name="telemetry"     type="transparent"/>
    <module name="imu"           type="krooz_sd"/>
    <module name="gps"           type="ublox"/>
    <module name="stabilization" type="int_euler"/>
    <module name="ahrs"          type="int_cmpl_quat"/>
    <module name="ins"           type="hff"/>
    <module name="motor_mixing"/>

    <define name="NO_RC_THRUST_LIMIT"/>
    <define name="USE_RC_FP_BLOCK_SWITCHING"/>
    <define name="USE_ATTITUDE_REF" value="0"/>
  </firmware>

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

  <servos driver="Pwm">
    <servo name="FRONT"   no="0" min="1000" neutral="1190" max="1900"/>
    <servo name="BACK"    no="1" min="1000" neutral="1190" max="1900"/>
    <servo name="RIGHT"   no="2" min="1000" neutral="1190" max="1900"/>
    <servo name="LEFT"    no="3" min="1000" neutral="1190" 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>

  <command_laws>
    <call fun="motor_mixing_run(autopilot_get_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="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="512"/>
    <define name="ROLL_COEF"   value="{    0,  MOTOR_MIXING_SCALE,    0, -MOTOR_MIXING_SCALE }"/>
    <define name="PITCH_COEF"  value="{  MOTOR_MIXING_SCALE,    0, -MOTOR_MIXING_SCALE,    0 }"/>
    <define name="YAW_COEF"    value="{ -MOTOR_MIXING_SCALE,  MOTOR_MIXING_SCALE, -MOTOR_MIXING_SCALE,  MOTOR_MIXING_SCALE }"/>
    <define name="THRUST_COEF" value="{  MOTOR_MIXING_SCALE,  MOTOR_MIXING_SCALE,  MOTOR_MIXING_SCALE,  MOTOR_MIXING_SCALE }"/>
  </section>

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

    <!-- replace this with your own calibration -->
    <define name="GYRO_P_NEUTRAL" value="0"/>
    <define name="GYRO_Q_NEUTRAL" value="0"/>
    <define name="GYRO_R_NEUTRAL" value="0"/>
    <define name="GYRO_P_SENS" value="0.5454" integer="16"/>
    <define name="GYRO_Q_SENS" value="0.5454" integer="16"/>
    <define name="GYRO_R_SENS" value="0.5454" integer="16"/>

    <define name="ACCEL_X_NEUTRAL" value="-48"/>
    <define name="ACCEL_Y_NEUTRAL" value="86"/>
    <define name="ACCEL_Z_NEUTRAL" value="-479"/>
    <define name="ACCEL_X_SENS" value="0.6131" integer="16"/>
    <define name="ACCEL_Y_SENS" value="0.6131" integer="16"/>
    <define name="ACCEL_Z_SENS" value="0.6131" integer="16"/>

    <define name="MAG_X_NEUTRAL" value="-282"/>
    <define name="MAG_Y_NEUTRAL" value="-78"/>
    <define name="MAG_Z_NEUTRAL" value="109"/>
    <define name="MAG_X_SENS" value="3.33430456557" integer="16"/>
    <define name="MAG_Y_SENS" value="3.53180739126" integer="16"/>
    <define name="MAG_Z_SENS" value="3.68297143457" 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>

  <section name="AHRS" prefix="AHRS_">
    <define name="PROPAGATE_LOW_PASS_RATES" value="1"/>
    <define name="GRAVITY_HEURISTIC_FACTOR" value="30"/>
    <define name="H_X" value="0.365258"/>
    <define name="H_Y" value="0.015505"/>
    <define name="H_Z" value="0.930777"/>
  </section>

  <section name="INS" prefix="INS_">
  </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_A"     value="0"/>
    <define name="DEADBAND_E"     value="0"/>
    <define name="DEADBAND_R"     value="250"/>

    <!-- reference -->
    <define name="REF_OMEGA_P"  value="400" unit="deg/s"/>
    <define name="REF_ZETA_P"   value="0.85"/>
    <define name="REF_MAX_P"    value="400." unit="deg/s"/>
    <define name="REF_MAX_PDOT" value="RadOfDeg(8000.)"/>

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

    <define name="REF_OMEGA_R"  value="250" 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(180.)"/>

    <!-- feedback -->
    <define name="PHI_PGAIN"  value="850"/>
    <define name="PHI_DGAIN"  value="400"/>
    <define name="PHI_IGAIN"  value="100"/>

    <define name="THETA_PGAIN"  value="850"/>
    <define name="THETA_DGAIN"  value="400"/>
    <define name="THETA_IGAIN"  value="100"/>

    <define name="PSI_PGAIN"  value="4000"/>
    <define name="PSI_DGAIN"  value="1500"/>
    <define name="PSI_IGAIN"  value="300"/>

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

  <section name="GUIDANCE_V" prefix="GUIDANCE_V_">
    <define name="REF_MIN_ZDD" value="-0.2*9.81"/> <!-- max descent acceleration -->
    <define name="REF_MAX_ZDD" value="0.2*9.81"/>  <!-- max climb acceleration -->
    <define name="REF_MIN_ZD" value="-3."/> <!-- max descent speed -->
    <define name="REF_MAX_ZD" value="3."/>  <!-- max climb speed -->
    <define name="HOVER_KP"    value="120"/>
    <define name="HOVER_KD"    value="80"/>
    <define name="HOVER_KI"    value="20"/>
	  <define name="RC_DESCENT_COEF" value ="200"/>
  </section>

  <section name="GUIDANCE_H" prefix="GUIDANCE_H_">
    <define name="USE_REF" value="1"/>
    <define name="USE_SPEED_REF" value="1"/> <!-- using RC to control horizontal setpoint -->
    <define name="MAX_BANK" value="27" unit="deg"/>
    <define name="REF_MAX_ACCEL" value="7."/> <!-- max reference horizontal acceleration in m/s2 -->
    <define name="REF_MAX_SPEED" value="2."/> <!-- max reference horizontal speed in m/s -->
		<define name="RC_SPEED_DEAD_BAND" value="200000"/>
    <define name="PGAIN" value="100"/>
    <define name="DGAIN" value="120"/>
    <define name="IGAIN" value="10"/>
    <define name="AGAIN" value="10"/>
  </section>

  <section name="SIMULATOR" prefix="NPS_">
    <define name="ACTUATOR_NAMES"  value="front_motor, back_motor, right_motor, left_motor" type="string[]"/>
    <define name="JSBSIM_MODEL" value="simple_quad" type="string"/>
    <define name="SENSORS_PARAMS" value="nps_sensors_params_default.h" type="string"/>
  </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_NAV"/>
  </section>

  <section name="BAT">
    <define name="CRITIC_BAT_LEVEL" value="14.0" unit="V"/>
    <define name="LOW_BAT_LEVEL" value="14.2" unit="V"/>
    <define name="MAX_BAT_LEVEL" value="16.8" unit="V"/>
  </section>

</airframe>
