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/Copter.h
Views: 1798
1
/*
2
This program is free software: you can redistribute it and/or modify
3
it under the terms of the GNU General Public License as published by
4
the Free Software Foundation, either version 3 of the License, or
5
(at your option) any later version.
6
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
11
12
You should have received a copy of the GNU General Public License
13
along with this program. If not, see <http://www.gnu.org/licenses/>.
14
*/
15
#pragma once
16
/*
17
This is the main Copter class
18
*/
19
20
////////////////////////////////////////////////////////////////////////////////
21
// Header includes
22
////////////////////////////////////////////////////////////////////////////////
23
24
#include <cmath>
25
#include <stdio.h>
26
#include <stdarg.h>
27
28
#include <AP_HAL/AP_HAL.h>
29
30
// Common dependencies
31
#include <AP_Common/AP_Common.h> // Common definitions and utility routines for the ArduPilot libraries
32
#include <AP_Common/Location.h> // Library having the implementation of location class
33
#include <AP_Param/AP_Param.h> // A system for managing and storing variables that are of general interest to the system.
34
#include <StorageManager/StorageManager.h> // library for Management for hal.storage to allow for backwards compatible mapping of storage offsets to available storage
35
36
// Application dependencies
37
#include <AP_Logger/AP_Logger.h> // ArduPilot Mega Flash Memory Library
38
#include <AP_Math/AP_Math.h> // ArduPilot Mega Vector/Matrix math Library
39
#include <AP_AccelCal/AP_AccelCal.h> // interface and maths for accelerometer calibration
40
#include <AP_InertialSensor/AP_InertialSensor.h> // ArduPilot Mega Inertial Sensor (accel & gyro) Library
41
#include <AP_AHRS/AP_AHRS.h> // AHRS (Attitude Heading Reference System) interface library for ArduPilot
42
#include <AP_Mission/AP_Mission.h> // Mission command library
43
#include <AP_Mission/AP_Mission_ChangeDetector.h> // Mission command change detection library
44
#include <AC_AttitudeControl/AC_AttitudeControl_Multi.h> // Attitude control library
45
#include <AC_AttitudeControl/AC_AttitudeControl_Multi_6DoF.h> // 6DoF Attitude control library
46
#include <AC_AttitudeControl/AC_AttitudeControl_Heli.h> // Attitude control library for traditional helicopter
47
#include <AC_AttitudeControl/AC_PosControl.h> // Position control library
48
#include <AC_AttitudeControl/AC_CommandModel.h> // Command model library
49
#include <AP_Motors/AP_Motors.h> // AP Motors library
50
#include <Filter/Filter.h> // Filter library
51
#include <AP_Vehicle/AP_Vehicle.h> // needed for AHRS build
52
#include <AP_InertialNav/AP_InertialNav.h> // inertial navigation library
53
#include <AC_WPNav/AC_WPNav.h> // ArduCopter waypoint navigation library
54
#include <AC_WPNav/AC_Loiter.h> // ArduCopter Loiter Mode Library
55
#include <AC_WPNav/AC_Circle.h> // circle navigation library
56
#include <AP_Declination/AP_Declination.h> // ArduPilot Mega Declination Helper Library
57
#include <AP_RCMapper/AP_RCMapper.h> // RC input mapping library
58
#include <AP_BattMonitor/AP_BattMonitor.h> // Battery monitor library
59
#include <AP_LandingGear/AP_LandingGear.h> // Landing Gear library
60
#include <AC_InputManager/AC_InputManager.h> // Pilot input handling library
61
#include <AC_InputManager/AC_InputManager_Heli.h> // Heli specific pilot input handling library
62
#include <AP_Arming/AP_Arming.h> // ArduPilot motor arming library
63
#include <AP_SmartRTL/AP_SmartRTL.h> // ArduPilot Smart Return To Launch Mode (SRTL) library
64
#include <AP_TempCalibration/AP_TempCalibration.h> // temperature calibration library
65
#include <AC_AutoTune/AC_AutoTune_Multi.h> // ArduCopter autotune library. support for autotune of multirotors.
66
#include <AC_AutoTune/AC_AutoTune_Heli.h> // ArduCopter autotune library. support for autotune of helicopters.
67
#include <AP_Parachute/AP_Parachute.h> // ArduPilot parachute release library
68
#include <AC_Sprayer/AC_Sprayer.h> // Crop sprayer library
69
#include <AP_ADSB/AP_ADSB.h> // ADS-B RF based collision avoidance module library
70
#include <AP_Proximity/AP_Proximity.h> // ArduPilot proximity sensor library
71
#include <AC_PrecLand/AC_PrecLand_config.h>
72
#include <AP_OpticalFlow/AP_OpticalFlow.h>
73
#include <AP_Winch/AP_Winch_config.h>
74
#include <AP_SurfaceDistance/AP_SurfaceDistance.h>
75
76
// Configuration
77
#include "defines.h"
78
#include "config.h"
79
80
#if FRAME_CONFIG == HELI_FRAME
81
#define MOTOR_CLASS AP_MotorsHeli
82
#else
83
#define MOTOR_CLASS AP_MotorsMulticopter
84
#endif
85
86
#if MODE_AUTOROTATE_ENABLED
87
#include <AC_Autorotation/AC_Autorotation.h> // Autorotation controllers
88
#endif
89
90
#include "RC_Channel_Copter.h" // RC Channel Library
91
92
#include "GCS_MAVLink_Copter.h"
93
#include "GCS_Copter.h"
94
#include "AP_Rally.h" // Rally point library
95
#include "AP_Arming.h"
96
97
#include <AP_ExternalControl/AP_ExternalControl_config.h>
98
#if AP_EXTERNAL_CONTROL_ENABLED
99
#include "AP_ExternalControl_Copter.h"
100
#endif
101
102
#include <AP_Beacon/AP_Beacon_config.h>
103
#if AP_BEACON_ENABLED
104
#include <AP_Beacon/AP_Beacon.h>
105
#endif
106
107
#if AP_AVOIDANCE_ENABLED
108
#include <AC_Avoidance/AC_Avoid.h>
109
#endif
110
#if AP_OAPATHPLANNER_ENABLED
111
#include <AC_WPNav/AC_WPNav_OA.h>
112
#include <AC_Avoidance/AP_OAPathPlanner.h>
113
#endif
114
#if AC_PRECLAND_ENABLED
115
# include <AC_PrecLand/AC_PrecLand.h>
116
# include <AC_PrecLand/AC_PrecLand_StateMachine.h>
117
#endif
118
#if MODE_FOLLOW_ENABLED
119
# include <AP_Follow/AP_Follow.h>
120
#endif
121
#if AP_TERRAIN_AVAILABLE
122
# include <AP_Terrain/AP_Terrain.h>
123
#endif
124
#if AP_RANGEFINDER_ENABLED
125
# include <AP_RangeFinder/AP_RangeFinder.h>
126
#endif
127
128
#include <AP_Mount/AP_Mount.h>
129
130
#include <AP_Camera/AP_Camera.h>
131
132
#if HAL_BUTTON_ENABLED
133
# include <AP_Button/AP_Button.h>
134
#endif
135
136
#if OSD_ENABLED || OSD_PARAM_ENABLED
137
#include <AP_OSD/AP_OSD.h>
138
#endif
139
140
#if AP_COPTER_ADVANCED_FAILSAFE_ENABLED
141
# include "afs_copter.h"
142
#endif
143
#if TOY_MODE_ENABLED
144
# include "toy_mode.h"
145
#endif
146
#if AP_WINCH_ENABLED
147
# include <AP_Winch/AP_Winch.h>
148
#endif
149
#include <AP_RPM/AP_RPM.h>
150
151
#if AP_SCRIPTING_ENABLED
152
#include <AP_Scripting/AP_Scripting.h>
153
#endif
154
155
#if AC_CUSTOMCONTROL_MULTI_ENABLED
156
#include <AC_CustomControl/AC_CustomControl.h> // Custom control library
157
#endif
158
159
#if AP_AVOIDANCE_ENABLED && !AP_FENCE_ENABLED
160
#error AC_Avoidance relies on AP_FENCE_ENABLED which is disabled
161
#endif
162
163
#if AP_OAPATHPLANNER_ENABLED && !AP_FENCE_ENABLED
164
#error AP_OAPathPlanner relies on AP_FENCE_ENABLED which is disabled
165
#endif
166
167
#if HAL_ADSB_ENABLED
168
#include "avoidance_adsb.h"
169
#endif
170
// Local modules
171
#include "Parameters.h"
172
#if USER_PARAMS_ENABLED
173
#include "UserParameters.h"
174
#endif
175
#include "mode.h"
176
177
class Copter : public AP_Vehicle {
178
public:
179
friend class GCS_MAVLINK_Copter;
180
friend class GCS_Copter;
181
friend class AP_Rally_Copter;
182
friend class Parameters;
183
friend class ParametersG2;
184
friend class AP_Avoidance_Copter;
185
186
#if AP_COPTER_ADVANCED_FAILSAFE_ENABLED
187
friend class AP_AdvancedFailsafe_Copter;
188
#endif
189
friend class AP_Arming_Copter;
190
#if AP_EXTERNAL_CONTROL_ENABLED
191
friend class AP_ExternalControl_Copter;
192
#endif
193
friend class ToyMode;
194
friend class RC_Channel_Copter;
195
friend class RC_Channels_Copter;
196
197
friend class AutoTune;
198
199
friend class Mode;
200
friend class ModeAcro;
201
friend class ModeAcro_Heli;
202
friend class ModeAltHold;
203
friend class ModeAuto;
204
friend class ModeAutoTune;
205
friend class ModeAvoidADSB;
206
friend class ModeBrake;
207
friend class ModeCircle;
208
friend class ModeDrift;
209
friend class ModeFlip;
210
friend class ModeFlowHold;
211
friend class ModeFollow;
212
friend class ModeGuided;
213
friend class ModeLand;
214
friend class ModeLoiter;
215
friend class ModePosHold;
216
friend class ModeRTL;
217
friend class ModeSmartRTL;
218
friend class ModeSport;
219
friend class ModeStabilize;
220
friend class ModeStabilize_Heli;
221
friend class ModeSystemId;
222
friend class ModeThrow;
223
friend class ModeZigZag;
224
friend class ModeAutorotate;
225
friend class ModeTurtle;
226
227
friend class _AutoTakeoff;
228
229
friend class PayloadPlace;
230
231
Copter(void);
232
233
private:
234
235
// key aircraft parameters passed to multiple libraries
236
AP_MultiCopter aparm;
237
238
// Global parameters are all contained within the 'g' class.
239
Parameters g;
240
ParametersG2 g2;
241
242
// used to detect MAVLink acks from GCS to stop compassmot
243
uint8_t command_ack_counter;
244
245
// primary input control channels
246
RC_Channel *channel_roll;
247
RC_Channel *channel_pitch;
248
RC_Channel *channel_throttle;
249
RC_Channel *channel_yaw;
250
251
// flight modes convenience array
252
AP_Int8 *flight_modes;
253
const uint8_t num_flight_modes = 6;
254
255
AP_SurfaceDistance rangefinder_state {ROTATION_PITCH_270, inertial_nav, 0U};
256
AP_SurfaceDistance rangefinder_up_state {ROTATION_PITCH_90, inertial_nav, 1U};
257
258
// helper function to get inertially interpolated rangefinder height.
259
bool get_rangefinder_height_interpolated_cm(int32_t& ret) const;
260
261
#if AP_RANGEFINDER_ENABLED
262
class SurfaceTracking {
263
public:
264
265
// update_surface_offset - manages the vertical offset of the position controller to follow the
266
// measured ground or ceiling level measured using the range finder.
267
void update_surface_offset();
268
269
// target has already been set by terrain following so do not initalise again
270
// this should be called by flight modes when switching from terrain following to surface tracking (e.g. ZigZag)
271
void external_init();
272
273
// get target and actual distances (in m) for logging purposes
274
bool get_target_dist_for_logging(float &target_dist) const;
275
float get_dist_for_logging() const;
276
void invalidate_for_logging() { valid_for_logging = false; }
277
278
// surface tracking surface
279
enum class Surface {
280
NONE = 0,
281
GROUND = 1,
282
CEILING = 2
283
};
284
// set surface to track
285
void set_surface(Surface new_surface);
286
// initialise surface tracking
287
void init(Surface surf) { surface = surf; }
288
289
private:
290
Surface surface;
291
uint32_t last_update_ms; // system time of last update to target_alt_cm
292
uint32_t last_glitch_cleared_ms; // system time of last handle glitch recovery
293
bool valid_for_logging; // true if we have a desired target altitude
294
bool reset_target; // true if target should be reset because of change in surface being tracked
295
} surface_tracking;
296
#endif
297
298
#if AP_RPM_ENABLED
299
AP_RPM rpm_sensor;
300
#endif
301
302
// Inertial Navigation EKF - different viewpoint
303
AP_AHRS_View *ahrs_view;
304
305
// Arming/Disarming management class
306
AP_Arming_Copter arming;
307
308
// Optical flow sensor
309
#if AP_OPTICALFLOW_ENABLED
310
AP_OpticalFlow optflow;
311
#endif
312
313
// external control library
314
#if AP_EXTERNAL_CONTROL_ENABLED
315
AP_ExternalControl_Copter external_control;
316
#endif
317
318
319
// system time in milliseconds of last recorded yaw reset from ekf
320
uint32_t ekfYawReset_ms;
321
int8_t ekf_primary_core;
322
323
// vibration check
324
struct {
325
bool high_vibes; // true while high vibration are detected
326
uint32_t start_ms; // system time high vibration were last detected
327
uint32_t clear_ms; // system time high vibrations stopped
328
} vibration_check;
329
330
// EKF variances are unfiltered and are designed to recover very quickly when possible
331
// thus failsafes should be triggered on filtered values in order to avoid transient errors
332
LowPassFilterFloat pos_variance_filt;
333
LowPassFilterFloat vel_variance_filt;
334
LowPassFilterFloat hgt_variance_filt;
335
bool variances_valid;
336
uint32_t last_ekf_check_us;
337
338
// takeoff check
339
uint32_t takeoff_check_warning_ms; // system time user was last warned of takeoff check failure
340
341
// GCS selection
342
GCS_Copter _gcs; // avoid using this; use gcs()
343
GCS_Copter &gcs() { return _gcs; }
344
345
// User variables
346
#ifdef USERHOOK_VARIABLES
347
# include USERHOOK_VARIABLES
348
#endif
349
350
// ap_value calculates a 32-bit bitmask representing various pieces of
351
// state about the Copter. It replaces a global variable which was
352
// used to track this state.
353
uint32_t ap_value() const;
354
355
// These variables are essentially global variables. These should
356
// be removed over time. It is critical that the offsets of these
357
// variables remain unchanged - the logging is dependent on this
358
// ordering!
359
struct PACKED {
360
bool unused1; // 0
361
bool unused_was_simple_mode_byte1; // 1
362
bool unused_was_simple_mode_byte2; // 2
363
bool pre_arm_rc_check; // 3 true if rc input pre-arm checks have been completed successfully
364
bool pre_arm_check; // 4 true if all pre-arm checks (rc, accel calibration, gps lock) have been performed
365
bool auto_armed; // 5 stops auto missions from beginning until throttle is raised
366
bool unused_log_started; // 6
367
bool land_complete; // 7 true if we have detected a landing
368
bool new_radio_frame; // 8 Set true if we have new PWM data to act on from the Radio
369
bool unused_usb_connected; // 9
370
bool unused_receiver_present; // 10
371
bool compass_mot; // 11 true if we are currently performing compassmot calibration
372
bool motor_test; // 12 true if we are currently performing the motors test
373
bool initialised; // 13 true once the init_ardupilot function has completed. Extended status to GCS is not sent until this completes
374
bool land_complete_maybe; // 14 true if we may have landed (less strict version of land_complete)
375
bool throttle_zero; // 15 true if the throttle stick is at zero, debounced, determines if pilot intends shut-down when not using motor interlock
376
bool system_time_set_unused; // 16 true if the system time has been set from the GPS
377
bool gps_glitching; // 17 true if GPS glitching is affecting navigation accuracy
378
bool using_interlock; // 18 aux switch motor interlock function is in use
379
bool land_repo_active; // 19 true if the pilot is overriding the landing position
380
bool motor_interlock_switch; // 20 true if pilot is requesting motor interlock enable
381
bool in_arming_delay; // 21 true while we are armed but waiting to spin motors
382
bool initialised_params; // 22 true when the all parameters have been initialised. we cannot send parameters to the GCS until this is done
383
bool unused_compass_init_location; // 23
384
bool unused2_aux_switch_rc_override_allowed; // 24
385
bool armed_with_airmode_switch; // 25 we armed using a arming switch
386
bool prec_land_active; // 26 true if precland is active
387
} ap;
388
389
AirMode air_mode; // air mode is 0 = not-configured ; 1 = disabled; 2 = enabled;
390
bool force_flying; // force flying is enabled when true;
391
392
// This is the state of the flight control system
393
// There are multiple states defined such as STABILIZE, ACRO,
394
Mode *flightmode;
395
396
RCMapper rcmap;
397
398
// inertial nav alt when we armed
399
float arming_altitude_m;
400
401
// Failsafe
402
struct {
403
uint32_t terrain_first_failure_ms; // the first time terrain data access failed - used to calculate the duration of the failure
404
uint32_t terrain_last_failure_ms; // the most recent time terrain data access failed
405
406
int8_t radio_counter; // number of iterations with throttle below throttle_fs_value
407
408
uint8_t radio : 1; // A status flag for the radio failsafe
409
uint8_t gcs : 1; // A status flag for the ground station failsafe
410
uint8_t ekf : 1; // true if ekf failsafe has occurred
411
uint8_t terrain : 1; // true if the missing terrain data failsafe has occurred
412
uint8_t adsb : 1; // true if an adsb related failsafe has occurred
413
uint8_t deadreckon : 1; // true if a dead reckoning failsafe has triggered
414
} failsafe;
415
416
bool any_failsafe_triggered() const {
417
return failsafe.radio || battery.has_failsafed() || failsafe.gcs || failsafe.ekf || failsafe.terrain || failsafe.adsb || failsafe.deadreckon;
418
}
419
420
// dead reckoning state
421
struct {
422
bool active; // true if dead reckoning (position estimate using estimated airspeed, no position or velocity source)
423
bool timeout; // true if dead reckoning has timedout and EKF's position and velocity estimate should no longer be trusted
424
uint32_t start_ms; // system time that EKF began deadreckoning
425
} dead_reckoning;
426
427
// Motor Output
428
MOTOR_CLASS *motors;
429
const struct AP_Param::GroupInfo *motors_var_info;
430
431
int32_t _home_bearing;
432
uint32_t _home_distance;
433
434
// SIMPLE Mode
435
// Used to track the orientation of the vehicle for Simple mode. This value is reset at each arming
436
// or in SuperSimple mode when the vehicle leaves a 20m radius from home.
437
enum class SimpleMode {
438
NONE = 0,
439
SIMPLE = 1,
440
SUPERSIMPLE = 2,
441
} simple_mode;
442
443
float simple_cos_yaw;
444
float simple_sin_yaw;
445
int32_t super_simple_last_bearing;
446
float super_simple_cos_yaw;
447
float super_simple_sin_yaw;
448
449
// Stores initial bearing when armed - initial simple bearing is modified in super simple mode so not suitable
450
int32_t initial_armed_bearing;
451
452
// Battery Sensors
453
AP_BattMonitor battery{MASK_LOG_CURRENT,
454
FUNCTOR_BIND_MEMBER(&Copter::handle_battery_failsafe, void, const char*, const int8_t),
455
_failsafe_priorities};
456
457
#if OSD_ENABLED || OSD_PARAM_ENABLED
458
AP_OSD osd;
459
#endif
460
461
// Altitude
462
int32_t baro_alt; // barometer altitude in cm above home
463
LowPassFilterVector3f land_accel_ef_filter; // accelerations for land and crash detector tests
464
465
// filtered pilot's throttle input used to cancel landing if throttle held high
466
LowPassFilterFloat rc_throttle_control_in_filter;
467
468
// 3D Location vectors
469
// Current location of the vehicle (altitude is relative to home)
470
Location current_loc;
471
472
// Inertial Navigation
473
AP_InertialNav inertial_nav;
474
475
// Attitude, Position and Waypoint navigation objects
476
// To-Do: move inertial nav up or other navigation variables down here
477
AC_AttitudeControl *attitude_control;
478
const struct AP_Param::GroupInfo *attitude_control_var_info;
479
AC_PosControl *pos_control;
480
AC_WPNav *wp_nav;
481
AC_Loiter *loiter_nav;
482
483
#if AC_CUSTOMCONTROL_MULTI_ENABLED
484
AC_CustomControl custom_control{ahrs_view, attitude_control, motors, scheduler.get_loop_period_s()};
485
#endif
486
487
#if MODE_CIRCLE_ENABLED
488
AC_Circle *circle_nav;
489
#endif
490
491
// System Timers
492
// --------------
493
// arm_time_ms - Records when vehicle was armed. Will be Zero if we are disarmed.
494
uint32_t arm_time_ms;
495
496
// Used to exit the roll and pitch auto trim function
497
uint8_t auto_trim_counter;
498
bool auto_trim_started = false;
499
500
// Camera
501
#if AP_CAMERA_ENABLED
502
AP_Camera camera{MASK_LOG_CAMERA};
503
#endif
504
505
// Camera/Antenna mount tracking and stabilisation stuff
506
#if HAL_MOUNT_ENABLED
507
AP_Mount camera_mount;
508
#endif
509
510
#if AP_AVOIDANCE_ENABLED
511
AC_Avoid avoid;
512
#endif
513
514
// Rally library
515
#if HAL_RALLY_ENABLED
516
AP_Rally_Copter rally;
517
#endif
518
519
// Crop Sprayer
520
#if HAL_SPRAYER_ENABLED
521
AC_Sprayer sprayer;
522
#endif
523
524
// Parachute release
525
#if HAL_PARACHUTE_ENABLED
526
AP_Parachute parachute;
527
#endif
528
529
// Landing Gear Controller
530
#if AP_LANDINGGEAR_ENABLED
531
AP_LandingGear landinggear;
532
#endif
533
534
// terrain handling
535
#if AP_TERRAIN_AVAILABLE
536
AP_Terrain terrain;
537
#endif
538
539
// Precision Landing
540
#if AC_PRECLAND_ENABLED
541
AC_PrecLand precland;
542
AC_PrecLand_StateMachine precland_statemachine;
543
#endif
544
545
// Pilot Input Management Library
546
// Only used for Helicopter for now
547
#if FRAME_CONFIG == HELI_FRAME
548
AC_InputManager_Heli input_manager;
549
#endif
550
551
#if HAL_ADSB_ENABLED
552
AP_ADSB adsb;
553
554
// avoidance of adsb enabled vehicles (normally manned vehicles)
555
AP_Avoidance_Copter avoidance_adsb{adsb};
556
#endif
557
558
// last valid RC input time
559
uint32_t last_radio_update_ms;
560
561
// last esc calibration notification update
562
uint32_t esc_calibration_notify_update_ms;
563
564
// Top-level logic
565
// setup the var_info table
566
AP_Param param_loader;
567
568
#if FRAME_CONFIG == HELI_FRAME
569
// Mode filter to reject RC Input glitches. Filter size is 5, and it draws the 4th element, so it can reject 3 low glitches,
570
// and 1 high glitch. This is because any "off" glitches can be highly problematic for a helicopter running an ESC
571
// governor. Even a single "off" frame can cause the rotor to slow dramatically and take a long time to restart.
572
ModeFilterInt16_Size5 rotor_speed_deglitch_filter {4};
573
574
// Tradheli flags
575
typedef struct {
576
uint8_t dynamic_flight : 1; // 0 // true if we are moving at a significant speed (used to turn on/off leaky I terms)
577
bool coll_stk_low ; // 1 // true when collective stick is on lower limit
578
} heli_flags_t;
579
heli_flags_t heli_flags;
580
581
int16_t hover_roll_trim_scalar_slew;
582
#endif
583
584
// ground effect detector
585
struct {
586
bool takeoff_expected;
587
bool touchdown_expected;
588
uint32_t takeoff_time_ms;
589
float takeoff_alt_cm;
590
} gndeffect_state;
591
592
bool standby_active;
593
594
static const AP_Scheduler::Task scheduler_tasks[];
595
static const AP_Param::Info var_info[];
596
static const struct LogStructure log_structure[];
597
598
// enum for ESC CALIBRATION
599
enum ESCCalibrationModes : uint8_t {
600
ESCCAL_NONE = 0,
601
ESCCAL_PASSTHROUGH_IF_THROTTLE_HIGH = 1,
602
ESCCAL_PASSTHROUGH_ALWAYS = 2,
603
ESCCAL_AUTO = 3,
604
ESCCAL_DISABLED = 9,
605
};
606
607
enum class FailsafeAction : uint8_t {
608
NONE = 0,
609
LAND = 1,
610
RTL = 2,
611
SMARTRTL = 3,
612
SMARTRTL_LAND = 4,
613
TERMINATE = 5,
614
AUTO_DO_LAND_START = 6,
615
BRAKE_LAND = 7
616
};
617
618
enum class FailsafeOption {
619
RC_CONTINUE_IF_AUTO = (1<<0), // 1
620
GCS_CONTINUE_IF_AUTO = (1<<1), // 2
621
RC_CONTINUE_IF_GUIDED = (1<<2), // 4
622
CONTINUE_IF_LANDING = (1<<3), // 8
623
GCS_CONTINUE_IF_PILOT_CONTROL = (1<<4), // 16
624
RELEASE_GRIPPER = (1<<5), // 32
625
};
626
627
628
enum class FlightOption : uint32_t {
629
DISABLE_THRUST_LOSS_CHECK = (1<<0), // 1
630
DISABLE_YAW_IMBALANCE_WARNING = (1<<1), // 2
631
RELEASE_GRIPPER_ON_THRUST_LOSS = (1<<2), // 4
632
REQUIRE_POSITION_FOR_ARMING = (1<<3), // 8
633
};
634
635
// type of fast rate attitude controller in operation
636
enum class FastRateType : uint8_t {
637
FAST_RATE_DISABLED = 0,
638
FAST_RATE_DYNAMIC = 1,
639
FAST_RATE_FIXED_ARMED = 2,
640
FAST_RATE_FIXED = 3,
641
};
642
643
FastRateType get_fast_rate_type() const { return FastRateType(g2.att_enable.get()); }
644
645
// returns true if option is enabled for this vehicle
646
bool option_is_enabled(FlightOption option) const {
647
return (g2.flight_options & uint32_t(option)) != 0;
648
}
649
650
static constexpr int8_t _failsafe_priorities[] = {
651
(int8_t)FailsafeAction::TERMINATE,
652
(int8_t)FailsafeAction::LAND,
653
(int8_t)FailsafeAction::RTL,
654
(int8_t)FailsafeAction::SMARTRTL_LAND,
655
(int8_t)FailsafeAction::SMARTRTL,
656
(int8_t)FailsafeAction::NONE,
657
-1 // the priority list must end with a sentinel of -1
658
};
659
660
#define FAILSAFE_LAND_PRIORITY 1
661
static_assert(_failsafe_priorities[FAILSAFE_LAND_PRIORITY] == (int8_t)FailsafeAction::LAND,
662
"FAILSAFE_LAND_PRIORITY must match the entry in _failsafe_priorities");
663
static_assert(_failsafe_priorities[ARRAY_SIZE(_failsafe_priorities) - 1] == -1,
664
"_failsafe_priorities is missing the sentinel");
665
666
667
668
// AP_State.cpp
669
void set_auto_armed(bool b);
670
void set_simple_mode(SimpleMode b);
671
void set_failsafe_radio(bool b);
672
void set_failsafe_gcs(bool b);
673
void update_using_interlock();
674
675
// Copter.cpp
676
void get_scheduler_tasks(const AP_Scheduler::Task *&tasks,
677
uint8_t &task_count,
678
uint32_t &log_bit) override;
679
#if AP_SCRIPTING_ENABLED || AP_EXTERNAL_CONTROL_ENABLED
680
#if MODE_GUIDED_ENABLED
681
bool set_target_location(const Location& target_loc) override;
682
bool start_takeoff(const float alt) override;
683
#endif // MODE_GUIDED_ENABLED
684
#endif // AP_SCRIPTING_ENABLED || AP_EXTERNAL_CONTROL_ENABLED
685
686
#if AP_SCRIPTING_ENABLED
687
#if MODE_GUIDED_ENABLED
688
bool get_target_location(Location& target_loc) override;
689
bool update_target_location(const Location &old_loc, const Location &new_loc) override;
690
bool set_target_pos_NED(const Vector3f& target_pos, bool use_yaw, float yaw_deg, bool use_yaw_rate, float yaw_rate_degs, bool yaw_relative, bool terrain_alt) override;
691
bool set_target_posvel_NED(const Vector3f& target_pos, const Vector3f& target_vel) override;
692
bool set_target_posvelaccel_NED(const Vector3f& target_pos, const Vector3f& target_vel, const Vector3f& target_accel, bool use_yaw, float yaw_deg, bool use_yaw_rate, float yaw_rate_degs, bool yaw_relative) override;
693
bool set_target_velocity_NED(const Vector3f& vel_ned) override;
694
bool set_target_velaccel_NED(const Vector3f& target_vel, const Vector3f& target_accel, bool use_yaw, float yaw_deg, bool use_yaw_rate, float yaw_rate_degs, bool relative_yaw) override;
695
bool set_target_angle_and_climbrate(float roll_deg, float pitch_deg, float yaw_deg, float climb_rate_ms, bool use_yaw_rate, float yaw_rate_degs) override;
696
bool set_target_rate_and_throttle(float roll_rate_dps, float pitch_rate_dps, float yaw_rate_dps, float throttle) override;
697
698
// Register a custom mode with given number and names
699
AP_Vehicle::custom_mode_state* register_custom_mode(const uint8_t number, const char* full_name, const char* short_name) override;
700
#endif
701
#if MODE_CIRCLE_ENABLED
702
bool get_circle_radius(float &radius_m) override;
703
bool set_circle_rate(float rate_dps) override;
704
#endif
705
bool set_desired_speed(float speed) override;
706
#if MODE_AUTO_ENABLED
707
bool nav_scripting_enable(uint8_t mode) override;
708
bool nav_script_time(uint16_t &id, uint8_t &cmd, float &arg1, float &arg2, int16_t &arg3, int16_t &arg4) override;
709
void nav_script_time_done(uint16_t id) override;
710
#endif
711
// lua scripts use this to retrieve EKF failsafe state
712
// returns true if the EKF failsafe has triggered
713
bool has_ekf_failsafed() const override;
714
#endif // AP_SCRIPTING_ENABLED
715
bool is_landing() const override;
716
bool is_taking_off() const override;
717
void rc_loop();
718
void throttle_loop();
719
void update_batt_compass(void);
720
void loop_rate_logging();
721
void ten_hz_logging_loop();
722
void twentyfive_hz_logging();
723
void three_hz_loop();
724
void one_hz_loop();
725
void init_simple_bearing();
726
void update_simple_mode(void);
727
void update_super_simple_bearing(bool force_update);
728
void read_AHRS(void);
729
void update_altitude();
730
bool get_wp_distance_m(float &distance) const override;
731
bool get_wp_bearing_deg(float &bearing) const override;
732
bool get_wp_crosstrack_error_m(float &xtrack_error) const override;
733
bool get_rate_ef_targets(Vector3f& rate_ef_targets) const override;
734
735
// Attitude.cpp
736
void update_throttle_hover();
737
float get_pilot_desired_climb_rate(float throttle_control);
738
float get_non_takeoff_throttle();
739
void set_accel_throttle_I_from_pilot_throttle();
740
void rotate_body_frame_to_NE(float &x, float &y);
741
uint16_t get_pilot_speed_dn() const;
742
void run_rate_controller_main();
743
744
// if AP_INERTIALSENSOR_FAST_SAMPLE_WINDOW_ENABLED
745
struct RateControllerRates {
746
uint8_t fast_logging_rate;
747
uint8_t medium_logging_rate;
748
uint8_t filter_rate;
749
uint8_t main_loop_rate;
750
};
751
752
uint8_t calc_gyro_decimation(uint8_t gyro_decimation, uint16_t rate_hz);
753
void rate_controller_thread();
754
void rate_controller_filter_update();
755
void rate_controller_log_update();
756
void rate_controller_set_rates(uint8_t rate_decimation, RateControllerRates& rates, bool warn_cpu_high);
757
void enable_fast_rate_loop(uint8_t rate_decimation, RateControllerRates& rates);
758
void disable_fast_rate_loop(RateControllerRates& rates);
759
void update_dynamic_notch_at_specified_rate_main();
760
// endif AP_INERTIALSENSOR_FAST_SAMPLE_WINDOW_ENABLED
761
762
#if AC_CUSTOMCONTROL_MULTI_ENABLED
763
void run_custom_controller() { custom_control.update(); }
764
#endif
765
766
// avoidance.cpp
767
void low_alt_avoidance();
768
769
#if HAL_ADSB_ENABLED
770
// avoidance_adsb.cpp
771
void avoidance_adsb_update(void);
772
#endif
773
774
// baro_ground_effect.cpp
775
void update_ground_effect_detector(void);
776
void update_ekf_terrain_height_stable();
777
778
// commands.cpp
779
void update_home_from_EKF();
780
void set_home_to_current_location_inflight();
781
bool set_home_to_current_location(bool lock) override WARN_IF_UNUSED;
782
bool set_home(const Location& loc, bool lock) override WARN_IF_UNUSED;
783
784
// compassmot.cpp
785
MAV_RESULT mavlink_compassmot(const GCS_MAVLINK &gcs_chan);
786
787
// crash_check.cpp
788
void crash_check();
789
void thrust_loss_check();
790
void yaw_imbalance_check();
791
LowPassFilterFloat yaw_I_filt{0.05f};
792
uint32_t last_yaw_warn_ms;
793
void parachute_check();
794
void parachute_release();
795
void parachute_manual_release();
796
797
// ekf_check.cpp
798
void ekf_check();
799
bool ekf_over_threshold();
800
void failsafe_ekf_event();
801
void failsafe_ekf_off_event(void);
802
void failsafe_ekf_recheck();
803
void check_ekf_reset();
804
void check_vibration();
805
806
// esc_calibration.cpp
807
void esc_calibration_startup_check();
808
void esc_calibration_passthrough();
809
void esc_calibration_auto();
810
void esc_calibration_notify();
811
void esc_calibration_setup();
812
813
// events.cpp
814
bool failsafe_option(FailsafeOption opt) const;
815
void failsafe_radio_on_event();
816
void failsafe_radio_off_event();
817
void handle_battery_failsafe(const char* type_str, const int8_t action);
818
void failsafe_gcs_check();
819
void failsafe_gcs_on_event(void);
820
void failsafe_gcs_off_event(void);
821
void failsafe_terrain_check();
822
void failsafe_terrain_set_status(bool data_ok);
823
void failsafe_terrain_on_event();
824
void gpsglitch_check();
825
void failsafe_deadreckon_check();
826
void set_mode_RTL_or_land_with_pause(ModeReason reason);
827
void set_mode_SmartRTL_or_RTL(ModeReason reason);
828
void set_mode_SmartRTL_or_land_with_pause(ModeReason reason);
829
void set_mode_auto_do_land_start_or_RTL(ModeReason reason);
830
void set_mode_brake_or_land_with_pause(ModeReason reason);
831
bool should_disarm_on_failsafe();
832
void do_failsafe_action(FailsafeAction action, ModeReason reason);
833
void announce_failsafe(const char *type, const char *action_undertaken=nullptr);
834
835
// failsafe.cpp
836
void failsafe_enable();
837
void failsafe_disable();
838
#if AP_COPTER_ADVANCED_FAILSAFE_ENABLED
839
void afs_fs_check(void);
840
#endif
841
842
// fence.cpp
843
#if AP_FENCE_ENABLED
844
void fence_check();
845
#endif
846
847
// heli.cpp
848
void heli_init();
849
void check_dynamic_flight(void);
850
bool should_use_landing_swash() const;
851
void update_heli_control_dynamics(void);
852
void heli_update_landing_swash();
853
float get_pilot_desired_rotor_speed() const;
854
void heli_update_rotor_speed_targets();
855
void heli_update_autorotation();
856
void update_collective_low_flag(int16_t throttle_control);
857
858
// inertia.cpp
859
void read_inertia();
860
861
// landing_detector.cpp
862
void update_land_and_crash_detectors();
863
void update_land_detector();
864
void set_land_complete(bool b);
865
void set_land_complete_maybe(bool b);
866
void update_throttle_mix();
867
bool get_force_flying() const;
868
#if HAL_LOGGING_ENABLED
869
enum class LandDetectorLoggingFlag : uint16_t {
870
LANDED = 1U << 0,
871
LANDED_MAYBE = 1U << 1,
872
LANDING = 1U << 2,
873
STANDBY_ACTIVE = 1U << 3,
874
WOW = 1U << 4,
875
RANGEFINDER_BELOW_2M = 1U << 5,
876
DESCENT_RATE_LOW = 1U << 6,
877
ACCEL_STATIONARY = 1U << 7,
878
LARGE_ANGLE_ERROR = 1U << 8,
879
LARGE_ANGLE_REQUEST = 1U << 8,
880
MOTOR_AT_LOWER_LIMIT = 1U << 9,
881
THROTTLE_MIX_AT_MIN = 1U << 10,
882
};
883
struct {
884
uint32_t last_logged_ms;
885
uint32_t last_logged_count;
886
uint16_t last_logged_flags;
887
} land_detector;
888
void Log_LDET(uint16_t logging_flags, uint32_t land_detector_count);
889
#endif
890
891
#if AP_LANDINGGEAR_ENABLED
892
// landing_gear.cpp
893
void landinggear_update();
894
#endif
895
896
// standby.cpp
897
void standby_update();
898
899
#if HAL_LOGGING_ENABLED
900
// methods for AP_Vehicle:
901
const AP_Int32 &get_log_bitmask() override { return g.log_bitmask; }
902
const struct LogStructure *get_log_structures() const override {
903
return log_structure;
904
}
905
uint8_t get_num_log_structures() const override;
906
907
// Log.cpp
908
void Log_Write_Control_Tuning();
909
void Log_Write_Attitude();
910
void Log_Write_Rate();
911
void Log_Write_EKF_POS();
912
void Log_Write_PIDS();
913
void Log_Write_Data(LogDataID id, int32_t value);
914
void Log_Write_Data(LogDataID id, uint32_t value);
915
void Log_Write_Data(LogDataID id, int16_t value);
916
void Log_Write_Data(LogDataID id, uint16_t value);
917
void Log_Write_Data(LogDataID id, float value);
918
void Log_Write_Parameter_Tuning(uint8_t param, float tuning_val, float tune_min, float tune_max);
919
void Log_Video_Stabilisation();
920
void Log_Write_Guided_Position_Target(ModeGuided::SubMode submode, const Vector3f& pos_target, bool terrain_alt, const Vector3f& vel_target, const Vector3f& accel_target);
921
void Log_Write_Guided_Attitude_Target(ModeGuided::SubMode target_type, float roll, float pitch, float yaw, const Vector3f &ang_vel, float thrust, float climb_rate);
922
void Log_Write_SysID_Setup(uint8_t systemID_axis, float waveform_magnitude, float frequency_start, float frequency_stop, float time_fade_in, float time_const_freq, float time_record, float time_fade_out);
923
void Log_Write_SysID_Data(float waveform_time, float waveform_sample, float waveform_freq, float angle_x, float angle_y, float angle_z, float accel_x, float accel_y, float accel_z);
924
void Log_Write_Vehicle_Startup_Messages();
925
void Log_Write_Rate_Thread_Dt(float dt, float dtAvg, float dtMax, float dtMin);
926
#endif // HAL_LOGGING_ENABLED
927
928
// mode.cpp
929
bool set_mode(Mode::Number mode, ModeReason reason);
930
bool set_mode(const uint8_t new_mode, const ModeReason reason) override;
931
ModeReason _last_reason;
932
// called when an attempt to change into a mode is unsuccessful:
933
void mode_change_failed(const Mode *mode, const char *reason);
934
uint8_t get_mode() const override { return (uint8_t)flightmode->mode_number(); }
935
bool current_mode_requires_mission() const override;
936
void update_flight_mode();
937
void notify_flight_mode();
938
939
// Check if this mode can be entered from the GCS
940
bool gcs_mode_enabled(const Mode::Number mode_num);
941
942
// mode_land.cpp
943
void set_mode_land_with_pause(ModeReason reason);
944
bool landing_with_GPS();
945
946
// motor_test.cpp
947
void motor_test_output();
948
bool mavlink_motor_control_check(const GCS_MAVLINK &gcs_chan, bool check_rc, const char* mode);
949
MAV_RESULT mavlink_motor_test_start(const GCS_MAVLINK &gcs_chan, uint8_t motor_seq, uint8_t throttle_type, float throttle_value, float timeout_sec, uint8_t motor_count);
950
void motor_test_stop();
951
952
// motors.cpp
953
void arm_motors_check();
954
void auto_disarm_check();
955
void motors_output(bool full_push = true);
956
void motors_output_main();
957
void lost_vehicle_check();
958
959
// navigation.cpp
960
void run_nav_updates(void);
961
int32_t home_bearing();
962
uint32_t home_distance();
963
964
// Parameters.cpp
965
void load_parameters(void) override;
966
void convert_pid_parameters(void);
967
#if HAL_PROXIMITY_ENABLED
968
void convert_prx_parameters();
969
#endif
970
void convert_lgr_parameters(void);
971
972
// precision_landing.cpp
973
void init_precland();
974
void update_precland();
975
976
// radio.cpp
977
void default_dead_zones();
978
void init_rc_in();
979
void init_rc_out();
980
void read_radio();
981
void set_throttle_and_failsafe(uint16_t throttle_pwm);
982
void set_throttle_zero_flag(int16_t throttle_control);
983
void radio_passthrough_to_motors();
984
int16_t get_throttle_mid(void);
985
986
// sensors.cpp
987
void read_barometer(void);
988
void init_rangefinder(void);
989
void read_rangefinder(void);
990
bool rangefinder_alt_ok() const;
991
bool rangefinder_up_ok() const;
992
void update_rangefinder_terrain_offset();
993
void update_optical_flow(void);
994
995
// takeoff_check.cpp
996
void takeoff_check();
997
998
// RC_Channel.cpp
999
void save_trim();
1000
void auto_trim();
1001
void auto_trim_cancel();
1002
1003
// system.cpp
1004
void init_ardupilot() override;
1005
void startup_INS_ground();
1006
bool position_ok() const;
1007
bool ekf_has_absolute_position() const;
1008
bool ekf_has_relative_position() const;
1009
bool ekf_alt_ok() const;
1010
void update_auto_armed();
1011
bool should_log(uint32_t mask);
1012
const char* get_frame_string() const;
1013
void allocate_motors(void);
1014
bool is_tradheli() const;
1015
1016
// terrain.cpp
1017
void terrain_update();
1018
void terrain_logging();
1019
1020
// tuning.cpp
1021
void tuning();
1022
1023
// UserCode.cpp
1024
void userhook_init();
1025
void userhook_FastLoop();
1026
void userhook_50Hz();
1027
void userhook_MediumLoop();
1028
void userhook_SlowLoop();
1029
void userhook_SuperSlowLoop();
1030
void userhook_auxSwitch1(const RC_Channel::AuxSwitchPos ch_flag);
1031
void userhook_auxSwitch2(const RC_Channel::AuxSwitchPos ch_flag);
1032
void userhook_auxSwitch3(const RC_Channel::AuxSwitchPos ch_flag);
1033
1034
#if MODE_ACRO_ENABLED
1035
#if FRAME_CONFIG == HELI_FRAME
1036
ModeAcro_Heli mode_acro;
1037
#else
1038
ModeAcro mode_acro;
1039
#endif
1040
#endif
1041
ModeAltHold mode_althold;
1042
#if MODE_AUTO_ENABLED
1043
ModeAuto mode_auto;
1044
#endif
1045
#if AUTOTUNE_ENABLED
1046
ModeAutoTune mode_autotune;
1047
#endif
1048
#if MODE_BRAKE_ENABLED
1049
ModeBrake mode_brake;
1050
#endif
1051
#if MODE_CIRCLE_ENABLED
1052
ModeCircle mode_circle;
1053
#endif
1054
#if MODE_DRIFT_ENABLED
1055
ModeDrift mode_drift;
1056
#endif
1057
#if MODE_FLIP_ENABLED
1058
ModeFlip mode_flip;
1059
#endif
1060
#if MODE_FOLLOW_ENABLED
1061
ModeFollow mode_follow;
1062
#endif
1063
#if MODE_GUIDED_ENABLED
1064
ModeGuided mode_guided;
1065
#if AP_SCRIPTING_ENABLED
1066
// Custom modes registered at runtime
1067
ModeGuidedCustom *mode_guided_custom[5];
1068
#endif
1069
#endif
1070
ModeLand mode_land;
1071
#if MODE_LOITER_ENABLED
1072
ModeLoiter mode_loiter;
1073
#endif
1074
#if MODE_POSHOLD_ENABLED
1075
ModePosHold mode_poshold;
1076
#endif
1077
#if MODE_RTL_ENABLED
1078
ModeRTL mode_rtl;
1079
#endif
1080
#if FRAME_CONFIG == HELI_FRAME
1081
ModeStabilize_Heli mode_stabilize;
1082
#else
1083
ModeStabilize mode_stabilize;
1084
#endif
1085
#if MODE_SPORT_ENABLED
1086
ModeSport mode_sport;
1087
#endif
1088
#if MODE_SYSTEMID_ENABLED
1089
ModeSystemId mode_systemid;
1090
#endif
1091
#if HAL_ADSB_ENABLED
1092
ModeAvoidADSB mode_avoid_adsb;
1093
#endif
1094
#if MODE_THROW_ENABLED
1095
ModeThrow mode_throw;
1096
#endif
1097
#if MODE_GUIDED_NOGPS_ENABLED
1098
ModeGuidedNoGPS mode_guided_nogps;
1099
#endif
1100
#if MODE_SMARTRTL_ENABLED
1101
ModeSmartRTL mode_smartrtl;
1102
#endif
1103
#if MODE_FLOWHOLD_ENABLED
1104
ModeFlowHold mode_flowhold;
1105
#endif
1106
#if MODE_ZIGZAG_ENABLED
1107
ModeZigZag mode_zigzag;
1108
#endif
1109
#if MODE_AUTOROTATE_ENABLED
1110
ModeAutorotate mode_autorotate;
1111
#endif
1112
#if MODE_TURTLE_ENABLED
1113
ModeTurtle mode_turtle;
1114
#endif
1115
1116
// mode.cpp
1117
Mode *mode_from_mode_num(const Mode::Number mode);
1118
void exit_mode(Mode *&old_flightmode, Mode *&new_flightmode);
1119
1120
bool started_rate_thread;
1121
bool using_rate_thread;
1122
1123
public:
1124
void failsafe_check(); // failsafe.cpp
1125
};
1126
1127
extern Copter copter;
1128
1129
using AP_HAL::millis;
1130
using AP_HAL::micros;
1131
1132