<!-- this is a synerani vehicle equiped with Lisa/M and generic china pwm motor controllers -->

<airframe name="jt_lisam">

  <servos>
    <servo name="GAS" no="0" min="1100" neutral="1100" max="1900"/>
    <servo name="CIC_LEFT" no="2" min="1900" neutral="1500" max="1100"/>
    <servo name="CIC_RIGHT" no="3" min="1900" neutral="1500" max="1100"/>
    <servo name="CIC_FRONT" no="1" min="1900" neutral="1500" max="1100"/>
    <servo name="TAIL" no="4" min="1100" neutral="1500" max="1900"/>
    <servo name="GYRO_GAIN" no="6" min="1100" neutral="1500" max="1900"/>
  </servos>

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

  <section name="MIXER">
    <define name="TCURVE" value="1"/>
    <define name="SUPERVISION_SCALE" value="9600" />
  </section>

  <rc_commands>
    <set command="THRUST" value="@THRUST * SUPERVISION_SCALE / MAX_PPRZ"/>
    <set command="ROLL" value="@ROLL * SUPERVISION_SCALE / MAX_PPRZ"/>
    <set command="PITCH" value="@PITCH * SUPERVISION_SCALE / MAX_PPRZ"/>
    <set command="YAW" value="@YAW * SUPERVISION_SCALE / MAX_PPRZ"/>
  </rc_commands>

  <command_laws>
    <let var="halfway" value="(@THRUST >= (3800) ? 1 : 0)"/>

    <let var="thrust" value="@THRUST * MAX_PPRZ / SUPERVISION_SCALE" />
    <let var="roll" value="@ROLL * MAX_PPRZ / SUPERVISION_SCALE" />
    <let var="pitch" value="@PITCH * MAX_PPRZ / SUPERVISION_SCALE" />
    <let var="yaw" value="@YAW * MAX_PPRZ / SUPERVISION_SCALE" />

    <let var="collective" value="$thrust * 1.8 - (MAX_PPRZ * 0.9)" />

    <set servo="GAS" value="(3800 * 1.6) + $halfway * ($thrust - 3800)  + (1 - $halfway) * ($thrust - 3800) * 1.6" />
    <set servo="CIC_LEFT" value="((-$pitch/2)+($roll*0.7))+($collective)"/>
    <set servo="CIC_RIGHT" value="(($pitch/2)+($roll*0.7))-($collective)"/>
    <set servo="CIC_FRONT" value="$pitch+($collective)"/>
    <set servo="TAIL" value="$yaw"/>
    <set servo="GYRO_GAIN" value="-MAX_PPRZ/2"/>
  </command_laws>

 <section name="AUTOPILOT">
   <define name="MODE_MANUAL" value="AP_MODE_RC_DIRECT"/>
   <define name="MODE_AUTO1"  value="AP_MODE_ATTITUDE_DIRECT"/>
   <define name="MODE_AUTO2"  value="AP_MODE_NAV"/>
 </section>
<!--
AP_MODE_RATE_DIRECT		AP_MODE_ATTITUDE_DIRECT			AP_MODE_HOVER_DIRECT
AP_MODE_RATE_RC_CLIMB		AP_MODE_ATTITUDE_RC_CLIMB
				AP_MODE_ATTITUDE_CLIMB			AP_MODE_HOVER_CLIMB
AP_MODE_RATE_Z_HOLD		AP_MODE_ATTITUDE_Z_HOLD			AP_MODE_HOVER_Z_HOLD
AP_MODE_NAV
-->

  <section name="IMU" prefix="IMU_">
<!--
      <define name="GYRO_P_CHAN" value="1"/>
      <define name="GYRO_Q_CHAN" value="0"/>
      <define name="GYRO_R_CHAN" value="2"/>
-->
      <define name="GYRO_P_SIGN" value="1"/>
      <define name="GYRO_Q_SIGN" value="1"/>
      <define name="GYRO_R_SIGN" value="1"/>

	<define name="GYRO_P_NEUTRAL" value="-31"/>
	<define name="GYRO_Q_NEUTRAL" value="-48"/>
	<define name="GYRO_R_NEUTRAL" value="-17"/>
	<define name="GYRO_P_SENS" value="4.412" integer="16"/>
	<define name="GYRO_Q_SENS" value="4.412" integer="16"/>
	<define name="GYRO_R_SENS" value="4.412" integer="16"/>
<!--
      <define name="ACCEL_X_CHAN" value="3"/>
      <define name="ACCEL_Y_CHAN" value="5"/>
      <define name="ACCEL_Z_CHAN" value="6"/>
-->
      <define name="ACCEL_X_SIGN" value="1"/>
      <define name="ACCEL_Y_SIGN" value="1"/>
      <define name="ACCEL_Z_SIGN" value="1"/>

	<define name="ACCEL_X_NEUTRAL" value="10"/>
	<define name="ACCEL_Y_NEUTRAL" value="1"/>
	<define name="ACCEL_Z_NEUTRAL" value="-3"/>
	<define name="ACCEL_X_SENS" value="38.4436411998" integer="16"/>
	<define name="ACCEL_Y_SENS" value="38.7116161958" integer="16"/>
	<define name="ACCEL_Z_SENS" value="39.970909149" integer="16"/>
<!--
      <define name="MAG_X_CHAN" value="4"/>
      <define name="MAG_Y_CHAN" value="0"/>
      <define name="MAG_Z_CHAN" value="2"/>

      <define name="MAG_45_HACK" value="1"/>
