2015-05-29 23:12:49 -03:00
|
|
|
#include "Copter.h"
|
|
|
|
|
2015-02-09 07:23:49 -04:00
|
|
|
/*
|
|
|
|
* the home_state has a number of possible values (see enum HomeState in defines.h's)
|
|
|
|
* HOME_UNSET = home is not set, no GPS positions yet received
|
|
|
|
* HOME_SET_NOT_LOCKED = home is set to EKF origin or armed location (can be moved)
|
|
|
|
* HOME_SET_AND_LOCKED = home has been set by user, cannot be moved except by user initiated do-set-home command
|
|
|
|
*/
|
|
|
|
|
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
|
|
|
|
if (ap.home_state != HOME_UNSET) {
|
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;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
if (ap.home_state == HOME_UNSET) {
|
|
|
|
// update navigation scalers. used to offset the shrinking longitude as we go towards the poles
|
|
|
|
scaleLongDown = longitude_scale(loc);
|
|
|
|
// record home is set
|
|
|
|
set_home_state(HOME_SET_NOT_LOCKED);
|
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) {
|
|
|
|
set_home_state(HOME_SET_AND_LOCKED);
|
|
|
|
}
|
|
|
|
|
2015-07-05 05:25:28 -03:00
|
|
|
// log ahrs home and ekf origin dataflash
|
|
|
|
Log_Write_Home_And_Origin();
|
|
|
|
|
2017-09-17 22:37:55 -03:00
|
|
|
// send new home and ekf origin to GCS
|
2017-07-08 01:42:22 -03:00
|
|
|
gcs().send_home(loc);
|
2017-09-17 22:37:55 -03:00
|
|
|
gcs().send_ekf_origin(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
|
|
|
}
|
|
|
|
|
2017-09-17 22:37:55 -03:00
|
|
|
// sets ekf_origin if it has not been set.
|
|
|
|
// should only be used when there is no GPS to provide an absolute position
|
|
|
|
void Copter::set_ekf_origin(const Location& loc)
|
|
|
|
{
|
|
|
|
// check location is valid
|
|
|
|
if (!check_latlng(loc)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check EKF origin has already been set
|
|
|
|
Location ekf_origin;
|
|
|
|
if (ahrs.get_origin(ekf_origin)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ahrs.set_origin(loc)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// log ahrs home and ekf origin dataflash
|
|
|
|
Log_Write_Home_And_Origin();
|
|
|
|
|
|
|
|
// send ekf origin to GCS
|
|
|
|
gcs().send_ekf_origin(loc);
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|