2015-05-29 23:12:49 -03:00
|
|
|
#include "Copter.h"
|
|
|
|
|
2015-02-25 10:13:59 -04:00
|
|
|
// checks if we should update ahrs/RTL home position from the EKF
|
2015-05-29 23:12:49 -03:00
|
|
|
void Copter::update_home_from_EKF()
|
2015-02-09 07:23:49 -04:00
|
|
|
{
|
2015-02-25 10:13:59 -04:00
|
|
|
// exit immediately if home already set
|
2018-03-15 22:23:28 -03:00
|
|
|
if (ahrs.home_is_set()) {
|
2015-02-09 07:23:49 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-05-13 03:25:50 -03:00
|
|
|
// special logic if home is set in-flight
|
2017-01-09 03:31:26 -04:00
|
|
|
if (motors->armed()) {
|
2015-05-13 03:25:50 -03:00
|
|
|
set_home_to_current_location_inflight();
|
|
|
|
} else {
|
|
|
|
// move home to current ekf location (this will set home_state to HOME_SET)
|
2018-05-29 21:47:08 -03:00
|
|
|
if (!set_home_to_current_location(false)) {
|
|
|
|
// ignore failure
|
|
|
|
}
|
2015-05-13 03:25:50 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-13 06:00:34 -03:00
|
|
|
// set_home_to_current_location_inflight - set home to current GPS location (horizontally) and EKF origin vertically
|
2015-05-29 23:12:49 -03:00
|
|
|
void Copter::set_home_to_current_location_inflight() {
|
2015-05-13 03:25:50 -03:00
|
|
|
// get current location from EKF
|
|
|
|
Location temp_loc;
|
2019-06-28 10:55:36 -03:00
|
|
|
Location ekf_origin;
|
2022-01-20 19:42:41 -04:00
|
|
|
if (ahrs.get_location(temp_loc) && ahrs.get_origin(ekf_origin)) {
|
2015-05-13 03:25:50 -03:00
|
|
|
temp_loc.alt = ekf_origin.alt;
|
2018-01-15 13:03:41 -04:00
|
|
|
if (!set_home(temp_loc, false)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// we have successfully set AHRS home, set it for SmartRTL
|
2024-09-04 00:48:37 -03:00
|
|
|
#if MODE_SMARTRTL_ENABLED
|
2018-01-15 13:03:41 -04:00
|
|
|
g2.smart_rtl.set_home(true);
|
2018-02-21 23:58:28 -04:00
|
|
|
#endif
|
2015-05-13 03:25:50 -03:00
|
|
|
}
|
2015-02-09 07:23:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// set_home_to_current_location - set home to current GPS location
|
2017-06-05 05:03:50 -03:00
|
|
|
bool Copter::set_home_to_current_location(bool lock) {
|
2015-02-25 10:13:59 -04:00
|
|
|
// get current location from EKF
|
|
|
|
Location temp_loc;
|
2022-01-20 19:42:41 -04:00
|
|
|
if (ahrs.get_location(temp_loc)) {
|
2018-01-15 13:03:41 -04:00
|
|
|
if (!set_home(temp_loc, lock)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// we have successfully set AHRS home, set it for SmartRTL
|
2024-09-04 00:48:37 -03:00
|
|
|
#if MODE_SMARTRTL_ENABLED
|
2018-01-15 13:03:41 -04:00
|
|
|
g2.smart_rtl.set_home(true);
|
2018-02-21 23:58:28 -04:00
|
|
|
#endif
|
2018-01-15 13:03:41 -04:00
|
|
|
return true;
|
2015-02-09 07:23:49 -04:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2014-03-31 04:07:46 -03:00
|
|
|
|
2015-02-09 07:23:49 -04:00
|
|
|
// set_home - sets ahrs home (used for RTL) to specified location
|
|
|
|
// returns true if home location set successfully
|
2017-06-05 05:03:50 -03:00
|
|
|
bool Copter::set_home(const Location& loc, bool lock)
|
2015-02-09 07:23:49 -04:00
|
|
|
{
|
2017-09-17 22:37:55 -03:00
|
|
|
// check EKF origin has been set
|
2017-04-19 04:17:56 -03:00
|
|
|
Location ekf_origin;
|
2017-09-17 22:37:55 -03:00
|
|
|
if (!ahrs.get_origin(ekf_origin)) {
|
|
|
|
return false;
|
2017-04-19 04:17:56 -03:00
|
|
|
}
|
|
|
|
|
2015-02-09 07:23:49 -04:00
|
|
|
// set ahrs home (used for RTL)
|
2018-05-29 21:47:08 -03:00
|
|
|
if (!ahrs.set_home(loc)) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-08-16 21:50:03 -03:00
|
|
|
|
2017-06-05 05:03:50 -03:00
|
|
|
// lock home position
|
|
|
|
if (lock) {
|
2018-05-17 23:58:50 -03:00
|
|
|
ahrs.lock_home();
|
2017-06-05 05:03:50 -03:00
|
|
|
}
|
|
|
|
|
2015-02-09 07:23:49 -04:00
|
|
|
// return success
|
|
|
|
return true;
|
2010-12-19 12:40:33 -04:00
|
|
|
}
|