Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Ardupilot
GitHub Repository: Ardupilot/ardupilot
Path: blob/master/Blimp/Blimp.h
9726 views
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 Blimp 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>
32
#include <AP_Common/Location.h>
33
#include <AP_Param/AP_Param.h>
34
#include <StorageManager/StorageManager.h>
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>
42
#include <Filter/Filter.h> // Filter library
43
#include <AP_Vehicle/AP_Vehicle.h> // needed for AHRS build
44
#include <AP_InertialNav/AP_InertialNav.h> // inertial navigation library
45
#include <AP_RCMapper/AP_RCMapper.h> // RC input mapping library
46
#include <AP_BattMonitor/AP_BattMonitor.h> // Battery monitor library
47
#include <AP_Arming/AP_Arming.h>
48
#include <AP_Scripting/AP_Scripting.h>
49
#include <AC_PID/AC_PID_2D.h>
50
#include <AC_PID/AC_PID_Basic.h>
51
#include <AC_PID/AC_PID.h>
52
53
#include <Filter/NotchFilter.h>
54
55
// Configuration
56
#include "defines.h"
57
#include "config.h"
58
59
#include "Fins.h"
60
#include "Loiter.h"
61
62
#include "RC_Channel_Blimp.h" // RC Channel Library
63
64
#include "GCS_MAVLink_Blimp.h"
65
#include "GCS_Blimp.h"
66
#include "AP_Arming_Blimp.h"
67
68
#include <AP_Mount/AP_Mount.h>
69
70
// Local modules
71
72
#include "Parameters.h"
73
74
#include "mode.h"
75
76
class Blimp : public AP_Vehicle
77
{
78
public:
79
friend class GCS_MAVLINK_Blimp;
80
friend class GCS_Blimp;
81
friend class Parameters;
82
friend class ParametersG2;
83
84
friend class AP_Arming_Blimp;
85
friend class RC_Channel_Blimp;
86
friend class RC_Channels_Blimp;
87
88
friend class Mode;
89
friend class ModeManual;
90
friend class ModeLand;
91
friend class ModeVelocity;
92
friend class ModeLoiter;
93
friend class ModeRTL;
94
95
friend class Fins;
96
friend class Loiter;
97
98
Blimp(void);
99
100
private:
101
102
// Global parameters are all contained within the 'g' class.
103
Parameters g;
104
ParametersG2 g2;
105
106
// primary input control channels
107
RC_Channel *channel_right;
108
RC_Channel *channel_front;
109
RC_Channel *channel_up;
110
RC_Channel *channel_yaw;
111
112
// flight modes convenience array
113
AP_Int8 *flight_modes;
114
const uint8_t num_flight_modes = 6;
115
116
// Arming/Disarming management class
117
AP_Arming_Blimp arming;
118
119
// system time in milliseconds of last recorded yaw reset from ekf
120
uint32_t ekfYawReset_ms;
121
int8_t ekf_primary_core;
122
123
// vibration check
124
struct {
125
bool high_vibes; // true while high vibration are detected
126
uint32_t start_ms; // system time high vibration were last detected
127
uint32_t clear_ms; // system time high vibrations stopped
128
} vibration_check;
129
130
// GCS selection
131
GCS_Blimp _gcs; // avoid using this; use gcs()
132
GCS_Blimp &gcs()
133
{
134
return _gcs;
135
}
136
137
// Documentation of Globals:
138
typedef union {
139
struct {
140
uint8_t pre_arm_rc_check : 1; // 1 // true if rc input pre-arm checks have been completed successfully
141
uint8_t pre_arm_check : 1; // 2 // true if all pre-arm checks (rc, accel calibration, gps lock) have been performed
142
uint8_t auto_armed : 1; // 3 // stops auto missions from beginning until throttle is raised
143
uint8_t logging_started : 1; // 4 // true if logging has started
144
uint8_t land_complete : 1; // 5 // true if we have detected a landing
145
uint8_t new_radio_frame : 1; // 6 // Set true if we have new PWM data to act on from the Radio
146
uint8_t rc_receiver_present_unused : 1; // 7 // UNUSED
147
uint8_t compass_mot : 1; // 8 // true if we are currently performing compassmot calibration
148
uint8_t motor_test : 1; // 9 // true if we are currently performing the motors test
149
uint8_t initialised : 1; // 10 // true once the init_ardupilot function has completed. Extended status to GCS is not sent until this completes
150
uint8_t land_complete_maybe : 1; // 11 // true if we may have landed (less strict version of land_complete)
151
uint8_t throttle_zero : 1; // 12 // true if the throttle stick is at zero, debounced, determines if pilot intends shut-down
152
uint8_t gps_glitching : 1; // 13 // true if GPS glitching is affecting navigation accuracy
153
uint8_t in_arming_delay : 1; // 14 // true while we are armed but waiting to spin motors
154
uint8_t initialised_params : 1; // 15 // true when the all parameters have been initialised. we cannot send parameters to the GCS until this is done
155
};
156
uint32_t value;
157
} ap_t;
158
159
ap_t ap;
160
161
static_assert(sizeof(uint32_t) == sizeof(ap), "ap_t must be uint32_t");
162
163
// This is the state of the flight control system
164
// There are multiple states defined such as STABILIZE, ACRO,
165
Mode::Number control_mode;
166
ModeReason control_mode_reason = ModeReason::UNKNOWN;
167
168
RCMapper rcmap;
169
170
// inertial nav alt when we armed
171
float arming_altitude_m;
172
173
// Failsafe
174
struct {
175
int8_t radio_counter; // number of iterations with throttle below throttle_fs_value
176
177
uint8_t radio : 1; // A status flag for the radio failsafe
178
uint8_t gcs : 1; // A status flag for the ground station failsafe
179
uint8_t ekf : 1; // true if ekf failsafe has occurred
180
} failsafe;
181
182
bool any_failsafe_triggered() const
183
{
184
return failsafe.radio || battery.has_failsafed() || failsafe.gcs || failsafe.ekf;
185
}
186
187
// Motor Output
188
Fins *motors;
189
Loiter *loiter;
190
191
int32_t _home_bearing;
192
uint32_t _home_distance;
193
194
// Stores initial bearing when armed - initial simple bearing is modified in super simple mode so not suitable
195
int32_t initial_armed_bearing;
196
197
// Battery Sensors
198
AP_BattMonitor battery{MASK_LOG_CURRENT,
199
FUNCTOR_BIND_MEMBER(&Blimp::handle_battery_failsafe, void, const char*, const int8_t),
200
_failsafe_priorities};
201
202
// Altitude
203
int32_t baro_alt; // barometer altitude in cm above home
204
205
// filtered pilot's throttle input used to cancel landing if throttle held high
206
LowPassFilterFloat rc_throttle_control_in_filter;
207
208
// 3D Location vectors
209
// Current location of the vehicle (altitude is relative to home)
210
Location current_loc;
211
Vector3f vel_ned;
212
Vector3f vel_ned_filtd;
213
214
Vector3f pos_ned;
215
float vel_yaw;
216
float vel_yaw_filtd;
217
NotchFilterVector2f vel_xy_filter;
218
NotchFilterFloat vel_z_filter;
219
NotchFilterFloat vel_yaw_filter;
220
221
// Inertial Navigation
222
AP_InertialNav inertial_nav;
223
224
// Vel & pos PIDs
225
AC_PID_2D pid_vel_xy{3, 0.2, 0, 0, 0.2, 3, 3}; //These are the defaults - P I D FF IMAX FiltHz FiltDHz DT
226
AC_PID_Basic pid_vel_z{7, 1.5, 0, 0, 1, 3, 3};
227
AC_PID_Basic pid_vel_yaw{3, 0.4, 0, 0, 0.2, 3, 3};
228
229
AC_PID_2D pid_pos_xy{1, 0.05, 0, 0, 0.1, 3, 3};
230
AC_PID_Basic pid_pos_z{0.7, 0, 0, 0, 0, 3, 3};
231
AC_PID pid_pos_yaw{1.2, 0.5, 0, 0, 2, 3, 3, 3}; //p, i, d, ff, imax, filt_t, filt_e, filt_d, dt, opt srmax, opt srtau
232
233
// System Timers
234
// --------------
235
// arm_time_ms - Records when vehicle was armed. Will be Zero if we are disarmed.
236
uint32_t arm_time_ms;
237
238
// last valid RC input time
239
uint32_t last_radio_update_ms;
240
241
// Top-level logic
242
// setup the var_info table
243
AP_Param param_loader;
244
245
bool standby_active;
246
247
static const AP_Scheduler::Task scheduler_tasks[];
248
static const AP_Param::Info var_info[];
249
static const struct LogStructure log_structure[];
250
251
enum Failsafe_Action {
252
Failsafe_Action_None = 0,
253
Failsafe_Action_Land = 1,
254
Failsafe_Action_Terminate = 5
255
};
256
257
enum class FailsafeOption {
258
RC_CONTINUE_IF_AUTO = (1<<0), // 1
259
GCS_CONTINUE_IF_AUTO = (1<<1), // 2
260
RC_CONTINUE_IF_GUIDED = (1<<2), // 4
261
CONTINUE_IF_LANDING = (1<<3), // 8
262
GCS_CONTINUE_IF_PILOT_CONTROL = (1<<4), // 16
263
RELEASE_GRIPPER = (1<<5), // 32
264
};
265
266
static constexpr int8_t _failsafe_priorities[] = {
267
Failsafe_Action_Terminate,
268
Failsafe_Action_Land,
269
Failsafe_Action_None,
270
-1 // the priority list must end with a sentinel of -1
271
};
272
273
#define FAILSAFE_LAND_PRIORITY 1
274
static_assert(_failsafe_priorities[FAILSAFE_LAND_PRIORITY] == Failsafe_Action_Land,
275
"FAILSAFE_LAND_PRIORITY must match the entry in _failsafe_priorities");
276
static_assert(_failsafe_priorities[ARRAY_SIZE(_failsafe_priorities) - 1] == -1,
277
"_failsafe_priorities is missing the sentinel");
278
279
// AP_State.cpp
280
void set_auto_armed(bool b);
281
void set_failsafe_radio(bool b);
282
void set_failsafe_gcs(bool b);
283
284
// Blimp.cpp
285
void get_scheduler_tasks(const AP_Scheduler::Task *&tasks,
286
uint8_t &task_count,
287
uint32_t &log_bit) override;
288
void rc_loop();
289
void throttle_loop();
290
void update_batt_compass(void);
291
void full_rate_logging();
292
void ten_hz_logging_loop();
293
void twentyfive_hz_logging();
294
void three_hz_loop();
295
void one_hz_loop();
296
void read_AHRS(void);
297
void update_altitude();
298
void rotate_NE_to_BF(Vector2f &vec);
299
void rotate_BF_to_NE(Vector2f &vec);
300
301
// commands.cpp
302
void update_home_from_EKF();
303
void set_home_to_current_location_inflight();
304
bool set_home_to_current_location(bool lock) override WARN_IF_UNUSED;
305
bool set_home(const Location& loc, bool lock) override WARN_IF_UNUSED;
306
307
// ekf_check.cpp
308
void ekf_check();
309
bool ekf_over_threshold();
310
void failsafe_ekf_event();
311
void failsafe_ekf_off_event(void);
312
void check_ekf_reset();
313
void check_vibration();
314
315
// events.cpp
316
bool failsafe_option(FailsafeOption opt) const;
317
void failsafe_radio_on_event();
318
void failsafe_radio_off_event();
319
void handle_battery_failsafe(const char* type_str, const int8_t action);
320
void failsafe_gcs_check();
321
bool should_disarm_on_failsafe();
322
void do_failsafe_action(Failsafe_Action action, ModeReason reason);
323
void gpsglitch_check();
324
325
// failsafe.cpp
326
void failsafe_enable();
327
void failsafe_disable();
328
329
// fence.cpp
330
void fence_check();
331
332
// inertia.cpp
333
void read_inertia();
334
335
// landing_detector.cpp
336
void update_land_and_crash_detectors();
337
void update_land_detector();
338
339
// landing_gear.cpp
340
void landinggear_update();
341
342
#if HAL_LOGGING_ENABLED
343
// methods for AP_Vehicle:
344
const AP_Int32 &get_log_bitmask() override { return g.log_bitmask; }
345
const struct LogStructure *get_log_structures() const override {
346
return log_structure;
347
}
348
uint8_t get_num_log_structures() const override;
349
350
// Log.cpp
351
void Log_Write_Attitude();
352
void Log_Write_PIDs();
353
void Log_Write_EKF_POS();
354
void Log_Write_Data(LogDataID id, int32_t value);
355
void Log_Write_Data(LogDataID id, uint32_t value);
356
void Log_Write_Data(LogDataID id, int16_t value);
357
void Log_Write_Data(LogDataID id, uint16_t value);
358
void Log_Write_Data(LogDataID id, float value);
359
void Log_Write_Parameter_Tuning(uint8_t param, float tuning_val, float tune_min, float tune_max);
360
361
362
void Log_Write_Vehicle_Startup_Messages();
363
void Write_FINI(float right, float front, float down, float yaw);
364
void Write_FINO(float *amp, float *off);
365
#endif
366
367
// mode.cpp
368
bool set_mode(Mode::Number mode, ModeReason reason);
369
bool set_mode(const uint8_t new_mode, const ModeReason reason) override;
370
uint8_t get_mode() const override
371
{
372
return (uint8_t)control_mode;
373
}
374
void update_flight_mode();
375
void notify_flight_mode();
376
377
// mode_land.cpp
378
void set_mode_land_failsafe(ModeReason reason);
379
bool landing_with_GPS();
380
381
// // motors.cpp
382
void arm_motors_check();
383
void motors_output();
384
385
// Parameters.cpp
386
void load_parameters(void) override;
387
void convert_pid_parameters(void);
388
void convert_fs_options_params(void);
389
390
// radio.cpp
391
void default_dead_zones();
392
void init_rc_in();
393
void init_rc_out();
394
void enable_motor_output();
395
void read_radio();
396
void set_throttle_and_failsafe(uint16_t throttle_pwm);
397
void set_throttle_zero_flag(int16_t throttle_control);
398
399
// sensors.cpp
400
void read_barometer(void);
401
void init_rangefinder(void);
402
void read_rangefinder(void);
403
bool rangefinder_alt_ok();
404
bool rangefinder_up_ok();
405
406
// system.cpp
407
void init_ardupilot() override;
408
void startup_INS_ground();
409
bool position_ok() const;
410
bool ekf_has_absolute_position() const;
411
bool ekf_has_relative_position() const;
412
bool ekf_alt_ok() const;
413
void update_auto_armed();
414
bool should_log(uint32_t mask);
415
MAV_TYPE get_frame_mav_type();
416
const char* get_frame_string();
417
void allocate_motors(void);
418
419
Mode *flightmode;
420
ModeManual mode_manual;
421
ModeLand mode_land;
422
ModeVelocity mode_velocity;
423
ModeLoiter mode_loiter;
424
ModeRTL mode_rtl;
425
426
// mode.cpp
427
Mode *mode_from_mode_num(const Mode::Number mode);
428
void exit_mode(Mode *&old_flightmode, Mode *&new_flightmode);
429
430
public:
431
void failsafe_check(); // failsafe.cpp
432
};
433
434
extern Blimp blimp;
435
436
using AP_HAL::millis;
437
using AP_HAL::micros;
438
439