CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
Ardupilot

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: Ardupilot/ardupilot
Path: blob/master/ArduCopter/commands.cpp
Views: 1798
1
#include "Copter.h"
2
3
// checks if we should update ahrs/RTL home position from the EKF
4
void Copter::update_home_from_EKF()
5
{
6
// exit immediately if home already set
7
if (ahrs.home_is_set()) {
8
return;
9
}
10
11
// special logic if home is set in-flight
12
if (motors->armed()) {
13
set_home_to_current_location_inflight();
14
} else {
15
// move home to current ekf location (this will set home_state to HOME_SET)
16
if (!set_home_to_current_location(false)) {
17
// ignore failure
18
}
19
}
20
}
21
22
// set_home_to_current_location_inflight - set home to current GPS location (horizontally) and EKF origin vertically
23
void Copter::set_home_to_current_location_inflight() {
24
// get current location from EKF
25
Location temp_loc;
26
Location ekf_origin;
27
if (ahrs.get_location(temp_loc) && ahrs.get_origin(ekf_origin)) {
28
temp_loc.alt = ekf_origin.alt;
29
if (!set_home(temp_loc, false)) {
30
return;
31
}
32
// we have successfully set AHRS home, set it for SmartRTL
33
#if MODE_SMARTRTL_ENABLED
34
g2.smart_rtl.set_home(true);
35
#endif
36
}
37
}
38
39
// set_home_to_current_location - set home to current GPS location
40
bool Copter::set_home_to_current_location(bool lock) {
41
// get current location from EKF
42
Location temp_loc;
43
if (ahrs.get_location(temp_loc)) {
44
if (!set_home(temp_loc, lock)) {
45
return false;
46
}
47
// we have successfully set AHRS home, set it for SmartRTL
48
#if MODE_SMARTRTL_ENABLED
49
g2.smart_rtl.set_home(true);
50
#endif
51
return true;
52
}
53
return false;
54
}
55
56
// set_home - sets ahrs home (used for RTL) to specified location
57
// returns true if home location set successfully
58
bool Copter::set_home(const Location& loc, bool lock)
59
{
60
// check EKF origin has been set
61
Location ekf_origin;
62
if (!ahrs.get_origin(ekf_origin)) {
63
return false;
64
}
65
66
// set ahrs home (used for RTL)
67
if (!ahrs.set_home(loc)) {
68
return false;
69
}
70
71
// lock home position
72
if (lock) {
73
ahrs.lock_home();
74
}
75
76
// return success
77
return true;
78
}
79
80