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
|
|
|
|
2013-08-29 02:34:47 -03:00
|
|
|
/*
|
|
|
|
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/>.
|
|
|
|
*/
|
2010-12-19 12:40:33 -04:00
|
|
|
/*
|
2013-05-01 05:53:11 -03:00
|
|
|
* ArduCopter Version 3.0
|
2013-05-03 04:58:00 -03:00
|
|
|
* 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
|
|
|
*
|
2014-04-23 23:26:10 -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
|
2014-04-23 23:26:10 -03:00
|
|
|
* Christof Schmid :Alpha testing
|
2013-11-15 23:10:38 -04:00
|
|
|
* Craig Elder :Release Management, Support
|
2013-01-11 02:28:08 -04:00
|
|
|
* Dani Saez :V Octo Support
|
2014-04-23 23:26:10 -03:00
|
|
|
* 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
|
2014-04-23 23:26:10 -03:00
|
|
|
* 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
|
2014-04-23 23:26:10 -03:00
|
|
|
* 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
|
2014-04-23 23:26:10 -03:00
|
|
|
* 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
|
2014-04-23 23:26:10 -03:00
|
|
|
* 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
|
|
|
*
|
|
|
|
*/
|
2010-12-19 12:40:33 -04:00
|
|
|
|
2015-05-29 23:12:49 -03:00
|
|
|
#include "Copter.h"
|
2014-03-31 04:07:46 -03:00
|
|
|
|
2015-05-31 18:50:39 -03:00
|
|
|
#define SCHED_TASK(func) FUNCTOR_BIND(&copter, &Copter::func, void)
|
2012-12-14 17:19:35 -04:00
|
|
|
|
2013-12-16 23:25:33 -04:00
|
|
|
/*
|
|
|
|
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
|
|
|
|
|
|
|
|
*/
|
2015-05-29 23:12:49 -03:00
|
|
|
const AP_Scheduler::Task Copter::scheduler_tasks[] PROGMEM = {
|
2015-06-08 19:41:04 -03:00
|
|
|
{ SCHED_TASK(rc_loop), 4, 130 },
|
|
|
|
{ SCHED_TASK(throttle_loop), 8, 75 },
|
|
|
|
{ SCHED_TASK(update_GPS), 8, 200 },
|
2014-07-11 23:33:49 -03:00
|
|
|
#if OPTFLOW == ENABLED
|
2015-06-08 19:41:04 -03:00
|
|
|
{ SCHED_TASK(update_optical_flow), 2, 160 },
|
2015-05-29 23:12:49 -03:00
|
|
|
#endif
|
2015-06-08 19:41:04 -03:00
|
|
|
{ SCHED_TASK(update_batt_compass), 40, 120 },
|
|
|
|
{ SCHED_TASK(read_aux_switches), 40, 50 },
|
|
|
|
{ SCHED_TASK(arm_motors_check), 40, 50 },
|
|
|
|
{ SCHED_TASK(auto_trim), 40, 75 },
|
|
|
|
{ SCHED_TASK(update_altitude), 40, 140 },
|
|
|
|
{ SCHED_TASK(run_nav_updates), 8, 100 },
|
|
|
|
{ SCHED_TASK(update_thr_average), 4, 90 },
|
|
|
|
{ SCHED_TASK(three_hz_loop), 133, 75 },
|
|
|
|
{ SCHED_TASK(compass_accumulate), 8, 100 },
|
|
|
|
{ SCHED_TASK(barometer_accumulate), 8, 90 },
|
2013-12-16 23:25:33 -04:00
|
|
|
#if FRAME_CONFIG == HELI_FRAME
|
2015-05-29 23:12:49 -03:00
|
|
|
{ SCHED_TASK(check_dynamic_flight), 8, 75 },
|
|
|
|
#endif
|
2015-06-08 19:41:04 -03:00
|
|
|
{ SCHED_TASK(update_notify), 8, 90 },
|
|
|
|
{ SCHED_TASK(one_hz_loop), 400, 100 },
|
|
|
|
{ SCHED_TASK(ekf_check), 40, 75 },
|
|
|
|
{ SCHED_TASK(landinggear_update), 40, 75 },
|
|
|
|
{ SCHED_TASK(lost_vehicle_check), 40, 50 },
|
|
|
|
{ SCHED_TASK(gcs_check_input), 1, 180 },
|
|
|
|
{ SCHED_TASK(gcs_send_heartbeat), 400, 110 },
|
|
|
|
{ SCHED_TASK(gcs_send_deferred), 8, 550 },
|
|
|
|
{ SCHED_TASK(gcs_data_stream_send), 8, 550 },
|
|
|
|
{ SCHED_TASK(update_mount), 8, 75 },
|
|
|
|
{ SCHED_TASK(ten_hz_logging_loop), 40, 350 },
|
|
|
|
{ SCHED_TASK(fifty_hz_logging_loop), 8, 110 },
|
|
|
|
{ SCHED_TASK(full_rate_logging_loop),1, 100 },
|
|
|
|
{ SCHED_TASK(perf_update), 4000, 75 },
|
|
|
|
{ SCHED_TASK(read_receiver_rssi), 40, 75 },
|
2015-08-07 02:34:56 -03:00
|
|
|
{ SCHED_TASK(rpm_update), 40, 200 },
|
2014-07-28 19:25:40 -03:00
|
|
|
#if FRSKY_TELEM_ENABLED == ENABLED
|
2015-06-08 19:41:04 -03:00
|
|
|
{ SCHED_TASK(frsky_telemetry_send), 80, 75 },
|
2014-07-28 19:25:40 -03:00
|
|
|
#endif
|
2014-09-15 03:52:21 -03:00
|
|
|
#if EPM_ENABLED == ENABLED
|
2015-06-08 19:41:04 -03:00
|
|
|
{ SCHED_TASK(epm_update), 40, 75 },
|
2014-09-15 03:52:21 -03:00
|
|
|
#endif
|
2013-12-16 23:25:33 -04:00
|
|
|
#ifdef USERHOOK_FASTLOOP
|
2015-05-29 23:12:49 -03:00
|
|
|
{ SCHED_TASK(userhook_FastLoop), 4, 75 },
|
2013-12-16 23:25:33 -04:00
|
|
|
#endif
|
|
|
|
#ifdef USERHOOK_50HZLOOP
|
2015-05-29 23:12:49 -03:00
|
|
|
{ SCHED_TASK(userhook_50Hz), 8, 75 },
|
2013-12-16 23:25:33 -04:00
|
|
|
#endif
|
|
|
|
#ifdef USERHOOK_MEDIUMLOOP
|
2015-05-29 23:12:49 -03:00
|
|
|
{ SCHED_TASK(userhook_MediumLoop), 40, 75 },
|
2013-12-16 23:25:33 -04:00
|
|
|
#endif
|
|
|
|
#ifdef USERHOOK_SLOWLOOP
|
2015-05-29 23:12:49 -03:00
|
|
|
{ SCHED_TASK(userhook_SlowLoop), 120, 75 },
|
2013-12-16 23:25:33 -04:00
|
|
|
#endif
|
|
|
|
#ifdef USERHOOK_SUPERSLOWLOOP
|
2015-05-29 23:12:49 -03:00
|
|
|
{ SCHED_TASK(userhook_SuperSlowLoop),400, 75 },
|
2013-12-16 23:25:33 -04:00
|
|
|
#endif
|
|
|
|
};
|
2013-01-11 21:01:10 -04:00
|
|
|
|
2015-05-29 23:12:49 -03:00
|
|
|
|
|
|
|
void Copter::setup()
|
2013-12-28 01:02:32 -04:00
|
|
|
{
|
2012-12-13 22:16:32 -04:00
|
|
|
cliSerial = hal.console;
|
|
|
|
|
|
|
|
// Load the default values of variables listed in var_info[]s
|
2012-12-14 17:19:35 -04:00
|
|
|
AP_Param::setup_sketch_defaults();
|
2012-12-12 19:53:48 -04:00
|
|
|
|
2014-08-13 01:44:31 -03:00
|
|
|
// 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-07-06 12:30:40 -03:00
|
|
|
scheduler.init(&scheduler_tasks[0], ARRAY_SIZE(scheduler_tasks));
|
2015-02-15 19:02:53 -04:00
|
|
|
|
|
|
|
// setup initial performance counters
|
|
|
|
perf_info_reset();
|
|
|
|
fast_loopTimer = hal.scheduler->micros();
|
2010-12-19 12:40:33 -04:00
|
|
|
}
|
|
|
|
|
2013-01-06 20:02:50 -04:00
|
|
|
/*
|
2013-01-08 01:45:12 -04:00
|
|
|
if the compass is enabled then try to accumulate a reading
|
2013-01-06 20:02:50 -04:00
|
|
|
*/
|
2015-05-29 23:12:49 -03:00
|
|
|
void Copter::compass_accumulate(void)
|
2013-01-08 01:45:12 -04:00
|
|
|
{
|
|
|
|
if (g.compass_enabled) {
|
|
|
|
compass.accumulate();
|
2013-10-29 01:28:27 -03:00
|
|
|
}
|
2013-01-08 01:45:12 -04:00
|
|
|
}
|
|
|
|
|
2013-01-09 08:08:19 -04:00
|
|
|
/*
|
|
|
|
try to accumulate a baro reading
|
|
|
|
*/
|
2015-05-29 23:12:49 -03:00
|
|
|
void Copter::barometer_accumulate(void)
|
2013-01-09 08:08:19 -04:00
|
|
|
{
|
|
|
|
barometer.accumulate();
|
|
|
|
}
|
|
|
|
|
2015-05-29 23:12:49 -03:00
|
|
|
void Copter::perf_update(void)
|
2013-01-06 20:02:50 -04:00
|
|
|
{
|
2014-10-16 21:37:49 -03:00
|
|
|
if (should_log(MASK_LOG_PM))
|
2013-01-08 01:45:12 -04:00
|
|
|
Log_Write_Performance();
|
2013-01-11 21:06:40 -04:00
|
|
|
if (scheduler.debug()) {
|
2015-02-15 19:02:53 -04:00
|
|
|
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());
|
2013-01-11 21:06:40 -04:00
|
|
|
}
|
2013-01-08 01:45:12 -04:00
|
|
|
perf_info_reset();
|
2013-04-29 09:30:22 -03:00
|
|
|
pmTest1 = 0;
|
2013-01-06 20:02:50 -04:00
|
|
|
}
|
|
|
|
|
2015-05-29 23:12:49 -03:00
|
|
|
void Copter::loop()
|
2010-12-19 12:40:33 -04:00
|
|
|
{
|
2013-10-08 03:30:43 -03:00
|
|
|
// wait for an INS sample
|
2014-10-15 20:33:24 -03:00
|
|
|
ins.wait_for_sample();
|
|
|
|
|
2012-11-10 01:39:41 -04:00
|
|
|
uint32_t timer = micros();
|
2012-08-21 23:19:50 -03:00
|
|
|
|
2013-10-08 03:30:43 -03:00
|
|
|
// check loop time
|
|
|
|
perf_info_check_loop_time(timer - fast_loopTimer);
|
2012-11-18 12:16:07 -04:00
|
|
|
|
2013-10-08 03:30:43 -03:00
|
|
|
// used by PI Loops
|
2015-04-24 02:19:45 -03:00
|
|
|
G_Dt = (float)(timer - fast_loopTimer) / 1000000.0f;
|
2013-10-08 03:30:43 -03:00
|
|
|
fast_loopTimer = timer;
|
2012-08-21 23:19:50 -03:00
|
|
|
|
2013-10-08 03:30:43 -03:00
|
|
|
// for mainloop failure monitoring
|
|
|
|
mainLoop_count++;
|
2012-10-09 00:30:17 -03:00
|
|
|
|
2013-10-08 03:30:43 -03:00
|
|
|
// Execute the fast loop
|
|
|
|
// ---------------------
|
|
|
|
fast_loop();
|
2012-08-21 23:19:50 -03:00
|
|
|
|
2013-10-08 03:30:43 -03:00
|
|
|
// tell the scheduler one tick has passed
|
|
|
|
scheduler.tick();
|
2013-07-23 04:05:16 -03:00
|
|
|
|
2013-10-08 03:30:43 -03:00
|
|
|
// 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
|
2013-12-16 23:25:33 -04:00
|
|
|
uint32_t time_available = (timer + MAIN_LOOP_MICROS) - micros();
|
|
|
|
scheduler.run(time_available);
|
2010-12-19 12:40:33 -04:00
|
|
|
}
|
2011-07-10 21:47:08 -03:00
|
|
|
|
2013-01-08 01:45:12 -04:00
|
|
|
|
2015-05-28 15:59:49 -03:00
|
|
|
// Main loop - 400hz
|
2015-05-29 23:12:49 -03:00
|
|
|
void Copter::fast_loop()
|
2010-12-19 12:40:33 -04:00
|
|
|
{
|
2013-12-17 00:58:11 -04:00
|
|
|
|
2012-08-21 23:19:50 -03:00
|
|
|
// IMU DCM Algorithm
|
|
|
|
// --------------------
|
|
|
|
read_AHRS();
|
2011-09-19 18:02:42 -03:00
|
|
|
|
2013-01-11 01:32:40 -04:00
|
|
|
// run low level rate controllers that only require IMU data
|
2013-12-06 02:08:11 -04:00
|
|
|
attitude_control.rate_controller_run();
|
2014-05-09 18:07:52 -03:00
|
|
|
|
|
|
|
#if FRAME_CONFIG == HELI_FRAME
|
|
|
|
update_heli_control_dynamics();
|
|
|
|
#endif //HELI_FRAME
|
2013-01-11 01:32:40 -04:00
|
|
|
|
2015-01-21 07:20:05 -04:00
|
|
|
// send outputs to the motors library
|
|
|
|
motors_output();
|
2013-01-11 01:32:40 -04:00
|
|
|
|
2012-08-21 23:19:50 -03:00
|
|
|
// Inertial Nav
|
|
|
|
// --------------------
|
2012-11-07 06:03:30 -04:00
|
|
|
read_inertia();
|
2012-06-14 02:27:03 -03:00
|
|
|
|
2015-06-23 04:49:42 -03:00
|
|
|
// check if ekf has reset target heading
|
|
|
|
check_ekf_yaw_reset();
|
|
|
|
|
2014-01-11 04:03:05 -04:00
|
|
|
// run the attitude controllers
|
|
|
|
update_flight_mode();
|
2015-02-26 01:34:21 -04:00
|
|
|
|
|
|
|
// update home from EKF if necessary
|
|
|
|
update_home_from_EKF();
|
2015-04-16 15:35:17 -03:00
|
|
|
|
2015-06-17 08:37:15 -03:00
|
|
|
// check if we've landed or crashed
|
|
|
|
update_land_and_crash_detectors();
|
2015-07-12 10:06:21 -03:00
|
|
|
|
|
|
|
// log sensor health
|
|
|
|
if (should_log(MASK_LOG_ANY)) {
|
|
|
|
Log_Sensor_Health();
|
|
|
|
}
|
2013-12-16 23:25:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// rc_loops - reads user input from transmitter/receiver
|
|
|
|
// called at 100hz
|
2015-05-29 23:12:49 -03:00
|
|
|
void Copter::rc_loop()
|
2013-12-16 23:25:33 -04:00
|
|
|
{
|
2012-11-12 23:50:51 -04:00
|
|
|
// Read radio and 3-position switch on radio
|
|
|
|
// -----------------------------------------
|
|
|
|
read_radio();
|
|
|
|
read_control_switch();
|
2010-12-19 12:40:33 -04:00
|
|
|
}
|
|
|
|
|
2013-10-11 05:03:26 -03:00
|
|
|
// throttle_loop - should be run at 50 hz
|
2013-05-19 23:05:14 -03:00
|
|
|
// ---------------------------
|
2015-05-29 23:12:49 -03:00
|
|
|
void Copter::throttle_loop()
|
2013-05-19 23:05:14 -03:00
|
|
|
{
|
|
|
|
// get altitude and climb rate from inertial lib
|
|
|
|
read_inertial_altitude();
|
|
|
|
|
2015-03-09 09:16:08 -03:00
|
|
|
// update throttle_low_comp value (controls priority of throttle vs attitude control)
|
2015-05-03 11:21:44 -03:00
|
|
|
update_throttle_thr_mix();
|
2015-03-09 09:16:08 -03:00
|
|
|
|
2013-05-19 23:05:14 -03:00
|
|
|
// check auto_armed status
|
|
|
|
update_auto_armed();
|
2013-11-06 08:39:39 -04:00
|
|
|
|
|
|
|
#if FRAME_CONFIG == HELI_FRAME
|
2013-11-08 04:29:54 -04:00
|
|
|
// update rotor speed
|
|
|
|
heli_update_rotor_speed_targets();
|
|
|
|
|
2013-11-06 08:39:39 -04:00
|
|
|
// update trad heli swash plate movement
|
|
|
|
heli_update_landing_swash();
|
|
|
|
#endif
|
2013-10-11 05:03:26 -03:00
|
|
|
}
|
2013-05-19 23:05:14 -03:00
|
|
|
|
2013-10-11 08:40:20 -03:00
|
|
|
// update_mount - update camera mount position
|
2013-10-11 05:03:26 -03:00
|
|
|
// should be run at 50hz
|
2015-05-29 23:12:49 -03:00
|
|
|
void Copter::update_mount()
|
2013-10-11 05:03:26 -03:00
|
|
|
{
|
2013-05-19 23:05:14 -03:00
|
|
|
#if MOUNT == ENABLED
|
|
|
|
// update camera mount's position
|
2015-01-11 02:37:26 -04:00
|
|
|
camera_mount.update();
|
2013-05-19 23:05:14 -03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if CAMERA == ENABLED
|
|
|
|
camera.trigger_pic_cleanup();
|
|
|
|
#endif
|
2013-10-11 05:03:26 -03:00
|
|
|
}
|
|
|
|
|
2013-10-11 08:40:20 -03:00
|
|
|
// update_batt_compass - read battery and compass
|
|
|
|
// should be called at 10hz
|
2015-05-29 23:12:49 -03:00
|
|
|
void Copter::update_batt_compass(void)
|
2013-10-11 08:40:20 -03:00
|
|
|
{
|
|
|
|
// read battery before compass because it may be used for motor interference compensation
|
|
|
|
read_battery();
|
|
|
|
|
|
|
|
if(g.compass_enabled) {
|
2014-02-03 01:06:08 -04:00
|
|
|
// update compass with throttle value - used for compassmot
|
2015-05-23 14:51:52 -03:00
|
|
|
compass.set_throttle(motors.get_throttle()/1000.0f);
|
2014-08-15 22:45:11 -03:00
|
|
|
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)) {
|
2015-01-16 18:27:37 -04:00
|
|
|
DataFlash.Log_Write_Compass(compass);
|
2013-10-11 08:40:20 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ten_hz_logging_loop
|
|
|
|
// should be run at 10hz
|
2015-05-29 23:12:49 -03:00
|
|
|
void Copter::ten_hz_logging_loop()
|
2013-10-11 08:40:20 -03:00
|
|
|
{
|
2015-04-19 23:23:57 -03:00
|
|
|
// 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)) {
|
2014-01-07 09:41:56 -04:00
|
|
|
Log_Write_Attitude();
|
2015-03-04 02:21:01 -04:00
|
|
|
Log_Write_Rate();
|
2015-05-27 01:32:05 -03:00
|
|
|
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() );
|
|
|
|
}
|
2015-03-04 02:21:01 -04:00
|
|
|
}
|
2015-03-18 09:08:30 -03:00
|
|
|
if (should_log(MASK_LOG_MOTBATT)) {
|
|
|
|
Log_Write_MotBatt();
|
2015-03-04 02:21:01 -04:00
|
|
|
}
|
2014-10-16 21:37:49 -03:00
|
|
|
if (should_log(MASK_LOG_RCIN)) {
|
2014-01-07 09:41:56 -04:00
|
|
|
DataFlash.Log_Write_RCIN();
|
|
|
|
}
|
2014-10-16 21:37:49 -03:00
|
|
|
if (should_log(MASK_LOG_RCOUT)) {
|
2014-01-07 09:41:56 -04:00
|
|
|
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();
|
|
|
|
}
|
2015-06-11 22:28:13 -03:00
|
|
|
if (should_log(MASK_LOG_IMU) || should_log(MASK_LOG_IMU_FAST) || should_log(MASK_LOG_IMU_RAW)) {
|
|
|
|
DataFlash.Log_Write_Vibration(ins);
|
|
|
|
}
|
2015-06-18 18:09:39 -03:00
|
|
|
#if FRAME_CONFIG == HELI_FRAME
|
|
|
|
Log_Write_Heli();
|
|
|
|
#endif
|
2013-10-11 08:40:20 -03:00
|
|
|
}
|
|
|
|
|
2013-10-11 05:03:26 -03:00
|
|
|
// fifty_hz_logging_loop
|
|
|
|
// should be run at 50hz
|
2015-05-29 23:12:49 -03:00
|
|
|
void Copter::fifty_hz_logging_loop()
|
2013-10-11 05:03:26 -03:00
|
|
|
{
|
|
|
|
#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
|
2013-05-19 23:05:14 -03:00
|
|
|
|
2014-01-07 09:41:56 -04:00
|
|
|
#if HIL_MODE == HIL_MODE_DISABLED
|
2014-10-16 21:37:49 -03:00
|
|
|
if (should_log(MASK_LOG_ATTITUDE_FAST)) {
|
2013-05-19 23:05:14 -03:00
|
|
|
Log_Write_Attitude();
|
2015-03-18 09:05:19 -03:00
|
|
|
Log_Write_Rate();
|
2015-05-27 01:32:05 -03:00
|
|
|
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() );
|
|
|
|
}
|
2013-05-19 23:05:14 -03:00
|
|
|
}
|
|
|
|
|
2015-04-19 23:23:57 -03:00
|
|
|
// log IMU data if we're not already logging at the higher rate
|
2015-06-11 22:03:11 -03:00
|
|
|
if (should_log(MASK_LOG_IMU) && !(should_log(MASK_LOG_IMU_FAST) || should_log(MASK_LOG_IMU_RAW))) {
|
2013-11-03 23:37:46 -04:00
|
|
|
DataFlash.Log_Write_IMU(ins);
|
2014-01-07 09:41:56 -04:00
|
|
|
}
|
2013-05-19 23:05:14 -03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-04-19 00:32:02 -03:00
|
|
|
// full_rate_logging_loop
|
|
|
|
// should be run at the MAIN_LOOP_RATE
|
2015-05-29 23:12:49 -03:00
|
|
|
void Copter::full_rate_logging_loop()
|
2015-04-19 00:32:02 -03:00
|
|
|
{
|
2015-06-11 22:03:11 -03:00
|
|
|
if (should_log(MASK_LOG_IMU_FAST) && !should_log(MASK_LOG_IMU_RAW)) {
|
2015-04-19 00:32:02 -03:00
|
|
|
DataFlash.Log_Write_IMU(ins);
|
2015-06-16 22:57:06 -03:00
|
|
|
}
|
|
|
|
if (should_log(MASK_LOG_IMU_FAST) || should_log(MASK_LOG_IMU_RAW)) {
|
2015-06-13 10:07:21 -03:00
|
|
|
DataFlash.Log_Write_IMUDT(ins);
|
2015-04-19 00:32:02 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-11 08:40:20 -03:00
|
|
|
// three_hz_loop - 3.3hz loop
|
2015-05-29 23:12:49 -03:00
|
|
|
void Copter::three_hz_loop()
|
2010-12-19 12:40:33 -04:00
|
|
|
{
|
2013-10-11 08:40:20 -03:00
|
|
|
// check if we've lost contact with the ground station
|
|
|
|
failsafe_gcs_check();
|
2012-04-04 10:50:43 -03:00
|
|
|
|
2013-04-26 06:51:07 -03:00
|
|
|
#if AC_FENCE == ENABLED
|
2013-10-11 08:40:20 -03:00
|
|
|
// check if we have breached a fence
|
|
|
|
fence_check();
|
2013-04-26 06:51:07 -03:00
|
|
|
#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
|
2011-06-01 02:50:17 -03:00
|
|
|
|
2013-10-11 08:40:20 -03:00
|
|
|
update_events();
|
2011-02-17 05:36:33 -04:00
|
|
|
|
2015-01-16 02:12:58 -04:00
|
|
|
// update ch6 in flight tuning
|
|
|
|
tuning();
|
2010-12-19 12:40:33 -04:00
|
|
|
}
|
|
|
|
|
2013-10-11 08:40:20 -03:00
|
|
|
// one_hz_loop - runs at 1Hz
|
2015-05-29 23:12:49 -03:00
|
|
|
void Copter::one_hz_loop()
|
2011-02-25 01:33:39 -04:00
|
|
|
{
|
2014-10-16 21:37:49 -03:00
|
|
|
if (should_log(MASK_LOG_ANY)) {
|
2013-01-12 11:17:44 -04:00
|
|
|
Log_Write_Data(DATA_AP_STATE, ap.value);
|
|
|
|
}
|
2012-11-11 21:59:53 -04:00
|
|
|
|
2013-11-18 04:20:39 -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);
|
|
|
|
}
|
2013-04-22 12:01:20 -03:00
|
|
|
|
2013-05-20 02:05:50 -03:00
|
|
|
// auto disarm checks
|
|
|
|
auto_disarm_check();
|
2012-03-07 02:22:14 -04:00
|
|
|
|
2013-06-03 03:52:52 -03:00
|
|
|
if (!motors.armed()) {
|
2013-10-11 08:40:20 -03:00
|
|
|
// make it possible to change ahrs orientation at runtime during initial config
|
2013-06-03 03:52:52 -03:00
|
|
|
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);
|
2015-05-14 22:04:41 -03:00
|
|
|
|
2015-07-15 02:35:20 -03:00
|
|
|
#if FRAME_CONFIG != HELI_FRAME
|
2015-05-14 22:04:41 -03:00
|
|
|
// set all throttle channel settings
|
|
|
|
motors.set_throttle_range(g.throttle_min, channel_throttle->radio_min, channel_throttle->radio_max);
|
2015-07-15 03:03:14 -03:00
|
|
|
// set hover throttle
|
|
|
|
motors.set_hover_throttle(g.throttle_mid);
|
2015-07-15 02:35:20 -03:00
|
|
|
#endif
|
2013-06-03 03:52:52 -03:00
|
|
|
}
|
|
|
|
|
2013-10-31 03:22:34 -03:00
|
|
|
// 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
|
|
|
|
2014-07-24 18:58:58 -03:00
|
|
|
#if AP_TERRAIN_AVAILABLE
|
2014-07-22 05:22:36 -03:00
|
|
|
terrain.update();
|
2015-02-22 21:50:35 -04:00
|
|
|
|
|
|
|
// 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
|
2015-01-30 00:35:23 -04:00
|
|
|
|
2015-04-23 02:57:49 -03:00
|
|
|
// update position controller alt limits
|
|
|
|
update_poscon_alt_max();
|
2015-05-06 23:09:00 -03:00
|
|
|
|
|
|
|
// enable/disable raw gyro/accel logging
|
|
|
|
ins.set_raw_logging(should_log(MASK_LOG_IMU_RAW));
|
2011-02-25 01:33:39 -04:00
|
|
|
}
|
|
|
|
|
2012-05-21 13:58:14 -03:00
|
|
|
// called at 50hz
|
2015-05-29 23:12:49 -03:00
|
|
|
void Copter::update_GPS(void)
|
2010-12-19 12:40:33 -04:00
|
|
|
{
|
2014-04-09 21:30:10 -03:00
|
|
|
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
|
|
|
|
2015-03-12 23:05:30 -03:00
|
|
|
// log after every gps message
|
2014-04-09 21:30:10 -03:00
|
|
|
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
|
|
|
|
2014-04-09 21:30:10 -03:00
|
|
|
// log GPS message
|
2014-10-16 21:37:49 -03:00
|
|
|
if (should_log(MASK_LOG_GPS)) {
|
2014-04-09 21:30:10 -03:00
|
|
|
DataFlash.Log_Write_GPS(gps, i, current_loc.alt);
|
|
|
|
}
|
|
|
|
|
|
|
|
gps_updated = true;
|
2013-03-25 04:25:13 -03:00
|
|
|
}
|
2014-04-09 21:30:10 -03:00
|
|
|
}
|
2012-08-21 23:19:50 -03:00
|
|
|
|
2014-04-09 21:30:10 -03:00
|
|
|
if (gps_updated) {
|
2015-02-09 07:23:49 -04:00
|
|
|
// set system time if necessary
|
|
|
|
set_system_time_from_GPS();
|
2014-03-31 04:07:46 -03:00
|
|
|
|
2015-02-09 07:23:49 -04:00
|
|
|
// checks to initialise home and take location based pictures
|
|
|
|
if (gps.status() >= AP_GPS::GPS_OK_FIX_3D) {
|
2014-06-06 19:17:38 -03:00
|
|
|
|
2013-06-24 23:52:44 -03:00
|
|
|
#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
|
|
|
}
|
2010-12-19 12:40:33 -04:00
|
|
|
}
|
2011-02-17 05:36:33 -04:00
|
|
|
|
2015-05-29 23:12:49 -03:00
|
|
|
void Copter::init_simple_bearing()
|
2012-01-04 02:54:29 -04:00
|
|
|
{
|
2013-10-05 06:25:03 -03:00
|
|
|
// 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();
|
2012-01-04 02:54:29 -04:00
|
|
|
|
2013-10-05 06:25:03 -03:00
|
|
|
// 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;
|
2012-01-04 02:54:29 -04:00
|
|
|
|
2013-10-05 06:25:03 -03:00
|
|
|
// log the simple bearing to dataflash
|
2014-10-16 21:37:49 -03:00
|
|
|
if (should_log(MASK_LOG_ANY)) {
|
2013-10-05 06:25:03 -03:00
|
|
|
Log_Write_Data(DATA_INIT_SIMPLE_BEARING, ahrs.yaw_sensor);
|
|
|
|
}
|
|
|
|
}
|
2012-01-04 02:54:29 -04:00
|
|
|
|
2013-10-05 06:25:03 -03:00
|
|
|
// update_simple_mode - rotates pilot input if we are in simple mode
|
2015-05-29 23:12:49 -03:00
|
|
|
void Copter::update_simple_mode(void)
|
2013-10-05 06:25:03 -03:00
|
|
|
{
|
|
|
|
float rollx, pitchx;
|
2012-01-04 02:54:29 -04:00
|
|
|
|
2013-10-05 06:25:03 -03:00
|
|
|
// exit immediately if no new radio frame or not in simple mode
|
2013-10-08 03:25:14 -03:00
|
|
|
if (ap.simple_mode == 0 || !ap.new_radio_frame) {
|
2013-10-05 06:25:03 -03:00
|
|
|
return;
|
2012-08-21 23:19:50 -03:00
|
|
|
}
|
2012-01-04 02:54:29 -04:00
|
|
|
|
2014-03-28 05:03:31 -03:00
|
|
|
// mark radio frame as consumed
|
|
|
|
ap.new_radio_frame = false;
|
|
|
|
|
2013-10-05 06:25:03 -03:00
|
|
|
if (ap.simple_mode == 1) {
|
|
|
|
// rotate roll, pitch input by -initial simple heading (i.e. north facing)
|
2015-05-11 23:24:13 -03:00
|
|
|
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;
|
2013-10-05 06:25:03 -03:00
|
|
|
}else{
|
|
|
|
// rotate roll, pitch input by -super simple heading (reverse of heading to home)
|
2015-05-11 23:24:13 -03:00
|
|
|
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;
|
2013-10-05 06:25:03 -03:00
|
|
|
}
|
2012-01-04 02:54:29 -04:00
|
|
|
|
2013-10-05 06:25:03 -03:00
|
|
|
// rotate roll, pitch input from north facing to vehicle's perspective
|
2015-05-11 23:24:13 -03:00
|
|
|
channel_roll->control_in = rollx*ahrs.cos_yaw() + pitchx*ahrs.sin_yaw();
|
|
|
|
channel_pitch->control_in = -rollx*ahrs.sin_yaw() + pitchx*ahrs.cos_yaw();
|
2011-09-04 21:15:36 -03:00
|
|
|
}
|
2011-07-10 21:47:08 -03:00
|
|
|
|
2013-01-27 10:48:07 -04:00
|
|
|
// update_super_simple_bearing - adjusts simple bearing based on location
|
2013-01-24 00:36:55 -04:00
|
|
|
// should be called after home_bearing has been updated
|
2015-05-29 23:12:49 -03:00
|
|
|
void Copter::update_super_simple_bearing(bool force_update)
|
2013-01-24 00:36:55 -04:00
|
|
|
{
|
2013-10-05 06:25:03 -03:00
|
|
|
// 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);
|
2013-01-24 00:36:55 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-29 23:12:49 -03:00
|
|
|
void Copter::read_AHRS(void)
|
2010-12-19 12:40:33 -04:00
|
|
|
{
|
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
|
2013-01-08 01:45:12 -04:00
|
|
|
gcs_check_input();
|
2012-08-21 23:19:50 -03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
ahrs.update();
|
2011-01-23 12:40:03 -04:00
|
|
|
}
|
2011-02-13 18:32:34 -04:00
|
|
|
|
2014-07-28 05:38:32 -03:00
|
|
|
// read baro and sonar altitude at 10hz
|
2015-05-29 23:12:49 -03:00
|
|
|
void Copter::update_altitude()
|
2011-02-21 00:30:56 -04:00
|
|
|
{
|
2013-02-01 23:00:09 -04:00
|
|
|
// read in baro altitude
|
2014-10-22 04:07:10 -03:00
|
|
|
read_barometer();
|
2012-08-21 23:19:50 -03:00
|
|
|
|
2013-02-01 23:00:09 -04:00
|
|
|
// read in sonar altitude
|
2013-01-31 04:00:28 -04:00
|
|
|
sonar_alt = read_sonar();
|
2012-08-21 23:19:50 -03:00
|
|
|
|
2013-02-01 23:00:09 -04:00
|
|
|
// write altitude info to dataflash logs
|
2014-10-16 21:37:49 -03:00
|
|
|
if (should_log(MASK_LOG_CTUN)) {
|
2013-02-01 23:00:09 -04:00
|
|
|
Log_Write_Control_Tuning();
|
2012-08-21 23:19:50 -03:00
|
|
|
}
|
2011-03-26 03:35:52 -03:00
|
|
|
}
|
|
|
|
|
2015-05-29 23:12:49 -03:00
|
|
|
/*
|
|
|
|
compatibility with old pde style build
|
|
|
|
*/
|
|
|
|
void setup(void);
|
|
|
|
void loop(void);
|
|
|
|
|
|
|
|
void setup(void)
|
|
|
|
{
|
|
|
|
copter.setup();
|
|
|
|
}
|
|
|
|
void loop(void)
|
|
|
|
{
|
|
|
|
copter.loop();
|
|
|
|
}
|
|
|
|
|
2012-12-13 16:02:27 -04:00
|
|
|
AP_HAL_MAIN();
|
|
|
|
|