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
2012-06-12 11:22:57 -03:00
#define THISFIRMWARE "ArduCopter V2.6 Epsilon"
2010-12-19 12:40:33 -04:00
/*
2012-06-12 11:22:57 -03:00
ArduCopter Version 2.6
2012-03-14 23:38:54 -03:00
Lead author: Jason Short
Based on code and ideas from the Arducopter team: Randy Mackay, Pat Hickey, Jose Julio, Jani Hirvinen, Andrew Tridgell, Justin Beech, Adam Rivera, Jean-Louis Naudin, Roberto Navoni
Thanks to: Chris Anderson, Mike Smith, Jordi Munoz, Doug Weibel, James Goppert, Benjamin Pelletier, Robert Lefebvre, Marco Robustini
2010-12-19 12:40:33 -04:00
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
2012-01-30 01:53:44 -04:00
Igor van Airde :Control Law optimization
Jean-Louis Naudin :Auto Landing
2012-02-17 02:07:00 -04:00
Sandro Benigno :Camera support
Olivier Adler :PPM Encoder
John Arne Birkeland :PPM Encoder
2012-03-10 19:20:54 -04:00
Adam M Rivera :Auto Compass Declination
2012-04-20 09:58:32 -03:00
Marco Robustini :Alpha testing
2012-06-10 16:38:08 -03:00
Angel Fernandez :Alpha testing
2012-04-20 09:58:32 -03:00
Robert Lefebvre :Heli Support & LEDs
2011-06-24 14:09:59 -03:00
And much more so PLEASE PM me on DIYDRONES to add your contribution to the List
2011-12-23 18:20:15 -04:00
Requires modified "mrelax" version of Arduino, which can be found here:
http://code.google.com/p/ardupilot-mega/downloads/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
2011-12-28 05:34:59 -04:00
#include <I2C.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-12-16 15:48:39 -04:00
#include <AP_PeriodicProcess.h> // Parent header of Timer
2011-11-12 23:47:16 -04:00
// (only included for makefile libpath to work)
#include <AP_TimerProcess.h> // TimerProcess is the scheduler for MPU6000 reads.
2012-03-11 05:36:12 -03:00
#include <AP_AHRS.h>
2011-09-04 21:15:36 -03:00
#include <APM_PI.h> // PI library
2012-01-29 02:00:05 -04:00
#include <AC_PID.h> // PID library
2011-02-19 22:03:01 -04:00
#include <RC_Channel.h> // RC Channel Library
2012-04-04 10:50:43 -03:00
#include <AP_Motors.h> // AP Motors library
#include <AP_MotorsQuad.h> // AP Motors library for Quad
#include <AP_MotorsTri.h> // AP Motors library for Tri
#include <AP_MotorsHexa.h> // AP Motors library for Hexa
#include <AP_MotorsY6.h> // AP Motors library for Y6
#include <AP_MotorsOcta.h> // AP Motors library for Octa
#include <AP_MotorsOctaQuad.h> // AP Motors library for OctaQuad
#include <AP_MotorsHeli.h> // AP Motors library for Heli
#include <AP_MotorsMatrix.h> // AP Motors library for Heli
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
2012-02-26 04:05:41 -04:00
#include <Filter.h> // Filter library
2012-02-26 02:35:14 -04:00
#include <ModeFilter.h> // Mode Filter from Filter library
2012-02-28 08:02:44 -04:00
#include <AverageFilter.h> // Mode Filter from Filter library
2011-10-02 09:16:31 -03:00
#include <AP_Relay.h> // APM relay
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"
2012-01-01 16:24:38 -04:00
#include "config_channels.h"
2010-12-19 12:40:33 -04:00
2012-04-24 09:17:56 -03:00
#include <GCS_MAVLink.h> // MAVLink GCS definitions
2010-12-19 12:40:33 -04:00
// 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
2012-03-11 07:46:41 -03:00
#include <AP_Declination.h> // ArduPilot Mega Declination Helper Library
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-30 00:35:08 -04:00
2011-12-09 02:47:59 -04:00
#if CONFIG_BARO == AP_BARO_BMP085
2011-12-13 02:05:48 -04:00
# if CONFIG_APM_HARDWARE == APM_HARDWARE_APM2
2011-12-09 02:47:59 -04:00
AP_Baro_BMP085 barometer(true);
# else
AP_Baro_BMP085 barometer(false);
# endif
#elif CONFIG_BARO == AP_BARO_MS5611
2011-11-30 00:35:08 -04:00
AP_Baro_MS5611 barometer;
2011-12-09 02:47:59 -04:00
#endif
2011-11-30 00:35:08 -04:00
2012-02-12 07:32:06 -04:00
AP_Compass_HMC5843 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
2012-04-21 08:15:55 -03:00
#if CONFIG_APM_HARDWARE == APM_HARDWARE_APM2
AP_OpticalFlow_ADNS3080_APM2 optflow(OPTFLOW_CS_PIN);
#else
AP_OpticalFlow_ADNS3080 optflow(OPTFLOW_CS_PIN);
#endif
2011-12-29 23:06:31 -04:00
#else
AP_OpticalFlow optflow;
2011-12-03 19:29:23 -04:00
#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
2012-02-12 07:27:51 -04:00
AP_IMU_INS imu(&ins);
2012-03-03 03:35:39 -04:00
2012-02-25 00:08:42 -04:00
// we don't want to use gps for yaw correction on ArduCopter, so pass
// a NULL GPS object pointer
static GPS *g_gps_null;
2012-03-03 03:35:39 -04:00
#if QUATERNION_ENABLE == ENABLED
2012-03-11 05:36:12 -03:00
AP_AHRS_Quaternion ahrs(&imu, g_gps_null);
2012-03-03 03:35:39 -04:00
#else
2012-03-11 05:36:12 -03:00
AP_AHRS_DCM ahrs(&imu, g_gps_null);
2012-03-03 03:35:39 -04:00
#endif
2011-11-13 02:15:45 -04:00
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;
2012-03-11 05:36:12 -03:00
AP_AHRS_DCM ahrs(&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;
2012-03-11 05:36:12 -03:00
AP_IMU_Shim imu; // never used
AP_AHRS_HIL ahrs(&imu, g_gps);
2011-02-25 01:33:39 -04:00
AP_GPS_HIL g_gps_driver(NULL);
AP_Compass_HIL compass; // never used
2012-01-20 04:32:25 -04:00
AP_Baro_BMP085_HIL barometer;
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-12-29 23:06:31 -04:00
AP_OpticalFlow_ADNS3080 optflow(OPTFLOW_CS_PIN);
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
////////////////////////////////////////////////////////////////////////////////
2012-02-12 07:27:51 -04:00
GCS_MAVLINK gcs0;
GCS_MAVLINK gcs3;
2011-03-26 03:35:52 -03:00
2011-06-16 14:03:26 -03:00
////////////////////////////////////////////////////////////////////////////////
// SONAR selection
////////////////////////////////////////////////////////////////////////////////
//
2012-02-28 08:02:44 -04:00
ModeFilterInt16_Size5 sonar_mode_filter(2);
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
////////////////////////////////////////////////////////////////////////////////
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",
2012-01-27 20:27:16 -04:00
"LAND",
"OF_LOITER"};
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
*/
2012-01-04 02:54:29 -04:00
//Documentation of GLobals:
////////////////////////////////////////////////////////////////////////////////
// The GPS based velocity calculated by offsetting the Latitude and Longitude
// updated after GPS read - 5-10hz
2011-11-07 18:24:32 -04:00
static int16_t x_actual_speed;
2011-12-14 01:15:45 -04:00
static int16_t y_actual_speed;
2012-01-04 02:54:29 -04:00
2012-03-12 03:17:41 -03:00
2012-01-04 02:54:29 -04:00
// The difference between the desired rate of travel and the actual rate of travel
// updated after GPS read - 5-10hz
2011-11-07 18:24:32 -04:00
static int16_t x_rate_error;
2011-12-14 01:15:45 -04:00
static int16_t y_rate_error;
2011-07-10 21:47:08 -03:00
2012-01-04 02:54:29 -04:00
////////////////////////////////////////////////////////////////////////////////
2010-12-19 12:40:33 -04:00
// Radio
2012-01-04 02:54:29 -04:00
////////////////////////////////////////////////////////////////////////////////
// This is the state of the flight control system
// There are multiple states defined such as STABILIZE, ACRO,
2012-01-11 03:31:38 -04:00
static int8_t control_mode = STABILIZE;
2012-01-04 02:54:29 -04:00
// This is the state of simple mode.
// Set in the control_mode.pde file when the control switch is read
2011-11-20 04:21:19 -04:00
static bool do_simple = false;
2012-01-04 02:54:29 -04:00
// Used to maintain the state of the previous control switch position
// This is set to -1 when we need to re-read the switch
static byte oldSwitchPosition;
////////////////////////////////////////////////////////////////////////////////
// Motor Output
////////////////////////////////////////////////////////////////////////////////
// This is the array of PWM values being sent to the motors
2012-04-04 10:50:43 -03:00
//static int16_t motor_out[11];
2012-01-04 02:54:29 -04:00
// This is the array of PWM values being sent to the motors that has been lightly filtered with a simple LPF
// This was added to try and deal with biger motors
2012-04-04 10:50:43 -03:00
//static int16_t motor_filtered[11];
2012-01-04 02:54:29 -04:00
2012-04-04 10:50:43 -03:00
#if FRAME_CONFIG == QUAD_FRAME
#define MOTOR_CLASS AP_MotorsQuad
#endif
#if FRAME_CONFIG == TRI_FRAME
#define MOTOR_CLASS AP_MotorsTri
#endif
#if FRAME_CONFIG == HEXA_FRAME
#define MOTOR_CLASS AP_MotorsHexa
#endif
#if FRAME_CONFIG == Y6_FRAME
#define MOTOR_CLASS AP_MotorsY6
#endif
#if FRAME_CONFIG == OCTA_FRAME
#define MOTOR_CLASS AP_MotorsOcta
#endif
#if FRAME_CONFIG == OCTA_QUAD_FRAME
#define MOTOR_CLASS AP_MotorsOctaQuad
#endif
#if FRAME_CONFIG == HELI_FRAME
#define MOTOR_CLASS AP_MotorsHeli
#endif
#if FRAME_CONFIG == HELI_FRAME // helicopter constructor requires more arguments
#if INSTANT_PWM == 1
2012-05-30 22:50:25 -03:00
MOTOR_CLASS motors(CONFIG_APM_HARDWARE, &APM_RC, &g.rc_1, &g.rc_2, &g.rc_3, &g.rc_4, &g.rc_8, &g.heli_servo_1, &g.heli_servo_2, &g.heli_servo_3, &g.heli_servo_4, AP_MOTORS_SPEED_INSTANT_PWM); // this hardware definition is slightly bad because it assumes APM_HARDWARE_APM2 == AP_MOTORS_APM2
2012-04-04 10:50:43 -03:00
#else
2012-05-30 22:50:25 -03:00
MOTOR_CLASS motors(CONFIG_APM_HARDWARE, &APM_RC, &g.rc_1, &g.rc_2, &g.rc_3, &g.rc_4, &g.rc_8, &g.heli_servo_1, &g.heli_servo_2, &g.heli_servo_3, &g.heli_servo_4);
2012-04-04 10:50:43 -03:00
#endif
2012-05-13 00:36:46 -03:00
#elif FRAME_CONFIG == TRI_FRAME // tri constructor requires additional rc_7 argument to allow tail servo reversing
#if INSTANT_PWM == 1
MOTOR_CLASS motors(CONFIG_APM_HARDWARE, &APM_RC, &g.rc_1, &g.rc_2, &g.rc_3, &g.rc_4, &g.rc_7, AP_MOTORS_SPEED_INSTANT_PWM); // this hardware definition is slightly bad because it assumes APM_HARDWARE_APM2 == AP_MOTORS_APM2
#else
MOTOR_CLASS motors(CONFIG_APM_HARDWARE, &APM_RC, &g.rc_1, &g.rc_2, &g.rc_3, &g.rc_4, &g.rc_7);
#endif
2012-04-04 10:50:43 -03:00
#else
#if INSTANT_PWM == 1
MOTOR_CLASS motors(CONFIG_APM_HARDWARE, &APM_RC, &g.rc_1, &g.rc_2, &g.rc_3, &g.rc_4, AP_MOTORS_SPEED_INSTANT_PWM); // this hardware definition is slightly bad because it assumes APM_HARDWARE_APM2 == AP_MOTORS_APM2
#else
MOTOR_CLASS motors(CONFIG_APM_HARDWARE, &APM_RC, &g.rc_1, &g.rc_2, &g.rc_3, &g.rc_4);
#endif
#endif
2011-11-20 04:21:19 -04:00
2012-01-04 02:54:29 -04:00
////////////////////////////////////////////////////////////////////////////////
// Mavlink/HIL control
////////////////////////////////////////////////////////////////////////////////
// Used to track the GCS based control input
// Allow override of RC channel values for HIL
2011-11-20 04:21:19 -04:00
static int16_t rc_override[8] = {0,0,0,0,0,0,0,0};
2012-01-04 02:54:29 -04:00
// Status flag that tracks whether we are under GCS control
2011-11-20 04:21:19 -04:00
static bool rc_override_active = false;
2012-01-04 02:54:29 -04:00
// Status flag that tracks whether we are under GCS control
2011-11-20 04:21:19 -04:00
static uint32_t rc_override_fs_timer = 0;
2012-01-04 02:54:29 -04:00
////////////////////////////////////////////////////////////////////////////////
2011-02-25 01:33:39 -04:00
// Failsafe
2012-01-04 02:54:29 -04:00
////////////////////////////////////////////////////////////////////////////////
// A status flag for the failsafe state
// did our throttle dip below the failsafe value?
static boolean failsafe;
2011-02-25 01:33:39 -04:00
2012-01-04 02:54:29 -04:00
////////////////////////////////////////////////////////////////////////////////
2011-02-17 03:09:13 -04:00
// PIDs
2012-01-04 02:54:29 -04:00
////////////////////////////////////////////////////////////////////////////////
// This is a convienience accessor for the IMU roll rates. It's currently the raw IMU rates
// and not the adjusted omega rates, but the name is stuck
2011-07-17 07:31:46 -03:00
static Vector3f omega;
2012-01-04 02:54:29 -04:00
// This is used to hold radio tuning values for in-flight CH6 tuning
2011-09-10 19:16:51 -03:00
float tuning_value;
2012-02-29 00:14:49 -04:00
// This will keep track of the percent of roll or pitch the user is applying
float roll_scale_d, pitch_scale_d;
2010-12-19 12:40:33 -04:00
2012-01-04 02:54:29 -04:00
////////////////////////////////////////////////////////////////////////////////
2011-02-17 03:09:13 -04:00
// LED output
2012-01-04 02:54:29 -04:00
////////////////////////////////////////////////////////////////////////////////
// status of LED based on the motor_armed variable
// Flashing indicates we are not armed
// Solid indicates Armed state
static boolean motor_light;
// Flashing indicates we are reading the GPS Strings
// Solid indicates we have full 3D lock and can navigate
static boolean GPS_light;
// This is current status for the LED lights state machine
// setting this value changes the output of the LEDs
2011-07-17 07:31:46 -03:00
static byte led_mode = NORMAL_LEDS;
2012-04-12 10:54:14 -03:00
// Blinking indicates GPS status
static byte copter_leds_GPS_blink = 0;
// Blinking indicates battery status
static byte copter_leds_motor_blink = 0;
2012-05-22 10:57:38 -03:00
// Navigation confirmation blinks
static int8_t copter_leds_nav_blink = 0;
2011-01-22 22:36:22 -04:00
2012-01-04 02:54:29 -04:00
////////////////////////////////////////////////////////////////////////////////
2010-12-19 12:40:33 -04:00
// GPS variables
2012-01-04 02:54:29 -04:00
////////////////////////////////////////////////////////////////////////////////
// This is used to scale GPS values for EEPROM storage
// 10^7 times Decimal GPS means 1 == 1cm
// This approximation makes calculations integer and it's easy to read
static const float t7 = 10000000.0;
// We use atan2 and other trig techniques to calaculate angles
// We need to scale the longitude up to make these calcs work
2012-01-15 19:11:02 -04:00
// to account for decreasing distance between lines of longitude away from the equator
2012-01-04 02:54:29 -04:00
static float scaleLongUp = 1;
// Sometimes we need to remove the scaling for distance calcs
static float scaleLongDown = 1;
2011-02-17 03:09:13 -04:00
2012-01-04 02:54:29 -04:00
////////////////////////////////////////////////////////////////////////////////
// Mavlink specific
////////////////////////////////////////////////////////////////////////////////
// Used by Mavlink for unknow reasons
2011-07-30 17:42:54 -03:00
static const float radius_of_earth = 6378100; // meters
2012-01-04 02:54:29 -04:00
// Used by Mavlink for unknow reasons
2011-07-30 17:42:54 -03:00
static const float gravity = 9.81; // meters/ sec^2
2011-09-04 21:15:36 -03:00
2012-04-21 19:17:09 -03:00
// Unions for getting byte values
union float_int{
int32_t int_value;
float float_value;
} float_int;
2011-07-17 07:31:46 -03:00
2012-01-04 02:54:29 -04:00
////////////////////////////////////////////////////////////////////////////////
// Location & Navigation
////////////////////////////////////////////////////////////////////////////////
// Status flag indicating we have data that can be used to navigate
// Set by a GPS read with 3D fix, or an optical flow read
static bool nav_ok;
// This is the angle from the copter to the "next_WP" location in degrees * 100
static int32_t target_bearing;
// This is the angle from the copter to the "next_WP" location
// with the addition of Crosstrack error in degrees * 100
static int32_t nav_bearing;
// Status of the Waypoint tracking mode. Options include:
// NO_NAV_MODE, WP_MODE, LOITER_MODE, CIRCLE_MODE
static byte wp_control;
// Register containing the index of the current navigation command in the mission script
static uint8_t command_nav_index;
// Register containing the index of the previous navigation command in the mission script
// Used to manage the execution of conditional commands
static uint8_t prev_nav_index;
// Register containing the index of the current conditional command in the mission script
static uint8_t command_cond_index;
// Used to track the required WP navigation information
// options include
// NAV_ALTITUDE - have we reached the desired altitude?
// NAV_LOCATION - have we reached the desired location?
// NAV_DELAY - have we waited at the waypoint the desired time?
static uint8_t wp_verify_byte; // used for tracking state of navigating waypoints
// used to limit the speed ramp up of WP navigation
// Acceleration is limited to .5m/s/s
static int16_t waypoint_speed_gov;
// Used to track how many cm we are from the "next_WP" location
static int32_t long_error, lat_error;
2012-01-29 02:00:05 -04:00
// Are we navigating while holding a positon? This is set to false once the speed drops below 1m/s
static boolean loiter_override;
2012-01-04 02:54:29 -04:00
2011-07-17 07:31:46 -03:00
2012-01-04 02:54:29 -04:00
////////////////////////////////////////////////////////////////////////////////
// Orientation
////////////////////////////////////////////////////////////////////////////////
// Convienience accessors for commonly used trig functions. These values are generated
// by the DCM through a few simple equations. They are used throughout the code where cos and sin
// would normally be used.
// The cos values are defaulted to 1 to get a decent initial value for a level state
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;
2012-02-23 19:44:55 -04:00
static float sin_yaw_y;
2011-09-04 21:15:36 -03:00
2012-01-04 02:54:29 -04:00
////////////////////////////////////////////////////////////////////////////////
// SIMPLE Mode
////////////////////////////////////////////////////////////////////////////////
// Used to track the orientation of the copter for Simple mode. This value is reset at each arming
// or in SuperSimple mode when the copter leaves a 20m radius from home.
static int32_t initial_simple_bearing;
2012-02-19 01:16:19 -04:00
////////////////////////////////////////////////////////////////////////////////
// ACRO Mode
////////////////////////////////////////////////////////////////////////////////
// Used to control Axis lock
int32_t roll_axis;
int32_t pitch_axis;
2012-03-09 03:13:04 -04:00
2012-02-28 09:56:26 -04:00
// Filters
AverageFilterInt32_Size3 roll_rate_d_filter; // filtered acceleration
AverageFilterInt32_Size3 pitch_rate_d_filter; // filtered pitch acceleration
2012-01-04 02:54:29 -04:00
2012-03-09 03:13:04 -04:00
// Barometer filter
AverageFilterInt32_Size5 baro_filter; // filtered pitch acceleration
2012-01-04 02:54:29 -04:00
////////////////////////////////////////////////////////////////////////////////
// Circle Mode / Loiter control
////////////////////////////////////////////////////////////////////////////////
// used to determin the desired location in Circle mode
// increments at circle_rate / second
2011-11-13 01:43:21 -04:00
static float circle_angle;
2012-01-04 02:54:29 -04:00
// used to control the speed of Circle mode
// units are in radians, default is 5° per second
2011-11-13 17:40:31 -04:00
static const float circle_rate = 0.0872664625;
2012-01-04 02:54:29 -04:00
// used to track the delat in Circle Mode
static int32_t old_target_bearing;
// deg : how many times to circle * 360 for Loiter/Circle Mission command
static int16_t loiter_total;
// deg : how far we have turned around a waypoint
static int16_t loiter_sum;
// How long we should stay in Loiter Mode for mission scripting
static uint16_t loiter_time_max;
// How long have we been loitering - The start time in millis
static uint32_t loiter_time;
// The synthetic location created to make the copter do circles around a WP
static struct Location circle_WP;
2011-11-13 01:43:21 -04:00
2012-01-04 02:54:29 -04:00
////////////////////////////////////////////////////////////////////////////////
// CH7 control
////////////////////////////////////////////////////////////////////////////////
// Used to enable Jose's flip code
// when true the Roll/Pitch/Throttle control is sent to the flip state machine
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
2012-01-04 02:54:29 -04:00
// Used to track the CH7 toggle state.
// When CH7 goes LOW PWM from HIGH PWM, this value will have been set true
// This allows advanced functionality to know when to execute
2011-10-29 01:29:10 -03:00
static boolean trim_flag;
2012-01-04 02:54:29 -04:00
// This register tracks the current Mission Command index when writing
// a mission using CH7 in flight
2011-11-13 01:43:21 -04:00
static int8_t CH7_wp_index;
2011-10-29 01:29:10 -03:00
2011-07-10 21:47:08 -03:00
2012-01-04 02:54:29 -04:00
////////////////////////////////////////////////////////////////////////////////
2011-02-25 01:33:39 -04:00
// Battery Sensors
2012-01-04 02:54:29 -04:00
////////////////////////////////////////////////////////////////////////////////
2012-01-15 20:10:02 -04:00
// Battery Voltage of battery, initialized above threshold for filter
static float battery_voltage1 = LOW_VOLTAGE * 1.05;
2012-01-04 02:54:29 -04:00
// refers to the instant amp draw – based on an Attopilot Current sensor
2012-01-15 20:10:02 -04:00
static float current_amps1;
2012-01-04 02:54:29 -04:00
// refers to the total amps drawn – based on an Attopilot Current sensor
2012-01-15 20:10:02 -04:00
static float current_total1;
2012-01-04 02:54:29 -04:00
// Used to track if the battery is low - LED output flashes when the batt is low
2011-07-30 17:42:54 -03:00
static bool low_batt = false;
2010-12-19 12:40:33 -04:00
2011-11-05 01:41:51 -03:00
2012-01-04 02:54:29 -04:00
////////////////////////////////////////////////////////////////////////////////
// Altitude
////////////////////////////////////////////////////////////////////////////////
// The pressure at home location - calibrated at arming
static int32_t ground_pressure;
// The ground temperature at home location - calibrated at arming
static int16_t ground_temperature;
2012-02-01 00:39:15 -04:00
// The cm we are off in altitude from next_WP.alt – Positive value means we are below the WP
2012-01-04 02:54:29 -04:00
static int32_t altitude_error;
2012-03-07 02:22:14 -04:00
// The cm/s we are moving up or down based on sensor data - Positive = UP
static int16_t climb_rate_actual;
// Used to dither our climb_rate over 50hz
static int16_t climb_rate_error;
// The cm/s we are moving up or down based on filtered data - Positive = UP
2012-01-04 02:54:29 -04:00
static int16_t climb_rate;
// The altitude as reported by Sonar in cm – Values are 20 to 700 generally.
2011-11-07 18:24:32 -04:00
static int16_t sonar_alt;
2012-01-04 02:54:29 -04:00
// The climb_rate as reported by sonar in cm/s
2011-11-07 18:24:32 -04:00
static int16_t sonar_rate;
2012-01-04 02:54:29 -04:00
// The altitude as reported by Baro in cm – Values can be quite high
2011-12-09 19:52:34 -04:00
static int32_t baro_alt;
2012-01-04 02:54:29 -04:00
// The climb_rate as reported by Baro in cm/s
2011-11-07 18:24:32 -04:00
static int16_t baro_rate;
2012-03-07 02:22:14 -04:00
// used to switch out of Manual Boost
2012-01-11 03:31:38 -04:00
static boolean reset_throttle_flag;
2012-03-07 02:22:14 -04:00
// used to track when to read sensors vs estimate alt
static boolean alt_sensor_flag;
2011-11-05 01:41:51 -03:00
2012-01-29 02:00:05 -04:00
2012-01-04 02:54:29 -04:00
////////////////////////////////////////////////////////////////////////////////
// flight modes
////////////////////////////////////////////////////////////////////////////////
// Flight modes are combinations of Roll/Pitch, Yaw and Throttle control modes
// Each Flight mode is a unique combination of these modes
//
// The current desired control scheme for Yaw
2011-09-04 21:15:36 -03:00
static byte yaw_mode;
2012-01-04 02:54:29 -04:00
// The current desired control scheme for roll and pitch / navigation
2011-09-04 21:15:36 -03:00
static byte roll_pitch_mode;
2012-01-04 02:54:29 -04:00
// The current desired control scheme for altitude hold
2011-09-04 21:15:36 -03:00
static byte throttle_mode;
2011-12-30 01:35:01 -04:00
2012-01-04 02:54:29 -04:00
////////////////////////////////////////////////////////////////////////////////
// flight specific
////////////////////////////////////////////////////////////////////////////////
// Flag for monitoring the status of flight
// We must be in the air with throttle for 5 seconds before this flag is true
// This flag is reset when we are in a manual throttle mode with 0 throttle or disarmed
static boolean takeoff_complete;
// Used to see if we have landed and if we should shut our engines - not fully implemented
static boolean land_complete = true;
// used to manually override throttle in interactive Alt hold modes
static int16_t manual_boost;
// An additional throttle added to keep the copter at the same altitude when banking
static int16_t angle_boost;
2012-01-13 02:28:51 -04:00
// Push copter down for clean landing
2012-01-20 14:06:46 -04:00
static int16_t landing_boost;
2012-01-29 02:00:05 -04:00
// for controlling the landing throttle curve
//verifies landings
static int16_t ground_detector;
2012-01-04 02:54:29 -04:00
////////////////////////////////////////////////////////////////////////////////
// Navigation general
////////////////////////////////////////////////////////////////////////////////
// The location of the copter in relation to home, updated every GPS read
static int32_t home_to_copter_bearing;
2012-01-22 02:04:35 -04:00
// distance between plane and home in cm
2012-01-04 02:54:29 -04:00
static int32_t home_distance;
2012-01-22 02:04:35 -04:00
// distance between plane and next_WP in cm
2012-01-04 02:54:29 -04:00
static int32_t wp_distance;
////////////////////////////////////////////////////////////////////////////////
// 3D Location vectors
////////////////////////////////////////////////////////////////////////////////
// home location is stored when we have a good GPS lock and arm the copter
// Can be reset each the copter is re-armed
static struct Location home;
// Flag for if we have g_gps lock and have set the home location
static boolean home_is_set;
// Current location of the copter
static struct Location current_loc;
// Next WP is the desired location of the copter - the next waypoint or loiter location
static struct Location next_WP;
// Prev WP is used to get the optimum path from one WP to the next
static struct Location prev_WP;
// Holds the current loaded command from the EEPROM for navigation
static struct Location command_nav_queue;
// Holds the current loaded command from the EEPROM for conditional scripts
static struct Location command_cond_queue;
// Holds the current loaded command from the EEPROM for guided mode
static struct Location guided_WP;
////////////////////////////////////////////////////////////////////////////////
// Crosstrack
////////////////////////////////////////////////////////////////////////////////
// deg * 100, The original angle to the next_WP when the next_WP was set
// Also used to check when we pass a WP
static int32_t original_target_bearing;
// The amount of angle correction applied to target_bearing to bring the copter back on its optimum path
2011-12-05 00:51:34 -04:00
static int16_t crosstrack_error;
2011-11-07 18:24:32 -04:00
2012-01-04 02:54:29 -04:00
////////////////////////////////////////////////////////////////////////////////
// Navigation Roll/Pitch functions
////////////////////////////////////////////////////////////////////////////////
// all angles are deg * 100 : target yaw angle
// The Commanded ROll from the autopilot.
static int32_t nav_roll;
// The Commanded pitch from the autopilot. negative Pitch means go forward.
static int32_t nav_pitch;
// The desired bank towards North (Positive) or South (Negative)
2012-02-15 13:09:52 -04:00
static int32_t auto_roll;
static int32_t auto_pitch;
2012-01-04 02:54:29 -04:00
// Don't be fooled by the fact that Pitch is reversed from Roll in its sign!
static int16_t nav_lat;
// The desired bank towards East (Positive) or West (Negative)
static int16_t nav_lon;
2012-01-09 00:53:54 -04:00
// The Commanded ROll from the autopilot based on optical flow sensor.
2012-02-19 17:11:59 -04:00
static int32_t of_roll;
2012-01-09 00:53:54 -04:00
// The Commanded pitch from the autopilot based on optical flow sensor. negative Pitch means go forward.
2012-02-19 17:11:59 -04:00
static int32_t of_pitch;
static bool slow_wp = false;
2012-01-09 00:53:54 -04:00
2012-01-04 02:54:29 -04:00
////////////////////////////////////////////////////////////////////////////////
// Navigation Throttle control
////////////////////////////////////////////////////////////////////////////////
// The Commanded Throttle from the autopilot.
static int16_t nav_throttle; // 0-1000 for throttle control
// This is a simple counter to track the amount of throttle used during flight
// This could be useful later in determining and debuging current usage and predicting battery life
static uint32_t throttle_integrator;
2011-07-17 07:31:46 -03:00
2012-01-11 03:31:38 -04:00
////////////////////////////////////////////////////////////////////////////////
// Climb rate control
////////////////////////////////////////////////////////////////////////////////
// Time when we intiated command in millis - used for controlling decent rate
// The orginal altitude used to base our new altitude during decent
static int32_t original_altitude;
// Used to track the altitude offset for climbrate control
static int32_t target_altitude;
static uint32_t alt_change_timer;
static int8_t alt_change_flag;
static uint32_t alt_change;
2012-01-04 02:54:29 -04:00
////////////////////////////////////////////////////////////////////////////////
// Navigation Yaw control
////////////////////////////////////////////////////////////////////////////////
// The Commanded Yaw from the autopilot.
static int32_t nav_yaw;
// A speed governer for Yaw control - limits the rate the quad can be turned by the autopilot
static int32_t auto_yaw;
// Used to manage the Yaw hold capabilities -
// Options include: no tracking, point at next wp, or at a target
static byte yaw_tracking = MAV_ROI_WPNEXT;
// In AP Mission scripting we have a fine level of control for Yaw
// This is our initial angle for relative Yaw movements
static int32_t command_yaw_start;
// Timer values used to control the speed of Yaw movements
static uint32_t command_yaw_start_time;
2011-11-07 18:24:32 -04:00
static uint16_t command_yaw_time; // how long we are turning
static int32_t command_yaw_end; // what angle are we trying to be
2012-01-04 02:54:29 -04:00
// how many degrees will we turn
static int32_t command_yaw_delta;
// Deg/s we should turn
static int16_t command_yaw_speed;
// Direction we will turn – 1 = CW, 0 or -1 = CCW
2011-07-30 17:42:54 -03:00
static byte command_yaw_dir;
2012-01-04 02:54:29 -04:00
// Direction we will turn – 1 = relative, 0 = Absolute
2011-07-30 17:42:54 -03:00
static byte command_yaw_relative;
2012-01-04 02:54:29 -04:00
// Yaw will point at this location if yaw_tracking is set to MAV_ROI_LOCATION
static struct Location target_WP;
2011-07-17 07:31:46 -03:00
2011-06-16 14:03:26 -03:00
2012-01-04 02:54:29 -04:00
////////////////////////////////////////////////////////////////////////////////
// Repeat Mission Scripting Command
////////////////////////////////////////////////////////////////////////////////
// The type of repeating event - Toggle a servo channel, Toggle the APM1 relay, etc
static byte event_id;
// Used to manage the timimng of repeating events
static uint32_t event_timer;
// How long to delay the next firing of event in millis
static uint16_t event_delay;
// how many times to fire : 0 = forever, 1 = do once, 2 = do twice
static int16_t event_repeat;
// per command value, such as PWM for servos
static int16_t event_value;
// the stored value used to undo commands - such as original PWM command
static int16_t event_undo_value;
////////////////////////////////////////////////////////////////////////////////
// Delay Mission Scripting Command
////////////////////////////////////////////////////////////////////////////////
2012-01-10 00:23:37 -04:00
static int32_t condition_value; // used in condition commands (eg delay, change alt, etc.)
static uint32_t condition_start;
2010-12-19 12:40:33 -04:00
2011-04-20 02:37:05 -03:00
2012-01-04 02:54:29 -04:00
////////////////////////////////////////////////////////////////////////////////
2010-12-19 12:40:33 -04:00
// IMU variables
2012-01-04 02:54:29 -04:00
////////////////////////////////////////////////////////////////////////////////
// Integration time for the gyros (DCM algorithm)
2012-05-17 14:55:13 -03:00
// Updated with the fast loop
2012-01-04 02:54:29 -04:00
static float G_Dt = 0.02;
2010-12-19 12:40:33 -04:00
2012-01-04 02:54:29 -04:00
////////////////////////////////////////////////////////////////////////////////
2010-12-19 12:40:33 -04:00
// Performance monitoring
2012-01-04 02:54:29 -04:00
////////////////////////////////////////////////////////////////////////////////
// Used to manage the rate of performance logging messages
static int16_t perf_mon_counter;
// The number of GPS fixes we have had
2011-11-07 18:24:32 -04:00
static int16_t gps_fix_count;
2012-05-17 14:55:13 -03:00
// gps_watchdog checks for bad reads and if we miss 12 in a row, we stop navigating
2012-01-04 02:54:29 -04:00
// by lowering nav_lat and navlon to 0 gradually
2011-09-17 16:23:16 -03:00
static byte gps_watchdog;
2010-12-19 12:40:33 -04:00
// System Timers
// --------------
2012-01-04 02:54:29 -04:00
// Time in microseconds of main control loop
static uint32_t fast_loopTimer;
// Time in microseconds of 50hz control loop
2011-11-07 18:24:32 -04:00
static uint32_t fiftyhz_loopTimer;
2012-01-04 02:54:29 -04:00
// Counters for branching from 10 hz control loop
static byte medium_loopCounter;
// Counters for branching from 3 1/3hz control loop
2011-07-17 07:31:46 -03:00
static byte slow_loopCounter;
2012-01-04 02:54:29 -04:00
// Counters for branching at 1 hz
static byte counter_one_herz;
// Stat machine counter for Simple Mode
static byte simple_counter;
// used to track the elapsed time between GPS reads
static uint32_t nav_loopTimer;
// Delta Time in milliseconds for navigation computations, updated with every good GPS read
static float dTnav;
// Counters for branching from 4 minute control loop used to save Compass offsets
2011-11-07 18:24:32 -04:00
static int16_t superslow_loopCounter;
2012-04-16 12:07:57 -03:00
// Loiter timer - Records how long we have been in loiter
static uint32_t loiter_timer;
2012-01-20 14:06:46 -04:00
// disarms the copter while in Acro or Stabilize mode after 30 seconds of no flight
2012-01-11 03:31:38 -04:00
static uint8_t auto_disarming_counter;
2012-05-18 13:54:18 -03:00
// prevents duplicate GPS messages from entering system
static uint32_t last_gps_time;
2010-12-19 12:40:33 -04:00
2012-01-04 02:54:29 -04:00
// Tracks if GPS is enabled based on statup routine
// If we do not detect GPS at startup, we stop trying and assume GPS is not connected
2011-09-04 21:15:36 -03:00
static bool GPS_enabled = false;
2012-01-04 02:54:29 -04:00
// Set true if we have new PWM data to act on from the Radio
2011-09-16 03:33:00 -03:00
static bool new_radio_frame;
2012-01-04 02:54:29 -04:00
// Used to auto exit the in-flight leveler
2012-01-11 03:31:38 -04:00
static int16_t auto_level_counter;
2011-03-09 02:37:09 -04:00
2012-01-04 02:54:29 -04:00
// Reference to the AP relay object - APM1 only
2011-10-02 09:52:02 -03:00
AP_Relay relay;
2012-01-04 02:54:29 -04:00
// APM2 only
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
2012-01-04 02:54:29 -04:00
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()
{
2012-01-04 21:31:06 -04:00
uint32_t timer = micros();
2012-03-03 03:35:39 -04:00
2011-05-02 01:34:15 -03:00
// We want this to execute fast
// ----------------------------
2012-03-07 00:10:08 -04:00
if ((timer - fast_loopTimer) >= 10000 && imu.new_data_available()) {
2012-02-17 19:20:39 -04:00
//Log_Write_Data(13, (int32_t)(timer - fast_loopTimer));
2012-02-17 02:07:00 -04:00
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();
2012-05-17 14:55:13 -03:00
} else {
#ifdef DESKTOP_BUILD
usleep(1000);
#endif
2010-12-19 12:40:33 -04:00
}
2011-12-29 14:28:01 -04:00
// port manipulation for external timing of main loops
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) {
2011-12-29 14:28:01 -04:00
// store the micros for the 50 hz timer
2011-09-20 02:24:57 -03:00
fiftyhz_loopTimer = timer;
2011-12-29 14:28:01 -04:00
// port manipulation for external timing of main loops
2011-06-26 03:38:11 -03:00
//PORTK |= B01000000;
// reads all of the necessary trig functions for cameras, throttle, etc.
2011-12-29 14:28:01 -04:00
// --------------------------------------------------------------------
2011-06-26 03:38:11 -03:00
update_trig();
2011-02-25 01:33:39 -04:00
2012-01-13 16:47:22 -04:00
// check for new GPS messages
// --------------------------
2012-05-17 14:55:13 -03:00
update_GPS();
2012-01-13 16:47:22 -04:00
2011-09-25 16:21:54 -03:00
// perform 10hz tasks
2011-12-23 18:20:15 -04:00
// ------------------
2010-12-19 12:40:33 -04:00
medium_loop();
2011-02-17 05:36:33 -04:00
2011-12-23 18:20:15 -04:00
// Stuff to run at full 50hz, but after the med 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
2012-01-11 03:31:38 -04:00
// trgger our 1 hz loop
2011-12-29 14:28:01 -04:00
if(counter_one_herz >= 50){
2011-02-25 01:33:39 -04:00
super_slow_loop();
2011-03-20 09:55:51 -03:00
counter_one_herz = 0;
2011-02-25 01:33:39 -04:00
}
2012-01-04 02:54:29 -04:00
perf_mon_counter++;
if (perf_mon_counter > 600 ) {
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;
2012-01-13 16:47:22 -04:00
perf_mon_counter = 0;
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
2012-05-17 14:55:13 -03:00
// Main loop - 100hz
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
2012-05-17 14:55:13 -03:00
// --------------------
2011-09-19 18:02:42 -03:00
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
2011-12-29 14:28:01 -04:00
// agmatthews - USERHOOKS
#ifdef USERHOOK_FASTLOOP
USERHOOK_FASTLOOP
#endif
2011-10-15 19:29:33 -03:00
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-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){
2011-12-28 05:35:14 -04:00
if (compass.read()) {
2012-03-03 03:35:39 -04:00
// Calculate heading
2012-03-11 05:36:12 -03:00
Matrix3f m = ahrs.get_dcm_matrix();
2012-03-07 00:11:19 -04:00
compass.calculate(m);
2012-03-27 01:16:27 -03:00
compass.null_offsets();
2011-12-28 05:35:14 -04:00
}
2011-05-09 21:00:05 -03:00
}
#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++;
2012-05-17 14:55:13 -03:00
// calculate the copter's desired bearing and WP distance
// ------------------------------------------------------
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
2012-05-17 14:55:13 -03:00
// calculate distance, angles to target
navigate();
// update flight control system
update_navigation();
// update log
if (g.log_bitmask & MASK_LOG_NTUN && motors.armed()){
Log_Write_Nav_Tuning();
2011-09-20 02:24:57 -03:00
}
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
// --------------------------
2012-03-07 02:22:14 -04:00
//#if HIL_MODE != HIL_MODE_ATTITUDE // don't execute in HIL mode
//update_altitude();
//#endif
alt_sensor_flag = 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
2012-04-04 10:50:43 -03:00
if(motors.armed()){
2011-11-07 16:53:41 -04:00
if (g.log_bitmask & MASK_LOG_ATTITUDE_MED)
Log_Write_Attitude();
2011-05-09 21:00:05 -03:00
2012-03-07 02:22:14 -04:00
if (g.log_bitmask & MASK_LOG_MOTORS)
Log_Write_Motors();
2011-11-07 16:53:41 -04:00
}
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
2012-03-09 03:13:04 -04:00
// Do an extra baro read for Temp sensing
// ---------------------------------------
2011-11-07 02:46:39 -04:00
#if HIL_MODE != HIL_MODE_ATTITUDE
2011-11-30 00:37:50 -04: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
2012-04-12 10:54:14 -03:00
#if COPTER_LEDS == ENABLED
update_copter_leds();
#endif
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
{
2012-03-07 02:22:14 -04:00
// read altitude sensors or estimate altitude
// ------------------------------------------
update_altitude_est();
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
2012-05-17 14:55:13 -03:00
if(g.sonar_enabled){
sonar_alt = sonar.read();
}
2011-11-12 23:47:16 -04:00
#endif
2012-01-09 00:53:54 -04:00
// syncronise optical flow reads with altitude reads
#ifdef OPTFLOW_ENABLED
2012-05-17 14:55:13 -03:00
if(g.optflow_enabled){
update_optical_flow();
}
2012-01-09 00:53:54 -04:00
#endif
2012-05-17 14:55:13 -03:00
2011-10-15 19:29:33 -03:00
#ifdef USERHOOK_50HZLOOP
2012-05-17 14:55:13 -03:00
USERHOOK_50HZLOOP
2011-10-15 19:29:33 -03:00
#endif
2011-08-13 23:30:37 -03:00
2012-05-17 14:55:13 -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
2011-05-09 21:00:05 -03:00
# if HIL_MODE == HIL_MODE_DISABLED
2012-04-04 10:50:43 -03:00
if (g.log_bitmask & MASK_LOG_ATTITUDE_FAST && motors.armed())
2011-05-09 21:00:05 -03:00
Log_Write_Attitude();
2010-12-19 12:40:33 -04:00
2012-04-04 10:50:43 -03:00
if (g.log_bitmask & MASK_LOG_RAW && motors.armed())
2011-03-15 02:54:48 -03:00
Log_Write_Raw();
#endif
2011-02-17 05:36:33 -04:00
2012-05-17 14:55:13 -03:00
camera_stabilization();
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();
2012-04-01 22:18:42 -03:00
gcs_data_stream_send();
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
}
2012-04-04 10:50:43 -03:00
// check the user hasn't updated the frame orientation
if( !motors.armed() ) {
motors.set_frame_orientation(g.frame_orientation);
}
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();
2011-12-29 14:28:01 -04:00
// agmatthews - USERHOOKS
#ifdef USERHOOK_SLOWLOOP
USERHOOK_SLOWLOOP
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-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-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;
}
}
2012-05-29 16:41:35 -03:00
#define AUTO_DISARMING_DELAY 25
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
{
2012-04-04 10:50:43 -03:00
if (g.log_bitmask & MASK_LOG_CUR && motors.armed())
2011-02-25 01:33:39 -04:00
Log_Write_Current();
2011-03-09 02:37:09 -04:00
2012-05-29 16:41:35 -03:00
// this function disarms the copter if it has been sitting on the ground for any moment of time greater than 25 seconds
2012-01-11 03:31:38 -04:00
// but only of the control mode is manual
if((control_mode <= ACRO) && (g.rc_3.control_in == 0)){
auto_disarming_counter++;
2012-03-07 02:22:14 -04:00
2012-05-29 16:41:35 -03:00
if(auto_disarming_counter == AUTO_DISARMING_DELAY){
2012-01-11 03:31:38 -04:00
init_disarm_motors();
2012-05-29 16:41:35 -03:00
}else if (auto_disarming_counter > AUTO_DISARMING_DELAY){
auto_disarming_counter = AUTO_DISARMING_DELAY + 1;
2012-01-11 03:31:38 -04:00
}
}else{
auto_disarming_counter = 0;
}
2012-03-07 02:22:14 -04:00
2011-10-11 06:12:37 -03:00
gcs_send_message(MSG_HEARTBEAT);
2012-03-07 02:22:14 -04:00
2011-10-15 19:29:33 -03:00
// agmatthews - USERHOOKS
#ifdef USERHOOK_SUPERSLOWLOOP
USERHOOK_SUPERSLOWLOOP
#endif
2011-12-16 00:50:52 -04:00
/*
2012-05-17 14:55:13 -03:00
//Serial.printf("alt %d, next.alt %d, alt_err: %d, cruise: %d, Alt_I:%1.2f, wp_dist %d, tar_bear %d, home_d %d, homebear %d\n",
2011-12-16 00:50:52 -04:00
current_loc.alt,
next_WP.alt,
altitude_error,
g.throttle_cruise.get(),
g.pi_alt_hold.get_integrator(),
wp_distance,
target_bearing,
home_distance,
home_to_copter_bearing);
*/
2011-02-25 01:33:39 -04:00
}
2011-12-23 18:20:15 -04:00
// updated at 10 Hz
#ifdef OPTFLOW_ENABLED
static void update_optical_flow(void)
{
2012-01-09 00:53:54 -04:00
static int log_counter = 0;
2011-12-29 23:06:31 -04:00
optflow.update();
2012-03-11 05:36:12 -03:00
optflow.update_position(ahrs.roll, ahrs.pitch, cos_yaw_x, sin_yaw_y, current_loc.alt); // updates internal lon and lat with estimation based on optical flow
2011-12-23 18:20:15 -04:00
// write to log
2012-01-09 00:53:54 -04:00
log_counter++;
if( log_counter >= 5 ) {
log_counter = 0;
if (g.log_bitmask & MASK_LOG_OPTFLOW){
Log_Write_Optflow();
}
2011-12-23 18:20:15 -04:00
}
2012-01-09 00:53:54 -04:00
/*if(g.optflow_enabled && current_loc.alt < 500){
2011-12-23 18:20:15 -04:00
if(GPS_enabled){
// if we have a GPS, we add some detail to the GPS
// XXX this may not ne right
current_loc.lng += optflow.vlon;
current_loc.lat += optflow.vlat;
// some sort of error correction routine
//current_loc.lng -= ERR_GAIN * (float)(current_loc.lng - x_GPS_speed); // error correction
//current_loc.lng -= ERR_GAIN * (float)(current_loc.lng - x_GPS_speed); // error correction
}else{
// if we do not have a GPS, use relative from 0 for lat and lon
current_loc.lng = optflow.vlon;
current_loc.lat = optflow.vlat;
}
// OK to run the nav routines
nav_ok = true;
2012-01-09 00:53:54 -04:00
}*/
2011-12-23 18:20:15 -04:00
}
#endif
2012-05-21 13:58:14 -03:00
// called at 50hz
2011-07-17 07:31:46 -03:00
static void update_GPS(void)
2010-12-19 12:40:33 -04:00
{
2012-01-04 02:54:29 -04:00
// A counter that is used to grab at least 10 reads before commiting the Home location
static byte ground_start_count = 10;
2012-06-03 11:48:45 -03:00
// return immediately if GPS is not enabled
if( !GPS_enabled ) {
return;
}
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
2012-01-14 15:20:33 -04:00
if(gps_watchdog < 30){
2011-09-17 16:23:16 -03:00
gps_watchdog++;
}else{
2012-01-04 02:54:29 -04:00
// after 12 reads we guess we may have lost GPS signal, stop navigating
2011-09-17 16:23:16 -03:00
// we have lost GPS signal for a moment. Reduce our error to avoid flyaways
2012-02-15 13:09:52 -04:00
auto_roll >>= 1;
auto_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) {
2012-01-13 02:59:47 -04:00
// clear new data flag
2012-05-18 13:54:18 -03:00
g_gps->new_data = false;
2012-01-13 02:59:47 -04:00
2012-05-18 13:54:18 -03:00
// check for duiplicate GPS messages
if(last_gps_time != g_gps->time){
2011-03-09 02:37:09 -04:00
2012-05-18 13:54:18 -03:00
// look for broken GPS
// ---------------
gps_watchdog = 0;
2011-11-14 20:43:30 -04:00
2012-05-18 13:54:18 -03:00
// OK to run the nav routines
// ---------------
nav_ok = true;
2011-02-17 05:36:33 -04:00
2012-05-18 13:54:18 -03:00
// for performance monitoring
// --------------------------
gps_fix_count++;
2011-11-14 20:43:30 -04:00
2012-05-18 13:54:18 -03:00
// used to calculate speed in X and Y, iterms
// ------------------------------------------
dTnav = (float)(millis() - nav_loopTimer)/ 1000.0;
nav_loopTimer = millis();
2011-11-14 20:43:30 -04:00
2012-05-18 13:54:18 -03:00
// prevent runup from bad GPS
// --------------------------
dTnav = min(dTnav, 1.0);
2011-02-17 05:36:33 -04:00
2012-05-18 13:54:18 -03:00
if(ground_start_count > 1){
ground_start_count--;
2011-02-17 05:36:33 -04:00
2012-05-18 13:54:18 -03:00
} else if (ground_start_count == 1) {
2011-02-17 05:36:33 -04:00
2012-05-18 13:54:18 -03: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;
}else{
if (g.compass_enabled) {
// Set compass declination automatically
compass.set_initial_location(g_gps->latitude, g_gps->longitude);
}
// save home to eeprom (we must have a good fix to have reached this point)
init_home();
ground_start_count = 0;
2012-03-10 19:20:54 -04:00
}
2010-12-19 12:40:33 -04:00
}
2012-05-18 13:54:18 -03:00
current_loc.lng = g_gps->longitude; // Lon * 10 * *7
current_loc.lat = g_gps->latitude; // Lat * 10 * *7
calc_XY_velocity();
2011-05-31 02:29:06 -03:00
2012-05-18 13:54:18 -03:00
if (g.log_bitmask & MASK_LOG_GPS && motors.armed()){
Log_Write_GPS();
}
2012-05-17 14:55:13 -03:00
2012-05-18 13:54:18 -03:00
#if HIL_MODE == HIL_MODE_ATTITUDE // only execute in HIL mode
//update_altitude();
alt_sensor_flag = true;
#endif
2011-05-31 02:29:06 -03:00
}
2011-11-14 02:54:57 -04:00
2012-05-18 13:54:18 -03:00
// save GPS time so we don't get duplicate reads
last_gps_time = g_gps->time;
2011-11-14 20:43:30 -04:00
}
2010-12-19 12:40:33 -04:00
}
2011-02-17 05:36:33 -04:00
2011-09-04 21:15:36 -03:00
void update_yaw_mode(void)
{
switch(yaw_mode){
case YAW_ACRO:
2012-02-11 02:22:03 -04:00
g.rc_4.servo_out = get_acro_yaw(g.rc_4.control_in);
2011-09-04 21:15:36 -03:00
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:
2012-01-21 02:51:52 -04:00
nav_yaw += constrain(wrap_180(auto_yaw - nav_yaw), -20, 20); // 40 deg a second
2012-02-19 18:21:22 -04:00
//Serial.printf("nav_yaw %d ", nav_yaw);
2011-09-04 21:15:36 -03:00
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
{
2012-01-04 02:54:29 -04:00
int control_roll, control_pitch;
// hack to do auto_flip - need to remove, no one is using.
2011-09-21 03:20:33 -03:00
#if CH7_OPTION == CH7_FLIP
2011-08-05 13:19:06 -03:00
if (do_flip){
2012-06-04 02:15:04 -03:00
if(g.rc_1.control_in == 0){
roll_flip();
return;
}else{
do_flip = false;
}
2011-08-05 13:19:06 -03:00
}
#endif
2011-09-04 21:15:36 -03:00
switch(roll_pitch_mode){
case ROLL_PITCH_ACRO:
2012-02-19 01:16:19 -04:00
if(g.axis_enabled){
roll_axis += (float)g.rc_1.control_in * g.axis_lock_p;
pitch_axis += (float)g.rc_2.control_in * g.axis_lock_p;
roll_axis = wrap_360(roll_axis);
pitch_axis = wrap_360(pitch_axis);
// in this mode, nav_roll and nav_pitch = the iterm
g.rc_1.servo_out = get_stabilize_roll(roll_axis);
g.rc_2.servo_out = get_stabilize_pitch(pitch_axis);
if (g.rc_3.control_in == 0){
roll_axis = 0;
pitch_axis = 0;
}
}else{
// ACRO does not get SIMPLE mode ability
g.rc_1.servo_out = get_acro_roll(g.rc_1.control_in);
g.rc_2.servo_out = get_acro_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:
2012-01-04 02:54:29 -04:00
// apply SIMPLE mode transform
if(do_simple && new_radio_frame){
update_simple_mode();
}
2012-01-08 02:21:31 -04:00
2012-01-13 02:28:51 -04:00
// in this mode, nav_roll and nav_pitch = the iterm
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:
2012-01-04 02:54:29 -04:00
// apply SIMPLE mode transform
if(do_simple && new_radio_frame){
update_simple_mode();
}
2011-09-04 21:15:36 -03:00
// mix in user control with Nav control
2012-02-17 02:07:00 -04:00
nav_roll += constrain(wrap_180(auto_roll - nav_roll), -g.auto_slew_rate.get(), g.auto_slew_rate.get()); // 40 deg a second
2012-02-15 13:09:52 -04:00
nav_pitch += constrain(wrap_180(auto_pitch - nav_pitch), -g.auto_slew_rate.get(), g.auto_slew_rate.get()); // 40 deg a second
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;
2012-01-09 00:53:54 -04:00
case ROLL_PITCH_STABLE_OF:
// apply SIMPLE mode transform
if(do_simple && new_radio_frame){
update_simple_mode();
}
2012-01-27 20:27:16 -04:00
// mix in user control with optical flow
2012-01-09 00:53:54 -04:00
g.rc_1.servo_out = get_stabilize_roll(get_of_roll(g.rc_1.control_in));
g.rc_2.servo_out = get_stabilize_pitch(get_of_pitch(g.rc_2.control_in));
break;
2011-09-04 21:15:36 -03:00
}
2012-01-04 02:54:29 -04:00
2012-02-11 02:50:56 -04:00
if(g.rc_3.control_in == 0 && roll_pitch_mode <= ROLL_PITCH_ACRO){
reset_rate_I();
reset_stability_I();
}
if(takeoff_complete == false){
// reset these I terms to prevent awkward tipping on takeoff
//reset_rate_I();
//reset_stability_I();
}
2012-02-29 00:14:49 -04:00
if(new_radio_frame){
// clear new radio frame info
new_radio_frame = false;
// These values can be used to scale the PID gains
// This allows for a simple gain scheduling implementation
roll_scale_d = g.stabilize_d_schedule * (float)abs(g.rc_1.control_in);
roll_scale_d = (1 - (roll_scale_d / 4500.0));
roll_scale_d = constrain(roll_scale_d, 0, 1) * g.stabilize_d;
pitch_scale_d = g.stabilize_d_schedule * (float)abs(g.rc_2.control_in);
pitch_scale_d = (1 - (pitch_scale_d / 4500.0));
pitch_scale_d = constrain(pitch_scale_d, 0, 1) * g.stabilize_d;
}
2012-01-04 02:54:29 -04:00
}
// new radio frame is used to make sure we only call this at 50hz
void update_simple_mode(void)
{
2012-01-14 15:32:48 -04:00
static float simple_sin_y=0, simple_cos_x=0;
2012-01-04 02:54:29 -04:00
// used to manage state machine
// which improves speed of function
simple_counter++;
2012-03-11 05:36:12 -03:00
int delta = wrap_360(ahrs.yaw_sensor - initial_simple_bearing)/100;
2012-01-04 02:54:29 -04:00
if (simple_counter == 1){
// roll
simple_cos_x = sin(radians(90 - delta));
}else if (simple_counter > 2){
// pitch
simple_sin_y = cos(radians(90 - delta));
simple_counter = 0;
}
// Rotate input by the initial bearing
int control_roll = g.rc_1.control_in * simple_cos_x + g.rc_2.control_in * simple_sin_y;
int control_pitch = -(g.rc_1.control_in * simple_sin_y - g.rc_2.control_in * simple_cos_x);
g.rc_1.control_in = control_roll;
g.rc_2.control_in = control_pitch;
2011-09-04 21:15:36 -03:00
}
2011-07-10 21:47:08 -03:00
2012-02-25 17:21:48 -04:00
#define THROTTLE_FILTER_SIZE 2
2011-12-23 18:20:15 -04:00
2012-05-17 14:55:13 -03:00
// 50 hz update rate
2011-12-29 14:28:01 -04:00
// controls all throttle behavior
2011-09-04 21:15:36 -03:00
void update_throttle_mode(void)
{
2011-12-23 18:20:15 -04:00
int16_t throttle_out;
2011-09-22 02:31:12 -03:00
2012-01-14 15:43:52 -04:00
#if AUTO_THROTTLE_HOLD != 0
2012-03-23 11:48:24 -03:00
static float throttle_avg = 0; // this is initialised to g.throttle_cruise later
2012-01-14 15:43:52 -04:00
#endif
2011-12-23 18:20:15 -04:00
switch(throttle_mode){
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
2012-01-04 02:54:29 -04:00
if (control_mode == ACRO){
g.rc_3.servo_out = g.rc_3.control_in;
}else{
angle_boost = get_angle_boost(g.rc_3.control_in);
g.rc_3.servo_out = g.rc_3.control_in + angle_boost;
}
2011-10-29 05:27:43 -03:00
#endif
2012-01-04 02:54:29 -04:00
2012-01-14 15:43:52 -04:00
#if AUTO_THROTTLE_HOLD != 0
2012-03-23 11:48:24 -03:00
// ensure throttle_avg has been initialised
if( throttle_avg == 0 ) {
throttle_avg = g.throttle_cruise;
}
2011-12-16 00:50:52 -04:00
// calc average throttle
2012-01-14 15:43:52 -04:00
if ((g.rc_3.control_in > MINIMUM_THROTTLE) && abs(climb_rate) < 60){
throttle_avg = throttle_avg * .98 + (float)g.rc_3.control_in * .02;
g.throttle_cruise = throttle_avg;
2011-12-16 00:50:52 -04:00
}
2012-01-14 15:43:52 -04:00
#endif
2011-12-29 14:28:01 -04:00
2012-05-29 15:19:28 -03:00
if (takeoff_complete == false && motors.armed()){
2012-05-29 15:28:40 -03:00
if (g.rc_3.control_in > g.throttle_cruise){
2012-05-29 15:19:28 -03:00
// we must be in the air by now
takeoff_complete = true;
}
2011-12-29 14:28:01 -04:00
}
2012-05-29 15:19:28 -03:00
2011-09-16 20:54:45 -03:00
}else{
2011-12-29 14:28:01 -04:00
// make sure we also request 0 throttle out
// so the props stop ... properly
// ----------------------------------------
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();
2011-12-29 14:28:01 -04:00
2011-09-04 21:15:36 -03:00
// fall through
2010-12-19 12:40:33 -04:00
2011-09-04 21:15:36 -03:00
case THROTTLE_AUTO:
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){
#if FRAME_CONFIG == HELI_FRAME
2011-12-23 18:20:15 -04:00
throttle_out = heli_get_angle_boost(g.throttle_cruise + manual_boost);
2011-11-06 06:25:13 -04:00
#else
2011-12-23 18:20:15 -04:00
throttle_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
2012-01-11 03:31:38 -04:00
//force a reset of the altitude change
clear_new_altitude();
2011-11-20 16:49:00 -04:00
2011-12-29 14:28:01 -04:00
/*
2012-01-11 03:31:38 -04:00
int16_t iterm = g.pi_alt_hold.get_integrator();
Serial.printf("tar_alt: %d, actual_alt: %d \talt_err: %d, \t manb: %d, iterm %d\n",
2011-12-29 14:28:01 -04:00
next_WP.alt,
current_loc.alt,
altitude_error,
2012-01-11 03:31:38 -04:00
manual_boost,
iterm);
2011-12-29 14:28:01 -04:00
//*/
2012-02-19 16:40:51 -04:00
// this lets us know we need to update the altitude after manual throttle control
2012-01-11 03:31:38 -04:00
reset_throttle_flag = true;
2011-12-29 14:28:01 -04:00
2011-11-06 06:25:13 -04:00
}else{
2012-02-19 16:40:51 -04:00
// we are under automatic throttle control
// ---------------------------------------
2012-01-11 03:31:38 -04:00
if(reset_throttle_flag) {
2012-03-10 16:37:54 -04:00
force_new_altitude(max(current_loc.alt, 100));
2012-01-11 03:31:38 -04:00
reset_throttle_flag = false;
2012-03-07 02:22:14 -04:00
update_throttle_cruise();
2012-01-11 03:31:38 -04:00
}
2012-04-04 10:50:43 -03:00
// 10hz, don't run up i term
2012-05-17 14:55:13 -03:00
if(motors.auto_armed() == true){
2011-11-20 09:30:42 -04:00
// how far off are we
altitude_error = get_altitude_error();
2012-01-29 02:00:05 -04:00
// get the AP throttle
nav_throttle = get_nav_throttle(altitude_error);
2011-11-20 09:30:42 -04:00
2011-12-23 18:20:15 -04:00
/*
2012-01-11 03:31:38 -04:00
Serial.printf("tar_alt: %d, actual_alt: %d \talt_err: %d, \tnav_thr: %d, \talt Int: %d\n",
2011-12-23 18:20:15 -04:00
next_WP.alt,
current_loc.alt,
altitude_error,
nav_throttle,
2012-01-11 03:31:38 -04:00
(int16_t)g.pi_alt_hold.get_integrator());
//*/
2012-03-07 02:22:14 -04:00
2011-11-20 09:30:42 -04:00
}
2011-11-20 16:49:00 -04:00
2012-01-29 02:00:05 -04:00
// hack to remove the influence of the ground effect
2012-03-16 17:53:20 -03:00
if(g.sonar_enabled && current_loc.alt < 100 && landing_boost != 0) {
nav_throttle = min(nav_throttle, 0);
2012-01-29 02:00:05 -04:00
}
2011-11-06 06:25:13 -04:00
#if FRAME_CONFIG == HELI_FRAME
2012-01-13 02:28:51 -04:00
throttle_out = heli_get_angle_boost(g.throttle_cruise + nav_throttle + get_z_damping() - landing_boost);
2011-11-06 06:25:13 -04:00
#else
2012-01-13 02:28:51 -04:00
throttle_out = g.throttle_cruise + nav_throttle + angle_boost + get_z_damping() - landing_boost;
2011-11-06 06:25:13 -04:00
#endif
}
2011-12-23 18:20:15 -04:00
// light filter of output
2012-02-25 17:21:48 -04:00
//g.rc_3.servo_out = (g.rc_3.servo_out * (THROTTLE_FILTER_SIZE - 1) + throttle_out) / THROTTLE_FILTER_SIZE;
// no filter
g.rc_3.servo_out = throttle_out;
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
{
2012-02-19 17:11:59 -04:00
// wp_distance is in CM
// --------------------
2011-05-23 02:53:00 -03:00
switch(control_mode){
case AUTO:
// note: wp_control is handled by commands_logic
2011-12-23 18:20:15 -04:00
verify_commands();
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:
2012-01-11 03:31:38 -04:00
// We have reached Home
2011-10-06 21:42:09 -03:00
if((wp_distance <= g.waypoint_radius) || check_missed_wp()){
2012-04-16 12:07:57 -03:00
// if loiter_timer value > 0, we are set to trigger auto_land or approach after 20 seconds
2011-10-07 15:06:31 -03:00
set_mode(LOITER);
2012-05-30 15:06:41 -03:00
// force loitering above home
next_WP.lat = home.lat;
next_WP.lng = home.lng;
2012-05-24 02:00:49 -03:00
if(g.rtl_land_enabled || failsafe)
2012-04-16 12:07:57 -03:00
loiter_timer = millis();
2012-02-15 13:09:52 -04:00
else
2012-04-16 12:07:57 -03:00
loiter_timer = 0;
2012-01-11 03:31:38 -04:00
break;
}
2011-12-09 19:52:34 -04:00
2012-01-29 02:00:05 -04:00
wp_control = WP_MODE;
2012-02-19 17:11:59 -04:00
slow_wp = true;
2012-01-08 02:21:31 -04:00
2012-01-29 02:00:05 -04:00
// calculates desired Yaw
#if FRAME_CONFIG == HELI_FRAME
update_auto_yaw();
#endif
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-29 14:28:01 -04:00
// This feature allows us to reposition the quad when the user lets
// go of the sticks
2012-01-29 02:00:05 -04:00
2011-12-29 14:28:01 -04:00
if((abs(g.rc_2.control_in) + abs(g.rc_1.control_in)) > 500){
2012-01-29 02:00:05 -04:00
loiter_override = true;
}
// Allow the user to take control temporarily,
if(loiter_override){
2011-12-29 14:28:01 -04:00
// this sets the copter to not try and nav while we control it
2012-01-29 20:14:56 -04:00
wp_control = NO_NAV_MODE;
2011-12-29 14:28:01 -04:00
// reset LOITER to current position
2012-01-29 20:14:56 -04:00
next_WP.lat = current_loc.lat;
next_WP.lng = current_loc.lng;
2011-12-29 14:28:01 -04:00
2012-03-18 03:16:04 -03:00
if( g.rc_2.control_in == 0 && g.rc_1.control_in == 0 ){
2012-01-29 20:14:56 -04:00
loiter_override = false;
wp_control = LOITER_MODE;
2012-01-29 02:00:05 -04:00
}
2011-12-29 14:28:01 -04:00
}else{
wp_control = LOITER_MODE;
}
2011-12-15 02:03:47 -04:00
2012-04-16 18:29:54 -03:00
if(loiter_timer != 0){
// If we have a safe approach alt set and we have been loitering for 20 seconds(default), begin approach
if(g.rtl_approach_alt >= 1 && (millis() - loiter_timer) > (RTL_APPROACH_DELAY * 1000)){
// just to make sure we clear the timer
loiter_timer = 0;
set_mode(APPROACH);
}
// Kick us out of loiter and begin landing if the loiter_timer is set
else if((millis() - loiter_timer) > (uint32_t)g.auto_land_timeout.get()){
// just to make sure we clear the timer
loiter_timer = 0;
set_mode(LAND);
}
2012-01-06 01:15:03 -04:00
}
2011-12-15 02:03:47 -04:00
// calculates the desired Roll and Pitch
update_nav_wp();
break;
2011-12-09 19:52:34 -04:00
case LAND:
2012-01-29 02:00:05 -04:00
if(g.sonar_enabled)
verify_land_sonar();
else
verify_land_baro();
2012-01-08 02:21:31 -04:00
2011-07-16 19:12:52 -03:00
// calculates the desired Roll and Pitch
2012-04-15 18:54:43 -03:00
update_nav_wp();
break;
case APPROACH:
// calculates the desired Roll and Pitch
2011-07-16 19:12:52 -03:00
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;
2011-12-31 14:54:31 -04:00
case STABILIZE:
2012-01-04 02:54:29 -04:00
wp_control = NO_NAV_MODE;
update_nav_wp();
2011-12-31 14:54:31 -04: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?
2011-12-16 00:50:52 -04:00
if(do_simple && g.super_simple){
2011-12-09 19:52:34 -04:00
// get distance to home
2012-01-22 02:04:35 -04:00
if(home_distance > 1000){ // 10m from home
2011-12-09 19:52:34 -04:00
// 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
2012-03-11 05:36:12 -03:00
// update hil before ahrs update
2011-10-11 06:12:37 -03:00
gcs_update();
2011-05-09 21:00:05 -03:00
#endif
2012-03-11 05:36:12 -03:00
ahrs.update();
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;
2012-03-11 05:36:12 -03:00
Matrix3f temp = ahrs.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();
2012-02-23 19:44:55 -04:00
cos_pitch_x = safe_sqrt(1 - (temp.c.x * temp.c.x)); // level = 1
cos_roll_x = temp.c.z / cos_pitch_x; // level = 1
2011-02-17 05:36:33 -04:00
2012-02-23 19:44:55 -04:00
cos_pitch_x = constrain(cos_pitch_x, 0, 1.0);
// this relies on constrain() of infinity doing the right thing,
// which it does do in avr-libc
cos_roll_x = constrain(cos_roll_x, -1.0, 1.0);
2011-07-10 21:47:08 -03:00
2011-12-23 18:20:15 -04:00
sin_yaw_y = yawvector.x; // 1y = north
cos_yaw_x = yawvector.y; // 0x = north
2011-09-16 03:33:00 -03:00
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
{
2012-01-29 02:00:05 -04:00
static int16_t old_sonar_alt = 0;
static int32_t old_baro_alt = 0;
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
2012-03-09 03:13:04 -04:00
baro_alt = read_barometer();
//Serial.printf("baro_alt: %d \n", baro_alt);
2011-11-19 17:58:41 -04:00
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;
2012-03-14 01:31:16 -03:00
baro_rate = constrain(baro_rate, -300, 300);
2011-11-19 17:58:41 -04:00
old_baro_alt = baro_alt;
2011-11-13 01:43:21 -04:00
2012-01-29 02:00:05 -04:00
// Note: sonar_alt is calculated in a faster loop and filtered with a mode filter
2011-11-13 01:43:21 -04:00
#endif
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
2012-01-13 02:28:51 -04:00
// positive = going up.
2011-11-13 01:43:21 -04:00
sonar_rate = (sonar_alt - old_sonar_alt) * 10;
2012-03-14 01:31:16 -03:00
sonar_rate = constrain(sonar_rate, -150, 150);
2011-11-13 01:43:21 -04:00
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
2012-03-16 17:32:18 -03:00
scale = (float)(sonar_alt - 400) / 200.0;
scale = constrain(scale, 0.0, 1.0);
2012-01-11 03:31:38 -04:00
// solve for a blended altitude
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
2012-03-07 02:22:14 -04:00
climb_rate_actual = ((float)sonar_rate * (1.0 - scale)) + (float)baro_rate * scale;
2011-11-05 01:41:51 -03:00
2011-06-30 21:19:10 -03:00
}else{
2011-12-23 18:20:15 -04:00
// we must be higher than sonar (>800), don't get tricked by bad sonar reads
2011-11-13 01:43:21 -04:00
current_loc.alt = baro_alt + home.alt; // home alt = 0
// dont blend, go straight baro
2012-03-07 02:22:14 -04:00
climb_rate_actual = 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;
2012-03-07 02:22:14 -04:00
climb_rate_actual = baro_rate;
2011-02-20 19:09:28 -04:00
}
2011-11-28 14:31:49 -04:00
2012-03-07 02:22:14 -04:00
// update the target altitude
next_WP.alt = get_new_altitude();
// calc error
climb_rate_error = (climb_rate_actual - climb_rate) / 5;
}
2012-01-29 02:00:05 -04:00
2012-03-07 02:22:14 -04:00
static void update_altitude_est()
{
if(alt_sensor_flag){
update_altitude();
alt_sensor_flag = false;
2012-01-11 03:31:38 -04:00
2012-04-04 10:50:43 -03:00
if(g.log_bitmask & MASK_LOG_CTUN && motors.armed()){
2012-03-07 02:22:14 -04:00
Log_Write_Control_Tuning();
}
2012-01-29 02:00:05 -04:00
2012-03-07 02:22:14 -04:00
}else{
// simple dithering of climb rate
climb_rate += climb_rate_error;
current_loc.alt += (climb_rate / 50);
}
//Serial.printf(" %d, %d, %d, %d\n", climb_rate_actual, climb_rate_error, climb_rate, current_loc.alt);
2011-03-26 03:35:52 -03:00
}
2012-03-10 16:37:28 -04:00
#define THROTTLE_ADJUST 225
2011-07-17 07:31:46 -03:00
static void
2011-04-10 20:07:24 -03:00
adjust_altitude()
{
2012-03-06 02:12:53 -04:00
if(g.rc_3.control_in <= (MINIMUM_THROTTLE + THROTTLE_ADJUST)){
2011-11-01 13:24:51 -03:00
// we remove 0 to 100 PWM from hover
2012-03-06 02:12:53 -04:00
manual_boost = (g.rc_3.control_in - MINIMUM_THROTTLE) - THROTTLE_ADJUST;
manual_boost = max(-THROTTLE_ADJUST, manual_boost);
2012-01-23 18:10:03 -04:00
2012-03-06 02:12:53 -04:00
}else if (g.rc_3.control_in >= (MAXIMUM_THROTTLE - THROTTLE_ADJUST)){
2011-11-01 13:24:51 -03:00
// we add 0 to 100 PWM to hover
2012-03-06 02:12:53 -04:00
manual_boost = g.rc_3.control_in - (MAXIMUM_THROTTLE - THROTTLE_ADJUST);
manual_boost = min(THROTTLE_ADJUST, manual_boost);
2011-11-01 13:24:51 -03:00
}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;
2012-02-15 13:01:34 -04:00
g.rc_6.set_range(g.radio_tuning_low,g.radio_tuning_high); // 0 to 1
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
2012-02-17 19:23:36 -04:00
case CH6_RATE_KD:
g.pid_rate_roll.kD(tuning_value);
g.pid_rate_pitch.kD(tuning_value);
2011-12-29 14:28:01 -04:00
break;
2011-09-10 19:16:51 -03:00
case CH6_STABILIZE_KP:
2012-01-31 00:53:25 -04:00
g.pi_stabilize_roll.kP(tuning_value);
g.pi_stabilize_pitch.kP(tuning_value);
2011-09-10 19:16:51 -03:00
break;
2011-06-16 14:03:26 -03:00
2011-09-10 19:16:51 -03:00
case CH6_STABILIZE_KI:
g.pi_stabilize_roll.kI(tuning_value);
g.pi_stabilize_pitch.kI(tuning_value);
break;
2011-06-16 14:03:26 -03:00
2012-04-21 11:24:57 -03:00
case CH6_DAMP:
2012-03-29 09:09:41 -03:00
case CH6_STABILIZE_KD:
g.stabilize_d = tuning_value;
break;
2012-03-28 11:58:58 -03:00
case CH6_ACRO_KP:
g.acro_p = tuning_value;
break;
2011-09-10 19:16:51 -03:00
case CH6_RATE_KP:
2012-01-29 02:00:05 -04:00
g.pid_rate_roll.kP(tuning_value);
g.pid_rate_pitch.kP(tuning_value);
2011-09-10 19:16:51 -03:00
break;
2011-06-16 14:03:26 -03:00
2011-09-10 19:16:51 -03:00
case CH6_RATE_KI:
2012-01-29 02:00:05 -04:00
g.pid_rate_roll.kI(tuning_value);
g.pid_rate_pitch.kI(tuning_value);
2011-09-10 19:16:51 -03:00
break;
2011-06-16 14:03:26 -03:00
2011-09-10 19:16:51 -03:00
case CH6_YAW_KP:
g.pi_stabilize_yaw.kP(tuning_value);
break;
2011-06-16 14:03:26 -03:00
2012-03-28 11:58:58 -03:00
case CH6_YAW_KI:
g.pi_stabilize_yaw.kI(tuning_value);
break;
2011-09-10 19:16:51 -03:00
case CH6_YAW_RATE_KP:
2012-01-29 02:00:05 -04:00
g.pid_rate_yaw.kP(tuning_value);
2011-09-10 19:16:51 -03:00
break;
2011-09-04 21:15:36 -03:00
2012-03-28 11:58:58 -03:00
case CH6_YAW_RATE_KD:
g.pid_rate_yaw.kD(tuning_value);
break;
2011-09-10 19:16:51 -03:00
case CH6_THROTTLE_KP:
2012-01-29 02:00:05 -04:00
g.pid_throttle.kP(tuning_value);
2011-09-10 19:16:51 -03:00
break;
2011-09-04 21:15:36 -03:00
2011-09-10 19:16:51 -03:00
case CH6_TOP_BOTTOM_RATIO:
2012-04-04 10:50:43 -03:00
motors.top_bottom_ratio = tuning_value;
2011-09-10 19:16:51 -03:00
break;
2011-04-21 20:07:31 -03:00
2011-09-10 19:16:51 -03:00
case CH6_RELAY:
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.waypoint_speed_max = g.rc_6.control_in;
break;
2011-09-10 22:37:28 -03:00
2012-03-28 11:58:58 -03:00
case CH6_LOITER_KP:
2011-09-27 13:39:32 -03:00
g.pi_loiter_lat.kP(tuning_value);
g.pi_loiter_lon.kP(tuning_value);
break;
2012-03-28 11:58:58 -03:00
case CH6_LOITER_KI:
g.pi_loiter_lat.kI(tuning_value);
g.pi_loiter_lon.kI(tuning_value);
break;
case CH6_NAV_KP:
2012-01-29 02:00:05 -04:00
g.pid_nav_lat.kP(tuning_value);
g.pid_nav_lon.kP(tuning_value);
2011-09-10 22:37:28 -03:00
break;
2011-11-01 13:24:51 -03:00
2012-03-28 11:58:58 -03:00
case CH6_LOITER_RATE_KP:
2012-02-26 15:15:13 -04:00
g.pid_loiter_rate_lon.kP(tuning_value);
g.pid_loiter_rate_lat.kP(tuning_value);
break;
2012-03-28 11:58:58 -03:00
case CH6_LOITER_RATE_KI:
g.pid_loiter_rate_lon.kI(tuning_value);
g.pid_loiter_rate_lat.kI(tuning_value);
break;
case CH6_LOITER_RATE_KD:
2012-03-10 18:17:57 -04:00
g.pid_loiter_rate_lon.kD(tuning_value);
g.pid_loiter_rate_lat.kD(tuning_value);
break;
2012-01-31 00:53:25 -04:00
case CH6_NAV_I:
g.pid_nav_lat.kI(tuning_value);
g.pid_nav_lon.kI(tuning_value);
break;
2011-10-29 08:34:21 -03:00
#if FRAME_CONFIG == HELI_FRAME
case CH6_HELI_EXTERNAL_GYRO:
2012-04-04 10:50:43 -03:00
motors.ext_gyro_gain = tuning_value;
2011-10-29 08:34:21 -03:00
break;
#endif
2011-11-20 16:49:00 -04:00
2011-11-20 09:30:42 -04:00
case CH6_THR_HOLD_KP:
g.pi_alt_hold.kP(tuning_value);
break;
2012-01-09 00:53:54 -04:00
case CH6_OPTFLOW_KP:
2012-01-29 02:00:05 -04:00
g.pid_optflow_roll.kP(tuning_value);
g.pid_optflow_pitch.kP(tuning_value);
2012-01-09 00:53:54 -04:00
break;
case CH6_OPTFLOW_KI:
2012-01-29 02:00:05 -04:00
g.pid_optflow_roll.kI(tuning_value);
g.pid_optflow_pitch.kI(tuning_value);
2012-01-09 00:53:54 -04:00
break;
2012-01-25 08:55:14 -04:00
case CH6_OPTFLOW_KD:
2012-01-29 02:00:05 -04:00
g.pid_optflow_roll.kD(tuning_value);
g.pid_optflow_pitch.kD(tuning_value);
2012-01-25 08:55:14 -04:00
break;
2012-04-16 11:47:57 -03:00
case CH6_AHRS_YAW_KP:
ahrs._kp_yaw.set(tuning_value);
break;
2011-09-10 19:16:51 -03:00
}
2011-05-09 14:40:32 -03:00
}
2012-01-13 02:28:51 -04:00
// Outputs Nav_Pitch and Nav_Roll
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
2012-01-21 15:57:21 -04:00
// calc error to 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;
2012-05-27 13:21:20 -03:00
next_WP.lng = circle_WP.lng + (g.loiter_radius * 100 * cos(1.57 - circle_angle) * scaleLongUp);
next_WP.lat = circle_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
2012-01-04 02:54:29 -04:00
calc_location_error(&circle_WP);
2011-09-21 17:19:36 -03:00
// 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();
2012-01-04 02:54:29 -04:00
// debug
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-12-15 02:03:47 -04:00
}else if(wp_control == WP_MODE){
2012-01-21 15:57:21 -04:00
// calc error to target
calc_location_error(&next_WP);
2012-02-19 17:11:59 -04:00
int16_t speed = calc_desired_speed(g.waypoint_speed_max, slow_wp);
2011-09-04 21:15:36 -03:00
// use error as the desired rate towards the target
2012-01-04 02:54:29 -04:00
calc_nav_rate(speed);
2012-05-17 14:55:13 -03:00
2011-09-24 21:40:29 -03:00
// rotate pitch and roll to the copter frame of reference
2011-12-31 03:19:11 -04:00
calc_loiter_pitch_roll();
2011-12-15 02:03:47 -04:00
}else if(wp_control == NO_NAV_MODE){
2012-01-13 02:28:51 -04:00
// clear out our nav so we can do things like land straight down
2012-01-29 02:00:05 -04:00
// or change Loiter position
2012-01-13 02:28:51 -04:00
2012-01-29 02:00:05 -04:00
// We bring copy over our Iterms for wind control, but we don't navigate
2012-03-12 02:43:25 -03:00
nav_lon = g.pid_loiter_rate_lon.get_integrator();
nav_lat = g.pid_loiter_rate_lon.get_integrator();
2012-01-13 02:28:51 -04:00
2012-03-18 18:50:02 -03:00
nav_lon = constrain(nav_lon, -2000, 2000); // 20°
nav_lat = constrain(nav_lat, -2000, 2000); // 20°
2012-01-13 02:28:51 -04:00
// rotate pitch and roll to the copter frame of reference
calc_loiter_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
{
2011-12-31 03:19:11 -04:00
// If we Loiter, don't start Yawing, allow Yaw control
if(wp_control == LOITER_MODE)
return;
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-12-31 03:19:11 -04:00
// Point towards next WP
2011-09-04 21:15:36 -03:00
auto_yaw = target_bearing;
2011-05-23 02:53:00 -03:00
}
2012-02-19 18:21:22 -04:00
//Serial.printf("auto_yaw %d ", auto_yaw);
2011-09-21 02:59:23 -03:00
// MAV_ROI_NONE = basic Yaw hold
2012-02-12 07:27:51 -04:00
}