<airframe name="Twin1">

  <servos>
    <servo name="GAZ"           no="9" min="1000" neutral="1000" max="2000"/>
    <servo name="AILERON_LEFT"  no="2" min="1100" neutral="1492" max="2000"/>
    <servo name="AILERON_RIGHT" no="6" min="1000" neutral="1385" max="1833"/>
    <servo name="ELEVATOR"      no="7" min="1816" neutral="1586" max="1220"/>
    <servo name="RUDDER"        no="3" min="1270" neutral="1546" max="1850"/>
  </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"/>
  </commands>

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

  <section name="MIXER">
    <define name="AILERON_DIFF" value="0.66"/>
    <define name="COMBI_SWITCH" value="1.0"/>
  </section>

  <command_laws>
    <set servo="GAZ" value="@THROTTLE"/>
    <set servo="ELEVATOR" value="@PITCH"/>
    <let var="roll" value="@ROLL"/>
    <set servo="AILERON_LEFT" value="($roll > 0 ? 1 : AILERON_DIFF) * $roll"/>
    <set servo="AILERON_RIGHT" value="($roll > 0 ? AILERON_DIFF : 1) * $roll"/>
    <set servo="RUDDER" value="@YAW + @ROLL*COMBI_SWITCH"/>
  </command_laws>

  <section name="adc" prefix="ADC_CHANNEL_">
    <define name="IR1" value="1"/>
    <define name="IR2" value="2"/>
    <define name="VSUPPLY" value="3"/>
  </section>

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

  <section name="INFRARED" prefix="IR_">
    <define name="ROLL_NEUTRAL_DEFAULT" value="0" unit="deg"/>
    <define name="PITCH_NEUTRAL_DEFAULT" value="0" unit="deg"/>
    <define name="ADC_ROLL_NEUTRAL" value="-1005"/>
    <define name="ADC_PITCH_NEUTRAL" value="75"/>
    <define name="DEFAULT_CONTRAST" value="200"/>
    <define name="RAD_OF_IR_CONTRAST" value="0.75"/>
    <linear name="RollOfIrs" arity="2" coeff1="-1" coeff2="-1"/>
    <linear name="PitchOfIrs" arity="2" coeff1="-1" coeff2="1"/>
  </section>

  <section name="PID">
    <define name="ROLL_PGAIN" value="10000."/>
    <define name="PITCH_OF_ROLL" value="0.0"/>
    <define name="PITCH_PGAIN" value="15000."/>
    <define name="MAX_ROLL" value="0.55"/>
    <define name="MAX_PITCH" value="0.35"/>
    <define name="MIN_PITCH" value="-0.35"/>
    <define name="AILERON_OF_GAZ" value="0"/>
    <define name="CRUISE_THROTTLE" value="0.45"/>
  </section>

  <section name="ALT" prefix="CLIMB_">
    <define name="PITCH_PGAIN" value="0.1"/>
    <define name="PITCH_IGAIN" value="0.025"/>
    <define name="PGAIN" value="-0.03"/>
    <define name="IGAIN" value="0.1"/>
    <define name="MAX" value="1."/>
    <define name="GAZ_OF_CLIMB" value="0.2" unit="%/(m/s)"/>
    <define name="PITCH_OF_VZ_PGAIN" value="0.00"/>
  </section>



  <section name="HORIZONTAL CONTROL" prefix="H_CTL_">
    <define name="COURSE_PGAIN" value="0.4"/>
    <define name="COURSE_PRE_BANK_CORRECTION" value="1."/>
    <define name="ROLL_MAX_SETPOINT" value="0.73" unit="rad"/>
    <define name="PITCH_MAX_SETPOINT" value="0.5" unit="rad"/>
    <define name="PITCH_MIN_SETPOINT" value="-0.5" unit="rad"/>

    <define name="ROLL_PGAIN" value="10000."/>
    <define name="AILERON_OF_THROTTLE" value="0.0"/>
    <define name="PITCH_PGAIN" value="6000."/>
    <define name="PITCH_DGAIN" value="0."/>
    <define name="ELEVATOR_OF_ROLL" value="0"/>

    <!--  roll rate loop -->
    <define name="ROLL_RATE_MODE_DEFAULT" value="1"/>
    <define name="ROLL_RATE_SETPOINT_PGAIN" value="-5.5"/>
    <define name="ROLL_RATE_MAX_SETPOINT" value="10"/>
    <define name="LO_THROTTLE_ROLL_RATE_PGAIN" value="1500."/>
    <define name="HI_THROTTLE_ROLL_RATE_PGAIN" value="900."/>
    <define name="ROLL_RATE_IGAIN" value="0."/>
    <define name="ROLL_RATE_DGAIN" value="0."/>
    <define name="ROLL_RATE_SUM_NB_SAMPLES" value="64"/>

  </section>



 <section name="VERTICAL CONTROL" prefix="V_CTL_">
    <!-- outer loop proportional gain -->
    <define name="ALTITUDE_PGAIN" value="0.025"/>
    <!-- outer loop saturation -->
    <define name="ALTITUDE_MAX_CLIMB" value="3."/>

    <!-- auto throttle inner loop -->
    <define name="AUTO_THROTTLE_NOMINAL_CRUISE_THROTTLE" value="0.60"/>
    <define name="AUTO_THROTTLE_MIN_CRUISE_THROTTLE" value=".45"/>
    <define name="AUTO_THROTTLE_MAX_CRUISE_THROTTLE" value="1"/>
    <define name="AUTO_THROTTLE_LOITER_TRIM" value="2000"/>
    <define name="AUTO_THROTTLE_DASH_TRIM" value="-1500"/>
    <define name="AUTO_THROTTLE_CLIMB_THROTTLE_INCREMENT" value="0.15" unit="%/(m/s)"/>
    <define name="AUTO_THROTTLE_PGAIN" value="0.008"/>
    <define name="AUTO_THROTTLE_IGAIN" value="0.25"/>
    <define name="AUTO_THROTTLE_PITCH_OF_VZ_PGAIN" value="0.14"/>

    <!-- auto pitch inner loop -->
    <define name="AUTO_PITCH_PGAIN" value="0.1"/>
    <define name="AUTO_PITCH_IGAIN" value="0.025"/>
    <define name="AUTO_PITCH_MAX_PITCH" value="0.5"/>
    <define name="AUTO_PITCH_MIN_PITCH" value="-0.5"/>

   <define name="THROTTLE_SLEW" value="0.05"/>

  </section>



  <section name="NAV">
    <define name="NAV_GLIDE_PITCH_TRIM" value="0"/>
    <define name="DEFAULT_CIRCLE_RADIUS" value="100"/>
  </section>


  <section name="BAT">
    <define name="MILLIAMP_PER_PERCENT" value="2.16"/>
    <define name="VOLTAGE_ADC_A" value="0.0175"/>
    <define name="VOLTAGE_ADC_B" value="0.088"/>
    <define name="VoltageOfAdc(adc)" value="(VOLTAGE_ADC_A * adc + VOLTAGE_ADC_B)"/>
    <define name="LOW_BATTERY" value="9.3" unit="V"/>
  </section>
  <section name="MISC">
    <define name="NOMINAL_AIRSPEED" value="12." unit="m/s"/>
    <define name="CARROT" value="5." unit="s"/>
  </section>
  <section name="SIMU">
    <define name="WEIGHT" value="1.3"/>
  </section>
  <section name="FAILSAFE" prefix="FAILSAFE_">
    <define name="DELAY_WITHOUT_GPS" value="2" unit="s"/>
    <define name="DEFAULT_THROTTLE" value="CRUISE_THROTTLE+0.05" unit="%"/>
    <define name="DEFAULT_ROLL" value="0.17" unit="rad"/>
    <define name="DEFAULT_PITCH" value="0.08" unit="rad"/>
    <define name="HOME_RADIUS" value="50" unit="m"/>
  </section>
  <section name="CAM" prefix="CAM_">
    <define name="THETA0" value="0" unit="deg"/>
    <define name="PHI0" value="30" unit="deg"/>
  </section>
 <makefile>

# Virtual AC

# Config for HITL simulation on a V1_2_1 bi-AVR board
include $(PAPARAZZI_SRC)/conf/firmwares/v1_2_1.makefile
include $(PAPARAZZI_SRC)/conf/firmwares/twin_mcu_avr.makefile

# Harware In The Loop
ap.CFLAGS += -DHITL

# Downlink on uart0
ap.CFLAGS += -DUSE_UART0 -DDOWNLINK -DDOWNLINK_TRANSPORT=PprzTransport -DDOWNLINK_AP_DEVICE=Uart0 -DUART0_BAUD=B38400
ap.EXTRA_SRCS += subsystems/datalink/pprz_transport.c subsystems/datalink/downlink.c

# Datalink (on uart0) used to emulate sensors (infrared and gps)
ap.EXTRA_SRCS += subsystems/navigation/traffic_info.c datalink.c
ap.CFLAGS += -DDATALINK -DPPRZ_INPUT -DPPRZ_UART=Uart0

# Config for SITL simulation
include $(PAPARAZZI_SRC)/conf/firmwares/sitl.makefile
sim.srcs += subsystems/navigation/nav_survey_rectangle.c

 </makefile>
</airframe>
