<!DOCTYPE airframe SYSTEM "airframe.dtd">

<!-- Twinstar II Multiplex (http://www.multiplex-rc.de/)
     Classix board (http://paparazzi.enac.fr/wiki/index.php/Classix)
     Aligned infrared sensor
     Digital still camera
-->

<airframe name="Twinstar 6">

  <servos>
<!-- Splitted power supplies for servos 0,1,2,3 and 4,5,6,7 from motor controller's +5V -->

<!-- Servo aileron gauche = rouge -->
    <servo name="AILERON_LEFT"  no="0" min="1000" neutral="1530" max="2000"/>
    <servo name="MOTOR_LEFT"   no="1" min="1000" neutral="1000" max="2000"/>
    <servo name="ELEVATOR" no="2" min="1000" neutral="1500" max="2100"/>

<!-- Servo aileron droit = bleu -->
    <servo name="AILERON_RIGHT" no="4" min="1000" neutral="1520" max="2000"/>
    <servo name="MOTOR_RIGHT"   no="5" min="1000" neutral="1000" max="2000"/>
    <servo name="RUDDER" no="6" min="2100" neutral="1470" max="900"/>
  </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="HATCH"     failsafe_value="0"/>
  </commands>

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

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

  <command_laws>
    <set servo="MOTOR_RIGHT" value="@THROTTLE"/>
    <set servo="MOTOR_LEFT" value="@THROTTLE"/>
    <set servo="RUDDER" value="@YAW + @ROLL*COMBI_SWITCH"/>

    <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="ELEVATOR" value="@PITCH"/>
  </command_laws>

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

  <section name="adc" prefix="ADC_CHANNEL_">
    <define name="IR1" value="AdcBank1(7)"/>
    <define name="IR2" value="AdcBank1(6)"/>
    <define name="IR_TOP" value="AdcBank1(5)"/>
    <define name="IR_NB_SAMPLES" value="16"/>
    <define name="VSUPPLY" value="AdcBank0(6)"/>
  </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="HORIZ_SENSOR_ALIGNED" value="1"/>
    <define name="IR1_SIGN" value="-1"/>
    <define name="TOP_SIGN" value="-1"/>

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

    <define name="ROLL_NEUTRAL_DEFAULT" value="-5.5" unit="deg"/>
    <define name="PITCH_NEUTRAL_DEFAULT" value="9.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="HORIZONTAL CONTROL" prefix="H_CTL_">
    <define name="COURSE_PGAIN" value="0.9"/>
    <define name="COURSE_PRE_BANK_CORRECTION" value="1."/>
    <define name="ROLL_MAX_SETPOINT" value="0.6" 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="6000."/>
    <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"/>
  </section>

  <section name="Digital camera telecommand">
    <!-- IOs are seen as LEDs -->

    <define name="LED_4_BANK" value="0"/>
    <define name="LED_4_PIN" value="16"/>

    <!-- ADC 5 -->
    <define name="LED_5_BANK" value="1"/>
    <define name="LED_5_PIN" value="20"/>

    <define name="DC_SHUTTER_LED" value="4"/>
    <define name="DC_ZOOM_LED" value="5"/>
  </section>

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

    <!-- auto throttle inner loop -->
    <define name="AUTO_THROTTLE_NOMINAL_CRUISE_THROTTLE" value="0.5"/>
    <define name="AUTO_THROTTLE_MIN_CRUISE_THROTTLE" value=".35"/>
    <define name="AUTO_THROTTLE_MAX_CRUISE_THROTTLE" value="0.8"/>
    <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"/>

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

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

  <section name="BAT">
    <define name="MILLIAMP_PER_PERCENT" value="0.86"/>
    <define name="VOLTAGE_ADC_A" value="0.0156"/>
    <define name="VOLTAGE_ADC_B" value="-0.029"/>
    <define name="VoltageOfAdc(adc)" value="(VOLTAGE_ADC_A * adc + VOLTAGE_ADC_B)"/>
    <define name="CATASTROPHIC_BAT_LEVEL" 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"/>
    <define name="KILL_MODE_DISTANCE" value="(MAX_DIST_FROM_HOME*1.5)" unit="s"/>
  </section>

  <section name="FAILSAFE" prefix="FAILSAFE_">
    <define name="DELAY_WITHOUT_GPS" value="2" unit="s"/>
    <define name="DEFAULT_THROTTLE" value="V_CTL_AUTO_THROTTLE_NOMINAL_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="75" unit="m"/>
  </section>


   <section name="AGGRESSIVE" prefix="AGR_">
    <define name="CLIMB_THROTTLE" value="0.9"/><!-- Gaz for Aggressive Climb -->
    <define name="DESCENT_THROTTLE" value="0.1"/><!-- Gaz for Aggressive Decent -->
    <define name="CLIMB_PITCH" value="0.25"/><!-- Pitch for Aggressive Climb -->
    <define name="DESCENT_PITCH" value="-0.25"/><!-- Pitch for Aggressive Decent -->
    <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_NAV_RATIO" value="0.8"/><!-- Percent Navigation for Altitude Error Equal to Start Altitude -->
    <define name="DESCENT_NAV_RATIO" value="1.0"/>
    </section>
  <makefile>
include $(PAPARAZZI_SRC)/conf/firmwares/classix.makefile

# FLASH_MODE=ISP
FLASH_MODE=IAP

fbw.CFLAGS += -DFBW -DBOARD_CONFIG=\"classix.h\" -DUSE_LED -DTIME_LED=1
fbw.srcs = sys_time.c $(SRC_ARCH)/sys_time_hw.c $(SRC_ARCH)/armVIC.c main_fbw.c main.c

fbw.srcs += commands.c

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

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

fbw.CFLAGS += -DDOWNLINK -DUSE_UART0 -DDOWNLINK_TRANSPORT=PprzTransport -DDOWNLINK_FBW_DEVICE=Uart0 -DUART0_BAUD=B9600
fbw.srcs += subsystems/datalink/downlink.c subsystems/datalink/pprz_transport.c $(SRC_ARCH)/mcu_periph/uart_arch.c

fbw.CFLAGS += -DINTER_MCU -DMCU_SPI_LINK -DUSE_SPI -DSPI_SLAVE
fbw.srcs += inter_mcu.c link_mcu_spi.c spi.c $(SRC_ARCH)/spi_hw.c

fbw.CFLAGS += -DADC -DUSE_AD0
fbw.srcs += $(SRC_ARCH)/adc_hw.c


ap.CFLAGS += -DAP -DRADIO_CONTROL_AUTO1 -DBOARD_CONFIG=\"classix.h\" -DUSE_LED -DTIME_LED=2
ap.srcs = sys_time.c $(SRC_ARCH)/sys_time_hw.c $(SRC_ARCH)/armVIC.c main_ap.c main.c

ap.CFLAGS += -DDOWNLINK -DUSE_UART0 -DDOWNLINK_TRANSPORT=PprzTransport -DDOWNLINK_AP_DEVICE=Uart0  -DPPRZ_UART=Uart0 -DDATALINK=PPRZ -DUART0_BAUD=B57600
ap.srcs += subsystems/datalink/downlink.c $(SRC_ARCH)/mcu_periph/uart_arch.c datalink.c subsystems/navigation/traffic_info.c subsystems/datalink/pprz_transport.c

ap.CFLAGS += -DINTER_MCU -DMCU_SPI_LINK -DUSE_SPI -DSPI_MASTER
ap.srcs += inter_mcu.c link_mcu_spi.c spi.c $(SRC_ARCH)/spi_hw.c

ap.CFLAGS += -DUSE_GPS -DUBX -DUSE_UART1 -DGPS_LINK=Uart1 -DUART1_BAUD=B38400
ap.srcs += gps_ubx.c gps.c latlong.c

# ADCs for infrared
ap.CFLAGS += -DADC -DUSE_AD1 -DUSE_AD1_5 -DUSE_AD1_6 -DUSE_AD1_7
ap.srcs += $(SRC_ARCH)/adc_hw.c

ap.CFLAGS += -DUSE_INFRARED
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.CFLAGS += -DUSE_GPIO
ap.srcs += $(SRC_ARCH)/gpio.c

# Digital camera
# Shutter: SPI0_SS1
# Zoom: SPI0_IT
ap.CFLAGS += -DDIGITAL_CAM
ap.srcs += dc.c


test.CFLAGS += -DFBW -DBOARD_CONFIG=\"classix.h\" -DUSE_LED -DTIME_LED=1 -DACTUATORS=\"servos_4017_hw.h\" -DSERVOS_4017 -DSERVOS_4017_CLOCK_FALLING  -DUSE_UART0 -DDATALINK -DPPRZ_INPUT -DPPRZ_UART=Uart0 -DUART0_BAUD=B9600
test.srcs += sys_time.c $(SRC_ARCH)/sys_time_hw.c $(SRC_ARCH)/armVIC.c subsystems/datalink/pprz_transport.c setup_actuators.c $(SRC_ARCH)/mcu_periph/uart_arch.c $(SRC_ARCH)/servos_4017_hw.c main.c

# Config for SITL simulation
include $(PAPARAZZI_SRC)/conf/firmwares/sitl.makefile
sim.CFLAGS += -DBOARD_CONFIG=\"classix.h\" -DAGR_CLIMB -DLOITER_TRIM
#sim.srcs += subsystems/navigation/traffic_info.c
sim.srcs += subsystems/navigation/nav_line.c subsystems/navigation/nav_survey_rectangle.c dc.c
# chemotaxis.c anemotaxis.c subsystems/navigation/discsurvey.c subsystems/navigation/bomb.c subsystems/navigation/nav_survey_rectangle.c


  </makefile>
</airframe>
