2016-01-14 15:30:56 -04:00
|
|
|
#include "Sub.h"
|
2015-12-30 18:57:56 -04:00
|
|
|
|
|
|
|
// checks if we should update ahrs/RTL home position from the EKF
|
2016-01-14 15:30:56 -04:00
|
|
|
void Sub::update_home_from_EKF()
|
2015-12-30 18:57:56 -04:00
|
|
|
{
|
|
|
|
// exit immediately if home already set
|
2018-03-15 23:12:42 -03:00
|
|
|
if (ahrs.home_is_set()) {
|
2015-12-30 18:57:56 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// special logic if home is set in-flight
|
|
|
|
if (motors.armed()) {
|
|
|
|
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:13 -03:00
|
|
|
if (!set_home_to_current_location(false)) {
|
|
|
|
// ignore this failure
|
|
|
|
}
|
2015-12-30 18:57:56 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// set_home_to_current_location_inflight - set home to current GPS location (horizontally) and EKF origin vertically
|
2017-02-03 17:33:27 -04:00
|
|
|
void Sub::set_home_to_current_location_inflight()
|
|
|
|
{
|
2015-12-30 18:57:56 -04:00
|
|
|
// get current location from EKF
|
|
|
|
Location temp_loc;
|
2019-06-28 10:55:49 -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-12-30 18:57:56 -04:00
|
|
|
temp_loc.alt = ekf_origin.alt;
|
2018-05-29 21:47:13 -03:00
|
|
|
if (!set_home(temp_loc, false)) {
|
|
|
|
// ignore this failure
|
|
|
|
}
|
2015-12-30 18:57:56 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// set_home_to_current_location - set home to current GPS location
|
2017-06-05 05:04:04 -03:00
|
|
|
bool Sub::set_home_to_current_location(bool lock)
|
2017-02-03 17:33:27 -04:00
|
|
|
{
|
2015-12-30 18:57:56 -04:00
|
|
|
// get current location from EKF
|
|
|
|
Location temp_loc;
|
2022-01-20 19:42:41 -04:00
|
|
|
if (ahrs.get_location(temp_loc)) {
|
2016-10-11 00:27:51 -03:00
|
|
|
|
2017-02-03 17:33:27 -04:00
|
|
|
// Make home always at the water's surface.
|
|
|
|
// This allows disarming and arming again at depth.
|
|
|
|
// This also ensures that mission items with relative altitude frame, are always
|
|
|
|
// relative to the water's surface, whether in a high elevation lake, or at sea level.
|
|
|
|
temp_loc.alt -= barometer.get_altitude() * 100.0f;
|
2017-06-05 05:04:04 -03:00
|
|
|
return set_home(temp_loc, lock);
|
2015-12-30 18:57:56 -04:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// set_home - sets ahrs home (used for RTL) to specified location
|
|
|
|
// returns true if home location set successfully
|
2017-06-05 05:04:04 -03:00
|
|
|
bool Sub::set_home(const Location& loc, bool lock)
|
2015-12-30 18:57:56 -04:00
|
|
|
{
|
2017-09-18 02:36:38 -03:00
|
|
|
// check if EKF origin has been set
|
|
|
|
Location ekf_origin;
|
|
|
|
if (!ahrs.get_origin(ekf_origin)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-12-30 18:57:56 -04:00
|
|
|
// set ahrs home (used for RTL)
|
2018-05-29 21:47:13 -03:00
|
|
|
if (!ahrs.set_home(loc)) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-12-30 18:57:56 -04:00
|
|
|
|
2017-06-05 05:04:04 -03:00
|
|
|
// lock home position
|
|
|
|
if (lock) {
|
2018-05-18 00:00:01 -03:00
|
|
|
ahrs.lock_home();
|
2017-06-05 05:04:04 -03:00
|
|
|
}
|
|
|
|
|
2015-12-30 18:57:56 -04:00
|
|
|
// return success
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// far_from_EKF_origin - checks if a location is too far from the EKF origin
|
|
|
|
// returns true if too far
|
2016-01-14 15:30:56 -04:00
|
|
|
bool Sub::far_from_EKF_origin(const Location& loc)
|
2015-12-30 18:57:56 -04:00
|
|
|
{
|
|
|
|
// check distance to EKF origin
|
2019-06-28 10:55:49 -03:00
|
|
|
Location ekf_origin;
|
|
|
|
return ahrs.get_origin(ekf_origin) && (ekf_origin.get_distance(loc) > EKF_ORIGIN_MAX_DIST_M);
|
2015-12-30 18:57:56 -04:00
|
|
|
}
|