<!DOCTYPE airframe SYSTEM "airframe.dtd">

<!-- Funjet Multiplex (http://www.multiplex-rc.de/), Jeti ECO 25
     Tiny 2.11 board (http://paparazzi.enac.fr/wiki/index.php/Tiny_v2)
     Tilted infrared sensor (http://paparazzi.enac.fr/wiki/index.php/Image:Tiny_v2_1_Funjet.jpg)
     XBee modem
     Payload: Sensirion humidity/temp, VTI pressure/temp, ETS airspeed
     LEA 5H
-->

<airframe name="Funjet FMI 2">

<!-- commands section -->
  <servos>
    <servo name="MOTOR"         no="0" min="1000" neutral="1000" max="2000"/>
    <servo name="AILEVON_LEFT"  no="2" min="1900" neutral="1500" max="1100"/>
    <servo name="AILEVON_RIGHT" no="6" 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"/>
  </commands>

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

  <section name="MIXER">
    <define name="AILEVON_AILERON_RATE" value="0.45"/>
    <define name="AILEVON_ELEVATOR_RATE" value="0.8"/>
  </section>

  <command_laws>
    <let var="aileron"  value="@ROLL  * AILEVON_AILERON_RATE"/>
    <let var="elevator" value="@PITCH * AILEVON_ELEVATOR_RATE"/>
    <set servo="MOTOR"           value="@THROTTLE"/>
    <set servo="AILEVON_LEFT"  value="$elevator + $aileron"/>
    <set servo="AILEVON_RIGHT" value="$elevator - $aileron"/>
  </command_laws>

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

  <section name="adc" prefix="ADC_CHANNEL_">
    <define name="IR1" value="ADC_1"/>
    <define name="IR2" value="ADC_2"/>
    <define name="IR_TOP" value="ADC_0"/>
    <define name="IR_NB_SAMPLES" value="16"/>
  </section>

  <section name="INFRARED" prefix="IR_">
    <define name="ADC_IR1_NEUTRAL" value="512"/>
    <define name="ADC_IR2_NEUTRAL" value="512"/>
    <define name="ADC_TOP_NEUTRAL" value="512"/>

    <define name="CORRECTION_UP" value="1."/>
    <define name="CORRECTION_DOWN" value="1."/>
    <define name="CORRECTION_LEFT" value="1."/>
    <define name="CORRECTION_RIGHT" value="1."/>

    <define name="LATERAL_CORRECTION" value="1"/>
    <define name="LONGITUDINAL_CORRECTION" value="1"/>
    <define name="VERTICAL_CORRECTION" value="1.5"/>

    <define name="HORIZ_SENSOR_TILTED" value="1"/>
    <define name="IR1_SIGN" value="-1"/>
    <define name="IR2_SIGN" value="-1"/>
    <define name="TOP_SIGN" value="1"/>

    <define name="ROLL_NEUTRAL_DEFAULT" value="0" unit="deg"/>
    <define name="PITCH_NEUTRAL_DEFAULT" value="0" unit="deg"/>
  </section>

  <section name="BAT">
    <define name="MILLIAMP_AT_FULL_THROTTLE" value="10000."/>
    <define name="CATASTROPHIC_BAT_LEVEL" value="9.3" unit="V"/>
                                        <!-- 0.0247311828 -->
                                        <!-- 0.02432905 -->
    <define name="VoltageOfAdc(adc)" value="(0.02454*adc)"/>
  </section>

  <section name="MISC">
    <define name="NOMINAL_AIRSPEED" value="15." unit="m/s"/>
    <define name="CARROT" value="5." unit="s"/>
    <define name="KILL_MODE_DISTANCE" value="(1.5*MAX_DIST_FROM_HOME)"/>
    <define name="CONTROL_FREQUENCY" value="60" unit="Hz"/>
<!--    <define name="XBEE_INIT" value="\"ATPL2\rATRN1\rATTT80\r\""/> -->
<!--    <define name="NO_XBEE_API_INIT" value="TRUE"/> -->
    <define name="ALT_KALMAN_ENABLED" value="FALSE"/>

    <define name="TRIGGER_DELAY" value="1."/>
    <define name="DEFAULT_CIRCLE_RADIUS" value="120."/>
    <define name="MIN_CIRCLE_RADIUS" value="50."/>
  </section>

  <section name="VERTICAL CONTROL" prefix="V_CTL_">

    <define name="POWER_CTL_BAT_NOMINAL" value="11.1" unit="volt"/>
    <!-- outer loop proportional gain -->
    <define name="ALTITUDE_PGAIN" value="0.06"/> <!-- -0.024 -->
    <!-- outer loop saturation -->
    <define name="ALTITUDE_MAX_CLIMB" value="2."/>

    <!-- auto throttle inner loop -->
    <define name="AUTO_THROTTLE_NOMINAL_CRUISE_THROTTLE" value="0.45"/>
    <define name="AUTO_THROTTLE_MIN_CRUISE_THROTTLE" value="0.35"/>
    <define name="AUTO_THROTTLE_MAX_CRUISE_THROTTLE" value="0.85"/>
    <define name="AUTO_THROTTLE_LOITER_TRIM" value="1000"/>
    <define name="AUTO_THROTTLE_DASH_TRIM" value="-1200"/>
    <define name="AUTO_THROTTLE_CLIMB_THROTTLE_INCREMENT" value="0.2" unit="%/(m/s)"/>
    <define name="AUTO_THROTTLE_PGAIN" value="0.023"/> <!-- -0.012 -->
    <define name="AUTO_THROTTLE_IGAIN" value="0.01"/>
    <define name="AUTO_THROTTLE_PITCH_OF_VZ_PGAIN" value="0.05"/>

    <!-- auto pitch inner loop -->
    <define name="AUTO_PITCH_PGAIN" value="0.06"/> <!-- -0.03 -->
    <define name="AUTO_PITCH_IGAIN" value="0.0"/>
    <define name="AUTO_PITCH_MAX_PITCH" value="0.35"/>
    <define name="AUTO_PITCH_MIN_PITCH" value="-0.35"/>

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

  </section>

  <section name="HORIZONTAL CONTROL" prefix="H_CTL_">
    <define name="COURSE_PGAIN" value="0.9"/>
    <define name="ROLL_MAX_SETPOINT" value="0.70" unit="rad"/> <!-- 0.5 -->
    <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="6600."/>
    <define name="AILERON_OF_THROTTLE" value="0.0"/>
    <define name="PITCH_PGAIN" value="5500."/>
    <define name="PITCH_DGAIN" value="0.4"/>

    <define name="ELEVATOR_OF_ROLL" value="2400"/>

    <!--define name="ROLL_ATTITUDE_GAIN" value="7500"/>
    <define name="ROLL_RATE_GAIN" value="1500"/-->

  </section>

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

  <section name="AGGRESSIVE" prefix="AGR_">
    <define name="BLEND_START" value="50"/><!-- Altitude Error to Initiate Aggressive Climb CANNOT BE ZERO!!-->
    <define name="BLEND_END" value="15"/><!-- Altitude Error to Blend Aggressive to Regular Climb Modes  CANNOT BE ZERO!!-->
    <define name="CLIMB_THROTTLE" value="0.9"/><!-- Gaz for Aggressive Climb -->
    <define name="CLIMB_PITCH" value="0.35"/><!-- Pitch for Aggressive Climb -->
    <define name="DESCENT_THROTTLE" value="0.05"/><!-- Gaz for Aggressive Decent -->
    <define name="DESCENT_PITCH" value="-0.35"/><!-- Pitch for Aggressive Decent -->
    <define name="CLIMB_NAV_RATIO" value="0.8"/><!-- Percent Navigation for Altitude Error Equal to Start Altitude -->
    <define name="DESCENT_NAV_RATIO" value="1.0"/>
    </section>

  <section name="FAILSAFE" prefix="FAILSAFE_">
	<define name="DELAY_WITHOUT_GPS" value="2" unit="s"/>
    	<define name="DEFAULT_THROTTLE" value="0.3" unit="%"/>
    	<define name="DEFAULT_ROLL" value="0.3" unit="rad"/>
    	<define name="DEFAULT_PITCH" value="0.5" unit="rad"/>
	<define name="HOME_RADIUS" value="100" unit="m"/>
</section>

 <section name="SIMU">
    <define name="YAW_RESPONSE_FACTOR" value="0.5"/>
 </section>

  <section name="MICROMAG">

    <define name="MM_SS_PIN" value="20"/>
    <define name="MM_SS_IODIR" value="IO0DIR"/>
    <define name="MM_SS_IOSET" value="IO0SET"/>
    <define name="MM_SS_IOCLR" value="IO0CLR"/>

    <define name="MM_RESET_PIN" value="29"/>
    <define name="MM_RESET_IODIR" value="IO0DIR"/>
    <define name="MM_RESET_IOSET" value="IO0SET"/>
    <define name="MM_RESET_IOCLR" value="IO0CLR"/>

    <define name="MM_DRDY_PINSEL" value="PINSEL1"/>
    <define name="MM_DRDY_PINSEL_BIT" value="0"/>
    <define name="MM_DRDY_PINSEL_VAL" value="1"/>
    <define name="MM_DRDY_EINT" value="0"/>
    <define name="MM_DRDY_VIC_IT" value="VIC_EINT0"/>

  </section>

 <makefile>
CONFIG = \"tiny_2_1.h\"

include $(PAPARAZZI_SRC)/conf/firmwares/tiny.makefile

FLASH_MODE=IAP

ap.CFLAGS +=  -DFBW -DAP -DBOARD_CONFIG=$(CONFIG) -DUSE_LED -DSYS_TIME_LED=1
ap.srcs = mcu_periph/sys_time.c $(SRC_ARCH)/mcu_periph/sys_time_arch.c $(SRC_ARCH)/armVIC.c main_fbw.c main_ap.c main.c

ap.srcs += commands.c

ap.CFLAGS += -DACTUATORS=\"servos_4017_hw.h\" -DSERVOS_4017
ap.srcs += $(SRC_ARCH)/servos_4017_hw.c actuators.c

ap.CFLAGS += -DRADIO_CONTROL
ap.srcs += radio_control.c $(SRC_ARCH)/ppm_hw.c

ap.CFLAGS += -DDOWNLINK -DUSE_UART1 -DDOWNLINK_TRANSPORT=XBeeTransport -DXBEE_UART=Uart1 -DDATALINK=XBEE -DUART1_BAUD=B57600
ap.srcs += subsystems/datalink/downlink.c $(SRC_ARCH)/mcu_periph/uart_arch.c datalink.c subsystems/datalink/xbee.c

#TRANSPARENT
#ap.CFLAGS += -DDOWNLINK -DUSE_UART1 -DDOWNLINK_TRANSPORT=PprzTransport -DDOWNLINK_FBW_DEVICE=Uart1 -DDOWNLINK_AP_DEVICE=Uart1 -DPPRZ_UART=Uart1 -DDATALINK=PPRZ -DUART1_BAUD=B9600
#ap.srcs += subsystems/datalink/downlink.c $(SRC_ARCH)/mcu_periph/uart_arch.c datalink.c subsystems/datalink/pprz_transport.c


ap.CFLAGS += -DINTER_MCU
ap.srcs += inter_mcu.c

ap.CFLAGS += -DADC -DUSE_ADC_0 -DUSE_ADC_1 -DUSE_ADC_2
ap.srcs += $(SRC_ARCH)/adc_hw.c

ap.CFLAGS += -DUSE_GPS -DUBX -DUSE_UART0 -DGPS_LINK=Uart0 -DUART0_BAUD=B38400 -DGPS_USE_LATLONG
# -DGPS_LED=2
ap.srcs += gps_ubx.c gps.c latlong.c

ap.CFLAGS += -DUSE_INFRARED -DALT_KALMAN -DWIND_INFO -DWIND_INFO_RET
ap.srcs += infrared.c estimator.c

ap.CFLAGS += -DNAV -DAGR_CLIMB -DLOITER_TRIM
ap.srcs += subsystems/nav.c fw_h_ctl.c fw_v_ctl.c

ap.srcs += subsystems/navigation/nav_line.c
ap.srcs += subsystems/navigation/nav_survey_rectangle.c

ap.srcs += humid_sht.c
ap.CFLAGS += -DUSE_HUMID_SHT -DDAT_PIN=30 -DSCK_PIN=4

ap.CFLAGS += -DUSE_I2C0
ap.srcs += i2c.c $(SRC_ARCH)/i2c_hw.c

ap.CFLAGS += -DUSE_AIRSPEED_ETS
ap.srcs += airspeed_ets_o.c

ap.CFLAGS += -DUSE_IR_MLX
ap.srcs += ir_mlx.c

ap.CFLAGS += -DUSE_BARO_SCP
ap.srcs += baro_scp_i2c.c

ap.CFLAGS += -DMICROMAG_DRDY_VIC_SLOT=12 -DSSP_VIC_SLOT=11 -DUSE_MICROMAG_FW
ap.srcs += micromag_fw.c $(SRC_ARCH)/micromag_fw_hw.c

# Config for SITL simulation
include $(PAPARAZZI_SRC)/conf/firmwares/sitl.makefile
sim.CFLAGS += -DBOARD_CONFIG=\"tiny.h\" -DAGR_CLIMB -DLOITER_TRIM -DALT_KALMAN
sim.srcs += subsystems/navigation/nav_line.c subsystems/navigation/nav_survey_rectangle.c


  </makefile>
</airframe>
