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;
|
|
|
|
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;
|
2019-06-28 10:55:36 -03: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
|
|
|
|
// initialises inertial nav and compass on first call
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2018-05-17 23:58:50 -03:00
|
|
|
const bool home_was_set = ahrs.home_is_set();
|
|
|
|
|
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
|
|
|
|
2015-02-09 07:23:49 -04:00
|
|
|
// init inav and compass declination
|
2018-05-17 23:58:50 -03:00
|
|
|
if (!home_was_set) {
|
2015-02-09 07:23:49 -04:00
|
|
|
// record home is set
|
2018-03-15 22:23:28 -03:00
|
|
|
Log_Write_Event(DATA_SET_HOME);
|
2012-11-07 06:03:30 -04:00
|
|
|
|
2018-02-21 22:06:07 -04:00
|
|
|
#if MODE_AUTO_ENABLED == ENABLED
|
2015-02-26 01:36:06 -04:00
|
|
|
// log new home position which mission library will pull from ahrs
|
|
|
|
if (should_log(MASK_LOG_CMD)) {
|
|
|
|
AP_Mission::Mission_Command temp_cmd;
|
2018-12-11 13:11:49 -04:00
|
|
|
if (mode_auto.mission.read_cmd_from_storage(0, temp_cmd)) {
|
2019-01-18 00:24:08 -04:00
|
|
|
logger.Write_Mission_Cmd(mode_auto.mission, temp_cmd);
|
2015-02-26 01:36:06 -04:00
|
|
|
}
|
2014-03-10 23:27:53 -03:00
|
|
|
}
|
2018-02-21 22:06:07 -04:00
|
|
|
#endif
|
2014-02-27 22:16:33 -04:00
|
|
|
}
|
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;
|
|
|
|
if (ahrs.get_origin(ekf_origin) && (ekf_origin.get_distance(loc) > EKF_ORIGIN_MAX_DIST_M)) {
|
2015-02-09 07:23:49 -04:00
|
|
|
return true;
|
|
|
|
}
|
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
|
|
|
}
|