<!DOCTYPE airframe SYSTEM "airframe.dtd">

<airframe name="GlassOne #1 Tiny V1.1">

<!-- commands section -->
  <servos>
    <servo name="THROTTLE"      no="6" min="1000" neutral="1000" max="2000"/>
    <servo name="AILEVON_LEFT"  no="4" min="2100" neutral="1594" max="900"/>
    <servo name="AILEVON_RIGHT" no="5" min="900"  neutral="1387" max="2000"/>
    <!-- +8.1 +2.0 -5.5 -->
  </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.5"/>
    <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="THROTTLE"      value="@THROTTLE"/>
    <set servo="AILEVON_RIGHT"  value="-$elevator + $aileron"/>
    <set servo="AILEVON_LEFT"  value="-$elevator - $aileron"/>
    <!--set servo="HATCH"         value="@HATCH"/-->
  </command_laws>

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

  <section name="adc" prefix="ADC_CHANNEL_">
    <define name="IR1" value="ADC_0"/>
    <define name="IR2" value="ADC_1"/>
    <define name="IR_TOP" value="ADC_2"/>
    <define name="IR_NB_SAMPLES" value="16"/>
    <define name="GYRO_ROLL" value="ADC_3"/>
    <define name="GYRO_TEMP" value="ADC_4"/>
    <define name="SUPPLY_CURRENT" value="ADC_5"/>
    <define name="GYRO_NB_SAMPLES" value="16"/>

  </section>

  <section name="INFRARED" prefix="IR_">

    <linear name="RollOfIrs" arity="2" coeff1="-0.7" coeff2="0.7"/>
    <linear name="PitchOfIrs" arity="2" coeff1="0.7" coeff2="0.7"/>

    <define name="ROLL_NEUTRAL_DEFAULT" value="0" unit="deg"/>
    <define name="PITCH_NEUTRAL_DEFAULT" value="5.7" unit="deg"/>
    <define name="DEFAULT_CONTRAST" value="200"/>
    <define name="RAD_OF_IR_CONTRAST" value="0.75"/>
    <linear name="TopOfIr" arity="1" coeff1="1"/>
    <define name="RAD_OF_IR_MAX_VALUE" value="0.0045"/>
    <define name="RAD_OF_IR_MIN_VALUE" value="0.00075"/>
    <define name="ADC_IR1_NEUTRAL" value="508"/>
    <define name="ADC_IR2_NEUTRAL" value="518"/>
    <define name="ADC_TOP_NEUTRAL" value="512"/>
    <!--define name="ADC_IR1_NEUTRAL" value="524"/>
    <define name="ADC_IR2_NEUTRAL" value="511"/>
    <define name="ADC_TOP_NEUTRAL" value="550"/-->

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

    <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="GYRO" prefix="GYRO_">
    <define name="ADC_ROLL_NEUTRAL" value="518"/>
    <define name="ADC_TEMP_NEUTRAL" value="476"/>
    <define name="ADC_TEMP_SLOPE" value="0"/>
    <define name="ROLL_SCALE" value="1.0"/>
    <define name="ROLL_DIRECTION" value="1."/>
  </section>

  <section name="BAT">
    <define name="ADC_CHANNEL_VSUPPLY"  value="AdcBank1(6)"/>
    <define name="VoltageOfAdc(adc)" value="(0.01787109375*adc)"/>
    <define name="LOW_BATTERY" value="9.0" unit="V"/>
  </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="TRIGGER_DELAY" value="1.5"/>
    <define name="NAV_GLIDE_PITCH_TRIM" value="(V_CTL_AUTO_THROTTLE_PITCH_OF_VZ_PGAIN*V_CTL_ALTITUDE_MAX_CLIMB)"/>

    <define name="DEFAULT_CIRCLE_RADIUS" value="75."/>
    <define name="LIGHT_PIN_1" value="3"/>
  </section>

  <section name="VERTICAL CONTROL" prefix="V_CTL_">
    <!--define name="POWER_CTL_BAT_NOMINAL" value="8.4" unit="volt"/-->
    <!-- outer loop proportional gain -->
    <define name="ALTITUDE_PGAIN" value="0.07"/>
    <!-- outer loop saturation -->
    <define name="ALTITUDE_MAX_CLIMB" value="4."/>

    <!-- auto throttle inner loop -->
    <define name="AUTO_THROTTLE_NOMINAL_CRUISE_THROTTLE" value="0.65"/>
    <define name="AUTO_THROTTLE_MIN_CRUISE_THROTTLE" value="0.30"/>
    <define name="AUTO_THROTTLE_MAX_CRUISE_THROTTLE" value="0.80"/>
    <define name="AUTO_THROTTLE_LOITER_TRIM" value="1000"/>
    <define name="AUTO_THROTTLE_DASH_TRIM" value="-1000"/>
    <define name="AUTO_THROTTLE_CLIMB_THROTTLE_INCREMENT" value="0.2" unit="%/(m/s)"/>
    <define name="AUTO_THROTTLE_PGAIN" value="0.024"/>
    <define name="AUTO_THROTTLE_IGAIN" value="0.06"/>
    <define name="AUTO_THROTTLE_PITCH_OF_VZ_PGAIN" value="0.05"/>

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

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

  </section>

  <section name="HORIZONTAL CONTROL" prefix="H_CTL_">
    <define name="COURSE_PGAIN" value="-1.2"/>
    <define name="ROLL_MAX_SETPOINT" value="0.5" 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="7000."/>
    <define name="AILERON_OF_THROTTLE" value="0.0"/>
    <define name="PITCH_PGAIN" value="8000."/>
    <define name="PITCH_DGAIN" value="0."/>
    <define name="ELEVATOR_OF_ROLL" value="4000"/>

    <!--  roll rate loop -->
    <define name="ROLL_RATE_MODE_DEFAULT" value="0"/>
    <define name="ROLL_RATE_SETPOINT_PGAIN" value="-5." unit="rad/s/rad"/>
    <define name="ROLL_RATE_MAX_SETPOINT" value="10"/>
    <define name="HI_THROTTLE_ROLL_RATE_PGAIN" value="1500."/>
    <define name="LO_THROTTLE_ROLL_RATE_PGAIN" value="1500."/>
    <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="NAV">
  </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="20"/><!-- Altitude Error to Blend Aggressive to Regular Climb Modes  CANNOT BE ZERO!!-->
    <define name="CLIMB_THROTTLE" value="0.9"/><!-- Throttle for Aggressive Climb -->
    <define name="CLIMB_PITCH" value="0.35"/><!-- Pitch for Aggressive Climb -->
    <define name="DESCENT_THROTTLE" value="0.1"/><!-- Throttle for Aggressive Decent -->
    <define name="DESCENT_PITCH" value="-0.35"/><!-- Pitch for Aggressive Decent -->
    <define name="CLIMB_NAV_RATIO" value="0.5"/><!-- Percent Navigation for Altitude Error Equal to Start Altitude -->
    <define name="DESCENT_NAV_RATIO" value="1.0"/>
    </section>


  <section name="GYRO_GAINS">
    <define name="GYRO_MAX_RATE" value="200."/>
    <define name="ROLLRATESUM_NB_SAMPLES" value="64"/>
    <define name="ALT_ROLL__PGAIN" value="1.0"/>
    <define name="ROLL_RATE_PGAIN" value="1000.0"/>
    <define name="ROLL_RATE_IGAIN" value="0.0"/>
    <define name="ROLL_RATE_DGAIN" value="0.0"/>
  </section>

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

  <section name="DATALINK" prefix="DATALINK_">
    <define name="DEVICE_TYPE" value="AEROCOMM"/>
    <define name="DEVICE_ADDRESS" value="0x005067397193"/>
  </section>

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

