Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Ardupilot
GitHub Repository: Ardupilot/ardupilot
Path: blob/master/ArduPlane/AP_Arming_Plane.cpp
9659 views
1
/*
2
additional arming checks for plane
3
*/
4
#include "AP_Arming_Plane.h"
5
#include "Plane.h"
6
7
#include "qautotune.h"
8
9
constexpr uint32_t AP_ARMING_DELAY_MS = 2000; // delay from arming to start of motor spoolup
10
11
const AP_Param::GroupInfo AP_Arming_Plane::var_info[] = {
12
// variables from parent vehicle
13
AP_NESTEDGROUPINFO(AP_Arming, 0),
14
15
// index 3 was RUDDER and should not be used
16
17
#if AP_PLANE_BLACKBOX_LOGGING
18
// @Param: BBOX_SPD
19
// @DisplayName: Blackbox speed
20
// @Description: This is a 3D GPS speed threshold above which we will force arm the vehicle to start logging. WARNING: This should only be used on a vehicle with no propellers attached to the flight controller and when the flight controller is not in control of the vehicle.
21
// @Units: m/s
22
// @Increment: 1
23
// @Range: 1 20
24
// @User: Advanced
25
AP_GROUPINFO("BBOX_SPD", 4, AP_Arming_Plane, blackbox_speed, 5),
26
#endif // AP_PLANE_BLACKBOX_LOGGING
27
28
AP_GROUPEND
29
};
30
31
// expected to return true if the terrain database is required to have
32
// all data loaded
33
bool AP_Arming_Plane::terrain_database_required() const
34
{
35
#if AP_TERRAIN_AVAILABLE
36
if (plane.g.terrain_follow) {
37
return true;
38
}
39
#endif
40
return AP_Arming::terrain_database_required();
41
}
42
43
/*
44
additional arming checks for plane
45
46
*/
47
bool AP_Arming_Plane::pre_arm_checks(bool display_failure)
48
{
49
if (armed || require == (uint8_t)Required::NO) {
50
// if we are already armed or don't need any arming checks
51
// then skip the checks
52
return true;
53
}
54
if (!hal.scheduler->is_system_initialized()) {
55
check_failed(display_failure, "System not initialised");
56
return false;
57
}
58
// are arming checks disabled?
59
if (should_skip_all_checks()) {
60
return mandatory_checks(display_failure);
61
}
62
if (hal.util->was_watchdog_armed()) {
63
// on watchdog reset bypass arming checks to allow for
64
// in-flight arming if we were armed before the reset. This
65
// allows a reset on a BVLOS flight to return home if the
66
// operator can command arming over telemetry
67
return true;
68
}
69
70
// call parent class checks
71
bool ret = AP_Arming::pre_arm_checks(display_failure);
72
73
#if AP_AIRSPEED_ENABLED
74
// Check airspeed sensor
75
ret &= AP_Arming::airspeed_checks(display_failure);
76
#endif
77
78
if (plane.aparm.roll_limit < 3) {
79
check_failed(display_failure, "ROLL_LIMIT_DEG too small (%.1f)", plane.aparm.roll_limit.get());
80
ret = false;
81
}
82
83
if (plane.aparm.pitch_limit_max < 3) {
84
check_failed(display_failure, "PTCH_LIM_MAX_DEG too small (%.1f)", plane.aparm.pitch_limit_max.get());
85
ret = false;
86
}
87
88
if (plane.aparm.pitch_limit_min > -3) {
89
check_failed(display_failure, "PTCH_LIM_MIN_DEG too large (%.1f)", plane.aparm.pitch_limit_min.get());
90
ret = false;
91
}
92
93
if (plane.aparm.airspeed_min < MIN_AIRSPEED_MIN) {
94
check_failed(display_failure, "AIRSPEED_MIN too low (%i < %i)", plane.aparm.airspeed_min.get(), MIN_AIRSPEED_MIN);
95
ret = false;
96
}
97
98
if (plane.channel_throttle->get_reverse() &&
99
plane.g.throttle_fs_enabled != Plane::ThrFailsafe::Disabled &&
100
plane.g.throttle_fs_value <
101
plane.channel_throttle->get_radio_max()) {
102
check_failed(display_failure, "Invalid THR_FS_VALUE for rev throttle");
103
ret = false;
104
}
105
106
ret &= rc_received_if_enabled_check(display_failure);
107
108
#if HAL_QUADPLANE_ENABLED
109
ret &= quadplane_checks(display_failure);
110
#endif
111
112
// check adsb avoidance failsafe
113
if (plane.failsafe.adsb) {
114
check_failed(display_failure, "ADSB threat detected");
115
ret = false;
116
}
117
118
if (plane.flight_option_enabled(FlightOptions::CENTER_THROTTLE_TRIM)){
119
int16_t trim = plane.channel_throttle->get_radio_trim();
120
if (trim < 1250 || trim > 1750) {
121
check_failed(display_failure, "Throttle trim not near center stick(%u)",trim );
122
ret = false;
123
}
124
}
125
126
if (plane.mission.get_in_landing_sequence_flag() &&
127
!plane.mission.starts_with_takeoff_cmd()) {
128
check_failed(display_failure,"In landing sequence");
129
ret = false;
130
}
131
132
char failure_msg[50] {};
133
if (!plane.control_mode->pre_arm_checks(ARRAY_SIZE(failure_msg), failure_msg)) {
134
check_failed(display_failure, "%s %s", plane.control_mode->name(), failure_msg);
135
return false;
136
}
137
138
return ret;
139
}
140
141
bool AP_Arming_Plane::mandatory_checks(bool display_failure)
142
{
143
bool ret = true;
144
145
ret &= rc_received_if_enabled_check(display_failure);
146
147
// Call parent class checks
148
ret &= AP_Arming::mandatory_checks(display_failure);
149
150
return ret;
151
}
152
153
154
#if HAL_QUADPLANE_ENABLED
155
bool AP_Arming_Plane::quadplane_checks(bool display_failure)
156
{
157
if (!plane.quadplane.enabled()) {
158
return true;
159
}
160
161
if (!plane.quadplane.available()) {
162
check_failed(display_failure, "Quadplane enabled but not running");
163
return false;
164
}
165
166
bool ret = true;
167
168
if (plane.scheduler.get_loop_rate_hz() < 100) {
169
check_failed(display_failure, "quadplane needs SCHED_LOOP_RATE >= 100");
170
ret = false;
171
}
172
173
char failure_msg[50] {};
174
if (!plane.quadplane.motors->arming_checks(ARRAY_SIZE(failure_msg), failure_msg)) {
175
check_failed(display_failure, "Motors: %s", failure_msg);
176
ret = false;
177
}
178
179
if ((plane.quadplane.tailsitter.enable > 0) && (plane.quadplane.tiltrotor.enable > 0)) {
180
check_failed(Check::PARAMETERS, display_failure, "set TAILSIT_ENABLE 0 or TILT_ENABLE 0");
181
ret = false;
182
183
} else {
184
185
if ((plane.quadplane.tailsitter.enable > 0) && !plane.quadplane.tailsitter.enabled()) {
186
check_failed(Check::PARAMETERS, display_failure, "tailsitter setup not complete, reboot");
187
ret = false;
188
}
189
190
if ((plane.quadplane.tiltrotor.enable > 0) && !plane.quadplane.tiltrotor.enabled()) {
191
check_failed(Check::PARAMETERS, display_failure, "tiltrotor setup not complete, reboot");
192
ret = false;
193
}
194
}
195
196
// ensure controllers are OK with us arming:
197
if (!plane.quadplane.pos_control->pre_arm_checks("Q_P", failure_msg, ARRAY_SIZE(failure_msg))) {
198
check_failed(Check::PARAMETERS, display_failure, "Bad parameter: %s", failure_msg);
199
ret = false;
200
}
201
if (!plane.quadplane.attitude_control->pre_arm_checks("Q_A", failure_msg, ARRAY_SIZE(failure_msg))) {
202
check_failed(Check::PARAMETERS, display_failure, "Bad parameter: %s", failure_msg);
203
ret = false;
204
}
205
206
/*
207
Q_ASSIST_SPEED really should be enabled for all quadplanes except tailsitters
208
*/
209
if (check_enabled(Check::PARAMETERS) &&
210
is_zero(plane.quadplane.assist.speed) &&
211
!plane.quadplane.tailsitter.enabled()) {
212
check_failed(display_failure,"Q_ASSIST_SPEED is not set");
213
ret = false;
214
}
215
216
if ((plane.quadplane.tailsitter.enable > 0) && (plane.quadplane.q_fwd_thr_use != QuadPlane::FwdThrUse::OFF)) {
217
check_failed(Check::PARAMETERS, display_failure, "set Q_FWD_THR_USE to 0");
218
ret = false;
219
}
220
221
// combining Q_RTL_MODE with either of the RTL_AUTOLAND options
222
// leads to precedence questions, so just don't allow it:
223
if (plane.g.rtl_autoland != RtlAutoland::RTL_DISABLE &&
224
plane.quadplane.rtl_mode != QuadPlane::RTL_MODE::NONE) {
225
check_failed(Check::PARAMETERS, display_failure, "unset one of RTL_AUTOLAND or Q_RTL_MODE");
226
ret = false;
227
}
228
229
return ret;
230
}
231
#endif // HAL_QUADPLANE_ENABLED
232
233
bool AP_Arming_Plane::ins_checks(bool display_failure)
234
{
235
// call parent class checks
236
if (!AP_Arming::ins_checks(display_failure)) {
237
return false;
238
}
239
240
// additional plane specific checks
241
if (check_enabled(Check::INS)) {
242
char failure_msg[50] = {};
243
if (!AP::ahrs().pre_arm_check(true, failure_msg, sizeof(failure_msg))) {
244
check_failed(Check::INS, display_failure, "AHRS: %s", failure_msg);
245
return false;
246
}
247
}
248
249
return true;
250
}
251
252
bool AP_Arming_Plane::arm_checks(AP_Arming::Method method)
253
{
254
255
// are arming checks disabled?
256
if (should_skip_all_checks()) {
257
return true;
258
}
259
260
if (hal.util->was_watchdog_armed()) {
261
// on watchdog reset bypass arming checks to allow for
262
// in-flight arming if we were armed before the reset. This
263
// allows a reset on a BVLOS flight to return home if the
264
// operator can command arming over telemetry
265
gcs().send_text(MAV_SEVERITY_WARNING, "watchdog: Bypassing arming checks");
266
return true;
267
}
268
269
// call parent class checks
270
return AP_Arming::arm_checks(method);
271
}
272
273
/*
274
update HAL soft arm state
275
*/
276
void AP_Arming_Plane::change_arm_state(void)
277
{
278
update_soft_armed();
279
#if HAL_QUADPLANE_ENABLED
280
plane.quadplane.set_armed(hal.util->get_soft_armed());
281
#endif
282
}
283
284
bool AP_Arming_Plane::arm(const AP_Arming::Method method, const bool do_arming_checks)
285
{
286
if (!AP_Arming::arm(method, do_arming_checks)) {
287
return false;
288
}
289
290
if (plane.update_home()) {
291
// after update_home the home position could still be
292
// different from the current_loc if the EKF refused the
293
// resetHeightDatum call. If we are updating home then we want
294
// to force the home to be the current_loc so relative alt
295
// takeoffs work correctly
296
if (plane.ahrs.set_home(plane.current_loc)) {
297
// update current_loc
298
plane.update_current_loc();
299
}
300
}
301
302
change_arm_state();
303
304
// rising edge of delay_arming oneshot
305
delay_arming = true;
306
307
#if MODE_AUTOLAND_ENABLED
308
plane.mode_autoland.arm_check();
309
#endif
310
311
if (method == AP_Arming::Method::RUDDER) {
312
// initialise the timer used to warn the user they're holding
313
// their stick over:
314
plane.takeoff_state.rudder_takeoff_warn_ms = AP_HAL::millis();
315
}
316
317
send_arm_disarm_statustext("Throttle armed");
318
319
return true;
320
}
321
322
/*
323
disarm motors
324
*/
325
bool AP_Arming_Plane::disarm(const AP_Arming::Method method, bool do_disarm_checks)
326
{
327
if (do_disarm_checks &&
328
(AP_Arming::method_is_GCS(method) ||
329
method == AP_Arming::Method::RUDDER)) {
330
if (plane.is_flying()) {
331
// don't allow mavlink or rudder disarm while flying
332
return false;
333
}
334
}
335
336
if (!AP_Arming::disarm(method, do_disarm_checks)) {
337
return false;
338
}
339
if (plane.control_mode != &plane.mode_auto) {
340
// reset the mission on disarm if we are not in auto
341
plane.mission.reset();
342
}
343
344
// suppress the throttle in auto-throttle modes
345
plane.throttle_suppressed = plane.control_mode->does_auto_throttle();
346
347
// if no airmode switch assigned, ensure airmode is off:
348
#if HAL_QUADPLANE_ENABLED
349
if ((plane.quadplane.air_mode == AirMode::ON) && (rc().find_channel_for_option(RC_Channel::AUX_FUNC::AIRMODE) == nullptr)) {
350
plane.quadplane.air_mode = AirMode::OFF;
351
}
352
#endif
353
354
//only log if disarming was successful
355
change_arm_state();
356
357
#if QAUTOTUNE_ENABLED
358
// Possibly save auto tuned parameters
359
plane.quadplane.qautotune.disarmed(plane.control_mode == &plane.mode_qautotune);
360
#endif
361
362
// re-initialize speed variable used in AUTO and GUIDED for
363
// DO_CHANGE_SPEED commands
364
plane.new_airspeed_cm = -1;
365
366
#if MODE_AUTOLAND_ENABLED
367
// takeoff direction always cleared on disarm
368
plane.takeoff_state.initial_direction.initialized = false;
369
#endif
370
send_arm_disarm_statustext("Throttle disarmed");
371
return true;
372
}
373
374
void AP_Arming_Plane::update_soft_armed()
375
{
376
bool _armed = is_armed();
377
#if HAL_QUADPLANE_ENABLED
378
if (plane.quadplane.motor_test.running){
379
_armed = true;
380
}
381
#endif
382
383
hal.util->set_soft_armed(_armed);
384
#if HAL_LOGGING_ENABLED
385
AP::logger().set_vehicle_armed(hal.util->get_soft_armed());
386
#endif
387
388
// update delay_arming oneshot
389
if (delay_arming &&
390
(AP_HAL::millis() - hal.util->get_last_armed_change() >= AP_ARMING_DELAY_MS)) {
391
392
delay_arming = false;
393
}
394
395
#if AP_PLANE_BLACKBOX_LOGGING
396
if (blackbox_speed > 0) {
397
const float speed3d = plane.gps.status() >= AP_GPS::GPS_OK_FIX_3D?plane.gps.velocity().length():0;
398
const uint32_t now = AP_HAL::millis();
399
if (speed3d > blackbox_speed) {
400
last_over_3dspeed_ms = now;
401
}
402
if (!_armed && speed3d > blackbox_speed) {
403
// force safety on so we don't run motors
404
hal.rcout->force_safety_on();
405
AP_Param::set_by_name("RC_PROTOCOLS", 0);
406
arm(Method::BLACKBOX, false);
407
gcs().send_text(MAV_SEVERITY_WARNING, "BlackBox: arming at %.1f m/s", speed3d);
408
}
409
if (_armed && now - last_over_3dspeed_ms > 20000U) {
410
gcs().send_text(MAV_SEVERITY_WARNING, "BlackBox: disarming at %.1f m/s", speed3d);
411
disarm(Method::BLACKBOX, false);
412
}
413
}
414
#endif
415
}
416
417
/*
418
extra plane mission checks
419
*/
420
bool AP_Arming_Plane::mission_checks(bool report)
421
{
422
// base checks
423
bool ret = AP_Arming::mission_checks(report);
424
if (plane.g.rtl_autoland == RtlAutoland::RTL_DISABLE) {
425
if (plane.mission.contains_item(MAV_CMD_DO_LAND_START)) {
426
ret = false;
427
check_failed(Check::MISSION, report, "DO_LAND_START set and RTL_AUTOLAND disabled");
428
}
429
if (plane.mission.contains_item(MAV_CMD_DO_RETURN_PATH_START)) {
430
ret = false;
431
check_failed(Check::MISSION, report, "DO_RETURN_PATH_START set and RTL_AUTOLAND disabled");
432
}
433
}
434
#if HAL_QUADPLANE_ENABLED
435
if (plane.quadplane.available()) {
436
const uint16_t num_commands = plane.mission.num_commands();
437
AP_Mission::Mission_Command prev_cmd {};
438
for (uint16_t i=1; i<num_commands; i++) {
439
AP_Mission::Mission_Command cmd;
440
if (!plane.mission.read_cmd_from_storage(i, cmd)) {
441
break;
442
}
443
if (plane.is_land_command(cmd.id) &&
444
prev_cmd.id == MAV_CMD_NAV_WAYPOINT) {
445
const float dist = cmd.content.location.get_distance(prev_cmd.content.location);
446
const float tecs_land_speed = plane.TECS_controller.get_land_airspeed();
447
const float landing_speed = is_positive(tecs_land_speed)?tecs_land_speed:plane.aparm.airspeed_cruise;
448
const float min_dist = 0.75 * plane.quadplane.stopping_distance_m(sq(landing_speed));
449
if (dist < min_dist) {
450
ret = false;
451
check_failed(Check::MISSION, report, "VTOL land too short, min %.0fm", min_dist);
452
}
453
}
454
prev_cmd = cmd;
455
}
456
}
457
#endif
458
return ret;
459
}
460
461
// Checks rc has been received if it is configured to be used
462
bool AP_Arming_Plane::rc_received_if_enabled_check(bool display_failure)
463
{
464
if (rc().enabled_protocols() == 0) {
465
// No protocols enabled, will never get RC, don't block arming
466
return true;
467
}
468
469
// If RC failsafe is enabled we must receive RC before arming
470
if ((plane.g.throttle_fs_enabled == Plane::ThrFailsafe::Enabled) &&
471
!(rc().has_had_rc_receiver() || rc().has_had_rc_override())) {
472
check_failed(display_failure, "Waiting for RC");
473
return false;
474
}
475
476
return true;
477
}
478
479