-->
      <define name="MAG_X_SIGN" value="1"/>
      <define name="MAG_Y_SIGN" value=" 1"/>
      <define name="MAG_Z_SIGN" value="1"/>

	<define name="MAG_X_NEUTRAL" value="-56"/>
	<define name="MAG_Y_NEUTRAL" value="-171"/>
	<define name="MAG_Z_NEUTRAL" value="-100"/>
	<define name="MAG_X_SENS" value="4.3833130016" integer="16"/>
	<define name="MAG_Y_SENS" value="4.59820229976" integer="16"/>
	<define name="MAG_Z_SENS" value="4.3085068988" integer="16"/>

    <define name="BODY_TO_IMU_PHI"   value="RadOfDeg(  0.)"/>
    <define name="BODY_TO_IMU_THETA" value="RadOfDeg(  0.)"/>
    <define name="BODY_TO_IMU_PSI"   value="RadOfDeg(  0.)"/>

  </section>

 <section name="BAT">
   <define name="MILLIAMP_AT_FULL_THROTTLE" value="12000"/>
   <define name="CATASTROPHIC_BAT_LEVEL" value="9.3" unit="V"/>
 </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="RadOfDeg(45.)"/>
    <define name="SP_MAX_THETA" value="RadOfDeg(45.)"/>
    <define name="SP_MAX_PSI" value="RadOfDeg(45.)"/>
    <define name="SP_MAX_R" value="RadOfDeg(90.)"/>
    <define name="SP_MAX_P" value="RadOfDeg(90.)"/>
    <define name="DEADBAND_R" value="250"/>
    <define name="DEADBAND_A" value="250"/>

    <!-- reference -->
    <define name="REF_OMEGA_R" value="RadOfDeg(600)"/>
    <define name="REF_ZETA_R" value="0.90"/>
    <define name="REF_MAX_R" value="RadOfDeg(400.)"/>
    <define name="REF_MAX_RDOT" value="RadOfDeg(8000.)"/>

    <define name="REF_OMEGA_Q" value="RadOfDeg(800)"/>
    <define name="REF_ZETA_Q" value="0.90"/>
    <define name="REF_MAX_Q" value="RadOfDeg(500.)"/>
    <define name="REF_MAX_QDOT" value="RadOfDeg(8000.)"/>

    <define name="REF_OMEGA_P" value="RadOfDeg(600)"/>
    <define name="REF_ZETA_P" value="0.85"/>
    <define name="REF_MAX_P" value="RadOfDeg(220.)"/>
    <define name="REF_MAX_PDOT" value="RadOfDeg(1800.)"/>

    <!-- feedback -->
    <define name="PSI_PGAIN" value="750"/>
    <define name="PSI_DGAIN" value="370"/>
    <define name="PSI_IGAIN" value="100"/>

    <define name="THETA_PGAIN" value="800"/>
    <define name="THETA_DGAIN" value="240"/>
    <define name="THETA_IGAIN" value="100"/>

    <define name="PHI_PGAIN" value="4000"/>
    <define name="PHI_DGAIN" value="600"/>
    <define name="PHI_IGAIN" value="10"/>

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

  </section>

  <section name="INS" prefix="INS_">
    <define name="BARO_SENS" value="3.3" integer="16"/>
  </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"/>
   <!-- 1.5m/s for full stick : BOOZ_SPEED_I_OF_F(1.5) / (MAX_PPRZ/2) -->
   <define name="RC_CLIMB_COEF" value ="163"/>
   <!-- BOOZ_SPEED_I_OF_F(1.5) * 20% -->
   <define name="RC_CLIMB_DEAD_BAND" value ="160000"/>
   <define name="INV_M" value ="0.21"/>
  </section>

  <section name="AHRS" prefix="AHRS_">
    <define name="PROPAGATE_FREQUENCY" value="512"/>
    <define name="MAG_UPDATE_YAW_ONLY" value="1"/>
    <!-- magnetic field for Santa cruz from http://www.ngdc.noaa.gov/geomagmodels/IGRFWMM.jsp -->
    <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="100"/>
   <define name="DGAIN" value="100"/>
   <define name="IGAIN" value="0"/>
 </section>

 <section name="SIMULATOR" prefix="NPS_">
   <define name="ACTUATOR_NAMES" value="{&quot;front_motor&quot;, &quot;back_motor&quot;, &quot;right_motor&quot;, &quot;left_motor&quot;}"/>
   <define name="INITIAL_CONDITITONS" value="&quot;reset00&quot;"/>
   <define name="SENSORS_PARAMS" value="&quot;nps_sensors_params_booz2_a1.h&quot;"/>
 </section>

 <section name="MISC">
   <define name="FACE_REINJ_1"  value="1024"/>
 </section>

<!--
<modules main_freq="512">
<load name="vehicle_interface_overo_link.xml"/>
</modules>
-->

  <firmware name="rotorcraft">
    <target name="ap" board="lisa_m_1.0">
<!-- <define name="BOOZ_START_DELAY" value="1"/> -->
      <subsystem name="radio_control" type="ppm"/>
      <subsystem name="actuators" type="heli"/>
      <subsystem name="telemetry" type="transparent"/>
      <define name="SERVO_HZ" value="50"/>
      <define name="SERVO_HZ_SECONDARY" value="50"/>
      <define name="USE_SERVOS_7AND8"/>
      <define name="ROTORCRAFT_COMMANDS_YAW_ALWAYS_ENABLED"/>
    </target>
<!--
    <target name="sim" board="pc">
      <subsystem name="fdm" type="jsbsim"/>
      <subsystem name="radio_control" type="ppm"/>
      <subsystem name="actuators" type="mkk"/>
    </target>
-->
    <subsystem name="imu" type="aspirin_v1.5"/>
    <subsystem name="gps" type="ublox"/>
    <subsystem name="ahrs" type="int_cmpl_euler"/>
    <subsystem name="stabilization" type="euler"/>

  </firmware>

</airframe>
