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/libraries/AP_AdvancedFailsafe/AP_AdvancedFailsafe.h
Views: 1798
1
#pragma once
2
3
/*
4
This program is free software: you can redistribute it and/or modify
5
it under the terms of the GNU General Public License as published by
6
the Free Software Foundation, either version 3 of the License, or
7
(at your option) any later version.
8
9
This program is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
GNU General Public License for more details.
13
14
You should have received a copy of the GNU General Public License
15
along with this program. If not, see <http://www.gnu.org/licenses/>.
16
*/
17
/*
18
Outback Challenge Failsafe module
19
20
Andrew Tridgell and CanberraUAV, August 2012
21
*/
22
23
#include "AP_AdvancedFailsafe_config.h"
24
25
#if AP_ADVANCEDFAILSAFE_ENABLED
26
27
#include <AP_Common/AP_Common.h>
28
#include <AP_Param/AP_Param.h>
29
#include <inttypes.h>
30
#include <AP_Common/Location.h>
31
32
class AP_AdvancedFailsafe
33
{
34
public:
35
enum control_mode {
36
AFS_MANUAL = 0,
37
AFS_STABILIZED = 1,
38
AFS_AUTO = 2
39
};
40
41
enum state {
42
STATE_PREFLIGHT = 0,
43
STATE_AUTO = 1,
44
STATE_DATA_LINK_LOSS = 2,
45
STATE_GPS_LOSS = 3
46
};
47
48
enum terminate_action {
49
TERMINATE_ACTION_TERMINATE = 42,
50
TERMINATE_ACTION_LAND = 43
51
};
52
53
/* Do not allow copies */
54
CLASS_NO_COPY(AP_AdvancedFailsafe);
55
56
// Constructor
57
AP_AdvancedFailsafe()
58
{
59
AP_Param::setup_object_defaults(this, var_info);
60
if (_singleton != nullptr) {
61
AP_HAL::panic("AP_AdvancedFailsafe must be singleton");
62
}
63
64
_singleton = this;
65
_state = STATE_PREFLIGHT;
66
_terminate.set(0);
67
68
_saved_wp = 0;
69
}
70
71
// get singleton instance
72
static AP_AdvancedFailsafe *get_singleton(void) {
73
return _singleton;
74
}
75
76
bool enabled() { return _enable; }
77
78
// check that everything is OK
79
void check(uint32_t last_valid_rc_ms);
80
81
// generate heartbeat msgs, so external failsafe boards are happy
82
// during sensor calibration
83
void heartbeat(void);
84
85
// return true if we are terminating (deliberately crashing the vehicle)
86
bool should_crash_vehicle(void);
87
88
// enables or disables a GCS based termination, returns true if AFS is in the desired termination state
89
bool gcs_terminate(bool should_terminate, const char *reason);
90
91
// called to set all outputs to termination state
92
virtual void terminate_vehicle(void) = 0;
93
94
// for holding parameters
95
static const struct AP_Param::GroupInfo var_info[];
96
97
bool terminating_vehicle_via_landing() const {
98
return _terminate_action == TERMINATE_ACTION_LAND;
99
};
100
101
protected:
102
// setup failsafe values for if FMU firmware stops running
103
virtual void setup_IO_failsafe(void) = 0;
104
105
// return the AFS mapped control mode
106
virtual enum control_mode afs_mode(void) = 0;
107
108
//to force entering auto mode when datalink loss
109
virtual void set_mode_auto(void) = 0;
110
111
enum state _state;
112
113
AP_Int8 _enable;
114
// digital output pins for communicating with the failsafe board
115
AP_Int8 _heartbeat_pin;
116
AP_Int8 _manual_pin;
117
AP_Int8 _terminate_pin;
118
AP_Int8 _terminate;
119
AP_Int8 _terminate_action;
120
121
// waypoint numbers to jump to on failsafe conditions
122
AP_Int8 _wp_comms_hold;
123
AP_Int8 _wp_gps_loss;
124
125
AP_Float _qnh_pressure;
126
AP_Int32 _amsl_limit;
127
AP_Int32 _amsl_margin_gps;
128
AP_Float _rc_fail_time_seconds;
129
AP_Float _gcs_fail_time_seconds;
130
AP_Int8 _max_gps_loss;
131
AP_Int8 _max_comms_loss;
132
AP_Int8 _enable_geofence_fs;
133
AP_Int8 _enable_RC_fs;
134
AP_Int8 _rc_term_manual_only;
135
AP_Int8 _enable_dual_loss;
136
AP_Int16 _max_range_km;
137
138
bool _heartbeat_pin_value;
139
140
// saved waypoint for resuming mission
141
uint8_t _saved_wp;
142
143
// number of times we've lost GPS
144
uint8_t _gps_loss_count;
145
146
// number of times we've lost data link
147
uint8_t _comms_loss_count;
148
149
// last comms loss time
150
uint32_t _last_comms_loss_ms;
151
152
// last GPS loss time
153
uint32_t _last_gps_loss_ms;
154
155
// have the failsafe values been setup?
156
bool _failsafe_setup:1;
157
158
Location _first_location;
159
bool _have_first_location;
160
uint32_t _term_range_notice_ms;
161
162
bool check_altlimit(void);
163
164
private:
165
static AP_AdvancedFailsafe *_singleton;
166
167
// update maximum range check
168
void max_range_update();
169
170
AP_Int16 options;
171
enum class Option {
172
CONTINUE_AFTER_RECOVERED = (1U<<0),
173
GCS_FS_ALL_AUTONOMOUS_MODES = (1U<<1),
174
};
175
bool option_is_set(Option option) const {
176
return (options.get() & int16_t(option)) != 0;
177
}
178
};
179
180
namespace AP {
181
AP_AdvancedFailsafe *advancedfailsafe();
182
};
183
184
#endif // AP_ADVANCEDFAILSAFE_ENABLED
185
186