<!DOCTYPE airframe SYSTEM "airframe.dtd">

<!-- Linux Flight Computer - Generic Configuration
     Pat Hickey
     pat@moreproductive.org
     Dec 2009
     -->

<airframe name="Generic Linux Flight Computer 0.0.1">
  <servos>
    <!-- For Pololu Mini-SSC Protocol Output. Channels start at 0,
         and physical maximums/minimums are set using Maestro setup software. 
         See http://www.pololu.com/docs/0J40/5.e for more info -->
    <servo name="AILERON"          no="0" min="254" neutral="128" max="0"/>
    <servo name="ELEVATOR"         no="1" min="0" neutral="128" max="254"/>
    <servo name="THROTTLE"         no="2" min="0" neutral="128" max="254"/>
    <servo name="RUDDER"           no="3" min="0" neutral="128" max="254"/>
  </servos>

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

  <command_laws>
    <!-- We're using an aircraft with traditional control surfaces -->
    <set servo="THROTTLE" value="@THROTTLE"/>
    <set servo="AILERON"  value="@ROLL"/>
    <set servo="RUDDER"   value="@RUDDER"/>
    <set servo="ELEVATOR" value="@PITCH"/>
  </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_3"/>
    <define name="IR_NB_SAMPLES" value="16"/>
  </section>

  <section name="INFRARED" prefix="IR_">
    <define name="ADC_IR1_NEUTRAL" value="820"/> <!-- 448 -->
    <define name="ADC_IR2_NEUTRAL" value="290"/> <!-- 92 -->
    <define name="ADC_TOP_NEUTRAL" value="649"/> <!-- 241 -->



    <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="-3.6" unit="deg"/>
    <define name="PITCH_NEUTRAL_DEFAULT" value="5" unit="deg"/>

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

  <section name="BAT">
    <!-- I've removed battery tracking, as our flight computer
         doesn't currently have the hardware to do that -->
    <define name="MILLIAMP_AT_FULL_THROTTLE" value="2000"/>
    <define name="CATASTROPHIC_BAT_LEVEL" value="-1" unit="V"/>
  </section>
 
  <section name="MISC">
    <define name="MINIMUM_AIRSPEED" value="12." unit="m/s"/>
    <define name="NOMINAL_AIRSPEED" value="15." unit="m/s"/>
    <define name="MAXIMUM_AIRSPEED" value="25." 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_RATE" value="60" unit="Hz"/>
    <define name="ALT_KALMAN_ENABLED" value="TRUE"/>

    <define name="DEFAULT_CIRCLE_RADIUS" value="80."/>
    <define name="MIN_CIRCLE_RADIUS" value="50."/>
  </section>
 
  <section name="VERTICAL CONTROL" prefix="V_CTL_">

    <!-- outer loop proportional gain -->
    <define name="ALTITUDE_PGAIN" value="-0.04"/>
    <!-- outer loop saturation -->
    <define name="ALTITUDE_MAX_CLIMB" value="2."/>

    <!-- auto throttle inner loop -->
    <define name="AUTO_THROTTLE_NOMINAL_CRUISE_THROTTLE" value="0.35"/>
    <define name="AUTO_THROTTLE_MIN_CRUISE_THROTTLE" value="0.25"/>
    <define name="AUTO_THROTTLE_MAX_CRUISE_THROTTLE" value="0.80"/>
    <define name="AUTO_THROTTLE_LOITER_TRIM" value="1500"/>
    <define name="AUTO_THROTTLE_DASH_TRIM" value="-1000"/>
    <define name="AUTO_THROTTLE_CLIMB_THROTTLE_INCREMENT" value="0.1" unit="%/(m/s)"/>
    <define name="AUTO_THROTTLE_PGAIN" value="-0.02"/>
    <define name="AUTO_THROTTLE_IGAIN" value="0.1"/>
    <define name="AUTO_THROTTLE_PITCH_OF_VZ_PGAIN" value="0.1"/>

    <!-- auto pitch inner loop -->
    <define name="AUTO_PITCH_PGAIN" value="-0.05"/>
    <define name="AUTO_PITCH_IGAIN" value="0.075"/>
    <define name="AUTO_PITCH_MAX_PITCH" value="0.35"/>
    <define name="AUTO_PITCH_MIN_PITCH" value="-0.35"/>

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

  </section>

  <section name="HORIZONTAL CONTROL" prefix="H_CTL_">
    <define name="COURSE_PGAIN" value="-0.8"/>

    <define name="ROLL_MAX_SETPOINT" value="0.7" unit="radians"/>
    <define name="PITCH_MAX_SETPOINT" value="0.5" unit="radians"/>
    <define name="PITCH_MIN_SETPOINT" value="-0.5" unit="radians"/>

    <define name="PITCH_PGAIN" value="-10000."/>
    <define name="PITCH_DGAIN" value="1.5"/>

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

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

  <section name="AGGRESSIVE" prefix="AGR_">
    <define name="BLEND_START" value="20"/><!-- Altitude Error to Initiate Aggressive Climb CANNOT BE ZERO!!-->
    <define name="BLEND_END" value="10"/><!-- Altitude Error to Blend Aggressive to Regular Climb Modes  CANNOT BE ZERO!!-->
    <define name="CLIMB_THROTTLE" value="0.6"/><!-- Gaz for Aggressive Climb -->
    <define name="CLIMB_PITCH" value="0.20"/><!-- Pitch for Aggressive Climb -->
    <define name="DESCENT_THROTTLE" value="0.1"/><!-- Gaz for Aggressive Decent -->
    <define name="DESCENT_PITCH" value="-0.15"/><!-- 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="1" 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>
 <makefile>

