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

<airframe name="TwinStarXSens">

  <servos driver="Ppm">
    <servo name="THROTTLE_LEFT" no="1" min="1100" neutral="1100" max="1900"/>
    <servo name="THROTTLE_RIGHT" no="0" min="1100" neutral="1100" max="1900"/>
    <servo name="AILERON_RIGHT" no="2" min="2300" neutral="1550" max="700"/>
    <servo name="AILERON_LEFT" no="3" min="2300" neutral="1550" max="700"/>
    <servo name="ELEVATOR" no="4" min="1100" neutral="1500" max="1900"/>
  </servos>

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

  <rc_commands>
    <set command="THROTTLE" value="@THROTTLE"/>
    <set command="ROLL" value="@ROLL"/>
    <set command="PITCH" value="@PITCH"/>
    <set command="BRAKE" value="@YAW"/>
  </rc_commands>

  <section name="SERVO_MIXER_GAINS">
    <define name="AILERON_RATE_UP" value="0.50f"/>
    <define name="AILERON_RATE_DOWN" value="0.25f"/>

    <define name="AILERON_RATE_UP_BRAKE" value="0.5f"/>
    <define name="AILERON_RATE_DOWN_BRAKE" value="0.9f"/>

    <define name="PITCH_GAIN" value="0.9f"/>

    <define name="YAW_THRUST" value="0.0f"/>
    <define name="BRAKE_AILEVON" value="-0.68f"/>
    <define name="BRAKE_PITCH" value="0.0f"/>
    <define name="MAX_BRAKE_RATE" value="130"/>
  </section>

  <command_laws>
    <!-- Brake Rate Limiter -->
    <let var="brake_value_nofilt" value="Chop(-@BRAKE, 0, MAX_PPRZ)"/>
    <ratelimit var="brake_value" value="$brake_value_nofilt" rate_min="-MAX_BRAKE_RATE" rate_max="MAX_BRAKE_RATE" />

    <!-- Differential Aileron Depending on Brake Value -->
    <let var="aileron_up_rate" value="(AILERON_RATE_UP   * (MAX_PPRZ - $brake_value)) + (AILERON_RATE_UP_BRAKE   * $brake_value)"/>
    <let var="aileron_down_rate" value="(AILERON_RATE_DOWN * (MAX_PPRZ - $brake_value)) + (AILERON_RATE_DOWN_BRAKE * $brake_value)"/>
    <let var="aileron_up" value="((-@ROLL)  *  (((float)$aileron_up_rate) / ((float)MAX_PPRZ)))"/>
    <let var="aileron_down" value="((-@ROLL)  * (((float)$aileron_down_rate) / ((float)MAX_PPRZ)))"/>
    <let var="leftturn" value="((-@ROLL) >= 0? 1 : 0)"/>
    <let var="rightturn" value="(1 - $leftturn)"/>
    <set servo="AILERON_LEFT" value="($aileron_up * $leftturn) + ($aileron_down * $rightturn) - $brake_value*(BRAKE_AILEVON)"/>
    <set servo="AILERON_RIGHT" value="($aileron_up * $rightturn) + ($aileron_down * $leftturn) + $brake_value*(BRAKE_AILEVON)"/>

    <!-- Differential Thurst -->
    <set servo="THROTTLE_LEFT" value="@THROTTLE - @ROLL * (YAW_THRUST)"/>
    <set servo="THROTTLE_RIGHT" value="@THROTTLE + @ROLL * (YAW_THRUST)"/>

    <!-- Pitch with Brake-Trim Function -->
    <set servo="ELEVATOR" value="-@PITCH * PITCH_GAIN + BRAKE_PITCH * $brake_value"/>
  </command_laws>

  <section name="AUTO1" prefix="AUTO1_">
    <define name="MAX_ROLL" value="0.7"/>
    <define name="MAX_PITCH" value="0.7"/>
  </section>

  <section name="INS" prefix="INS_">
    <define name="ROLL_NEUTRAL_DEFAULT" value="0"/>
    <define name="PITCH_NEUTRAL_DEFAULT" value="0"/>
  </section>

  <section name="AHRS" prefix="AHRS_">
    <!-- Local magnetic field -->
    <define name="H_X" value="0.51562740288882"/>
    <define name="H_Y" value="-0.05707735220832"/>
    <define name="H_Z" value="0.85490967783446"/>
  </section>

  <section name="XSENS">
    <define name="GPS_IMU_LEVER_ARM_X" value="0.0f"/>
    <define name="GPS_IMU_LEVER_ARM_Y" value="0.0f"/>
    <define name="GPS_IMU_LEVER_ARM_Z" value="0.0f"/>
  </section>


  <section name="ADC" prefix="ADC_CHANNEL_">
    <define name="CURRENT" value="AdcBank1(5)"/>
  </section>

  <section name="BAT">
    <define name="VoltageOfAdc(adc)" value="(0.01688000f*(adc)+2)"/>
    <define name="MilliAmpereOfAdc(adc)" value="((adc) - 505) * 124.0f"/>

    <define name="LOW_BAT_LEVEL" value="10.5" unit="V"/>
    <define name="CRITIC_BAT_LEVEL" value="10" unit="V"/>
    <define name="CATASTROPHIC_BAT_LEVEL" value="9.1" unit="V"/>
  </section>

  <section name="FAILSAFE" prefix="FAILSAFE_">
    <define name="DEFAULT_THROTTLE" value="0.45" unit="%"/>
    <define name="DEFAULT_ROLL" value="0.17" unit="rad"/>
    <define name="DEFAULT_PITCH" value="0.08" unit="rad"/>

    <define name="HOME_RADIUS" value="DEFAULT_CIRCLE_RADIUS" unit="m"/>
    <define name="KILL_MODE_DISTANCE" value="(MAX_DIST_FROM_HOME*1.5)"/>
    <define name="DELAY_WITHOUT_GPS" value="3" unit="s"/>
  </section>

  <section name="HORIZONTAL CONTROL" prefix="H_CTL_">
    <define name="COURSE_PGAIN" value="0.75"/>
    <define name="COURSE_DGAIN" value="0.45"/>
    <define name="COURSE_PRE_BANK_CORRECTION" value="0.935"/>

    <define name="ROLL_MAX_SETPOINT" value="0.8" unit="radians"/>
    <define name="PITCH_MAX_SETPOINT" value="0.4" unit="radians"/>
    <define name="PITCH_MIN_SETPOINT" value="-0.275" unit="radians"/>

    <define name="PITCH_PGAIN" value="11000"/>
    <define name="PITCH_DGAIN" value="0."/>
    <define name="ELEVATOR_OF_ROLL" value="1000."/>

    <!--  roll rate loop -->
    <define name="ROLL_ATTITUDE_GAIN" value="7000."/>
    <define name="ROLL_RATE_GAIN" value="1100"/>
  </section>


 <section name="VERTICAL CONTROL" prefix="V_CTL_">
    <!-- outer loop proportional gain -->
    <define name="ALTITUDE_PGAIN" value="0.1"/>

    <!-- outer loop saturation -->
    <define name="ALTITUDE_MAX_CLIMB" value="3.0"/>

    <!-- auto throttle inner loop -->
    <define name="AUTO_THROTTLE_MIN_CRUISE_THROTTLE" value=".38"/>
    <define name="AUTO_THROTTLE_NOMINAL_CRUISE_THROTTLE" value="0.45"/>
    <define name="AUTO_THROTTLE_MAX_CRUISE_THROTTLE" value="0.62"/>

    <define name="AUTO_THROTTLE_LOITER_TRIM" value="1000."/>
    <define name="AUTO_THROTTLE_DASH_TRIM" value="-1500"/>

    <define name="AUTO_THROTTLE_CLIMB_THROTTLE_INCREMENT" value="0.2" unit="%/(m/s)"/>
    <define name="AUTO_THROTTLE_PGAIN" value="0."/>
    <define name="AUTO_THROTTLE_IGAIN" value="0."/>
    <define name="AUTO_THROTTLE_PITCH_OF_VZ_PGAIN" value="0.175"/>

    <define name="THROTTLE_SLEW_LIMITER" value="1" unit="s"/>
  </section>

  <section name="NAV">
    <define name="AGR_CLIMB_PITCH" value="0."/> <!-- Needed For OSAMNav -->
  </section>

  <section name="MISC">
    <define name="NOMINAL_AIRSPEED" value="15." unit="m/s"/>
    <define name="CARROT" value="5." unit="s"/>
    <define name="CONTROL_FREQUENCY" value="60" unit="Hz"/>

    <define name="DEFAULT_CIRCLE_RADIUS" value="75."/>

    <define name="GLIDE_AIRSPEED" value="10"/>
    <define name="GLIDE_PITCH" value="45" unit="deg"/>

    <define name="COMMAND_ROLL_TRIM" value="180"/>
    <define name="COMMAND_PITCH_TRIM" value="-194."/>
  </section>

  <section name="DIGITAL_CAMERA" prefix="DC_">
    <define name="AUTOSHOOT_QUARTERSEC_PERIOD" value="8" unit="quarter_second"/>
    <define name="AUTOSHOOT_DISTANCE_INTERVAL" value="50" unit="meter"/>
  </section>

  <firmware name="fixedwing">

    <target name="ap" board="classix">
      <configure name="SYS_TIME_LED" value="2"/>

      <define name="USE_AD0"/>
      <define name="USE_AD0_3"/>
      <define name="ADC_6" value="AdcBank0\(3\)"/>

      <subsystem name="control"/>
      <subsystem name="navigation"/>
    </target>

    <target name="fbw" board="classix">
      <configure name="SYS_TIME_LED" value="2"/>
      <define name="RADIO_CONTROL_LED" value="1"/>
      <define name="USE_AD1_5" value="1"/>
      <define name="USE_AD1" value="1"/>
      <configure name="ACTUATORS" value="actuators_ppm"/>
      <define name="ADC_CHANNEL_VSUPPLY" value="AdcBank0\(6\)"/>
    </target>

    <target name="sim" board="pc">
      <subsystem name="control"/>
      <subsystem name="navigation"/>
    </target>

    <define name="DC_IMAGE_BUFFER" value="65000" />
    <define name="STRONG_WIND"/>
    <define name="WIND_INFO"/>
    <define name="WIND_INFO_RET"/>
    <define name="LOITER_TRIM"/>
    <define name="RADIO_CONTROL_AUTO1"/>

    <subsystem name="radio_control" type="ppm"/>

    <subsystem name="telemetry" type="xbee_api">
      <configure name="MODEM_PORT" value="UART0"/>
      <configure name="MODEM_BAUD" value="B9600"/>
    </subsystem>

    <subsystem name="ins" type="xsens">
      <configure name="XSENS_UART_NR" value="1"/>
    </subsystem>

  </firmware>

  <modules>
    <load name="digital_cam.xml">
      <define name="DC_SHUTTER_GPIO" value="GPIOA,GPIO15"/>
   </load>
   <load name="adc_generic.xml">
      <define name="ADC_CHANNEL_GENERIC_NB_SAMPLES" value="32"/>
      <configure name="ADC_CHANNEL_GENERIC1" value="ADC_6"/> <!-- e.g. current sensor -->
   </load>
   <load name="photogrammetry_calculator.xml" />
    <load name="nav_line.xml"/>
  </modules>

<!--+-+-+-+-+-+-+- PHOTOGRAMMETRY -+-+-+-+-+-+-+-->


  <section name="Photogrammetry" prefix="PHOTOGRAMMETRY_">
    <!-- Camera Parameters -->
    <define name="FOCAL_LENGTH" value="20" unit="mm"/>
    <define name="SENSOR_WIDTH" value="24" unit="mm"/>
    <define name="SENSOR_HEIGHT" value="13.5" unit="mm"/>
    <define name="PIXELS_WIDTH" value="1280" unit=""/>

    <!-- Flight Safety Parameters -->
    <define name="HEIGHT_MIN" value="40" unit="m"/>
    <define name="HEIGHT_MAX" value="300" unit="m"/>
    <define name="RADIUS_MIN" value="70" unit="m"/>

  </section>


</airframe>