FLASH_MODE=IAP

ap.CFLAGS +=  -DFBW -DAP -DBOARD_CONFIG=\"tiny_1_1.h\" -DUSE_LED -DSYS_TIME_LED=1
ap.srcs = sys_time.c $(SRC_ARCH)/sys_time_hw.c $(SRC_ARCH)/armVIC.c main_fbw.c main_ap.c main.c

ap.srcs += commands.c

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

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

#ap.CFLAGS += -DDOWNLINK -DUSE_UART0 -DDOWNLINK_TRANSPORT=XBeeTransport -DDOWNLINK_FBW_DEVICE=Uart0 -DDOWNLINK_AP_DEVICE=Uart0 -DXBEE_UART=Uart0 -DDATALINK=XBEE -DUART0_BAUD=B9600
#ap.srcs += subsystems/datalink/downlink.c $(SRC_ARCH)/mcu_periph/uart_arch.c datalink.c subsystems/datalink/xbee.c

ap.CFLAGS += -DDOWNLINK -DUSE_UART0 -DDOWNLINK_TRANSPORT=PprzTransport -DDOWNLINK_FBW_DEVICE=Uart0 -DDOWNLINK_AP_DEVICE=Uart0 -DPPRZ_UART=Uart0 -DDATALINK=PPRZ -DUART0_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 -DUSE_ADC_3 -DUSE_ADC_4 -DUSE_ADC_5
ap.srcs += $(SRC_ARCH)/adc_hw.c

ap.CFLAGS += -DUSE_GPS -DUBX -DUSE_UART1 -DGPS_LINK=Uart1 -DUART1_BAUD=B38400 -DGPS_CONFIGURE -DZ_CONTRAST_DEFAULT=1
ap.CFLAGS += -DGPS_BAUD=38400

ap.srcs += gps_ubx.c gps.c

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

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

#ap.CFLAGS += -DUSE_GYRO -DADXRS150
#ap.srcs += gyro.c

#ap.CFLAGS += -DCAM -DMOBILE_CAM -DPOINT_CAM -DPOINT_CAM_PITCH
#ap.srcs += point.c cam.c

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

# Video switch
ap.CFLAGS += -DUSE_GPIO
ap.srcs += $(SRC_ARCH)/gpio.c

# Hardware In The Loop

#ap.CFLAGS += -DHITL


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

sim.srcs += subsystems/navigation/traffic_info.c subsystems/navigation/nav_line.c


  </makefile>
</airframe>