CONFIG = \"linuxfc.h\"

include $(PAPARAZZI_SRC)/conf/autopilot/linuxfc.makefile
ap.srcs += commands.c
ap.CFLAGS +=  -DFBW -DAP -DBOARD_CONFIG=$(CONFIG) -DLED -DTIME_LED=1
ap.srcs = sys_time.c $(SRC_ARCH)/sys_time_hw.c $(SRC_ARCH)/fbw.c $(SRC_ARCH)/ap.c $(SRC_ARCH)/main.c


ap.CFLAGS += -DACTUATORS=\"servos_hw.h\" -DSERVOS -DPOLOLU_PATH=\"/dev/serial/by-id/usb-Pololu_Corporation_Pololu_Micro_Maestro_6-Servo_Controller_00001644-if00\"
ap.srcs += $(SRC_ARCH)/servos_hw.c actuators.c

ap.CFLAGS += -DDOWNLINK -DUSE_UART1 -DDOWNLINK_TRANSPORT=PprzTransport -DDOWNLINK_FBW_DEVICE=Uart1 -DDOWNLINK_AP_DEVICE=Uart1 -DPPRZ_UART=Uart1 -DDATALINK=PPRZ -DUART1_FNAME=\"/dev/ftdi2\" -DUART1_BAUD=B9600
ap.srcs += downlink.c $(SRC_ARCH)/uart_hw.c datalink.c pprz_transport.c

ap.CFLAGS += -DADC -DUSE_ADC_0 -DUSE_ADC_1 -DUSE_ADC_2 -DUSE_ADC_3 -DADC_PATH=\"/dev/ftdi1\" -DADC_BAUD=B19200
ap.srcs += $(SRC_ARCH)/adc_hw.c

ap.CFLAGS += -DGPS -DGPS_USE_LATLONG
ap.srcs += gps_libgps.c gps.c latlong.c

ap.CFLAGS += -DINFRARED -DALT_KALMAN
ap.srcs += infrared.c estimator.c

ap.CFLAGS += -DNAV -DAGR_CLIMB -DLOITER_TRIM
ap.srcs += nav.c fw_h_ctl.c fw_v_ctl.c
ap.srcs += nav_line.c nav_survey_rectangle.c


  </makefile>
</airframe>
