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/ArduPlane/events.cpp
Views: 1798
1
#include "Plane.h"
2
3
// returns true if the vehicle is in landing sequence. Intended only
4
// for use in failsafe code.
5
bool Plane::failsafe_in_landing_sequence() const
6
{
7
if (flight_stage == AP_FixedWing::FlightStage::LAND) {
8
return true;
9
}
10
#if HAL_QUADPLANE_ENABLED
11
if (quadplane.in_vtol_land_sequence()) {
12
return true;
13
}
14
#endif
15
if (mission.get_in_landing_sequence_flag()) {
16
return true;
17
}
18
return false;
19
}
20
21
void Plane::failsafe_short_on_event(enum failsafe_state fstype, ModeReason reason)
22
{
23
// This is how to handle a short loss of control signal failsafe.
24
failsafe.state = fstype;
25
failsafe.short_timer_ms = millis();
26
failsafe.saved_mode_number = control_mode->mode_number();
27
switch (control_mode->mode_number())
28
{
29
case Mode::Number::MANUAL:
30
case Mode::Number::STABILIZE:
31
case Mode::Number::ACRO:
32
case Mode::Number::FLY_BY_WIRE_A:
33
case Mode::Number::AUTOTUNE:
34
case Mode::Number::FLY_BY_WIRE_B:
35
case Mode::Number::CRUISE:
36
case Mode::Number::TRAINING:
37
if(plane.emergency_landing) {
38
set_mode(mode_fbwa, reason); // emergency landing switch overrides normal action to allow out of range landing
39
break;
40
}
41
if(g.fs_action_short == FS_ACTION_SHORT_FBWA) {
42
set_mode(mode_fbwa, reason);
43
} else if (g.fs_action_short == FS_ACTION_SHORT_FBWB) {
44
set_mode(mode_fbwb, reason);
45
} else {
46
set_mode(mode_circle, reason); // circle if action = 0 or 1
47
}
48
break;
49
50
#if HAL_QUADPLANE_ENABLED
51
case Mode::Number::QSTABILIZE:
52
case Mode::Number::QLOITER:
53
case Mode::Number::QHOVER:
54
#if QAUTOTUNE_ENABLED
55
case Mode::Number::QAUTOTUNE:
56
#endif
57
case Mode::Number::QACRO:
58
if (quadplane.option_is_set(QuadPlane::OPTION::FS_RTL)) {
59
set_mode(mode_rtl, reason);
60
} else if (quadplane.option_is_set(QuadPlane::OPTION::FS_QRTL)) {
61
set_mode(mode_qrtl, reason);
62
} else {
63
set_mode(mode_qland, reason);
64
}
65
break;
66
#endif // HAL_QUADPLANE_ENABLED
67
68
case Mode::Number::AUTO:
69
#if MODE_AUTOLAND_ENABLED
70
case Mode::Number::AUTOLAND:
71
#endif
72
{
73
if (failsafe_in_landing_sequence()) {
74
// don't failsafe in a landing sequence
75
break;
76
}
77
FALLTHROUGH;
78
}
79
case Mode::Number::AVOID_ADSB:
80
case Mode::Number::GUIDED:
81
case Mode::Number::LOITER:
82
case Mode::Number::THERMAL:
83
if (g.fs_action_short != FS_ACTION_SHORT_BESTGUESS) { // if acton = 0(BESTGUESS) this group of modes take no action
84
failsafe.saved_mode_number = control_mode->mode_number();
85
if (g.fs_action_short == FS_ACTION_SHORT_FBWA) {
86
set_mode(mode_fbwa, reason);
87
} else if (g.fs_action_short == FS_ACTION_SHORT_FBWB) {
88
set_mode(mode_fbwb, reason);
89
} else {
90
set_mode(mode_circle, reason);
91
}
92
}
93
break;
94
case Mode::Number::CIRCLE: // these modes never take any short failsafe action and continue
95
case Mode::Number::TAKEOFF:
96
case Mode::Number::RTL:
97
#if HAL_QUADPLANE_ENABLED
98
case Mode::Number::QLAND:
99
case Mode::Number::QRTL:
100
case Mode::Number::LOITER_ALT_QLAND:
101
#endif
102
case Mode::Number::INITIALISING:
103
break;
104
}
105
if (failsafe.saved_mode_number != control_mode->mode_number()) {
106
gcs().send_text(MAV_SEVERITY_WARNING, "RC Short Failsafe: switched to %s", control_mode->name());
107
} else {
108
gcs().send_text(MAV_SEVERITY_WARNING, "RC Short Failsafe On");
109
}
110
}
111
112
void Plane::failsafe_long_on_event(enum failsafe_state fstype, ModeReason reason)
113
{
114
115
// This is how to handle a long loss of control signal failsafe.
116
// If the GCS is locked up we allow control to revert to RC
117
RC_Channels::clear_overrides();
118
failsafe.state = fstype;
119
switch (control_mode->mode_number())
120
{
121
case Mode::Number::MANUAL:
122
case Mode::Number::STABILIZE:
123
case Mode::Number::ACRO:
124
case Mode::Number::FLY_BY_WIRE_A:
125
case Mode::Number::AUTOTUNE:
126
case Mode::Number::FLY_BY_WIRE_B:
127
case Mode::Number::CRUISE:
128
case Mode::Number::TRAINING:
129
case Mode::Number::CIRCLE:
130
case Mode::Number::LOITER:
131
case Mode::Number::THERMAL:
132
case Mode::Number::TAKEOFF:
133
if (plane.flight_stage == AP_FixedWing::FlightStage::TAKEOFF && !(g.fs_action_long == FS_ACTION_LONG_GLIDE || g.fs_action_long == FS_ACTION_LONG_PARACHUTE)) {
134
// don't failsafe if in inital climb of TAKEOFF mode and FS action is not parachute or glide
135
// long failsafe will be re-called if still in fs after initial climb
136
long_failsafe_pending = true;
137
break;
138
}
139
140
if(plane.emergency_landing) {
141
set_mode(mode_fbwa, reason); // emergency landing switch overrides normal action to allow out of range landing
142
break;
143
}
144
if(g.fs_action_long == FS_ACTION_LONG_PARACHUTE) {
145
#if HAL_PARACHUTE_ENABLED
146
parachute_release();
147
#endif
148
} else if (g.fs_action_long == FS_ACTION_LONG_GLIDE) {
149
set_mode(mode_fbwa, reason);
150
} else if (g.fs_action_long == FS_ACTION_LONG_AUTO) {
151
set_mode(mode_auto, reason);
152
#if MODE_AUTOLAND_ENABLED
153
} else if (g.fs_action_long == FS_ACTION_LONG_AUTOLAND) {
154
if (!set_mode(mode_autoland, reason)) {
155
set_mode(mode_rtl, reason);
156
}
157
#endif
158
} else {
159
set_mode(mode_rtl, reason);
160
}
161
break;
162
163
#if HAL_QUADPLANE_ENABLED
164
case Mode::Number::QSTABILIZE:
165
case Mode::Number::QHOVER:
166
case Mode::Number::QLOITER:
167
case Mode::Number::QACRO:
168
#if QAUTOTUNE_ENABLED
169
case Mode::Number::QAUTOTUNE:
170
#endif
171
if (quadplane.option_is_set(QuadPlane::OPTION::FS_RTL)) {
172
set_mode(mode_rtl, reason);
173
} else if (quadplane.option_is_set(QuadPlane::OPTION::FS_QRTL)) {
174
set_mode(mode_qrtl, reason);
175
} else {
176
set_mode(mode_qland, reason);
177
}
178
break;
179
#endif // HAL_QUADPLANE_ENABLED
180
181
case Mode::Number::AUTO:
182
if (failsafe_in_landing_sequence()) {
183
// don't failsafe in a landing sequence
184
break;
185
}
186
187
#if HAL_QUADPLANE_ENABLED
188
if (quadplane.in_vtol_takeoff()) {
189
set_mode(mode_qland, reason);
190
// QLAND if in VTOL takeoff
191
break;
192
}
193
#endif
194
FALLTHROUGH;
195
196
case Mode::Number::AVOID_ADSB:
197
case Mode::Number::GUIDED:
198
199
if(g.fs_action_long == FS_ACTION_LONG_PARACHUTE) {
200
#if HAL_PARACHUTE_ENABLED
201
parachute_release();
202
#endif
203
} else if (g.fs_action_long == FS_ACTION_LONG_GLIDE) {
204
set_mode(mode_fbwa, reason);
205
} else if (g.fs_action_long == FS_ACTION_LONG_AUTO) {
206
set_mode(mode_auto, reason);
207
#if MODE_AUTOLAND_ENABLED
208
} else if (g.fs_action_long == FS_ACTION_LONG_AUTOLAND) {
209
if (!set_mode(mode_autoland, reason)) {
210
set_mode(mode_rtl, reason);
211
}
212
#endif
213
} else if (g.fs_action_long == FS_ACTION_LONG_RTL) {
214
set_mode(mode_rtl, reason);
215
}
216
break;
217
case Mode::Number::RTL:
218
if (g.fs_action_long == FS_ACTION_LONG_AUTO) {
219
set_mode(mode_auto, reason);
220
#if MODE_AUTOLAND_ENABLED
221
} else if (g.fs_action_long == FS_ACTION_LONG_AUTOLAND) {
222
set_mode(mode_autoland, reason);
223
#endif
224
}
225
break;
226
#if HAL_QUADPLANE_ENABLED
227
case Mode::Number::QLAND:
228
case Mode::Number::QRTL:
229
case Mode::Number::LOITER_ALT_QLAND:
230
#endif
231
case Mode::Number::INITIALISING:
232
#if MODE_AUTOLAND_ENABLED
233
case Mode::Number::AUTOLAND:
234
#endif
235
break;
236
}
237
gcs().send_text(MAV_SEVERITY_WARNING, "%s Failsafe On: %s", (reason == ModeReason:: GCS_FAILSAFE) ? "GCS" : "RC Long", control_mode->name());
238
}
239
240
void Plane::failsafe_short_off_event(ModeReason reason)
241
{
242
// We're back in radio contact
243
gcs().send_text(MAV_SEVERITY_WARNING, "Short Failsafe Cleared");
244
failsafe.state = FAILSAFE_NONE;
245
// restore entry mode if desired but check that our current mode is still due to failsafe
246
if (control_mode_reason == ModeReason::RADIO_FAILSAFE) {
247
set_mode_by_number(failsafe.saved_mode_number, ModeReason::RADIO_FAILSAFE_RECOVERY);
248
gcs().send_text(MAV_SEVERITY_INFO,"Flight mode %s restored",control_mode->name());
249
}
250
}
251
252
void Plane::failsafe_long_off_event(ModeReason reason)
253
{
254
long_failsafe_pending = false;
255
// We're back in radio contact with RC or GCS
256
if (reason == ModeReason:: GCS_FAILSAFE) {
257
gcs().send_text(MAV_SEVERITY_WARNING, "GCS Failsafe Off");
258
}
259
else {
260
gcs().send_text(MAV_SEVERITY_WARNING, "RC Long Failsafe Cleared");
261
}
262
failsafe.state = FAILSAFE_NONE;
263
}
264
265
void Plane::handle_battery_failsafe(const char *type_str, const int8_t action)
266
{
267
switch ((Failsafe_Action)action) {
268
#if HAL_QUADPLANE_ENABLED
269
case Failsafe_Action_Loiter_alt_QLand:
270
if (quadplane.available()) {
271
plane.set_mode(mode_loiter_qland, ModeReason::BATTERY_FAILSAFE);
272
break;
273
}
274
FALLTHROUGH;
275
276
case Failsafe_Action_QLand:
277
if (quadplane.available()) {
278
plane.set_mode(mode_qland, ModeReason::BATTERY_FAILSAFE);
279
break;
280
}
281
FALLTHROUGH;
282
#endif // HAL_QUADPLANE_ENABLED
283
case Failsafe_Action_Land: {
284
bool already_landing = flight_stage == AP_FixedWing::FlightStage::LAND;
285
#if HAL_QUADPLANE_ENABLED
286
if (control_mode == &mode_qland || control_mode == &mode_loiter_qland) {
287
already_landing = true;
288
}
289
#endif
290
if (!already_landing && plane.have_position) {
291
// never stop a landing if we were already committed
292
if (plane.mission.is_best_land_sequence(plane.current_loc)) {
293
// continue mission as it will reach a landing in less distance
294
plane.mission.set_in_landing_sequence_flag(true);
295
break;
296
}
297
if (plane.mission.jump_to_landing_sequence(plane.current_loc)) {
298
plane.set_mode(mode_auto, ModeReason::BATTERY_FAILSAFE);
299
break;
300
}
301
}
302
FALLTHROUGH;
303
}
304
case Failsafe_Action_RTL: {
305
bool already_landing = flight_stage == AP_FixedWing::FlightStage::LAND;
306
#if HAL_QUADPLANE_ENABLED
307
if (control_mode == &mode_qland || control_mode == &mode_loiter_qland ||
308
quadplane.in_vtol_land_sequence()) {
309
already_landing = true;
310
}
311
#endif
312
if (!already_landing) {
313
// never stop a landing if we were already committed
314
if ((g.rtl_autoland == RtlAutoland::RTL_IMMEDIATE_DO_LAND_START) && plane.have_position && plane.mission.is_best_land_sequence(plane.current_loc)) {
315
// continue mission as it will reach a landing in less distance
316
plane.mission.set_in_landing_sequence_flag(true);
317
break;
318
}
319
set_mode(mode_rtl, ModeReason::BATTERY_FAILSAFE);
320
aparm.throttle_cruise.load();
321
}
322
break;
323
}
324
325
case Failsafe_Action_Terminate:
326
#if AP_ADVANCEDFAILSAFE_ENABLED
327
char battery_type_str[17];
328
snprintf(battery_type_str, 17, "%s battery", type_str);
329
afs.gcs_terminate(true, battery_type_str);
330
#else
331
arming.disarm(AP_Arming::Method::FAILSAFE_ACTION_TERMINATE);
332
#endif
333
break;
334
335
case Failsafe_Action_Parachute:
336
#if HAL_PARACHUTE_ENABLED
337
parachute_release();
338
#endif
339
break;
340
341
case Failsafe_Action_None:
342
// don't actually do anything, however we should still flag the system as having hit a failsafe
343
// and ensure all appropriate flags are going off to the user
344
break;
345
}
346
}
347
348