2011-03-19 07:20:11 -03:00
/// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
2010-12-19 12:40:33 -04:00
2011-11-25 20:07:06 -04:00
#define THISFIRMWARE "ArduCopter V2.1.0 Alpha"
2010-12-19 12:40:33 -04:00
/*
2011-05-07 16:41:55 -03:00
ArduCopter Version 2.0 Beta
2010-12-19 12:40:33 -04:00
Authors: Jason Short
Based on code and ideas from the Arducopter team: Jose Julio, Randy Mackay, Jani Hirvinen
Thanks to: Chris Anderson, Mike Smith, Jordi Munoz, Doug Weibel, James Goppert, Benjamin Pelletier
2011-02-19 22:03:01 -04:00
This firmware is free software; you can redistribute it and/or
2010-12-19 12:40:33 -04:00
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
2011-06-24 14:09:59 -03:00
Special Thanks for Contributors:
Hein Hollander :Octo Support
Dani Saez :V Ocoto Support
Max Levine :Tri Support, Graphics
Jose Julio :Stabilization Control laws
Randy MacKay :Heli Support
Jani Hiriven :Testing feedback
Andrew Tridgell :Mavlink Support
James Goppert :Mavlink Support
Doug Weibel :Libraries
Mike Smith :Libraries, Coding support
HappyKillmore :Mavlink GCS
2011-08-30 17:14:29 -03:00
Michael Oborne :Mavlink GCS
2011-06-24 14:09:59 -03:00
Jack Dunkle :Alpha testing
Christof Schmid :Alpha testing
2011-09-18 21:12:59 -03:00
Oliver :Piezo support
2011-06-25 02:59:06 -03:00
Guntars :Arming safety suggestion
2011-06-24 14:09:59 -03:00
And much more so PLEASE PM me on DIYDRONES to add your contribution to the List
2010-12-19 12:40:33 -04:00
*/
2011-02-17 03:09:13 -04:00
////////////////////////////////////////////////////////////////////////////////
// Header includes
////////////////////////////////////////////////////////////////////////////////
2010-12-19 12:40:33 -04:00
// AVR runtime
#include <avr/io.h>
#include <avr/eeprom.h>
#include <avr/pgmspace.h>
#include <math.h>
// Libraries
#include <FastSerial.h>
#include <AP_Common.h>
2011-11-12 23:47:16 -04:00
#include <Arduino_Mega_ISR_Registry.h>
2011-02-24 01:56:59 -04:00
#include <APM_RC.h> // ArduPilot Mega RC Library
#include <AP_GPS.h> // ArduPilot GPS library
2010-12-19 12:40:33 -04:00
#include <Wire.h> // Arduino I2C lib
2011-09-18 09:57:55 -03:00
#include <SPI.h> // Arduino SPI lib
2011-02-24 01:56:59 -04:00
#include <DataFlash.h> // ArduPilot Mega Flash Memory Library
#include <AP_ADC.h> // ArduPilot Mega Analog to Digital Converter Library
2011-11-12 23:47:16 -04:00
#include <AP_AnalogSource.h>
2011-11-27 01:35:23 -04:00
#include <AP_Baro.h>
2011-02-24 01:56:59 -04:00
#include <AP_Compass.h> // ArduPilot Mega Magnetometer Library
#include <AP_Math.h> // ArduPilot Mega Vector/Matrix math Library
2011-11-12 23:47:16 -04:00
#include <AP_InertialSensor.h> // ArduPilot Mega Inertial Sensor (accel & gyro) Library
2011-02-24 01:56:59 -04:00
#include <AP_IMU.h> // ArduPilot Mega IMU Library
2011-11-12 23:47:16 -04:00
#include <AP_PeriodicProcess.h> // Parent header of Timer and TimerAperiodic
// (only included for makefile libpath to work)
#include <AP_TimerProcess.h> // TimerProcess is the scheduler for MPU6000 reads.
#include <AP_TimerAperiodicProcess.h> // TimerAperiodicProcess is the scheduler for ADC reads.
2011-02-24 01:56:59 -04:00
#include <AP_DCM.h> // ArduPilot Mega DCM Library
2011-09-04 21:15:36 -03:00
#include <APM_PI.h> // PI library
2011-02-19 22:03:01 -04:00
#include <RC_Channel.h> // RC Channel Library
2011-02-20 19:09:28 -04:00
#include <AP_RangeFinder.h> // Range finder library
2011-09-17 00:38:18 -03:00
#include <AP_OpticalFlow.h> // Optical Flow library
2011-07-30 17:42:54 -03:00
#include <ModeFilter.h>
2011-10-02 09:16:31 -03:00
#include <AP_Relay.h> // APM relay
2011-02-19 22:03:01 -04:00
#include <GCS_MAVLink.h> // MAVLink GCS definitions
2011-08-31 21:52:08 -03:00
#include <memcheck.h>
2011-04-13 13:33:06 -03:00
2010-12-19 12:40:33 -04:00
// Configuration
2011-06-24 01:37:54 -03:00
#include "defines.h"
2010-12-19 12:40:33 -04:00
#include "config.h"
// Local modules
2011-02-17 03:09:13 -04:00
#include "Parameters.h"
2011-02-17 05:36:33 -04:00
#include "GCS.h"
2010-12-19 12:40:33 -04:00
2011-02-17 03:09:13 -04:00
////////////////////////////////////////////////////////////////////////////////
2010-12-19 12:40:33 -04:00
// Serial ports
2011-02-17 03:09:13 -04:00
////////////////////////////////////////////////////////////////////////////////
2010-12-19 12:40:33 -04:00
//
// Note that FastSerial port buffers are allocated at ::begin time,
// so there is not much of a penalty to defining ports that we don't
// use.
2011-02-17 03:09:13 -04:00
//
2011-02-19 22:03:01 -04:00
FastSerialPort0(Serial); // FTDI/console
FastSerialPort1(Serial1); // GPS port
FastSerialPort3(Serial3); // Telemetry port
2010-12-19 12:40:33 -04:00
2011-11-12 23:47:16 -04:00
Arduino_Mega_ISR_Registry isr_registry;
2011-02-17 03:09:13 -04:00
////////////////////////////////////////////////////////////////////////////////
// Parameters
////////////////////////////////////////////////////////////////////////////////
//
// Global parameters are all contained within the 'g' class.
//
2011-07-17 07:31:46 -03:00
static Parameters g;
2011-02-17 03:09:13 -04:00
2011-05-09 14:40:32 -03:00
2011-03-17 22:50:46 -03:00
////////////////////////////////////////////////////////////////////////////////
// prototypes
2011-07-17 07:31:46 -03:00
static void update_events(void);
2011-03-17 22:50:46 -03:00
2011-11-12 23:47:16 -04:00
////////////////////////////////////////////////////////////////////////////////
// RC Hardware
////////////////////////////////////////////////////////////////////////////////
2011-11-25 19:11:25 -04:00
#if CONFIG_APM_HARDWARE == APM_HARDWARE_APM2
APM_RC_APM2 APM_RC;
2011-11-12 23:47:16 -04:00
#else
APM_RC_APM1 APM_RC;
#endif
////////////////////////////////////////////////////////////////////////////////
// Dataflash
////////////////////////////////////////////////////////////////////////////////
2011-11-25 19:11:25 -04:00
#if CONFIG_APM_HARDWARE == APM_HARDWARE_APM2
DataFlash_APM2 DataFlash;
2011-11-12 23:47:16 -04:00
#else
DataFlash_APM1 DataFlash;
#endif
2011-05-09 14:40:32 -03:00
2011-02-17 03:09:13 -04:00
////////////////////////////////////////////////////////////////////////////////
// Sensors
////////////////////////////////////////////////////////////////////////////////
//
// There are three basic options related to flight sensor selection.
//
// - Normal flight mode. Real sensors are used.
// - HIL Attitude mode. Most sensors are disabled, as the HIL
// protocol supplies attitude information directly.
// - HIL Sensors mode. Synthetic sensors are configured that
// supply data from the simulation.
//
2011-02-19 03:44:44 -04:00
// All GPS access should be through this pointer.
2011-07-17 07:31:46 -03:00
static GPS *g_gps;
2011-02-17 03:09:13 -04:00
2011-07-17 07:30:53 -03:00
// flight modes convenience array
2011-07-17 07:31:46 -03:00
static AP_Int8 *flight_modes = &g.flight_mode1;
2011-07-17 07:30:53 -03:00
2011-05-09 09:20:22 -03:00
#if HIL_MODE == HIL_MODE_DISABLED
2011-02-24 01:56:59 -04:00
2011-12-03 19:29:23 -04:00
// real sensors
#if CONFIG_ADC == ENABLED
2011-02-25 01:33:39 -04:00
AP_ADC_ADS7844 adc;
2011-12-03 19:29:23 -04:00
#endif
2011-11-12 23:47:16 -04:00
2011-11-16 21:49:56 -04:00
#ifdef DESKTOP_BUILD
2011-11-27 01:35:23 -04:00
AP_Baro_BMP085_HIL barometer;
2011-11-16 21:49:56 -04:00
AP_Compass_HIL compass;
#else
2011-11-27 01:35:23 -04:00
AP_Baro_BMP085 barometer;
2011-06-03 22:22:04 -03:00
AP_Compass_HMC5843 compass(Parameters::k_param_compass);
2011-11-16 21:49:56 -04:00
#endif
2011-08-13 02:10:19 -03:00
2011-12-03 19:29:23 -04:00
#ifdef OPTFLOW_ENABLED
AP_OpticalFlow_ADNS3080 optflow;
#endif
2011-02-25 01:33:39 -04:00
2011-12-03 19:29:23 -04:00
// real GPS selection
#if GPS_PROTOCOL == GPS_PROTOCOL_AUTO
AP_GPS_Auto g_gps_driver(&Serial1, &g_gps);
2011-02-25 01:33:39 -04:00
2011-12-03 19:29:23 -04:00
#elif GPS_PROTOCOL == GPS_PROTOCOL_NMEA
AP_GPS_NMEA g_gps_driver(&Serial1);
2011-02-25 01:33:39 -04:00
2011-12-03 19:29:23 -04:00
#elif GPS_PROTOCOL == GPS_PROTOCOL_SIRF
AP_GPS_SIRF g_gps_driver(&Serial1);
2011-02-25 01:33:39 -04:00
2011-12-03 19:29:23 -04:00
#elif GPS_PROTOCOL == GPS_PROTOCOL_UBLOX
AP_GPS_UBLOX g_gps_driver(&Serial1);
2011-02-25 01:33:39 -04:00
2011-12-03 19:29:23 -04:00
#elif GPS_PROTOCOL == GPS_PROTOCOL_MTK
AP_GPS_MTK g_gps_driver(&Serial1);
2011-02-25 01:33:39 -04:00
2011-12-03 19:29:23 -04:00
#elif GPS_PROTOCOL == GPS_PROTOCOL_MTK16
AP_GPS_MTK16 g_gps_driver(&Serial1);
2011-02-25 01:33:39 -04:00
2011-12-03 19:29:23 -04:00
#elif GPS_PROTOCOL == GPS_PROTOCOL_NONE
AP_GPS_None g_gps_driver(NULL);
2011-02-25 01:33:39 -04:00
2011-12-03 19:29:23 -04:00
#else
#error Unrecognised GPS_PROTOCOL setting.
#endif // GPS PROTOCOL
2011-02-19 03:21:42 -04:00
2011-11-13 02:15:45 -04:00
#if CONFIG_IMU_TYPE == CONFIG_IMU_MPU6000
AP_InertialSensor_MPU6000 ins( CONFIG_MPU6000_CHIP_SELECT_PIN );
#else
AP_InertialSensor_Oilpan ins(&adc);
#endif
AP_IMU_INS imu(&ins, Parameters::k_param_IMU_calibration);
AP_DCM dcm(&imu, g_gps);
AP_TimerProcess timer_scheduler;
2011-02-24 01:56:59 -04:00
#elif HIL_MODE == HIL_MODE_SENSORS
2011-02-25 01:33:39 -04:00
// sensor emulators
AP_ADC_HIL adc;
2011-11-27 01:35:23 -04:00
AP_Baro_BMP085_HIL barometer;
2011-02-25 01:33:39 -04:00
AP_Compass_HIL compass;
AP_GPS_HIL g_gps_driver(NULL);
2011-11-13 02:15:45 -04:00
AP_IMU_Shim imu;
AP_DCM dcm(&imu, g_gps);
2011-11-13 02:44:04 -04:00
AP_PeriodicProcessStub timer_scheduler;
AP_InertialSensor_Stub ins;
static int32_t gps_base_alt;
2011-02-24 01:56:59 -04:00
#elif HIL_MODE == HIL_MODE_ATTITUDE
2011-07-30 22:24:20 -03:00
AP_ADC_HIL adc;
2011-02-25 01:33:39 -04:00
AP_DCM_HIL dcm;
AP_GPS_HIL g_gps_driver(NULL);
AP_Compass_HIL compass; // never used
AP_IMU_Shim imu; // never used
2011-11-13 02:55:56 -04:00
AP_InertialSensor_Stub ins;
AP_PeriodicProcessStub timer_scheduler;
2011-07-29 22:39:12 -03:00
#ifdef OPTFLOW_ENABLED
2011-09-16 23:17:50 -03:00
AP_OpticalFlow_ADNS3080 optflow;
2011-07-29 22:39:12 -03:00
#endif
2011-07-18 09:44:02 -03:00
static int32_t gps_base_alt;
2011-02-24 01:56:59 -04:00
#else
2011-02-25 01:33:39 -04:00
#error Unrecognised HIL_MODE setting.
2011-02-24 01:56:59 -04:00
#endif // HIL MODE
2011-11-13 01:06:25 -04:00
2011-09-16 03:33:00 -03:00
2011-02-17 03:09:13 -04:00
////////////////////////////////////////////////////////////////////////////////
// GCS selection
////////////////////////////////////////////////////////////////////////////////
2011-10-11 06:12:37 -03:00
GCS_MAVLINK gcs0(Parameters::k_param_streamrates_port0);
GCS_MAVLINK gcs3(Parameters::k_param_streamrates_port3);
2011-03-26 03:35:52 -03:00
2011-06-16 14:03:26 -03:00
////////////////////////////////////////////////////////////////////////////////
// SONAR selection
////////////////////////////////////////////////////////////////////////////////
//
2011-07-30 17:42:54 -03:00
ModeFilter sonar_mode_filter;
2011-11-12 23:47:16 -04:00
#if CONFIG_SONAR == ENABLED
2011-12-03 19:29:23 -04:00
#if CONFIG_SONAR_SOURCE == SONAR_SOURCE_ADC
AP_AnalogSource_ADC sonar_analog_source( &adc, CONFIG_SONAR_SOURCE_ADC_CHANNEL, 0.25);
#elif CONFIG_SONAR_SOURCE == SONAR_SOURCE_ANALOG_PIN
AP_AnalogSource_Arduino sonar_analog_source(CONFIG_SONAR_SOURCE_ANALOG_PIN);
#endif
2011-12-11 03:40:59 -04:00
AP_RangeFinder_MaxsonarXL sonar(&sonar_analog_source, &sonar_mode_filter);
2011-11-12 23:47:16 -04:00
#endif
2011-02-20 19:09:28 -04:00
2011-10-15 19:29:33 -03:00
// agmatthews USERHOOKS
////////////////////////////////////////////////////////////////////////////////
// User variables
////////////////////////////////////////////////////////////////////////////////
#ifdef USERHOOK_VARIABLES
#include USERHOOK_VARIABLES
#endif
2011-02-17 03:09:13 -04:00
////////////////////////////////////////////////////////////////////////////////
// Global variables
////////////////////////////////////////////////////////////////////////////////
2011-07-17 07:31:46 -03:00
static const char *comma = ",";
2010-12-19 12:40:33 -04:00
2011-07-17 07:31:46 -03:00
static const char* flight_mode_strings[] = {
2010-12-19 12:40:33 -04:00
"STABILIZE",
2011-02-19 22:03:01 -04:00
"ACRO",
2011-06-26 14:02:47 -03:00
"ALT_HOLD",
2010-12-19 12:40:33 -04:00
"AUTO",
2011-07-02 19:44:59 -03:00
"GUIDED",
2011-03-09 02:37:09 -04:00
"LOITER",
2011-07-30 17:42:54 -03:00
"RTL",
2011-09-30 03:27:23 -03:00
"CIRCLE",
2011-12-09 19:52:34 -04:00
"POSITION",
"LAND"};
2010-12-19 12:40:33 -04:00
/* Radio values
2011-02-17 05:36:33 -04:00
Channel assignments
2010-12-19 12:40:33 -04:00
1 Ailerons (rudder if no ailerons)
2 Elevator
3 Throttle
4 Rudder (if we have ailerons)
5 Mode - 3 position switch
2011-02-25 01:33:39 -04:00
6 User assignable
2010-12-19 12:40:33 -04:00
7 trainer switch - sets throttle nominal (toggle switch), sets accels to Level (hold > 1 second)
8 TBD
*/
2011-09-04 21:15:36 -03:00
// temp
2011-11-07 18:24:32 -04:00
static int16_t y_actual_speed;
static int16_t y_rate_error;
2011-09-04 21:15:36 -03:00
// calc the
2011-11-07 18:24:32 -04:00
static int16_t x_actual_speed;
static int16_t x_rate_error;
2011-07-10 21:47:08 -03:00
2010-12-19 12:40:33 -04:00
// Radio
// -----
2011-09-05 02:09:07 -03:00
static byte control_mode = STABILIZE;
static byte old_control_mode = STABILIZE;
static byte oldSwitchPosition; // for remembering the control mode switch
2011-11-07 18:24:32 -04:00
static int16_t motor_out[8];
2011-11-20 04:21:19 -04:00
static bool do_simple = false;
static int16_t rc_override[8] = {0,0,0,0,0,0,0,0};
static bool rc_override_active = false;
static uint32_t rc_override_fs_timer = 0;
2011-01-25 01:53:36 -04:00
2011-06-12 09:14:10 -03:00
// Heli
// ----
2011-08-01 19:00:26 -03:00
#if FRAME_CONFIG == HELI_FRAME
2011-07-17 07:31:46 -03:00
static float heli_rollFactor[3], heli_pitchFactor[3]; // only required for 3 swashplate servos
2011-11-07 18:24:32 -04:00
static int16_t heli_servo_min[3], heli_servo_max[3]; // same here. for yaw servo we use heli_servo4_min/max parameter directly
static int32_t heli_servo_out[4]; // used for servo averaging for analog servos
static int16_t heli_servo_out_count = 0; // use for servo averaging
2011-08-01 19:00:26 -03:00
#endif
2011-06-12 09:14:10 -03:00
2011-02-25 01:33:39 -04:00
// Failsafe
// --------
2011-09-05 02:09:07 -03:00
static boolean failsafe; // did our throttle dip below the failsafe value?
static boolean ch3_failsafe;
2011-07-17 07:31:46 -03:00
static boolean motor_armed;
static boolean motor_auto_armed; // if true,
2011-02-25 01:33:39 -04:00
2011-02-17 03:09:13 -04:00
// PIDs
2011-02-25 01:33:39 -04:00
// ----
2011-07-17 07:31:46 -03:00
static Vector3f omega;
2011-09-10 19:16:51 -03:00
float tuning_value;
2010-12-19 12:40:33 -04:00
2011-02-17 03:09:13 -04:00
// LED output
// ----------
2011-07-17 07:31:46 -03:00
static boolean motor_light; // status of the Motor safety
static boolean GPS_light; // status of the GPS light
static byte led_mode = NORMAL_LEDS;
2011-01-22 22:36:22 -04:00
2010-12-19 12:40:33 -04:00
// GPS variables
// -------------
2011-07-17 07:31:46 -03:00
static const float t7 = 10000000.0; // used to scale GPS values for EEPROM storage
2011-09-18 09:57:55 -03:00
static float scaleLongUp = 1; // used to reverse longitude scaling
static float scaleLongDown = 1; // used to reverse longitude scaling
2011-07-17 07:31:46 -03:00
static byte ground_start_count = 10; // have we achieved first lock and set Home?
2011-07-30 17:42:54 -03:00
static bool did_ground_start = false; // have we ground started after first arming
2011-02-17 03:09:13 -04:00
2011-02-17 05:36:33 -04:00
// Location & Navigation
2010-12-19 12:40:33 -04:00
// ---------------------
2011-11-14 20:43:30 -04:00
static bool nav_ok;
2011-07-30 17:42:54 -03:00
static const float radius_of_earth = 6378100; // meters
static const float gravity = 9.81; // meters/ sec^2
2011-12-09 19:49:41 -04:00
static int32_t target_bearing; // deg * 100 : 0 to 360 location of the plane to the target
static int32_t home_bearing; // used to track difference in angle
2011-09-04 21:15:36 -03:00
2011-09-24 21:40:29 -03:00
static byte wp_control; // used to control - navgation or loiter
2011-07-17 07:31:46 -03:00
2011-11-16 04:15:59 -04:00
static byte command_nav_index; // current command memory location
static byte prev_nav_index;
static byte command_cond_index; // current command memory location
//static byte command_nav_ID; // current command ID
//static byte command_cond_ID; // current command ID
2011-09-24 21:40:29 -03:00
static byte wp_verify_byte; // used for tracking state of navigating waypoints
2011-07-17 07:31:46 -03:00
static float cos_roll_x = 1;
static float cos_pitch_x = 1;
2011-09-05 02:09:07 -03:00
static float cos_yaw_x = 1;
2011-07-17 07:31:46 -03:00
static float sin_pitch_y, sin_yaw_y, sin_roll_y;
2011-11-07 18:24:32 -04:00
static int32_t initial_simple_bearing; // used for Simple mode
2011-09-16 20:54:45 -03:00
static float simple_sin_y, simple_cos_x;
2011-11-16 04:15:59 -04:00
static int8_t jump = -10; // used to track loops in jump command
2011-11-07 18:24:32 -04:00
static int16_t waypoint_speed_gov;
2011-09-04 21:15:36 -03:00
2011-11-13 01:43:21 -04:00
static float circle_angle;
// replace with param
2011-11-13 17:40:31 -04:00
static const float circle_rate = 0.0872664625;
2011-11-13 01:43:21 -04:00
2011-08-05 13:19:06 -03:00
// Acro
2011-09-21 03:20:33 -03:00
#if CH7_OPTION == CH7_FLIP
2011-08-05 13:19:06 -03:00
static bool do_flip = false;
#endif
2011-10-29 01:29:10 -03:00
static boolean trim_flag;
2011-11-13 01:43:21 -04:00
static int8_t CH7_wp_index;
2011-10-29 01:29:10 -03:00
2010-12-19 12:40:33 -04:00
// Airspeed
// --------
2011-11-07 18:24:32 -04:00
static int16_t airspeed; // m/s * 100
2011-01-25 01:53:36 -04:00
2010-12-19 12:40:33 -04:00
// Location Errors
// ---------------
2011-11-07 18:24:32 -04:00
static int32_t long_error, lat_error; // temp for debugging
2011-07-10 21:47:08 -03:00
2011-02-25 01:33:39 -04:00
// Battery Sensors
// ---------------
2011-07-17 07:31:46 -03:00
static float battery_voltage = LOW_VOLTAGE * 1.05; // Battery Voltage of total battery, initialized above threshold for filter
static float battery_voltage1 = LOW_VOLTAGE * 1.05; // Battery Voltage of cell 1, initialized above threshold for filter
static float battery_voltage2 = LOW_VOLTAGE * 1.05; // Battery Voltage of cells 1 + 2, initialized above threshold for filter
static float battery_voltage3 = LOW_VOLTAGE * 1.05; // Battery Voltage of cells 1 + 2+3, initialized above threshold for filter
static float battery_voltage4 = LOW_VOLTAGE * 1.05; // Battery Voltage of cells 1 + 2+3 + 4, initialized above threshold for filter
2010-12-19 12:40:33 -04:00
2011-07-17 07:31:46 -03:00
static float current_amps;
static float current_total;
2011-07-30 17:42:54 -03:00
static bool low_batt = false;
2010-12-19 12:40:33 -04:00
// Barometer Sensor variables
// --------------------------
2011-11-07 18:24:32 -04:00
static int32_t abs_pressure;
static int32_t ground_pressure;
static int16_t ground_temperature;
2010-12-19 12:40:33 -04:00
2011-02-25 01:33:39 -04:00
// Altitude Sensor variables
2011-02-20 19:09:28 -04:00
// ----------------------
2011-11-05 01:41:51 -03:00
static byte altitude_sensor = BARO; // used to know which sensor is active, BARO or SONAR
2011-11-07 18:24:32 -04:00
static int32_t altitude_error; // meters * 100 we are off in altitude
2011-11-05 01:41:51 -03:00
2011-11-07 18:24:32 -04:00
static int16_t climb_rate; // m/s * 100
2011-11-05 01:41:51 -03:00
2011-11-07 18:24:32 -04:00
static int16_t sonar_alt;
static int16_t old_sonar_alt;
static int16_t sonar_rate;
2011-11-05 01:41:51 -03:00
2011-12-09 19:52:34 -04:00
static int32_t baro_alt;
static int32_t old_baro_alt;
2011-11-07 18:24:32 -04:00
static int16_t baro_rate;
2011-11-05 01:41:51 -03:00
2011-07-10 21:47:08 -03:00
2010-12-19 12:40:33 -04:00
// flight mode specific
// --------------------
2011-09-04 21:15:36 -03:00
static byte yaw_mode;
static byte roll_pitch_mode;
static byte throttle_mode;
2011-07-17 07:31:46 -03:00
static boolean takeoff_complete; // Flag for using take-off controls
static boolean land_complete;
2011-11-07 18:24:32 -04:00
static int32_t old_alt; // used for managing altitude rates
2011-12-05 00:51:34 -04:00
static int16_t velocity_land;
2011-07-17 07:31:46 -03:00
static byte yaw_tracking = MAV_ROI_WPNEXT; // no tracking, point at next wp, or at a target
2011-12-05 00:51:34 -04:00
static int16_t manual_boost; // used in adjust altitude to make changing alt faster
static int16_t angle_boost; // used in adjust altitude to make changing alt faster
2011-03-09 02:37:09 -04:00
2010-12-19 12:40:33 -04:00
// Loiter management
// -----------------
2011-11-07 18:24:32 -04:00
static int32_t original_target_bearing; // deg * 100, used to check we are not passing the WP
static int32_t old_target_bearing; // used to track difference in angle
2011-09-21 17:19:36 -03:00
2011-12-05 00:51:34 -04:00
static int16_t loiter_total; // deg : how many times to loiter * 360
static int16_t loiter_sum; // deg : how far we have turned around a waypoint
2011-11-07 18:24:32 -04:00
static uint32_t loiter_time; // millis : when we started LOITER mode
2011-10-11 06:24:29 -03:00
static unsigned loiter_time_max; // millis : how long to stay in LOITER mode
2011-09-21 17:19:36 -03:00
2010-12-19 12:40:33 -04:00
// these are the values for navigation control functions
// ----------------------------------------------------
2011-12-05 00:51:34 -04:00
static int32_t nav_roll; // deg * 100 : target roll angle
static int32_t nav_pitch; // deg * 100 : target pitch angle
static int32_t nav_yaw; // deg * 100 : target yaw angle
2011-12-09 19:52:34 -04:00
static int32_t home_to_copter_bearing; // deg * 100 : target yaw angle
2011-12-05 00:51:34 -04:00
static int32_t auto_yaw; // deg * 100 : target yaw angle
static int32_t nav_lat; // for error calcs
static int32_t nav_lon; // for error calcs
static int16_t nav_throttle; // 0-1000 for throttle control
static int16_t crosstrack_error;
2011-11-07 18:24:32 -04:00
static uint32_t throttle_integrator; // used to integrate throttle output to predict battery life
2011-07-17 07:31:46 -03:00
static bool invalid_throttle; // used to control when we calculate nav_throttle
2011-09-05 02:09:07 -03:00
//static bool set_throttle_cruise_flag = false; // used to track the throttle crouse value
2011-07-17 07:31:46 -03:00
2011-11-07 18:24:32 -04:00
static int32_t command_yaw_start; // what angle were we to begin with
static uint32_t command_yaw_start_time; // when did we start turning
static uint16_t command_yaw_time; // how long we are turning
static int32_t command_yaw_end; // what angle are we trying to be
static int32_t command_yaw_delta; // how many degrees will we turn
static int16_t command_yaw_speed; // how fast to turn
2011-07-30 17:42:54 -03:00
static byte command_yaw_dir;
static byte command_yaw_relative;
2011-07-17 07:31:46 -03:00
2011-11-07 18:24:32 -04:00
static int16_t auto_level_counter;
2011-06-16 14:03:26 -03:00
2010-12-19 12:40:33 -04:00
// Waypoints
// ---------
2011-12-09 19:52:34 -04:00
static int32_t home_distance; // meters - distance between plane and next waypoint
2011-11-07 18:24:32 -04:00
static int32_t wp_distance; // meters - distance between plane and next waypoint
static int32_t wp_totalDistance; // meters - distance between old and next waypoint
2011-09-05 02:09:07 -03:00
//static byte next_wp_index; // Current active command index
2010-12-19 12:40:33 -04:00
// repeating event control
// -----------------------
2011-12-05 00:51:34 -04:00
static byte event_id; // what to do - see defines
static uint32_t event_timer; // when the event was asked for in ms
static uint16_t event_delay; // how long to delay the next firing of event in millis
static int16_t event_repeat; // how many times to fire : 0 = forever, 1 = do once, 2 = do twice
static int16_t event_value; // per command value, such as PWM for servos
static int16_t event_undo_value; // the value used to undo commands
2011-09-05 02:09:07 -03:00
//static byte repeat_forever;
2011-10-15 17:09:04 -03:00
//static byte undo_event; // counter for timing the undo
2010-12-19 12:40:33 -04:00
// delay command
// --------------
2011-11-07 18:24:32 -04:00
static int32_t condition_value; // used in condition commands (eg delay, change alt, etc.)
static int32_t condition_start;
//static int16_t condition_rate;
2010-12-19 12:40:33 -04:00
2011-04-20 02:37:05 -03:00
// land command
// ------------
2011-11-07 18:24:32 -04:00
static int32_t land_start; // when we intiated command in millis()
static int32_t original_alt; // altitide reference for start of command
2011-04-20 02:37:05 -03:00
2010-12-19 12:40:33 -04:00
// 3D Location vectors
// -------------------
2011-07-17 07:31:46 -03:00
static struct Location home; // home location
static struct Location prev_WP; // last waypoint
static struct Location current_loc; // current location
static struct Location next_WP; // next waypoint
static struct Location target_WP; // where do we want to you towards?
2011-11-16 04:15:59 -04:00
static struct Location command_nav_queue; // command preloaded
static struct Location command_cond_queue; // command preloaded
2011-07-21 20:14:53 -03:00
static struct Location guided_WP; // guided mode waypoint
2011-11-07 18:24:32 -04:00
static int32_t target_altitude; // used for
2011-07-17 07:31:46 -03:00
static boolean home_is_set; // Flag for if we have g_gps lock and have set the home location
2010-12-19 12:40:33 -04:00
// IMU variables
// -------------
2011-07-17 07:31:46 -03:00
static float G_Dt = 0.02; // Integration time for the gyros (DCM algorithm)
2010-12-19 12:40:33 -04:00
// Performance monitoring
// ----------------------
2011-11-07 18:24:32 -04:00
static int32_t perf_mon_timer;
2011-09-05 02:09:07 -03:00
//static float imu_health; // Metric based on accel gain deweighting
2011-11-07 18:24:32 -04:00
static int16_t gps_fix_count;
2011-09-17 16:23:16 -03:00
static byte gps_watchdog;
2011-11-20 04:21:19 -04:00
static int pmTest1;
2010-12-19 12:40:33 -04:00
// System Timers
// --------------
2011-11-07 18:24:32 -04:00
static uint32_t fast_loopTimer; // Time in miliseconds of main control loop
2011-07-17 07:31:46 -03:00
static byte medium_loopCounter; // Counters for branching from main control loop to slower loops
2010-12-19 12:40:33 -04:00
2011-11-07 18:24:32 -04:00
static uint32_t fiftyhz_loopTimer;
2011-05-09 21:00:05 -03:00
2011-07-17 07:31:46 -03:00
static byte slow_loopCounter;
2011-11-07 18:24:32 -04:00
static int16_t superslow_loopCounter;
2011-09-04 21:15:36 -03:00
static byte simple_timer; // for limiting the execution of flight mode thingys
2010-12-19 12:40:33 -04:00
2011-01-17 22:48:44 -04:00
2011-09-19 18:02:42 -03:00
static float dTnav; // Delta Time in milliseconds for navigation computations
2011-11-07 18:24:32 -04:00
static uint32_t nav_loopTimer; // used to track the elapsed ime for GPS nav
2010-12-19 12:40:33 -04:00
2011-09-04 21:15:36 -03:00
static byte counter_one_herz;
static bool GPS_enabled = false;
2011-09-16 03:33:00 -03:00
static bool new_radio_frame;
2011-03-09 02:37:09 -04:00
2011-10-02 09:52:02 -03:00
AP_Relay relay;
2011-11-20 05:42:51 -04:00
#if USB_MUX_PIN > 0
2011-12-03 19:29:23 -04:00
static bool usb_connected;
2011-11-20 05:42:51 -04:00
#endif
2011-02-17 03:09:13 -04:00
////////////////////////////////////////////////////////////////////////////////
// Top-level logic
////////////////////////////////////////////////////////////////////////////////
2010-12-23 10:05:59 -04:00
2010-12-19 12:40:33 -04:00
void setup() {
2011-08-31 21:52:08 -03:00
memcheck_init();
2010-12-19 12:40:33 -04:00
init_ardupilot();
}
void loop()
{
2011-11-07 18:24:32 -04:00
int32_t timer = micros();
2011-05-02 01:34:15 -03:00
// We want this to execute fast
// ----------------------------
2011-11-07 02:46:39 -04:00
if ((timer - fast_loopTimer) >= 5000) {
2011-06-25 02:59:06 -03:00
//PORTK |= B00010000;
2011-09-20 02:24:57 -03:00
G_Dt = (float)(timer - fast_loopTimer) / 1000000.f; // used by PI Loops
fast_loopTimer = timer;
2011-06-16 14:03:26 -03:00
2010-12-19 12:40:33 -04:00
// Execute the fast loop
// ---------------------
fast_loop();
}
2011-09-11 15:22:01 -03:00
//PORTK &= B11101111;
2011-02-17 05:36:33 -04:00
2011-09-20 02:24:57 -03:00
if ((timer - fiftyhz_loopTimer) >= 20000) {
fiftyhz_loopTimer = timer;
2011-06-26 03:38:11 -03:00
//PORTK |= B01000000;
// reads all of the necessary trig functions for cameras, throttle, etc.
update_trig();
2011-02-25 01:33:39 -04:00
2011-09-25 16:21:54 -03:00
// perform 10hz tasks
2010-12-19 12:40:33 -04:00
medium_loop();
2011-02-17 05:36:33 -04:00
2011-06-26 03:38:11 -03:00
// Stuff to run at full 50hz, but after the loops
2011-05-09 21:00:05 -03:00
fifty_hz_loop();
2011-06-26 03:38:11 -03:00
2011-02-25 01:33:39 -04:00
counter_one_herz++;
2011-06-16 14:03:26 -03:00
2011-02-25 01:33:39 -04:00
if(counter_one_herz == 50){
super_slow_loop();
2011-03-20 09:55:51 -03:00
counter_one_herz = 0;
2011-02-25 01:33:39 -04:00
}
2011-10-28 02:36:25 -03:00
if (millis() - perf_mon_timer > 1200 /*20000*/) {
2011-09-16 03:33:00 -03:00
if (g.log_bitmask & MASK_LOG_PM)
Log_Write_Performance();
2011-02-24 01:56:59 -04:00
2011-09-25 16:21:54 -03:00
gps_fix_count = 0;
perf_mon_timer = millis();
2011-02-17 03:09:13 -04:00
}
2011-06-26 03:38:11 -03:00
//PORTK &= B10111111;
2010-12-19 12:40:33 -04:00
}
}
2011-06-26 03:38:11 -03:00
// PORTK |= B01000000;
// PORTK &= B10111111;
2011-07-10 21:47:08 -03:00
2011-06-16 14:03:26 -03:00
// Main loop
2011-07-17 07:31:46 -03:00
static void fast_loop()
2010-12-19 12:40:33 -04:00
{
2011-09-04 20:56:26 -03:00
// try to send any deferred messages if the serial port now has
2011-09-05 02:09:07 -03:00
// some space available
2011-10-11 06:12:37 -03:00
gcs_send_message(MSG_RETRY_DEFERRED);
2011-09-04 20:56:26 -03:00
2011-09-19 18:02:42 -03:00
// Read radio
// ----------
read_radio();
// IMU DCM Algorithm
read_AHRS();
2010-12-19 12:40:33 -04:00
// custom code/exceptions for flight modes
// ---------------------------------------
2011-09-04 21:15:36 -03:00
update_yaw_mode();
update_roll_pitch_mode();
2010-12-19 12:40:33 -04:00
// write out the servo PWM values
// ------------------------------
set_servos_4();
2011-09-27 13:35:05 -03:00
//if(motor_armed)
2011-10-02 15:36:23 -03:00
//Log_Write_Attitude();
2011-10-15 19:29:33 -03:00
// agmatthews - USERHOOKS
#ifdef USERHOOK_FASTLOOP
USERHOOK_FASTLOOP
#endif
2010-12-19 12:40:33 -04:00
}
2011-07-17 07:31:46 -03:00
static void medium_loop()
2010-12-19 12:40:33 -04:00
{
// This is the start of the medium (10 Hz) loop pieces
// -----------------------------------------
switch(medium_loopCounter) {
2011-02-17 05:36:33 -04:00
2011-03-15 02:54:48 -03:00
// This case deals with the GPS and Compass
//-----------------------------------------
2010-12-19 12:40:33 -04:00
case 0:
medium_loopCounter++;
2011-03-09 02:37:09 -04:00
2011-09-16 03:41:15 -03:00
#ifdef OPTFLOW_ENABLED
2011-09-14 17:58:18 -03:00
if(g.optflow_enabled){
2011-09-16 03:33:00 -03:00
optflow.read();
2011-09-16 23:17:50 -03:00
optflow.update_position(dcm.roll, dcm.pitch, cos_yaw_x, sin_yaw_y, current_loc.alt); // updates internal lon and lat with estimation based on optical flow
2011-09-16 03:33:00 -03:00
// write to log
if (g.log_bitmask & MASK_LOG_OPTFLOW){
Log_Write_Optflow();
}
2011-09-14 17:58:18 -03:00
}
2011-09-16 03:41:15 -03:00
#endif
2011-09-14 17:58:18 -03:00
2011-04-02 16:56:35 -03:00
if(GPS_enabled){
2011-03-28 02:18:10 -03:00
update_GPS();
}
2011-02-17 05:36:33 -04:00
2011-11-14 02:54:57 -04:00
#if HIL_MODE != HIL_MODE_ATTITUDE // don't execute in HIL mode
2011-05-09 21:00:05 -03:00
if(g.compass_enabled){
compass.read(); // Read magnetometer
2011-06-25 02:59:06 -03:00
compass.calculate(dcm.get_dcm_matrix()); // Calculate heading
2011-05-09 21:00:05 -03:00
compass.null_offsets(dcm.get_dcm_matrix());
}
#endif
2011-06-16 14:03:26 -03:00
// auto_trim, uses an auto_level algorithm
auto_trim();
2011-09-14 17:58:18 -03:00
// record throttle output
// ------------------------------
throttle_integrator += g.rc_3.servo_out;
2010-12-19 12:40:33 -04:00
break;
// This case performs some navigation computations
//------------------------------------------------
case 1:
medium_loopCounter++;
2011-03-15 02:54:48 -03:00
// Auto control modes:
2011-11-14 20:43:30 -04:00
if(nav_ok){
// clear nav flag
nav_ok = false;
2011-06-16 14:03:26 -03:00
// invalidate GPS data
2011-11-14 20:43:30 -04:00
// -------------------
2011-02-24 01:56:59 -04:00
g_gps->new_data = false;
2011-01-11 17:14:57 -04:00
2011-03-15 02:54:48 -03:00
// calculate the copter's desired bearing and WP distance
// ------------------------------------------------------
2011-09-20 02:24:57 -03:00
if(navigate()){
2011-04-03 18:11:14 -03:00
2011-09-20 02:24:57 -03:00
// control mode specific updates
// -----------------------------
update_navigation();
2011-05-31 02:29:06 -03:00
2011-09-20 02:24:57 -03:00
if (g.log_bitmask & MASK_LOG_NTUN)
Log_Write_Nav_Tuning();
}
2010-12-19 12:40:33 -04:00
}
break;
// command processing
//-------------------
case 2:
medium_loopCounter++;
2011-02-17 05:36:33 -04:00
2011-02-21 00:30:56 -04:00
// Read altitude from sensors
2011-03-15 02:54:48 -03:00
// --------------------------
2011-11-14 02:54:57 -04:00
#if HIL_MODE != HIL_MODE_ATTITUDE // don't execute in HIL mode
2011-09-04 21:15:36 -03:00
update_altitude();
2011-11-14 02:54:57 -04:00
#endif
2011-02-20 19:09:28 -04:00
2011-04-25 02:12:59 -03:00
// invalidate the throttle hold value
// ----------------------------------
invalid_throttle = true;
2011-09-14 17:58:18 -03:00
2011-06-16 14:03:26 -03:00
break;
// This case deals with sending high rate telemetry
//-------------------------------------------------
case 3:
medium_loopCounter++;
2011-12-03 19:29:23 -04:00
2010-12-19 12:40:33 -04:00
// perform next command
// --------------------
2011-05-14 23:02:09 -03:00
if(control_mode == AUTO){
2011-11-16 04:15:59 -04:00
if(home_is_set == true && g.command_total > 1){
2011-11-28 14:31:49 -04:00
update_commands();
2011-11-16 04:15:59 -04:00
}
2011-03-02 22:32:50 -04:00
}
2011-02-17 05:36:33 -04:00
2011-11-07 16:53:41 -04:00
if(motor_armed){
if (g.log_bitmask & MASK_LOG_ATTITUDE_MED)
Log_Write_Attitude();
2011-05-09 21:00:05 -03:00
2011-11-07 16:53:41 -04:00
if (g.log_bitmask & MASK_LOG_CTUN)
Log_Write_Control_Tuning();
}
2010-12-19 12:40:33 -04:00
2011-03-20 09:55:51 -03:00
// send all requested output streams with rates requested
// between 5 and 45 Hz
2011-10-11 06:12:37 -03:00
gcs_data_stream_send(5,45);
2011-06-16 14:03:26 -03:00
if (g.log_bitmask & MASK_LOG_MOTORS)
Log_Write_Motors();
2010-12-19 12:40:33 -04:00
break;
2011-02-17 05:36:33 -04:00
2010-12-19 12:40:33 -04:00
// This case controls the slow loop
//---------------------------------
case 4:
2011-03-15 02:54:48 -03:00
medium_loopCounter = 0;
2011-05-27 15:21:55 -03:00
2011-05-09 12:46:56 -03:00
if (g.battery_monitoring != 0){
read_battery();
2011-01-16 23:44:12 -04:00
}
2011-02-17 05:36:33 -04:00
2011-03-15 02:54:48 -03:00
// Accel trims = hold > 2 seconds
// Throttle cruise = switch less than 1 second
// --------------------------------------------
2010-12-19 12:40:33 -04:00
read_trim_switch();
2011-02-17 05:36:33 -04:00
2011-03-15 02:54:48 -03:00
// Check for engine arming
// -----------------------
2010-12-19 12:40:33 -04:00
arm_motors();
2011-02-17 05:36:33 -04:00
2011-11-01 13:24:51 -03:00
// Do an extra baro read
// ---------------------
2011-11-07 02:46:39 -04:00
#if HIL_MODE != HIL_MODE_ATTITUDE
2011-11-01 13:24:51 -03:00
barometer.Read();
2011-11-07 02:46:39 -04:00
#endif
// agmatthews - USERHOOKS
#ifdef USERHOOK_MEDIUMLOOP
USERHOOK_MEDIUMLOOP
#endif
2011-06-16 14:03:26 -03:00
2010-12-19 12:40:33 -04:00
slow_loop();
break;
2011-02-17 05:36:33 -04:00
2010-12-19 12:40:33 -04:00
default:
2011-03-15 02:54:48 -03:00
// this is just a catch all
// ------------------------
2010-12-19 12:40:33 -04:00
medium_loopCounter = 0;
break;
}
2011-05-09 21:00:05 -03:00
}
2011-04-17 20:08:16 -03:00
2011-05-09 21:00:05 -03:00
// stuff that happens at 50 hz
// ---------------------------
2011-07-17 07:31:46 -03:00
static void fifty_hz_loop()
2011-05-09 21:00:05 -03:00
{
2011-09-14 17:58:18 -03:00
// moved to slower loop
// --------------------
update_throttle_mode();
2011-07-30 17:42:54 -03:00
2011-08-13 23:30:37 -03:00
// Read Sonar
// ----------
2011-11-12 23:47:16 -04:00
# if CONFIG_SONAR == ENABLED
2011-08-13 23:30:37 -03:00
if(g.sonar_enabled){
2011-09-05 02:09:07 -03:00
sonar_alt = sonar.read();
2011-08-13 23:30:37 -03:00
}
2011-11-12 23:47:16 -04:00
#endif
2011-10-15 19:29:33 -03:00
// agmatthews - USERHOOKS
#ifdef USERHOOK_50HZLOOP
USERHOOK_50HZLOOP
#endif
2011-08-13 23:30:37 -03:00
2011-10-11 06:12:37 -03:00
#if HIL_MODE != HIL_MODE_DISABLED && FRAME_CONFIG != HELI_FRAME
2011-07-19 06:49:57 -03:00
// HIL for a copter needs very fast update of the servo values
2011-10-11 06:12:37 -03:00
gcs_send_message(MSG_RADIO_OUT);
2011-07-19 06:49:57 -03:00
#endif
2011-09-08 01:59:44 -03:00
camera_stabilization();
2011-05-09 21:00:05 -03:00
# if HIL_MODE == HIL_MODE_DISABLED
if (g.log_bitmask & MASK_LOG_ATTITUDE_FAST)
Log_Write_Attitude();
2010-12-19 12:40:33 -04:00
2011-03-15 02:54:48 -03:00
if (g.log_bitmask & MASK_LOG_RAW)
Log_Write_Raw();
#endif
2011-02-17 05:36:33 -04:00
2011-05-09 21:00:05 -03:00
// kick the GCS to process uplink data
2011-10-11 06:12:37 -03:00
gcs_update();
gcs_data_stream_send(45,1000);
2011-05-14 01:45:28 -03:00
2011-06-16 14:03:26 -03:00
#if FRAME_CONFIG == TRI_FRAME
2011-05-14 01:45:28 -03:00
// servo Yaw
2011-06-16 14:03:26 -03:00
g.rc_4.calc_pwm();
2011-05-14 01:45:28 -03:00
APM_RC.OutputCh(CH_7, g.rc_4.radio_out);
2011-05-14 23:02:09 -03:00
#endif
2010-12-19 12:40:33 -04:00
}
2011-05-09 21:00:05 -03:00
2011-07-17 07:31:46 -03:00
static void slow_loop()
2010-12-19 12:40:33 -04:00
{
// This is the slow (3 1/3 Hz) loop pieces
//----------------------------------------
switch (slow_loopCounter){
case 0:
slow_loopCounter++;
superslow_loopCounter++;
2011-02-17 05:36:33 -04:00
2011-10-12 02:20:23 -03:00
if(superslow_loopCounter > 1200){
2011-02-24 01:56:59 -04:00
#if HIL_MODE != HIL_MODE_ATTITUDE
2011-10-12 02:20:23 -03:00
if(g.rc_3.control_in == 0 && control_mode == STABILIZE && g.compass_enabled){
2011-02-24 01:56:59 -04:00
compass.save_offsets();
2011-03-16 02:43:48 -03:00
superslow_loopCounter = 0;
2011-02-24 01:56:59 -04:00
}
#endif
}
2010-12-19 12:40:33 -04:00
break;
2011-02-17 05:36:33 -04:00
2010-12-19 12:40:33 -04:00
case 1:
slow_loopCounter++;
2011-02-17 03:09:13 -04:00
2010-12-19 12:40:33 -04:00
// Read 3-position switch on radio
// -------------------------------
2011-02-17 05:36:33 -04:00
read_control_switch();
2010-12-19 12:40:33 -04:00
// Read main battery voltage if hooked up - does not read the 5v from radio
// ------------------------------------------------------------------------
2011-07-30 17:42:54 -03:00
//#if BATTERY_EVENT == 1
// read_battery();
//#endif
2011-02-17 05:36:33 -04:00
2011-06-01 02:50:17 -03:00
#if AUTO_RESET_LOITER == 1
if(control_mode == LOITER){
2011-09-16 03:33:00 -03:00
//if((abs(g.rc_2.control_in) + abs(g.rc_1.control_in)) > 1500){
2011-06-01 02:50:17 -03:00
// reset LOITER to current position
2011-08-14 15:18:32 -03:00
//next_WP = current_loc;
2011-09-16 03:33:00 -03:00
//}
2011-06-01 02:50:17 -03:00
}
#endif
2010-12-19 12:40:33 -04:00
break;
2011-02-17 05:36:33 -04:00
2010-12-19 12:40:33 -04:00
case 2:
slow_loopCounter = 0;
update_events();
2011-02-17 05:36:33 -04:00
2011-03-15 02:54:48 -03:00
// blink if we are armed
2011-06-16 14:03:26 -03:00
update_lights();
2011-03-15 02:54:48 -03:00
2011-10-11 06:12:37 -03:00
// send all requested output streams with rates requested
2011-12-03 18:32:08 -04:00
// between 3 and 5 Hz
gcs_data_stream_send(3,5);
2011-02-17 05:36:33 -04:00
2011-09-10 19:16:51 -03:00
if(g.radio_tuning > 0)
2011-04-17 20:08:16 -03:00
tuning();
2011-01-18 02:12:11 -04:00
2011-05-18 20:38:24 -03:00
#if MOTOR_LEDS == 1
2011-05-23 23:14:18 -03:00
update_motor_leds();
2011-05-18 20:38:24 -03:00
#endif
2011-12-03 19:29:23 -04:00
#if USB_MUX_PIN > 0
2011-11-20 05:42:51 -04:00
check_usb_mux();
2011-12-03 19:29:23 -04:00
#endif
2010-12-19 12:40:33 -04:00
break;
default:
slow_loopCounter = 0;
break;
}
2011-10-15 19:29:33 -03:00
// agmatthews - USERHOOKS
#ifdef USERHOOK_SLOWLOOP
USERHOOK_SLOWLOOP
#endif
2010-12-19 12:40:33 -04:00
}
2011-03-09 02:37:09 -04:00
// 1Hz loop
2011-07-17 07:31:46 -03:00
static void super_slow_loop()
2011-02-25 01:33:39 -04:00
{
2011-07-30 17:42:54 -03:00
if (g.log_bitmask & MASK_LOG_CUR)
2011-02-25 01:33:39 -04:00
Log_Write_Current();
2011-03-09 02:37:09 -04:00
2011-10-11 06:12:37 -03:00
gcs_send_message(MSG_HEARTBEAT);
2011-12-03 18:32:08 -04:00
gcs_data_stream_send(1,3);
2011-10-15 19:29:33 -03:00
// agmatthews - USERHOOKS
#ifdef USERHOOK_SUPERSLOWLOOP
USERHOOK_SUPERSLOWLOOP
#endif
2011-02-25 01:33:39 -04:00
}
2011-07-17 07:31:46 -03:00
static void update_GPS(void)
2010-12-19 12:40:33 -04:00
{
2011-02-17 05:36:33 -04:00
g_gps->update();
2010-12-19 12:40:33 -04:00
update_GPS_light();
2011-02-17 05:36:33 -04:00
2011-04-17 02:17:42 -03:00
//current_loc.lng = 377697000; // Lon * 10 * *7
//current_loc.lat = -1224318000; // Lat * 10 * *7
//current_loc.alt = 100; // alt * 10 * *7
//return;
2011-09-29 03:11:19 -03:00
if(gps_watchdog < 12){
2011-09-17 16:23:16 -03:00
gps_watchdog++;
}else{
// we have lost GPS signal for a moment. Reduce our error to avoid flyaways
2011-11-14 20:43:30 -04:00
nav_roll >>= 1;
nav_pitch >>= 1;
2011-09-17 16:23:16 -03:00
}
2011-04-17 02:17:42 -03:00
2011-02-19 22:03:01 -04:00
if (g_gps->new_data && g_gps->fix) {
2011-09-17 16:23:16 -03:00
gps_watchdog = 0;
2011-03-09 02:37:09 -04:00
2011-11-14 20:43:30 -04:00
// OK to run the nav routines
nav_ok = true;
2010-12-19 12:40:33 -04:00
// for performance
// ---------------
gps_fix_count++;
2011-02-17 05:36:33 -04:00
2011-11-14 20:43:30 -04:00
// we are not tracking I term on navigation, so this isn't needed
dTnav = (float)(millis() - nav_loopTimer)/ 1000.0;
nav_loopTimer = millis();
// prevent runup from bad GPS
dTnav = min(dTnav, 1.0);
2010-12-19 12:40:33 -04:00
if(ground_start_count > 1){
ground_start_count--;
2011-02-17 05:36:33 -04:00
2010-12-19 12:40:33 -04:00
} else if (ground_start_count == 1) {
2011-02-17 05:36:33 -04:00
2010-12-19 12:40:33 -04:00
// We countdown N number of good GPS fixes
// so that the altitude is more accurate
// -------------------------------------
if (current_loc.lat == 0) {
ground_start_count = 5;
2011-02-17 05:36:33 -04:00
2011-02-24 01:56:59 -04:00
}else{
2010-12-19 12:40:33 -04:00
init_home();
ground_start_count = 0;
}
}
2011-02-17 05:36:33 -04:00
current_loc.lng = g_gps->longitude; // Lon * 10 * *7
2011-07-04 03:37:29 -03:00
current_loc.lat = g_gps->latitude; // Lat * 10 * *7
2011-05-31 02:29:06 -03:00
if (g.log_bitmask & MASK_LOG_GPS){
Log_Write_GPS();
}
2011-11-14 02:54:57 -04:00
2011-11-14 20:43:30 -04:00
#if HIL_MODE == HIL_MODE_ATTITUDE // only execute in HIL mode
update_altitude();
#endif
} else {
g_gps->new_data = false;
}
2010-12-19 12:40:33 -04:00
}
2011-02-17 05:36:33 -04:00
2011-09-16 03:33:00 -03:00
2011-09-04 21:15:36 -03:00
void update_yaw_mode(void)
{
switch(yaw_mode){
case YAW_ACRO:
g.rc_4.servo_out = get_rate_yaw(g.rc_4.control_in);
return;
break;
case YAW_HOLD:
// calcualte new nav_yaw offset
2011-09-14 17:58:18 -03:00
if (control_mode <= STABILIZE){
nav_yaw = get_nav_yaw_offset(g.rc_4.control_in, g.rc_3.control_in);
}else{
nav_yaw = get_nav_yaw_offset(g.rc_4.control_in, 1);
}
2011-09-04 21:15:36 -03:00
break;
case YAW_LOOK_AT_HOME:
2011-09-22 02:31:12 -03:00
//nav_yaw updated in update_navigation()
2011-09-04 21:15:36 -03:00
break;
case YAW_AUTO:
nav_yaw += constrain(wrap_180(auto_yaw - nav_yaw), -20, 20);
nav_yaw = wrap_360(nav_yaw);
break;
}
// Yaw control
g.rc_4.servo_out = get_stabilize_yaw(nav_yaw);
//Serial.printf("4: %d\n",g.rc_4.servo_out);
}
2011-07-21 20:14:53 -03:00
2011-09-04 21:15:36 -03:00
void update_roll_pitch_mode(void)
2010-12-19 12:40:33 -04:00
{
2011-09-21 03:20:33 -03:00
#if CH7_OPTION == CH7_FLIP
2011-08-05 13:19:06 -03:00
if (do_flip){
roll_flip();
return;
}
#endif
2011-09-04 22:15:55 -03:00
int control_roll = 0, control_pitch = 0;
2011-02-17 05:36:33 -04:00
2011-09-16 03:33:00 -03:00
//read_radio();
if(do_simple && new_radio_frame){
new_radio_frame = false;
2011-09-16 20:54:45 -03:00
simple_timer++;
int delta = wrap_360(dcm.yaw_sensor - initial_simple_bearing)/100;
if (simple_timer == 1){
// roll
simple_cos_x = sin(radians(90 - delta));
}else if (simple_timer > 2){
// pitch
simple_sin_y = cos(radians(90 - delta));
simple_timer = 0;
}
2011-09-14 17:58:18 -03:00
2011-09-16 03:33:00 -03:00
// Rotate input by the initial bearing
2011-09-16 20:54:45 -03:00
control_roll = g.rc_1.control_in * simple_cos_x + g.rc_2.control_in * simple_sin_y;
control_pitch = -(g.rc_1.control_in * simple_sin_y - g.rc_2.control_in * simple_cos_x);
2011-09-14 17:58:18 -03:00
2011-09-16 03:33:00 -03:00
g.rc_1.control_in = control_roll;
g.rc_2.control_in = control_pitch;
2011-09-14 17:58:18 -03:00
}
2011-02-17 05:36:33 -04:00
2011-09-04 21:15:36 -03:00
switch(roll_pitch_mode){
case ROLL_PITCH_ACRO:
g.rc_1.servo_out = get_rate_roll(g.rc_1.control_in);
g.rc_2.servo_out = get_rate_pitch(g.rc_2.control_in);
2011-03-14 03:04:07 -03:00
break;
2011-09-04 21:15:36 -03:00
case ROLL_PITCH_STABLE:
2011-09-16 03:33:00 -03:00
g.rc_1.servo_out = get_stabilize_roll(g.rc_1.control_in);
g.rc_2.servo_out = get_stabilize_pitch(g.rc_2.control_in);
2011-09-04 21:15:36 -03:00
break;
2011-02-17 05:36:33 -04:00
2011-09-04 21:15:36 -03:00
case ROLL_PITCH_AUTO:
// mix in user control with Nav control
2011-09-16 03:33:00 -03:00
control_roll = g.rc_1.control_mix(nav_roll);
control_pitch = g.rc_2.control_mix(nav_pitch);
g.rc_1.servo_out = get_stabilize_roll(control_roll);
g.rc_2.servo_out = get_stabilize_pitch(control_pitch);
2011-09-04 21:15:36 -03:00
break;
}
}
2011-07-10 21:47:08 -03:00
2011-09-16 03:33:00 -03:00
// 50 hz update rate, not 250
2011-09-04 21:15:36 -03:00
void update_throttle_mode(void)
{
switch(throttle_mode){
2011-09-22 02:31:12 -03:00
2011-09-04 21:15:36 -03:00
case THROTTLE_MANUAL:
2011-09-16 20:54:45 -03:00
if (g.rc_3.control_in > 0){
2011-10-29 05:27:43 -03:00
#if FRAME_CONFIG == HELI_FRAME
2011-11-20 09:30:42 -04:00
g.rc_3.servo_out = heli_get_angle_boost(g.rc_3.control_in);
2011-10-29 05:27:43 -03:00
#else
angle_boost = get_angle_boost(g.rc_3.control_in);
g.rc_3.servo_out = g.rc_3.control_in + angle_boost;
#endif
2011-09-16 20:54:45 -03:00
}else{
2011-10-27 16:33:19 -03:00
g.pi_stabilize_roll.reset_I();
g.pi_stabilize_pitch.reset_I();
2011-09-23 03:10:55 -03:00
g.pi_rate_roll.reset_I();
g.pi_rate_pitch.reset_I();
2011-09-16 20:54:45 -03:00
g.rc_3.servo_out = 0;
}
2011-09-22 02:31:12 -03:00
break;
2011-02-17 05:36:33 -04:00
2011-09-04 21:15:36 -03:00
case THROTTLE_HOLD:
// allow interactive changing of atitude
adjust_altitude();
// fall through
2010-12-19 12:40:33 -04:00
2011-09-04 21:15:36 -03:00
case THROTTLE_AUTO:
2011-01-25 01:53:36 -04:00
2011-11-20 09:30:42 -04:00
// calculate angle boost
2011-11-06 06:25:13 -04:00
angle_boost = get_angle_boost(g.throttle_cruise);
2011-11-20 16:49:00 -04:00
2011-11-20 09:30:42 -04:00
// manual command up or down?
2011-11-06 06:25:13 -04:00
if(manual_boost != 0){
2011-11-20 16:49:00 -04:00
2011-11-06 06:25:13 -04:00
//remove alt_hold_velocity when implemented
#if FRAME_CONFIG == HELI_FRAME
2011-12-05 01:31:42 -04:00
g.rc_3.servo_out = heli_get_angle_boost(g.throttle_cruise + manual_boost);
2011-11-06 06:25:13 -04:00
#else
2011-12-05 01:31:42 -04:00
g.rc_3.servo_out = g.throttle_cruise + angle_boost + manual_boost;
2011-11-06 06:25:13 -04:00
#endif
2011-11-20 16:49:00 -04:00
2011-11-06 06:25:13 -04:00
// reset next_WP.alt
next_WP.alt = max(current_loc.alt, 100);
2011-11-20 16:49:00 -04:00
2011-11-06 06:25:13 -04:00
}else{
2011-11-20 09:30:42 -04:00
// 10hz, don't run up i term
if(invalid_throttle && motor_auto_armed == true){
// how far off are we
altitude_error = get_altitude_error();
// get the AP throttle
2011-11-20 16:49:00 -04:00
nav_throttle = get_nav_throttle(altitude_error);
2011-11-20 09:30:42 -04:00
// clear the new data flag
invalid_throttle = false;
}
2011-11-20 16:49:00 -04:00
2011-11-06 06:25:13 -04:00
#if FRAME_CONFIG == HELI_FRAME
2011-11-20 09:30:42 -04:00
g.rc_3.servo_out = heli_get_angle_boost(g.throttle_cruise + nav_throttle + get_z_damping());
2011-11-06 06:25:13 -04:00
#else
2011-11-02 01:18:47 -03:00
g.rc_3.servo_out = g.throttle_cruise + nav_throttle + angle_boost + get_z_damping();
2011-11-06 06:25:13 -04:00
#endif
}
2011-09-04 21:15:36 -03:00
break;
2010-12-19 12:40:33 -04:00
}
}
// called after a GPS read
2011-07-17 07:31:46 -03:00
static void update_navigation()
2010-12-19 12:40:33 -04:00
{
// wp_distance is in ACTUAL meters, not the *100 meters we get from the GPS
// ------------------------------------------------------------------------
2011-05-23 02:53:00 -03:00
switch(control_mode){
case AUTO:
verify_commands();
// note: wp_control is handled by commands_logic
2011-03-05 01:12:16 -04:00
2011-05-23 02:53:00 -03:00
// calculates desired Yaw
2011-09-04 21:15:36 -03:00
update_auto_yaw();
2011-04-25 02:12:59 -03:00
2011-05-23 02:53:00 -03:00
// calculates the desired Roll and Pitch
update_nav_wp();
break;
2011-05-14 23:02:09 -03:00
2011-07-16 19:12:52 -03:00
case GUIDED:
2011-09-14 17:58:18 -03:00
wp_control = WP_MODE;
2011-10-13 07:34:32 -03:00
// check if we are close to point > loiter
2011-10-15 17:09:04 -03:00
wp_verify_byte = 0;
verify_nav_wp();
2011-09-21 17:19:36 -03:00
2011-10-15 17:09:04 -03:00
if (wp_control == WP_MODE) {
update_auto_yaw();
} else {
set_mode(LOITER);
}
2011-09-14 17:58:18 -03:00
update_nav_wp();
break;
2011-08-01 19:00:26 -03:00
case RTL:
2011-10-06 21:42:09 -03:00
if((wp_distance <= g.waypoint_radius) || check_missed_wp()){
2011-12-09 19:52:34 -04:00
//lets just jump to Loiter Mode after RTL
//if(land after RTL)
//set_mode(LAND);
//else
2011-10-07 15:06:31 -03:00
set_mode(LOITER);
2011-12-09 19:52:34 -04:00
2011-10-07 15:06:31 -03:00
}else{
2011-07-16 19:12:52 -03:00
// calculates desired Yaw
2011-08-01 19:00:26 -03:00
// XXX this is an experiment
#if FRAME_CONFIG == HELI_FRAME
2011-09-04 21:15:36 -03:00
update_auto_yaw();
2011-08-01 19:00:26 -03:00
#endif
2011-08-14 15:18:32 -03:00
2011-09-04 21:15:36 -03:00
wp_control = WP_MODE;
}
2011-08-14 15:18:32 -03:00
2011-09-04 21:15:36 -03:00
// calculates the desired Roll and Pitch
update_nav_wp();
2011-05-23 02:53:00 -03:00
break;
2011-07-16 19:12:52 -03:00
// switch passthrough to LOITER
case LOITER:
2011-09-30 03:27:23 -03:00
case POSITION:
2011-12-09 19:52:34 -04:00
case LAND:
2011-09-21 17:19:36 -03:00
wp_control = LOITER_MODE;
2011-07-16 19:12:52 -03:00
// calculates the desired Roll and Pitch
update_nav_wp();
2011-05-23 02:53:00 -03:00
break;
2011-07-30 17:42:54 -03:00
case CIRCLE:
2011-09-21 17:19:36 -03:00
yaw_tracking = MAV_ROI_WPNEXT;
wp_control = CIRCLE_MODE;
2011-07-30 17:42:54 -03:00
2011-09-04 21:15:36 -03:00
// calculates desired Yaw
update_auto_yaw();
2011-09-21 17:19:36 -03:00
update_nav_wp();
2011-07-30 17:42:54 -03:00
break;
2010-12-19 12:40:33 -04:00
}
2011-09-22 02:31:12 -03:00
2011-12-09 19:52:34 -04:00
// are we in SIMPLE mode?
if(do_simple){
// get distance to home
2011-12-11 03:29:29 -04:00
if(home_distance > 10){
2011-12-09 19:52:34 -04:00
// 10m
// we reset the angular offset to be a vector from home to the quad
2011-12-11 03:29:29 -04:00
initial_simple_bearing = home_to_copter_bearing;
//Serial.printf("ISB: %d\n", initial_simple_bearing);
2011-12-09 19:52:34 -04:00
}
}
2011-09-22 02:31:12 -03:00
if(yaw_mode == YAW_LOOK_AT_HOME){
if(home_is_set){
//nav_yaw = point_at_home_yaw();
nav_yaw = get_bearing(¤t_loc, &home);
} else {
nav_yaw = 0;
}
}
2010-12-19 12:40:33 -04:00
}
2011-07-17 07:31:46 -03:00
static void read_AHRS(void)
2010-12-19 12:40:33 -04:00
{
// Perform IMU calculations and get attitude info
2011-01-25 01:53:36 -04:00
//-----------------------------------------------
2011-11-09 22:17:06 -04:00
#if HIL_MODE != HIL_MODE_DISABLED
2011-05-09 21:00:05 -03:00
// update hil before dcm update
2011-10-11 06:12:37 -03:00
gcs_update();
2011-05-09 21:00:05 -03:00
#endif
2011-09-15 00:04:53 -03:00
dcm.update_DCM_fast();
2011-12-03 19:29:23 -04:00
omega = imu.get_gyro();
2011-01-23 12:40:03 -04:00
}
2011-02-13 18:32:34 -04:00
2011-07-17 07:31:46 -03:00
static void update_trig(void){
2011-02-13 18:32:34 -04:00
Vector2f yawvector;
Matrix3f temp = dcm.get_dcm_matrix();
2011-02-17 05:36:33 -04:00
2011-02-13 18:32:34 -04:00
yawvector.x = temp.a.x; // sin
yawvector.y = temp.b.x; // cos
yawvector.normalize();
2011-02-17 05:36:33 -04:00
2011-02-13 18:32:34 -04:00
sin_pitch_y = -temp.c.x;
cos_pitch_x = sqrt(1 - (temp.c.x * temp.c.x));
2011-02-17 05:36:33 -04:00
2011-02-13 18:32:34 -04:00
cos_roll_x = temp.c.z / cos_pitch_x;
sin_roll_y = temp.c.y / cos_pitch_x;
2011-07-10 21:47:08 -03:00
2011-09-16 03:33:00 -03:00
cos_yaw_x = yawvector.y; // 0 x = north
sin_yaw_y = yawvector.x; // 1 y
2011-09-04 21:15:36 -03:00
//flat:
2011-09-24 21:40:29 -03:00
// 0 ° = cos_yaw: 0.00, sin_yaw: 1.00,
// 90° = cos_yaw: 1.00, sin_yaw: 0.00,
// 180 = cos_yaw: 0.00, sin_yaw: -1.00,
// 270 = cos_yaw: -1.00, sin_yaw: 0.00,
2011-02-17 05:36:33 -04:00
}
2011-02-20 19:09:28 -04:00
2011-06-16 14:03:26 -03:00
// updated at 10hz
2011-09-04 21:15:36 -03:00
static void update_altitude()
2011-02-21 00:30:56 -04:00
{
2011-04-25 02:12:59 -03:00
altitude_sensor = BARO;
2011-11-13 01:43:21 -04:00
//current_loc.alt = g_gps->altitude - gps_base_alt;
//climb_rate = (g_gps->altitude - old_baro_alt) * 10;
//old_baro_alt = g_gps->altitude;
//baro_alt = g_gps->altitude;
2011-04-25 02:12:59 -03:00
2011-06-16 14:03:26 -03:00
#if HIL_MODE == HIL_MODE_ATTITUDE
2011-11-13 01:43:21 -04:00
// we are in the SIM, fake out the baro and Sonar
int fake_relative_alt = g_gps->altitude - gps_base_alt;
baro_alt = fake_relative_alt;
sonar_alt = fake_relative_alt;
2011-11-14 20:43:30 -04:00
baro_rate = (baro_alt - old_baro_alt) * 5; // 5hz
old_baro_alt = baro_alt;
2011-06-16 14:03:26 -03:00
#else
2011-11-13 01:43:21 -04:00
// This is real life
2011-11-19 17:58:41 -04:00
// read in Actual Baro Altitude
baro_alt = (baro_alt + read_barometer()) >> 1;
2011-11-13 01:43:21 -04:00
// calc the vertical accel rate
2011-11-25 15:33:50 -04:00
int temp = (baro_alt - old_baro_alt) * 10;
baro_rate = (temp + baro_rate) >> 1;
2011-11-19 17:58:41 -04:00
old_baro_alt = baro_alt;
2011-11-13 01:43:21 -04:00
// sonar_alt is calculaed in a faster loop and filtered with a mode filter
#endif
2011-06-16 14:03:26 -03:00
2011-11-05 01:41:51 -03:00
2011-06-16 14:03:26 -03:00
if(g.sonar_enabled){
// filter out offset
float scale;
2011-11-05 01:41:51 -03:00
// calc rate of change for Sonar
2011-11-13 01:43:21 -04:00
#if HIL_MODE == HIL_MODE_ATTITUDE
// we are in the SIM, fake outthe Sonar rate
sonar_rate = baro_rate;
#else
// This is real life
// calc the vertical accel rate
sonar_rate = (sonar_alt - old_sonar_alt) * 10;
old_sonar_alt = sonar_alt;
#endif
2011-07-02 19:44:59 -03:00
2011-11-13 01:43:21 -04:00
if(baro_alt < 800){
2011-09-04 21:15:36 -03:00
#if SONAR_TILT_CORRECTION == 1
// correct alt for angle of the sonar
float temp = cos_pitch_x * cos_roll_x;
temp = max(temp, 0.707);
sonar_alt = (float)sonar_alt * temp;
#endif
2011-07-02 19:44:59 -03:00
scale = (sonar_alt - 400) / 200;
2011-06-30 21:19:10 -03:00
scale = constrain(scale, 0, 1);
2011-11-05 01:41:51 -03:00
current_loc.alt = ((float)sonar_alt * (1.0 - scale)) + ((float)baro_alt * scale) + home.alt;
2011-11-13 01:43:21 -04:00
// solve for a blended climb_rate
2011-11-05 01:41:51 -03:00
climb_rate = ((float)sonar_rate * (1.0 - scale)) + (float)baro_rate * scale;
2011-06-30 21:19:10 -03:00
}else{
2011-11-13 01:43:21 -04:00
// we must be higher than sonar, don't get tricked by bad sonar reads
current_loc.alt = baro_alt + home.alt; // home alt = 0
// dont blend, go straight baro
climb_rate = baro_rate;
2011-06-30 21:19:10 -03:00
}
2011-02-21 00:46:26 -04:00
2011-02-20 19:09:28 -04:00
}else{
2011-11-13 01:43:21 -04:00
// NO Sonar case
2011-02-21 00:30:56 -04:00
current_loc.alt = baro_alt + home.alt;
2011-11-05 01:41:51 -03:00
climb_rate = baro_rate;
2011-02-20 19:09:28 -04:00
}
2011-11-28 14:31:49 -04:00
// manage bad data
climb_rate = constrain(climb_rate, -300, 300);
2011-03-26 03:35:52 -03:00
}
2011-07-17 07:31:46 -03:00
static void
2011-04-10 20:07:24 -03:00
adjust_altitude()
{
2011-11-01 13:24:51 -03:00
/*
2011-11-01 13:28:36 -03:00
// old vert control
2011-09-14 17:58:18 -03:00
if(g.rc_3.control_in <= 200){
next_WP.alt -= 1; // 1 meter per second
2011-09-27 13:35:05 -03:00
next_WP.alt = max(next_WP.alt, (current_loc.alt - 500)); // don't go less than 4 meters below current location
2011-09-14 17:58:18 -03:00
next_WP.alt = max(next_WP.alt, 100); // don't go less than 1 meter
2011-10-29 01:29:10 -03:00
//manual_boost = (g.rc_3.control_in == 0) ? -20 : 0;
2011-09-14 17:58:18 -03:00
}else if (g.rc_3.control_in > 700){
next_WP.alt += 1; // 1 meter per second
2011-09-27 13:35:05 -03:00
next_WP.alt = min(next_WP.alt, (current_loc.alt + 500)); // don't go more than 4 meters below current location
2011-10-29 01:29:10 -03:00
//manual_boost = (g.rc_3.control_in == 800) ? 20 : 0;
2011-11-01 13:24:51 -03:00
}*/
if(g.rc_3.control_in <= 180){
// we remove 0 to 100 PWM from hover
manual_boost = g.rc_3.control_in - 180;
manual_boost = max(-120, manual_boost);
2011-12-05 01:31:42 -04:00
g.throttle_cruise += g.pi_alt_hold.get_integrator();
2011-11-01 13:24:51 -03:00
g.pi_alt_hold.reset_I();
g.pi_throttle.reset_I();
}else if (g.rc_3.control_in >= 650){
// we add 0 to 100 PWM to hover
manual_boost = g.rc_3.control_in - 650;
2011-12-05 01:31:42 -04:00
g.throttle_cruise += g.pi_alt_hold.get_integrator();
2011-11-01 13:24:51 -03:00
g.pi_alt_hold.reset_I();
g.pi_throttle.reset_I();
}else {
manual_boost = 0;
2011-04-10 20:07:24 -03:00
}
2011-04-16 17:42:00 -03:00
}
2011-07-17 07:31:46 -03:00
static void tuning(){
2011-09-10 19:16:51 -03:00
tuning_value = (float)g.rc_6.control_in / 1000.0;
2011-04-17 20:08:16 -03:00
2011-09-10 19:16:51 -03:00
switch(g.radio_tuning){
2011-10-02 15:36:23 -03:00
/*case CH6_STABILIZE_KP:
g.rc_6.set_range(0,2000); // 0 to 8
tuning_value = (float)g.rc_6.control_in / 100.0;
alt_hold_gain = tuning_value;
break;*/
2011-09-10 19:16:51 -03:00
case CH6_STABILIZE_KP:
g.rc_6.set_range(0,8000); // 0 to 8
g.pi_stabilize_roll.kP(tuning_value);
g.pi_stabilize_pitch.kP(tuning_value);
break;
2011-06-16 14:03:26 -03:00
2011-09-10 19:16:51 -03:00
case CH6_STABILIZE_KI:
g.rc_6.set_range(0,300); // 0 to .3
tuning_value = (float)g.rc_6.control_in / 1000.0;
g.pi_stabilize_roll.kI(tuning_value);
g.pi_stabilize_pitch.kI(tuning_value);
break;
2011-06-16 14:03:26 -03:00
2011-09-10 19:16:51 -03:00
case CH6_RATE_KP:
2011-12-07 01:03:07 -04:00
g.rc_6.set_range(40,300); // 0 to .3
2011-09-10 19:16:51 -03:00
g.pi_rate_roll.kP(tuning_value);
g.pi_rate_pitch.kP(tuning_value);
break;
2011-06-16 14:03:26 -03:00
2011-09-10 19:16:51 -03:00
case CH6_RATE_KI:
g.rc_6.set_range(0,300); // 0 to .3
g.pi_rate_roll.kI(tuning_value);
g.pi_rate_pitch.kI(tuning_value);
break;
2011-06-16 14:03:26 -03:00
2011-09-10 19:16:51 -03:00
case CH6_YAW_KP:
g.rc_6.set_range(0,1000);
g.pi_stabilize_yaw.kP(tuning_value);
break;
2011-06-16 14:03:26 -03:00
2011-09-10 19:16:51 -03:00
case CH6_YAW_RATE_KP:
g.rc_6.set_range(0,1000);
g.pi_rate_yaw.kP(tuning_value);
break;
2011-09-04 21:15:36 -03:00
2011-09-10 19:16:51 -03:00
case CH6_THROTTLE_KP:
2011-11-20 09:30:42 -04:00
g.rc_6.set_range(0,1000); // 0 to 1
2011-09-10 19:16:51 -03:00
g.pi_throttle.kP(tuning_value);
break;
2011-09-04 21:15:36 -03:00
2011-09-10 19:16:51 -03:00
case CH6_TOP_BOTTOM_RATIO:
g.rc_6.set_range(800,1000); // .8 to 1
g.top_bottom_ratio = tuning_value;
break;
2011-04-21 20:07:31 -03:00
2011-09-10 19:16:51 -03:00
case CH6_RELAY:
g.rc_6.set_range(0,1000);
2011-10-02 09:16:31 -03:00
if (g.rc_6.control_in > 525) relay.on();
if (g.rc_6.control_in < 475) relay.off();
2011-09-10 19:16:51 -03:00
break;
2011-07-26 00:37:41 -03:00
2011-09-10 19:16:51 -03:00
case CH6_TRAVERSE_SPEED:
g.rc_6.set_range(0,1000);
g.waypoint_speed_max = g.rc_6.control_in;
break;
2011-09-10 22:37:28 -03:00
2011-09-27 13:39:32 -03:00
case CH6_LOITER_P:
g.rc_6.set_range(0,1000);
g.pi_loiter_lat.kP(tuning_value);
g.pi_loiter_lon.kP(tuning_value);
break;
2011-09-10 22:37:28 -03:00
case CH6_NAV_P:
g.rc_6.set_range(0,6000);
g.pi_nav_lat.kP(tuning_value);
g.pi_nav_lon.kP(tuning_value);
break;
2011-11-01 13:24:51 -03:00
2011-10-29 08:34:21 -03:00
#if FRAME_CONFIG == HELI_FRAME
case CH6_HELI_EXTERNAL_GYRO:
g.rc_6.set_range(1000,2000);
g.heli_ext_gyro_gain = tuning_value * 1000;
break;
#endif
2011-11-20 16:49:00 -04:00
2011-11-20 09:30:42 -04:00
case CH6_THR_HOLD_KP:
g.rc_6.set_range(0,1000); // 0 to 1
g.pi_alt_hold.kP(tuning_value);
break;
2011-09-10 19:16:51 -03:00
}
2011-05-09 14:40:32 -03:00
}
2011-07-17 07:31:46 -03:00
static void update_nav_wp()
2011-05-23 02:53:00 -03:00
{
if(wp_control == LOITER_MODE){
2011-08-14 15:18:32 -03:00
2011-05-23 02:53:00 -03:00
// calc a pitch to the target
2011-09-08 22:59:42 -03:00
calc_location_error(&next_WP);
2011-05-23 02:53:00 -03:00
2011-09-04 21:15:36 -03:00
// use error as the desired rate towards the target
2011-09-27 13:35:05 -03:00
calc_loiter(long_error, lat_error);
2011-08-14 15:18:32 -03:00
2011-09-24 21:40:29 -03:00
// rotate pitch and roll to the copter frame of reference
2011-09-27 13:35:05 -03:00
calc_loiter_pitch_roll();
2011-09-24 21:40:29 -03:00
2011-09-21 17:19:36 -03:00
}else if(wp_control == CIRCLE_MODE){
// check if we have missed the WP
int loiter_delta = (target_bearing - old_target_bearing)/100;
// reset the old value
old_target_bearing = target_bearing;
// wrap values
if (loiter_delta > 180) loiter_delta -= 360;
if (loiter_delta < -180) loiter_delta += 360;
// sum the angle around the WP
2011-10-13 00:59:12 -03:00
loiter_sum += loiter_delta;
2011-09-21 17:19:36 -03:00
2011-10-11 17:52:02 -03:00
// create a virtual waypoint that circles the next_WP
2011-09-21 17:19:36 -03:00
// Count the degrees we have circulated the WP
2011-11-13 01:43:21 -04:00
//int circle_angle = wrap_360(target_bearing + 3000 + 18000) / 100;
2011-09-21 17:19:36 -03:00
2011-11-13 01:43:21 -04:00
circle_angle += (circle_rate * dTnav);
//1° = 0.0174532925 radians
// wrap
if (circle_angle > 6.28318531)
circle_angle -= 6.28318531;
2011-11-20 04:21:19 -04:00
target_WP.lng = next_WP.lng + (g.loiter_radius * 100 * cos(1.57 - circle_angle) * scaleLongUp);
target_WP.lat = next_WP.lat + (g.loiter_radius * 100 * sin(1.57 - circle_angle));
2011-09-21 17:19:36 -03:00
// calc the lat and long error to the target
calc_location_error(&target_WP);
// use error as the desired rate towards the target
// nav_lon, nav_lat is calculated
2011-09-27 13:35:05 -03:00
calc_loiter(long_error, lat_error);
2011-09-21 17:19:36 -03:00
2011-11-13 01:43:21 -04:00
//CIRCLE: angle:29, dist:0, lat:400, lon:242
2011-09-24 21:40:29 -03:00
// rotate pitch and roll to the copter frame of reference
2011-09-27 13:35:05 -03:00
calc_loiter_pitch_roll();
2011-11-14 02:54:57 -04:00
//int angleTest = degrees(circle_angle);
//int nroll = nav_roll;
//int npitch = nav_pitch;
2011-11-13 01:43:21 -04:00
//Serial.printf("CIRCLE: angle:%d, dist:%d, X:%d, Y:%d, P:%d, R:%d \n", angleTest, (int)wp_distance , (int)long_error, (int)lat_error, npitch, nroll);
2011-05-23 02:53:00 -03:00
} else {
2011-09-04 21:15:36 -03:00
// use error as the desired rate towards the target
2011-09-27 13:35:05 -03:00
calc_nav_rate(g.waypoint_speed_max);
2011-09-24 21:40:29 -03:00
// rotate pitch and roll to the copter frame of reference
2011-09-27 13:35:05 -03:00
calc_nav_pitch_roll();
2011-05-23 02:53:00 -03:00
}
}
2011-09-04 21:15:36 -03:00
static void update_auto_yaw()
2011-05-23 02:53:00 -03:00
{
// this tracks a location so the copter is always pointing towards it.
if(yaw_tracking == MAV_ROI_LOCATION){
2011-09-04 21:15:36 -03:00
auto_yaw = get_bearing(¤t_loc, &target_WP);
2011-05-23 02:53:00 -03:00
}else if(yaw_tracking == MAV_ROI_WPNEXT){
2011-09-04 21:15:36 -03:00
auto_yaw = target_bearing;
2011-05-23 02:53:00 -03:00
}
2011-09-21 02:59:23 -03:00
// MAV_ROI_NONE = basic Yaw hold
2011-06-16 14:03:26 -03:00
}
2011-08-01 02:12:10 -03:00