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/ArduSub/commands.cpp
Views: 1798
1
#include "Sub.h"
2
3
// checks if we should update ahrs/RTL home position from the EKF
4
void Sub::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 this 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 Sub::set_home_to_current_location_inflight()
24
{
25
// get current location from EKF
26
Location temp_loc;
27
Location ekf_origin;
28
if (ahrs.get_location(temp_loc) && ahrs.get_origin(ekf_origin)) {
29
temp_loc.alt = ekf_origin.alt;
30
if (!set_home(temp_loc, false)) {
31
// ignore this failure
32
}
33
}
34
}
35
36
// set_home_to_current_location - set home to current GPS location
37
bool Sub::set_home_to_current_location(bool lock)
38
{
39
// get current location from EKF
40
Location temp_loc;
41
if (ahrs.get_location(temp_loc)) {
42
43
// Make home always at the water's surface.
44
// This allows disarming and arming again at depth.
45
// This also ensures that mission items with relative altitude frame, are always
46
// relative to the water's surface, whether in a high elevation lake, or at sea level.
47
temp_loc.alt -= barometer.get_altitude() * 100.0f;
48
return set_home(temp_loc, lock);
49
}
50
return false;
51
}
52
53
// set_home - sets ahrs home (used for RTL) to specified location
54
// returns true if home location set successfully
55
bool Sub::set_home(const Location& loc, bool lock)
56
{
57
// check if EKF origin has been set
58
Location ekf_origin;
59
if (!ahrs.get_origin(ekf_origin)) {
60
return false;
61
}
62
63
// set ahrs home (used for RTL)
64
if (!ahrs.set_home(loc)) {
65
return false;
66
}
67
68
// lock home position
69
if (lock) {
70
ahrs.lock_home();
71
}
72
73
// return success
74
return true;
75
}
76
77