ardupilot/ArduCopter/ArduCopter.pde

1239 lines
45 KiB
Plaintext
Raw Normal View History

/// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
2015-05-23 02:57:33 -03:00
#define THISFIRMWARE "APM:Copter V3.4-dev"
/*
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* ArduCopter Version 3.0
* Creator: Jason Short
* Lead Developer: Randy Mackay
2013-11-15 23:10:38 -04:00
* Lead Tester: Marco Robustini
* Based on code and ideas from the Arducopter team: Leonard Hall, Andrew Tridgell, Robert Lefebvre, Pat Hickey, Michael Oborne, Jani Hirvinen,
Olivier Adler, Kevin Hester, Arthur Benemann, Jonathan Challinger, John Arne Birkeland,
Jean-Louis Naudin, Mike Smith, and more
* Thanks to: Chris Anderson, Jordi Munoz, Jason Short, Doug Weibel, Jose Julio
2012-08-21 23:19:50 -03:00
*
2013-11-15 23:10:38 -04:00
* Special Thanks to contributors (in alphabetical order by first name):
2012-08-21 23:19:50 -03:00
*
* Adam M Rivera :Auto Compass Declination
* Amilcar Lucas :Camera mount library
* Andrew Tridgell :General development, Mavlink Support
* Angel Fernandez :Alpha testing
2013-11-15 23:10:38 -04:00
* AndreasAntonopoulous:GeoFence
* Arthur Benemann :DroidPlanner GCS
* Benjamin Pelletier :Libraries
* Bill King :Single Copter
* Christof Schmid :Alpha testing
2013-11-15 23:10:38 -04:00
* Craig Elder :Release Management, Support
* Dani Saez :V Octo Support
* Doug Weibel :DCM, Libraries, Control law advice
* Emile Castelnuovo :VRBrain port, bug fixes
* Gregory Fletcher :Camera mount orientation math
* Guntars :Arming safety suggestion
* HappyKillmore :Mavlink GCS
2013-11-15 23:10:38 -04:00
* Hein Hollander :Octo Support, Heli Testing
2012-08-21 23:19:50 -03:00
* Igor van Airde :Control Law optimization
* Jack Dunkle :Alpha testing
* James Goppert :Mavlink Support
* Jani Hiriven :Testing feedback
2013-11-15 23:10:38 -04:00
* Jean-Louis Naudin :Auto Landing
2012-08-21 23:19:50 -03:00
* John Arne Birkeland :PPM Encoder
* Jose Julio :Stabilization Control laws, MPU6k driver
2014-07-11 02:08:09 -03:00
* Julien Dubois :PosHold flight mode
2013-11-15 23:10:38 -04:00
* Julian Oes :Pixhawk
* Jonathan Challinger :Inertial Navigation, CompassMot, Spin-When-Armed
* Kevin Hester :Andropilot GCS
* Max Levine :Tri Support, Graphics
* Leonard Hall :Flight Dynamics, Throttle, Loiter and Navigation Controllers
* Marco Robustini :Lead tester
* Michael Oborne :Mission Planner GCS
* Mike Smith :Pixhawk driver, coding support
2013-11-15 23:10:38 -04:00
* Olivier Adler :PPM Encoder, piezo buzzer
* Pat Hickey :Hardware Abstraction Layer (HAL)
* Robert Lefebvre :Heli Support, Copter LEDs
2013-11-15 23:10:38 -04:00
* Roberto Navoni :Library testing, Porting to VRBrain
* Sandro Benigno :Camera support, MinimOSD
2014-07-11 02:08:09 -03:00
* Sandro Tognana :PosHold flight mode
2013-11-15 23:10:38 -04:00
* ..and many more.
2012-08-21 23:19:50 -03:00
*
2013-11-15 23:10:38 -04:00
* Code commit statistics can be found here: https://github.com/diydrones/ardupilot/graphs/contributors
* Wiki: http://copter.ardupilot.com/
* Requires modified version of Arduino, which can be found here: http://ardupilot.com/downloads/?category=6
2012-08-21 23:19:50 -03:00
*
*/
////////////////////////////////////////////////////////////////////////////////
// Header includes
////////////////////////////////////////////////////////////////////////////////
2012-12-12 21:25:09 -04:00
#include <math.h>
#include <stdio.h>
#include <stdarg.h>
// Common dependencies
#include <AP_Common.h>
#include <AP_Progmem.h>
#include <AP_Menu.h>
#include <AP_Param.h>
#include <StorageManager.h>
// AP_HAL
#include <AP_HAL.h>
#include <AP_HAL_AVR.h>
2015-05-04 03:18:46 -03:00
#include <AP_HAL_SITL.h>
2013-01-02 07:08:40 -04:00
#include <AP_HAL_PX4.h>
#include <AP_HAL_VRBRAIN.h>
#include <AP_HAL_FLYMAPLE.h>
2013-09-28 08:46:14 -03:00
#include <AP_HAL_Linux.h>
2012-12-18 06:15:11 -04:00
#include <AP_HAL_Empty.h>
2012-12-12 21:25:09 -04:00
// Application dependencies
2014-01-16 03:35:43 -04:00
#include <GCS.h>
#include <GCS_MAVLink.h> // MAVLink GCS definitions
#include <AP_SerialManager.h> // Serial manager library
2012-11-10 01:29:56 -04:00
#include <AP_GPS.h> // ArduPilot GPS library
#include <DataFlash.h> // ArduPilot Mega Flash Memory Library
#include <AP_ADC.h> // ArduPilot Mega Analog to Digital Converter Library
2012-12-12 20:16:32 -04:00
#include <AP_ADC_AnalogSource.h>
#include <AP_Baro.h>
2012-11-10 01:29:56 -04:00
#include <AP_Compass.h> // ArduPilot Mega Magnetometer Library
#include <AP_Math.h> // ArduPilot Mega Vector/Matrix math Library
#include <AP_Curve.h> // Curve used to linearlise throttle pwm to thrust
#include <AP_InertialSensor.h> // ArduPilot Mega Inertial Sensor (accel & gyro) Library
#include <AP_AHRS.h>
#include <AP_NavEKF.h>
2014-02-27 21:16:54 -04:00
#include <AP_Mission.h> // Mission command library
2014-04-06 22:18:43 -03:00
#include <AP_Rally.h> // Rally point library
2012-11-10 01:29:56 -04:00
#include <AC_PID.h> // PID library
#include <AC_PI_2D.h> // PID library (2-axis)
#include <AC_HELI_PID.h> // Heli specific Rate PID library
#include <AC_P.h> // P library
2014-01-29 09:51:08 -04:00
#include <AC_AttitudeControl.h> // Attitude control library
#include <AC_AttitudeControl_Heli.h> // Attitude control library for traditional helicopter
#include <AC_PosControl.h> // Position control library
2012-11-10 01:29:56 -04:00
#include <RC_Channel.h> // RC Channel Library
2012-08-21 23:19:50 -03:00
#include <AP_Motors.h> // AP Motors library
#include <AP_RangeFinder.h> // Range finder library
2012-11-10 01:29:56 -04:00
#include <AP_OpticalFlow.h> // Optical Flow library
#include <Filter.h> // Filter library
#include <AP_Buffer.h> // APM FIFO Buffer
2012-08-21 23:19:50 -03:00
#include <AP_Relay.h> // APM relay
#include <AP_ServoRelayEvents.h>
2012-08-21 23:19:50 -03:00
#include <AP_Camera.h> // Photo or video camera
#include <AP_Mount.h> // Camera/Antenna mount
2012-11-10 01:29:56 -04:00
#include <AP_Airspeed.h> // needed for AHRS build
2013-09-12 22:47:03 -03:00
#include <AP_Vehicle.h> // needed for AHRS build
#include <AP_InertialNav.h> // ArduPilot Mega inertial navigation library
2013-03-20 10:29:08 -03:00
#include <AC_WPNav.h> // ArduCopter waypoint navigation library
2014-01-27 01:10:12 -04:00
#include <AC_Circle.h> // circle navigation library
#include <AP_Declination.h> // ArduPilot Mega Declination Helper Library
#include <AC_Fence.h> // Arducopter Fence library
2013-01-11 21:01:10 -04:00
#include <SITL.h> // software in the loop support
#include <AP_Scheduler.h> // main loop scheduler
2013-03-24 23:48:06 -03:00
#include <AP_RCMapper.h> // RC input mapping library
2013-08-24 05:58:48 -03:00
#include <AP_Notify.h> // Notify library
2013-10-01 10:34:44 -03:00
#include <AP_BattMonitor.h> // Battery monitor library
2014-01-19 21:58:12 -04:00
#include <AP_BoardConfig.h> // board configuration library
#include <AP_Frsky_Telem.h>
2013-09-08 22:18:06 -03:00
#if SPRAYER == ENABLED
#include <AC_Sprayer.h> // crop sprayer library
#endif
2013-12-17 00:58:11 -04:00
#if EPM_ENABLED == ENABLED
#include <AP_EPM.h> // EPM cargo gripper stuff
#endif
2014-04-02 12:19:54 -03:00
#if PARACHUTE == ENABLED
#include <AP_Parachute.h> // Parachute release library
#endif
#include <AP_LandingGear.h> // Landing Gear library
2014-07-22 05:22:36 -03:00
#include <AP_Terrain.h>
2012-12-12 20:16:32 -04:00
// AP_HAL to Arduino compatibility layer
#include "compat.h"
// Configuration
#include "defines.h"
#include "config.h"
#include "config_channels.h"
// key aircraft parameters passed to multiple libraries
static AP_Vehicle::MultiCopter aparm;
// Local modules
#include "Parameters.h"
2014-05-10 10:34:30 -03:00
// Heli modules
#include "heli.h"
////////////////////////////////////////////////////////////////////////////////
// cliSerial
////////////////////////////////////////////////////////////////////////////////
// cliSerial isn't strictly necessary - it is an alias for hal.console. It may
// be deprecated in favor of hal.console in later releases.
2013-04-17 08:35:11 -03:00
static AP_HAL::BetterStream* cliSerial;
// N.B. we need to keep a static declaration which isn't guarded by macros
2013-10-29 01:28:27 -03:00
// at the top to cooperate with the prototype mangler.
////////////////////////////////////////////////////////////////////////////////
// AP_HAL instance
////////////////////////////////////////////////////////////////////////////////
2012-12-18 06:15:11 -04:00
const AP_HAL::HAL& hal = AP_HAL_BOARD_DRIVER;
////////////////////////////////////////////////////////////////////////////////
// Parameters
////////////////////////////////////////////////////////////////////////////////
//
// Global parameters are all contained within the 'g' class.
//
2012-08-21 23:19:50 -03:00
static Parameters g;
2013-01-11 21:01:10 -04:00
// main loop scheduler
2013-04-17 08:35:11 -03:00
static AP_Scheduler scheduler;
2013-08-29 00:14:07 -03:00
// AP_Notify instance
static AP_Notify notify;
2013-08-08 10:15:25 -03:00
// used to detect MAVLink acks from GCS to stop compassmot
static uint8_t command_ack_counter;
2013-08-09 00:18:01 -03:00
2014-10-16 21:37:49 -03:00
// has a log download started?
static bool in_log_download;
// primary input control channels
static RC_Channel *channel_roll;
static RC_Channel *channel_pitch;
static RC_Channel *channel_throttle;
static RC_Channel *channel_yaw;
////////////////////////////////////////////////////////////////////////////////
// prototypes
////////////////////////////////////////////////////////////////////////////////
static void update_events(void);
2013-04-20 02:18:22 -03:00
static void print_flight_mode(AP_HAL::BetterStream *port, uint8_t mode);
static void gcs_send_text_fmt(const prog_char_t *fmt, ...);
////////////////////////////////////////////////////////////////////////////////
// Dataflash
////////////////////////////////////////////////////////////////////////////////
2015-03-13 03:25:08 -03:00
#if defined(HAL_BOARD_LOG_DIRECTORY)
static DataFlash_File DataFlash(HAL_BOARD_LOG_DIRECTORY);
2013-01-02 07:08:40 -04:00
#else
2013-04-17 08:35:11 -03:00
static DataFlash_Empty DataFlash;
#endif
////////////////////////////////////////////////////////////////////////////////
// the rate we run the main loop at
////////////////////////////////////////////////////////////////////////////////
static const AP_InertialSensor::Sample_rate ins_sample_rate = AP_InertialSensor::RATE_400HZ;
////////////////////////////////////////////////////////////////////////////////
// Sensors
////////////////////////////////////////////////////////////////////////////////
//
// There are three basic options related to flight sensor selection.
//
2012-11-10 01:29:56 -04:00
// - Normal flight mode. Real sensors are used.
// - HIL Attitude mode. Most sensors are disabled, as the HIL
// protocol supplies attitude information directly.
2012-11-10 01:29:56 -04:00
// - HIL Sensors mode. Synthetic sensors are configured that
// supply data from the simulation.
//
2014-03-31 04:07:46 -03:00
static AP_GPS gps;
// flight modes convenience array
static AP_Int8 *flight_modes = &g.flight_mode1;
2015-01-05 07:28:00 -04:00
static AP_Baro barometer;
static Compass compass;
AP_InertialSensor ins;
////////////////////////////////////////////////////////////////////////////////
// SONAR
#if CONFIG_SONAR == ENABLED
static RangeFinder sonar;
static bool sonar_enabled = true; // enable user switch for sonar
#endif
2014-02-16 21:34:34 -04:00
// Inertial Navigation EKF
#if AP_AHRS_NAVEKF_AVAILABLE
AP_AHRS_NavEKF ahrs(ins, barometer, gps, sonar);
2014-02-16 21:34:34 -04:00
#else
2014-03-31 04:07:46 -03:00
AP_AHRS_DCM ahrs(ins, barometer, gps);
2014-02-16 21:34:34 -04:00
#endif
2015-05-04 03:18:46 -03:00
#if CONFIG_HAL_BOARD == HAL_BOARD_SITL
SITL sitl;
#endif
2014-02-27 21:16:54 -04:00
// Mission library
// forward declaration to keep compiler happy
static bool start_command(const AP_Mission::Mission_Command& cmd);
static bool verify_command(const AP_Mission::Mission_Command& cmd);
static void exit_mission();
AP_Mission mission(ahrs, &start_command, &verify_command, &exit_mission);
2014-02-27 21:16:54 -04:00
////////////////////////////////////////////////////////////////////////////////
// Optical flow sensor
////////////////////////////////////////////////////////////////////////////////
2015-01-02 04:30:44 -04:00
#if OPTFLOW == ENABLED
static OpticalFlow optflow;
#endif
// gnd speed limit required to observe optical flow sensor limits
static float ekfGndSpdLimit;
// scale factor applied to velocity controller gain to prevent optical flow noise causing excessive angle demand noise
static float ekfNavVelGainScaler;
////////////////////////////////////////////////////////////////////////////////
// GCS selection
////////////////////////////////////////////////////////////////////////////////
static AP_SerialManager serial_manager;
static const uint8_t num_gcs = MAVLINK_COMM_NUM_BUFFERS;
static GCS_MAVLINK gcs[MAVLINK_COMM_NUM_BUFFERS];
2011-10-15 19:29:33 -03:00
////////////////////////////////////////////////////////////////////////////////
// User variables
////////////////////////////////////////////////////////////////////////////////
#ifdef USERHOOK_VARIABLES
2012-08-21 23:19:50 -03:00
#include USERHOOK_VARIABLES
2011-10-15 19:29:33 -03:00
#endif
////////////////////////////////////////////////////////////////////////////////
// Global variables
////////////////////////////////////////////////////////////////////////////////
/* Radio values
2012-08-21 23:19:50 -03:00
* Channel assignments
* 1 Ailerons (rudder if no ailerons)
* 2 Elevator
* 3 Throttle
* 4 Rudder (if we have ailerons)
* 5 Mode - 3 position switch
* 6 User assignable
* 7 trainer switch - sets throttle nominal (toggle switch), sets accels to Level (hold > 1 second)
* 8 TBD
* Each Aux channel can be configured to have any of the available auxiliary functions assigned to it.
* See libraries/RC_Channel/RC_Channel_aux.h for more information
*/
//Documentation of GLobals:
static union {
struct {
uint8_t unused1 : 1; // 0
uint8_t simple_mode : 2; // 1,2 // This is the state of simple mode : 0 = disabled ; 1 = SIMPLE ; 2 = SUPERSIMPLE
uint8_t pre_arm_rc_check : 1; // 3 // true if rc input pre-arm checks have been completed successfully
uint8_t pre_arm_check : 1; // 4 // true if all pre-arm checks (rc, accel calibration, gps lock) have been performed
uint8_t auto_armed : 1; // 5 // stops auto missions from beginning until throttle is raised
uint8_t logging_started : 1; // 6 // true if dataflash logging has started
uint8_t land_complete : 1; // 7 // true if we have detected a landing
uint8_t new_radio_frame : 1; // 8 // Set true if we have new PWM data to act on from the Radio
uint8_t usb_connected : 1; // 9 // true if APM is powered from USB connection
uint8_t rc_receiver_present : 1; // 10 // true if we have an rc receiver present (i.e. if we've ever received an update
uint8_t compass_mot : 1; // 11 // true if we are currently performing compassmot calibration
uint8_t motor_test : 1; // 12 // true if we are currently performing the motors test
uint8_t initialised : 1; // 13 // true once the init_ardupilot function has completed. Extended status to GCS is not sent until this completes
uint8_t land_complete_maybe : 1; // 14 // true if we may have landed (less strict version of land_complete)
uint8_t throttle_zero : 1; // 15 // true if the throttle stick is at zero, debounced, determines if pilot intends shut-down when not using motor interlock
uint8_t system_time_set : 1; // 16 // true if the system time has been set from the GPS
uint8_t gps_base_pos_set : 1; // 17 // true when the gps base position has been set (used for RTK gps only)
enum HomeState home_state : 2; // 18,19 // home status (unset, set, locked)
uint8_t using_interlock : 1; // 20 // aux switch motor interlock function is in use
uint8_t motor_emergency_stop: 1; // 21 // motor estop switch, shuts off motors when enabled
};
uint32_t value;
} ap;
////////////////////////////////////////////////////////////////////////////////
// Radio
////////////////////////////////////////////////////////////////////////////////
// This is the state of the flight control system
// There are multiple states defined such as STABILIZE, ACRO,
static int8_t control_mode = STABILIZE;
// Structure used to detect changes in the flight mode control switch
static struct {
int8_t debounced_switch_position; // currently used switch position
int8_t last_switch_position; // switch position in previous iteration
uint32_t last_edge_time_ms; // system time that switch position was last changed
} control_switch_state;
static struct {
bool running;
float speed;
uint32_t start_ms;
uint32_t time_ms;
} takeoff_state;
2013-03-24 23:48:06 -03:00
static RCMapper rcmap;
2014-01-19 21:58:12 -04:00
// board specific config
static AP_BoardConfig BoardConfig;
// receiver RSSI
static uint8_t receiver_rssi;
////////////////////////////////////////////////////////////////////////////////
// Failsafe
////////////////////////////////////////////////////////////////////////////////
static struct {
uint8_t rc_override_active : 1; // 0 // true if rc control are overwritten by ground station
uint8_t radio : 1; // 1 // A status flag for the radio failsafe
uint8_t battery : 1; // 2 // A status flag for the battery failsafe
uint8_t gcs : 1; // 4 // A status flag for the ground station failsafe
uint8_t ekf : 1; // 5 // true if ekf failsafe has occurred
int8_t radio_counter; // number of iterations with throttle below throttle_fs_value
uint32_t last_heartbeat_ms; // the time when the last HEARTBEAT message arrived from a GCS - used for triggering gcs failsafe
} failsafe;
////////////////////////////////////////////////////////////////////////////////
// Motor Output
////////////////////////////////////////////////////////////////////////////////
2012-11-10 01:29:56 -04:00
#if FRAME_CONFIG == QUAD_FRAME
2012-08-21 23:19:50 -03:00
#define MOTOR_CLASS AP_MotorsQuad
#elif FRAME_CONFIG == TRI_FRAME
2012-08-21 23:19:50 -03:00
#define MOTOR_CLASS AP_MotorsTri
#elif FRAME_CONFIG == HEXA_FRAME
2012-08-21 23:19:50 -03:00
#define MOTOR_CLASS AP_MotorsHexa
#elif FRAME_CONFIG == Y6_FRAME
2012-08-21 23:19:50 -03:00
#define MOTOR_CLASS AP_MotorsY6
#elif FRAME_CONFIG == OCTA_FRAME
2012-08-21 23:19:50 -03:00
#define MOTOR_CLASS AP_MotorsOcta
#elif FRAME_CONFIG == OCTA_QUAD_FRAME
2012-08-21 23:19:50 -03:00
#define MOTOR_CLASS AP_MotorsOctaQuad
#elif FRAME_CONFIG == HELI_FRAME
2012-08-21 23:19:50 -03:00
#define MOTOR_CLASS AP_MotorsHeli
#elif FRAME_CONFIG == SINGLE_FRAME
#define MOTOR_CLASS AP_MotorsSingle
#elif FRAME_CONFIG == COAX_FRAME
#define MOTOR_CLASS AP_MotorsCoax
#else
#error Unrecognised frame type
#endif
2012-11-10 01:29:56 -04:00
#if FRAME_CONFIG == HELI_FRAME // helicopter constructor requires more arguments
static MOTOR_CLASS motors(g.rc_7, g.rc_8, g.heli_servo_1, g.heli_servo_2, g.heli_servo_3, g.heli_servo_4, MAIN_LOOP_RATE);
#elif FRAME_CONFIG == TRI_FRAME // tri constructor requires additional rc_7 argument to allow tail servo reversing
static MOTOR_CLASS motors(MAIN_LOOP_RATE);
#elif FRAME_CONFIG == SINGLE_FRAME // single constructor requires extra servos for flaps
static MOTOR_CLASS motors(g.single_servo_1, g.single_servo_2, g.single_servo_3, g.single_servo_4, MAIN_LOOP_RATE);
#elif FRAME_CONFIG == COAX_FRAME // single constructor requires extra servos for flaps
static MOTOR_CLASS motors(g.single_servo_1, g.single_servo_2, MAIN_LOOP_RATE);
#else
static MOTOR_CLASS motors(MAIN_LOOP_RATE);
#endif
////////////////////////////////////////////////////////////////////////////////
// GPS variables
////////////////////////////////////////////////////////////////////////////////
// We use atan2 and other trig techniques to calaculate angles
// We need to scale the longitude up to make these calcs work
// to account for decreasing distance between lines of longitude away from the equator
2012-11-10 01:29:56 -04:00
static float scaleLongUp = 1;
// Sometimes we need to remove the scaling for distance calcs
2012-11-10 01:29:56 -04:00
static float scaleLongDown = 1;
////////////////////////////////////////////////////////////////////////////////
// Location & Navigation
////////////////////////////////////////////////////////////////////////////////
// This is the angle from the copter to the next waypoint in centi-degrees
static int32_t wp_bearing;
// The location of home in relation to the copter in centi-degrees
static int32_t home_bearing;
// distance between plane and home in cm
static int32_t home_distance;
// distance between plane and next waypoint in cm.
static uint32_t wp_distance;
static uint8_t land_state; // records state of land (flying to location, descending)
2014-01-27 23:21:16 -04:00
////////////////////////////////////////////////////////////////////////////////
// Auto
////////////////////////////////////////////////////////////////////////////////
static AutoMode auto_mode; // controls which auto controller is run
////////////////////////////////////////////////////////////////////////////////
// Guided
////////////////////////////////////////////////////////////////////////////////
static GuidedMode guided_mode; // controls which controller is run (pos or vel)
2014-01-27 23:21:16 -04:00
////////////////////////////////////////////////////////////////////////////////
// RTL
////////////////////////////////////////////////////////////////////////////////
RTLState rtl_state; // records state of rtl (initial climb, returning home, etc)
bool rtl_state_complete; // set to true if the current state is completed
////////////////////////////////////////////////////////////////////////////////
// Circle
////////////////////////////////////////////////////////////////////////////////
bool circle_pilot_yaw_override; // true if pilot is overriding yaw
////////////////////////////////////////////////////////////////////////////////
// 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 float simple_cos_yaw = 1.0;
static float simple_sin_yaw;
static int32_t super_simple_last_bearing;
static float super_simple_cos_yaw = 1.0;
static float super_simple_sin_yaw;
// Stores initial bearing when armed - initial simple bearing is modified in super simple mode so not suitable
static int32_t initial_armed_bearing;
////////////////////////////////////////////////////////////////////////////////
// Throttle variables
////////////////////////////////////////////////////////////////////////////////
static float throttle_average; // estimated throttle required to hover
static int16_t desired_climb_rate; // pilot desired climb rate - for logging purposes only
2012-02-19 01:16:19 -04:00
////////////////////////////////////////////////////////////////////////////////
// ACRO Mode
////////////////////////////////////////////////////////////////////////////////
2013-08-04 05:46:04 -03:00
static float acro_level_mix; // scales back roll, pitch and yaw inversely proportional to input from pilot
////////////////////////////////////////////////////////////////////////////////
// Loiter control
////////////////////////////////////////////////////////////////////////////////
static uint16_t loiter_time_max; // How long we should stay in Loiter Mode for mission scripting (time in seconds)
static uint32_t loiter_time; // How long have we been loitering - The start time in millis
////////////////////////////////////////////////////////////////////////////////
// Flip
////////////////////////////////////////////////////////////////////////////////
static Vector3f flip_orig_attitude; // original copter attitude before flip
////////////////////////////////////////////////////////////////////////////////
// Battery Sensors
////////////////////////////////////////////////////////////////////////////////
2013-10-01 10:34:44 -03:00
static AP_BattMonitor battery;
////////////////////////////////////////////////////////////////////////////////
// FrSky telemetry support
#if FRSKY_TELEM_ENABLED == ENABLED
static AP_Frsky_Telem frsky_telemetry(ahrs, battery);
#endif
////////////////////////////////////////////////////////////////////////////////
// Altitude
////////////////////////////////////////////////////////////////////////////////
// The cm/s we are moving up or down based on filtered data - Positive = UP
2012-08-21 23:19:50 -03:00
static int16_t climb_rate;
// The altitude as reported by Sonar in cm - Values are 20 to 700 generally.
2012-08-21 23:19:50 -03:00
static int16_t sonar_alt;
static uint8_t sonar_alt_health; // true if we can trust the altitude from the sonar
static float target_sonar_alt; // desired altitude in cm above the ground
static int32_t baro_alt; // barometer altitude in cm above home
static float baro_climbrate; // barometer climbrate in cm/s
static LowPassFilterVector3f land_accel_ef_filter(LAND_DETECTOR_ACCEL_LPF_CUTOFF); // accelerations for land detector test
////////////////////////////////////////////////////////////////////////////////
// 3D Location vectors
////////////////////////////////////////////////////////////////////////////////
// Current location of the copter (altitude is relative to home)
2012-08-21 23:19:50 -03:00
static struct Location current_loc;
////////////////////////////////////////////////////////////////////////////////
// Navigation Yaw control
////////////////////////////////////////////////////////////////////////////////
// auto flight mode's yaw mode
static uint8_t auto_yaw_mode = AUTO_YAW_LOOK_AT_NEXT_WP;
// Yaw will point at this location if auto_yaw_mode is set to AUTO_YAW_ROI
static Vector3f roi_WP;
// bearing from current location to the yaw_look_at_WP
static float yaw_look_at_WP_bearing;
// yaw used for YAW_LOOK_AT_HEADING yaw_mode
static int32_t yaw_look_at_heading;
// Deg/s we should turn
static int16_t yaw_look_at_heading_slew;
// heading when in yaw_look_ahead_bearing
static float yaw_look_ahead_bearing;
////////////////////////////////////////////////////////////////////////////////
// Delay Mission Scripting Command
////////////////////////////////////////////////////////////////////////////////
2012-08-21 23:19:50 -03:00
static int32_t condition_value; // used in condition commands (eg delay, change alt, etc.)
2012-01-10 00:23:37 -04:00
static uint32_t condition_start;
////////////////////////////////////////////////////////////////////////////////
// IMU variables
////////////////////////////////////////////////////////////////////////////////
2013-08-08 10:09:27 -03:00
// Integration time (in seconds) for the gyros (DCM algorithm)
// Updated with the fast loop
static float G_Dt = 0.02;
////////////////////////////////////////////////////////////////////////////////
// Inertial Navigation
////////////////////////////////////////////////////////////////////////////////
static AP_InertialNav_NavEKF inertial_nav(ahrs);
2013-03-20 10:29:08 -03:00
////////////////////////////////////////////////////////////////////////////////
// Attitude, Position and Waypoint navigation objects
2013-03-20 10:29:08 -03:00
// To-Do: move inertial nav up or other navigation variables down here
////////////////////////////////////////////////////////////////////////////////
2014-01-29 09:51:08 -04:00
#if FRAME_CONFIG == HELI_FRAME
AC_AttitudeControl_Heli attitude_control(ahrs, aparm, motors, g.p_stabilize_roll, g.p_stabilize_pitch, g.p_stabilize_yaw,
g.pid_rate_roll, g.pid_rate_pitch, g.pid_rate_yaw);
2014-01-29 09:51:08 -04:00
#else
AC_AttitudeControl attitude_control(ahrs, aparm, motors, g.p_stabilize_roll, g.p_stabilize_pitch, g.p_stabilize_yaw,
g.pid_rate_roll, g.pid_rate_pitch, g.pid_rate_yaw);
2014-01-29 09:51:08 -04:00
#endif
AC_PosControl pos_control(ahrs, inertial_nav, motors, attitude_control,
g.p_alt_hold, g.p_vel_z, g.pid_accel_z,
g.p_pos_xy, g.pi_vel_xy);
static AC_WPNav wp_nav(inertial_nav, ahrs, pos_control, attitude_control);
2014-01-27 01:10:12 -04:00
static AC_Circle circle_nav(inertial_nav, ahrs, pos_control);
2013-03-20 10:29:08 -03:00
////////////////////////////////////////////////////////////////////////////////
// Performance monitoring
////////////////////////////////////////////////////////////////////////////////
2013-04-29 09:30:22 -03:00
static int16_t pmTest1;
// System Timers
// --------------
// Time in microseconds of main control loop
2012-08-21 23:19:50 -03:00
static uint32_t fast_loopTimer;
// Counter of main loop executions. Used for performance monitoring and failsafe processing
static uint16_t mainLoop_count;
// Loiter timer - Records how long we have been in loiter
static uint32_t rtl_loiter_start_time;
// Used to exit the roll and pitch auto trim function
static uint8_t auto_trim_counter;
2015-03-13 03:25:08 -03:00
// Reference to the relay object
2013-04-17 08:35:11 -03:00
static AP_Relay relay;
2011-10-02 09:52:02 -03:00
// handle repeated servo and relay events
static AP_ServoRelayEvents ServoRelayEvents(relay);
//Reference to the camera object (it uses the relay object inside it)
#if CAMERA == ENABLED
2013-04-17 08:35:11 -03:00
static AP_Camera camera(&relay);
#endif
// a pin for reading the receiver RSSI voltage.
2013-04-17 08:35:11 -03:00
static AP_HAL::AnalogSource* rssi_analog_source;
#if CLI_ENABLED == ENABLED
static int8_t setup_show (uint8_t argc, const Menu::arg *argv);
2012-09-11 01:25:53 -03:00
#endif
// Camera/Antenna mount tracking and stabilisation stuff
// --------------------------------------
#if MOUNT == ENABLED
// current_loc uses the baro/gps soloution for altitude rather than gps only.
static AP_Mount camera_mount(ahrs, current_loc);
#endif
////////////////////////////////////////////////////////////////////////////////
// AC_Fence library to reduce fly-aways
////////////////////////////////////////////////////////////////////////////////
#if AC_FENCE == ENABLED
2015-04-13 19:08:54 -03:00
AC_Fence fence(inertial_nav);
#endif
2014-04-06 22:18:43 -03:00
////////////////////////////////////////////////////////////////////////////////
// Rally library
////////////////////////////////////////////////////////////////////////////////
#if AC_RALLY == ENABLED
AP_Rally rally(ahrs);
2014-04-06 22:18:43 -03:00
#endif
2013-08-04 11:20:21 -03:00
////////////////////////////////////////////////////////////////////////////////
// Crop Sprayer
////////////////////////////////////////////////////////////////////////////////
#if SPRAYER == ENABLED
static AC_Sprayer sprayer(&inertial_nav);
#endif
2013-12-17 00:58:11 -04:00
////////////////////////////////////////////////////////////////////////////////
// EPM Cargo Griper
////////////////////////////////////////////////////////////////////////////////
#if EPM_ENABLED == ENABLED
static AP_EPM epm;
#endif
2014-04-02 12:19:54 -03:00
////////////////////////////////////////////////////////////////////////////////
// Parachute release
////////////////////////////////////////////////////////////////////////////////
#if PARACHUTE == ENABLED
static AP_Parachute parachute(relay);
#endif
////////////////////////////////////////////////////////////////////////////////
// Landing Gear Controller
////////////////////////////////////////////////////////////////////////////////
static AP_LandingGear landinggear;
2014-07-22 05:22:36 -03:00
////////////////////////////////////////////////////////////////////////////////
// terrain handling
#if AP_TERRAIN_AVAILABLE
2014-08-06 03:17:52 -03:00
AP_Terrain terrain(ahrs, mission, rally);
2014-07-22 05:22:36 -03:00
#endif
////////////////////////////////////////////////////////////////////////////////
// function definitions to keep compiler from complaining about undeclared functions
////////////////////////////////////////////////////////////////////////////////
static bool pre_arm_checks(bool display_failure);
////////////////////////////////////////////////////////////////////////////////
// Top-level logic
////////////////////////////////////////////////////////////////////////////////
// setup the var_info table
AP_Param param_loader(var_info);
/*
scheduler table for fast CPUs - all regular tasks apart from the fast_loop()
should be listed here, along with how often they should be called
(in 2.5ms units) and the maximum time they are expected to take (in
microseconds)
1 = 400hz
2 = 200hz
4 = 100hz
8 = 50hz
20 = 20hz
40 = 10hz
133 = 3hz
400 = 1hz
4000 = 0.1hz
*/
static const AP_Scheduler::Task scheduler_tasks[] PROGMEM = {
2015-05-18 05:28:05 -03:00
{ rc_loop, 4, 130 }, // 0
{ throttle_loop, 8, 75 }, // 1
{ update_GPS, 8, 200 }, // 2
#if OPTFLOW == ENABLED
2015-05-18 05:28:05 -03:00
{ update_optical_flow, 2, 160 }, // 3
#endif
{ update_batt_compass, 40, 120 }, // 4
{ read_aux_switches, 40, 50 }, // 5
{ arm_motors_check, 40, 50 }, // 6
{ auto_trim, 40, 75 }, // 7
{ update_altitude, 40, 140 }, // 8
{ run_nav_updates, 8, 100 }, // 9
2015-05-18 05:28:05 -03:00
{ update_thr_average, 4, 90 }, // 10
{ three_hz_loop, 133, 75 }, // 11
{ compass_accumulate, 8, 100 }, // 12
{ barometer_accumulate, 8, 90 }, // 13
#if FRAME_CONFIG == HELI_FRAME
2015-05-18 05:28:05 -03:00
{ check_dynamic_flight, 8, 75 },
#endif
{ update_notify, 8, 90 }, // 14
{ one_hz_loop, 400, 100 }, // 15
{ ekf_check, 40, 75 }, // 16
{ crash_check, 40, 75 }, // 17
{ landinggear_update, 40, 75 }, // 18
{ lost_vehicle_check, 40, 50 }, // 19
{ gcs_check_input, 1, 180 }, // 20
{ gcs_send_heartbeat, 400, 110 }, // 21
{ gcs_send_deferred, 8, 550 }, // 22
2015-05-18 05:28:05 -03:00
{ gcs_data_stream_send, 8, 550 }, // 23
{ update_mount, 8, 75 }, // 24
{ ten_hz_logging_loop, 40, 350 }, // 25
{ fifty_hz_logging_loop, 8, 110 }, // 26
{ full_rate_logging_loop,1, 100 }, // 27
{ perf_update, 4000, 75 }, // 28
{ read_receiver_rssi, 40, 75 }, // 29
#if FRSKY_TELEM_ENABLED == ENABLED
2015-05-18 05:28:05 -03:00
{ frsky_telemetry_send, 80, 75 }, // 30
#endif
2014-09-15 03:52:21 -03:00
#if EPM_ENABLED == ENABLED
2015-05-18 05:28:05 -03:00
{ epm_update, 40, 75 }, // 31
2014-09-15 03:52:21 -03:00
#endif
#ifdef USERHOOK_FASTLOOP
2015-05-18 05:28:05 -03:00
{ userhook_FastLoop, 4, 75 },
#endif
#ifdef USERHOOK_50HZLOOP
2015-05-18 05:28:05 -03:00
{ userhook_50Hz, 8, 75 },
#endif
#ifdef USERHOOK_MEDIUMLOOP
2015-05-18 05:28:05 -03:00
{ userhook_MediumLoop, 40, 75 },
#endif
#ifdef USERHOOK_SLOWLOOP
2015-05-18 05:28:05 -03:00
{ userhook_SlowLoop, 120, 75 },
#endif
#ifdef USERHOOK_SUPERSLOWLOOP
2015-05-18 05:28:05 -03:00
{ userhook_SuperSlowLoop,400, 75 },
#endif
};
2013-01-11 21:01:10 -04:00
2013-12-28 01:02:32 -04:00
void setup()
{
cliSerial = hal.console;
// Load the default values of variables listed in var_info[]s
AP_Param::setup_sketch_defaults();
// setup storage layout for copter
StorageManager::set_layout_copter();
2012-08-21 23:19:50 -03:00
init_ardupilot();
2013-01-11 21:01:10 -04:00
// initialise the main loop scheduler
2015-05-26 01:33:47 -03:00
scheduler.init(&scheduler_tasks[0], sizeof(scheduler_tasks)/sizeof(scheduler_tasks[0]));
// setup initial performance counters
perf_info_reset();
fast_loopTimer = hal.scheduler->micros();
}
/*
if the compass is enabled then try to accumulate a reading
*/
static void compass_accumulate(void)
{
if (g.compass_enabled) {
compass.accumulate();
2013-10-29 01:28:27 -03:00
}
}
/*
try to accumulate a baro reading
*/
static void barometer_accumulate(void)
{
barometer.accumulate();
}
static void perf_update(void)
{
2014-10-16 21:37:49 -03:00
if (should_log(MASK_LOG_PM))
Log_Write_Performance();
if (scheduler.debug()) {
gcs_send_text_fmt(PSTR("PERF: %u/%u %lu %lu\n"),
(unsigned)perf_info_get_num_long_running(),
(unsigned)perf_info_get_num_loops(),
(unsigned long)perf_info_get_max_time(),
(unsigned long)perf_info_get_min_time());
}
perf_info_reset();
2013-04-29 09:30:22 -03:00
pmTest1 = 0;
}
void loop()
{
// wait for an INS sample
ins.wait_for_sample();
uint32_t timer = micros();
2012-08-21 23:19:50 -03:00
// check loop time
perf_info_check_loop_time(timer - fast_loopTimer);
// used by PI Loops
G_Dt = (float)(timer - fast_loopTimer) / 1000000.0f;
fast_loopTimer = timer;
2012-08-21 23:19:50 -03:00
// for mainloop failure monitoring
mainLoop_count++;
// Execute the fast loop
// ---------------------
fast_loop();
2012-08-21 23:19:50 -03:00
// tell the scheduler one tick has passed
scheduler.tick();
// run all the tasks that are due to run. Note that we only
// have to call this once per loop, as the tasks are scheduled
// in multiples of the main loop tick. So if they don't run on
// the first call to the scheduler they won't run on a later
// call until scheduler.tick() is called again
uint32_t time_available = (timer + MAIN_LOOP_MICROS) - micros();
scheduler.run(time_available);
}
// Main loop - 400hz
static void fast_loop()
{
2013-12-17 00:58:11 -04:00
2012-08-21 23:19:50 -03:00
// IMU DCM Algorithm
// --------------------
read_AHRS();
// run low level rate controllers that only require IMU data
2013-12-06 02:08:11 -04:00
attitude_control.rate_controller_run();
#if FRAME_CONFIG == HELI_FRAME
update_heli_control_dynamics();
#endif //HELI_FRAME
// send outputs to the motors library
motors_output();
2012-08-21 23:19:50 -03:00
// Inertial Nav
// --------------------
read_inertia();
// run the attitude controllers
update_flight_mode();
// update home from EKF if necessary
update_home_from_EKF();
2015-04-16 15:35:17 -03:00
// check if we've landed
update_land_detector();
}
// rc_loops - reads user input from transmitter/receiver
// called at 100hz
static void rc_loop()
{
// Read radio and 3-position switch on radio
// -----------------------------------------
read_radio();
read_control_switch();
}
// throttle_loop - should be run at 50 hz
// ---------------------------
static void throttle_loop()
{
// get altitude and climb rate from inertial lib
read_inertial_altitude();
// update throttle_low_comp value (controls priority of throttle vs attitude control)
update_throttle_thr_mix();
// check auto_armed status
update_auto_armed();
#if FRAME_CONFIG == HELI_FRAME
// update rotor speed
heli_update_rotor_speed_targets();
// update trad heli swash plate movement
heli_update_landing_swash();
#endif
}
2013-10-11 08:40:20 -03:00
// update_mount - update camera mount position
// should be run at 50hz
2013-10-11 08:40:20 -03:00
static void update_mount()
{
#if MOUNT == ENABLED
// update camera mount's position
camera_mount.update();
#endif
#if CAMERA == ENABLED
camera.trigger_pic_cleanup();
#endif
}
2013-10-11 08:40:20 -03:00
// update_batt_compass - read battery and compass
// should be called at 10hz
static void update_batt_compass(void)
{
// read battery before compass because it may be used for motor interference compensation
read_battery();
if(g.compass_enabled) {
// update compass with throttle value - used for compassmot
compass.set_throttle(motors.get_throttle()/1000.0f);
compass.read();
2013-10-11 08:40:20 -03:00
// log compass information
2014-10-16 21:37:49 -03:00
if (should_log(MASK_LOG_COMPASS)) {
DataFlash.Log_Write_Compass(compass);
2013-10-11 08:40:20 -03:00
}
}
}
// ten_hz_logging_loop
// should be run at 10hz
static void ten_hz_logging_loop()
{
// log attitude data if we're not already logging at the higher rate
if (should_log(MASK_LOG_ATTITUDE_MED) && !should_log(MASK_LOG_ATTITUDE_FAST)) {
Log_Write_Attitude();
Log_Write_Rate();
if (should_log(MASK_LOG_PID)) {
DataFlash.Log_Write_PID(LOG_PIDR_MSG, g.pid_rate_roll.get_pid_info() );
DataFlash.Log_Write_PID(LOG_PIDP_MSG, g.pid_rate_pitch.get_pid_info() );
DataFlash.Log_Write_PID(LOG_PIDY_MSG, g.pid_rate_yaw.get_pid_info() );
DataFlash.Log_Write_PID(LOG_PIDA_MSG, g.pid_accel_z.get_pid_info() );
}
}
if (should_log(MASK_LOG_MOTBATT)) {
Log_Write_MotBatt();
}
2014-10-16 21:37:49 -03:00
if (should_log(MASK_LOG_RCIN)) {
DataFlash.Log_Write_RCIN();
}
2014-10-16 21:37:49 -03:00
if (should_log(MASK_LOG_RCOUT)) {
DataFlash.Log_Write_RCOUT();
2013-10-11 08:40:20 -03:00
}
2014-10-16 21:37:49 -03:00
if (should_log(MASK_LOG_NTUN) && (mode_requires_GPS(control_mode) || landing_with_GPS())) {
2014-01-21 23:56:07 -04:00
Log_Write_Nav_Tuning();
}
2013-10-11 08:40:20 -03:00
}
// fifty_hz_logging_loop
// should be run at 50hz
static void fifty_hz_logging_loop()
{
#if HIL_MODE != HIL_MODE_DISABLED
// HIL for a copter needs very fast update of the servo values
gcs_send_message(MSG_RADIO_OUT);
#endif
#if HIL_MODE == HIL_MODE_DISABLED
2014-10-16 21:37:49 -03:00
if (should_log(MASK_LOG_ATTITUDE_FAST)) {
Log_Write_Attitude();
2015-03-18 09:05:19 -03:00
Log_Write_Rate();
if (should_log(MASK_LOG_PID)) {
DataFlash.Log_Write_PID(LOG_PIDR_MSG, g.pid_rate_roll.get_pid_info() );
DataFlash.Log_Write_PID(LOG_PIDP_MSG, g.pid_rate_pitch.get_pid_info() );
DataFlash.Log_Write_PID(LOG_PIDY_MSG, g.pid_rate_yaw.get_pid_info() );
DataFlash.Log_Write_PID(LOG_PIDA_MSG, g.pid_accel_z.get_pid_info() );
}
}
// log IMU data if we're not already logging at the higher rate
if (should_log(MASK_LOG_IMU) && !should_log(MASK_LOG_IMU_FAST)) {
2013-11-03 23:37:46 -04:00
DataFlash.Log_Write_IMU(ins);
}
#endif
}
// full_rate_logging_loop
// should be run at the MAIN_LOOP_RATE
static void full_rate_logging_loop()
{
if (should_log(MASK_LOG_IMU_FAST)) {
DataFlash.Log_Write_IMU(ins);
}
}
2013-10-11 08:40:20 -03:00
// three_hz_loop - 3.3hz loop
static void three_hz_loop()
{
2013-10-11 08:40:20 -03:00
// check if we've lost contact with the ground station
failsafe_gcs_check();
#if AC_FENCE == ENABLED
2013-10-11 08:40:20 -03:00
// check if we have breached a fence
fence_check();
#endif // AC_FENCE_ENABLED
2013-08-04 11:20:21 -03:00
#if SPRAYER == ENABLED
2013-10-11 08:40:20 -03:00
sprayer.update();
2012-08-21 23:19:50 -03:00
#endif
2013-10-11 08:40:20 -03:00
update_events();
2015-01-16 02:12:58 -04:00
// update ch6 in flight tuning
tuning();
}
2013-10-11 08:40:20 -03:00
// one_hz_loop - runs at 1Hz
static void one_hz_loop()
{
2014-10-16 21:37:49 -03:00
if (should_log(MASK_LOG_ANY)) {
Log_Write_Data(DATA_AP_STATE, ap.value);
}
2012-11-11 21:59:53 -04:00
// perform pre-arm checks & display failures every 30 seconds
static uint8_t pre_arm_display_counter = 15;
pre_arm_display_counter++;
if (pre_arm_display_counter >= 30) {
pre_arm_checks(true);
pre_arm_display_counter = 0;
}else{
pre_arm_checks(false);
}
// auto disarm checks
auto_disarm_check();
if (!motors.armed()) {
2013-10-11 08:40:20 -03:00
// make it possible to change ahrs orientation at runtime during initial config
ahrs.set_orientation();
2013-10-11 08:40:20 -03:00
// check the user hasn't updated the frame orientation
motors.set_frame_orientation(g.frame_orientation);
// set all throttle channel settings
motors.set_throttle_range(g.throttle_min, channel_throttle->radio_min, channel_throttle->radio_max);
}
// update assigned functions and enable auxiliar servos
2014-02-05 19:12:31 -04:00
RC_Channel_aux::enable_aux_servos();
2013-10-11 08:40:20 -03:00
check_usb_mux();
2014-07-22 05:22:36 -03:00
#if AP_TERRAIN_AVAILABLE
2014-07-22 05:22:36 -03:00
terrain.update();
// tell the rangefinder our height, so it can go into power saving
// mode if available
#if CONFIG_SONAR == ENABLED
float height;
if (terrain.height_above_terrain(height, true)) {
sonar.set_estimated_terrain_height(height);
}
#endif
2014-07-22 05:22:36 -03:00
#endif
// update position controller alt limits
update_poscon_alt_max();
// enable/disable raw gyro/accel logging
ins.set_raw_logging(should_log(MASK_LOG_IMU_RAW));
}
2012-05-21 13:58:14 -03:00
// called at 50hz
static void update_GPS(void)
{
static uint32_t last_gps_reading[GPS_MAX_INSTANCES]; // time of last gps message
bool gps_updated = false;
2012-08-21 23:19:50 -03:00
2014-03-31 04:07:46 -03:00
gps.update();
2012-08-21 23:19:50 -03:00
// log after every gps message
for (uint8_t i=0; i<gps.num_sensors(); i++) {
if (gps.last_message_time_ms(i) != last_gps_reading[i]) {
last_gps_reading[i] = gps.last_message_time_ms(i);
2012-08-21 23:19:50 -03:00
// log GPS message
2014-10-16 21:37:49 -03:00
if (should_log(MASK_LOG_GPS)) {
DataFlash.Log_Write_GPS(gps, i, current_loc.alt);
}
gps_updated = true;
}
}
2012-08-21 23:19:50 -03:00
if (gps_updated) {
// set system time if necessary
set_system_time_from_GPS();
2014-03-31 04:07:46 -03:00
// checks to initialise home and take location based pictures
if (gps.status() >= AP_GPS::GPS_OK_FIX_3D) {
#if CAMERA == ENABLED
2014-03-31 04:07:46 -03:00
if (camera.update_location(current_loc) == true) {
do_take_picture();
}
2013-10-29 01:28:27 -03:00
#endif
2014-03-31 04:07:46 -03:00
}
2012-08-21 23:19:50 -03:00
}
}
static void
init_simple_bearing()
{
// capture current cos_yaw and sin_yaw values
2014-02-08 03:40:45 -04:00
simple_cos_yaw = ahrs.cos_yaw();
simple_sin_yaw = ahrs.sin_yaw();
// initialise super simple heading (i.e. heading towards home) to be 180 deg from simple mode heading
super_simple_last_bearing = wrap_360_cd(ahrs.yaw_sensor+18000);
super_simple_cos_yaw = simple_cos_yaw;
super_simple_sin_yaw = simple_sin_yaw;
// log the simple bearing to dataflash
2014-10-16 21:37:49 -03:00
if (should_log(MASK_LOG_ANY)) {
Log_Write_Data(DATA_INIT_SIMPLE_BEARING, ahrs.yaw_sensor);
}
}
// update_simple_mode - rotates pilot input if we are in simple mode
void update_simple_mode(void)
{
float rollx, pitchx;
// exit immediately if no new radio frame or not in simple mode
if (ap.simple_mode == 0 || !ap.new_radio_frame) {
return;
2012-08-21 23:19:50 -03:00
}
// mark radio frame as consumed
ap.new_radio_frame = false;
if (ap.simple_mode == 1) {
// rotate roll, pitch input by -initial simple heading (i.e. north facing)
rollx = channel_roll->control_in*simple_cos_yaw - channel_pitch->control_in*simple_sin_yaw;
pitchx = channel_roll->control_in*simple_sin_yaw + channel_pitch->control_in*simple_cos_yaw;
}else{
// rotate roll, pitch input by -super simple heading (reverse of heading to home)
rollx = channel_roll->control_in*super_simple_cos_yaw - channel_pitch->control_in*super_simple_sin_yaw;
pitchx = channel_roll->control_in*super_simple_sin_yaw + channel_pitch->control_in*super_simple_cos_yaw;
}
// rotate roll, pitch input from north facing to vehicle's perspective
channel_roll->control_in = rollx*ahrs.cos_yaw() + pitchx*ahrs.sin_yaw();
channel_pitch->control_in = -rollx*ahrs.sin_yaw() + pitchx*ahrs.cos_yaw();
}
// update_super_simple_bearing - adjusts simple bearing based on location
// should be called after home_bearing has been updated
void update_super_simple_bearing(bool force_update)
{
// check if we are in super simple mode and at least 10m from home
if(force_update || (ap.simple_mode == 2 && home_distance > SUPER_SIMPLE_RADIUS)) {
// check the bearing to home has changed by at least 5 degrees
if (labs(super_simple_last_bearing - home_bearing) > 500) {
super_simple_last_bearing = home_bearing;
float angle_rad = radians((super_simple_last_bearing+18000)/100);
super_simple_cos_yaw = cosf(angle_rad);
super_simple_sin_yaw = sinf(angle_rad);
}
}
}
static void read_AHRS(void)
{
2012-08-21 23:19:50 -03:00
// Perform IMU calculations and get attitude info
//-----------------------------------------------
#if HIL_MODE != HIL_MODE_DISABLED
// update hil before ahrs update
gcs_check_input();
2012-08-21 23:19:50 -03:00
#endif
ahrs.update();
}
// read baro and sonar altitude at 10hz
static void update_altitude()
{
// read in baro altitude
read_barometer();
2012-08-21 23:19:50 -03:00
// read in sonar altitude
sonar_alt = read_sonar();
2012-08-21 23:19:50 -03:00
// write altitude info to dataflash logs
2014-10-16 21:37:49 -03:00
if (should_log(MASK_LOG_CTUN)) {
Log_Write_Control_Tuning();
2012-08-21 23:19:50 -03:00
}
}
2012-12-13 16:02:27 -04:00
AP_HAL_MAIN();