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)
|
2017-06-05 05:03:50 -03:00
|
|
|
set_home_to_current_location(false);
|
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;
|
|
|
|
if (inertial_nav.get_location(temp_loc)) {
|
|
|
|
const struct Location &ekf_origin = inertial_nav.get_origin();
|
|
|
|
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;
|
|
|
|
if (inertial_nav.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
|
|
|
{
|
|
|
|
// check location is valid
|
|
|
|
if (loc.lat == 0 && loc.lng == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-03-31 04:07:46 -03: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)
|
2014-03-31 04:07:46 -03:00
|
|
|
ahrs.set_home(loc);
|
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
|
|
|
// update navigation scalers. used to offset the shrinking longitude as we go towards the poles
|
|
|
|
scaleLongDown = longitude_scale(loc);
|
|
|
|
// 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;
|
|
|
|
if (mission.read_cmd_from_storage(0, temp_cmd)) {
|
2015-06-23 23:13:10 -03:00
|
|
|
DataFlash.Log_Write_Mission_Cmd(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-07-05 05:25:28 -03:00
|
|
|
// log ahrs home and ekf origin dataflash
|
2018-05-16 00:29:13 -03:00
|
|
|
ahrs.Log_Write_Home_And_Origin();
|
2015-07-05 05:25:28 -03:00
|
|
|
|
2017-09-17 22:37:55 -03:00
|
|
|
// send new home and ekf origin to GCS
|
2018-05-16 01:49:07 -03:00
|
|
|
gcs().send_home();
|
|
|
|
gcs().send_ekf_origin();
|
2015-10-02 05:56:31 -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
|
|
|
|
const struct Location &ekf_origin = inertial_nav.get_origin();
|
|
|
|
if (get_distance(ekf_origin, loc) > EKF_ORIGIN_MAX_DIST_M) {
|
|
|
|
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
|
|
|
}
|
2010-12-19 12:40:33 -04:00
|
|
|
|
2015-02-09 07:23:49 -04:00
|
|
|
// checks if we should update ahrs/RTL home position from GPS
|
2015-05-29 23:12:49 -03:00
|
|
|
void Copter::set_system_time_from_GPS()
|
2015-02-09 07:23:49 -04:00
|
|
|
{
|
|
|
|
// exit immediately if system time already set
|
|
|
|
if (ap.system_time_set) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we have a 3d lock and valid location
|
|
|
|
if (gps.status() >= AP_GPS::GPS_OK_FIX_3D) {
|
|
|
|
// set system clock for log timestamps
|
2016-01-21 02:57:52 -04:00
|
|
|
uint64_t gps_timestamp = gps.time_epoch_usec();
|
|
|
|
|
|
|
|
hal.util->set_system_clock(gps_timestamp);
|
|
|
|
|
|
|
|
// update signing timestamp
|
|
|
|
GCS_MAVLINK::update_signing_timestamp(gps_timestamp);
|
|
|
|
|
2015-02-09 07:23:49 -04:00
|
|
|
ap.system_time_set = true;
|
|
|
|
Log_Write_Event(DATA_SYSTEM_TIME_SET);
|
|
|
|
}
|
|
|
|
}
|