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
|
2018-02-21 23:58:28 -04:00
|
|
|
#if MODE_SMARTRTL_ENABLED == 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
|
2018-02-21 23:58:28 -04:00
|
|
|
#if MODE_SMARTRTL_ENABLED == 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
|
|
|
}
|
|
|
|
|
|
|
|
// check home is close to EKF origin
|
|
|
|
if (far_from_EKF_origin(loc)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-02-09 07:23:49 -04:00
|
|
|
// far_from_EKF_origin - checks if a location is too far from the EKF origin
|
|
|
|
// returns true if too far
|
2015-05-29 23:12:49 -03:00
|
|
|
bool Copter::far_from_EKF_origin(const Location& loc)
|
2015-01-05 00:51:20 -04:00
|
|
|
{
|
2015-02-09 07:23:49 -04:00
|
|
|
// check distance to EKF origin
|
2019-06-28 10:55:36 -03:00
|
|
|
Location ekf_origin;
|
2021-06-27 22:05:19 -03:00
|
|
|
if (ahrs.get_origin(ekf_origin)) {
|
|
|
|
if (labs(ekf_origin.alt - loc.alt)*0.01 > EKF_ORIGIN_MAX_ALT_KM*1000.0) {
|
|
|
|
return true;
|
|
|
|
}
|
2015-02-09 07:23:49 -04:00
|
|
|
}
|
2015-01-05 00:51:20 -04:00
|
|
|
|
2015-02-09 07:23:49 -04:00
|
|
|
// close enough to origin
|
|
|
|
return false;
|
2015-01-05 00:51:20 -04:00
|
|
|
}
|