Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Ardupilot
GitHub Repository: Ardupilot/ardupilot
Path: blob/master/Rover/Rover.h
9674 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
/*
16
main Rover class, containing all vehicle specific state
17
*/
18
#pragma once
19
20
#include <cmath>
21
#include <stdarg.h>
22
#include <stdint.h>
23
24
// Libraries
25
#include <AP_Common/AP_Common.h>
26
#include <AP_HAL/AP_HAL.h>
27
#include <AP_BattMonitor/AP_BattMonitor.h> // Battery monitor library
28
#include <AP_Camera/AP_Camera.h> // Camera triggering
29
#include <AP_Mount/AP_Mount.h> // Camera/Antenna mount
30
#include <AP_Param/AP_Param.h>
31
#include <AP_RangeFinder/AP_RangeFinder.h> // Range finder library
32
#include <AP_RCMapper/AP_RCMapper.h> // RC input mapping library
33
#include <AP_RPM/AP_RPM.h> // RPM input library
34
#include <AP_Scheduler/AP_Scheduler.h> // main loop scheduler
35
#include <AP_Vehicle/AP_Vehicle.h> // needed for AHRS build
36
#include <AP_WheelEncoder/AP_WheelEncoder.h>
37
#include <AP_WheelEncoder/AP_WheelRateControl.h>
38
#include <AP_Logger/AP_Logger.h>
39
#include <AP_OSD/AP_OSD.h>
40
#include <AR_Motors/AP_MotorsUGV.h>
41
#include <AP_Mission/AP_Mission.h>
42
#include <AP_Mission/AP_Mission_ChangeDetector.h>
43
#include <AR_WPNav/AR_WPNav_OA.h>
44
#include <AP_OpticalFlow/AP_OpticalFlow.h>
45
#include <AC_PrecLand/AC_PrecLand_config.h>
46
#include <AP_Follow/AP_Follow_config.h>
47
#include <AP_ExternalControl/AP_ExternalControl_config.h>
48
#if AP_EXTERNAL_CONTROL_ENABLED
49
#include "AP_ExternalControl_Rover.h"
50
#endif
51
52
// Configuration
53
#include "defines.h"
54
#include "config.h"
55
56
#if AP_SCRIPTING_ENABLED
57
#include <AP_Scripting/AP_Scripting.h>
58
#endif
59
60
// Local modules
61
#include "AP_Arming_Rover.h"
62
#include "sailboat.h"
63
#if AP_ROVER_ADVANCED_FAILSAFE_ENABLED
64
#include "afs_rover.h"
65
#endif
66
#include "Parameters.h"
67
#include "GCS_MAVLink_Rover.h"
68
#include "GCS_Rover.h"
69
#include "AP_Rally.h"
70
#if AC_PRECLAND_ENABLED
71
#include <AC_PrecLand/AC_PrecLand.h>
72
#endif
73
#include "RC_Channel_Rover.h" // RC Channel Library
74
75
#include "mode.h"
76
77
class Rover : public AP_Vehicle {
78
public:
79
friend class GCS_MAVLINK_Rover;
80
friend class Parameters;
81
friend class ParametersG2;
82
friend class AP_Rally_Rover;
83
friend class AP_Arming_Rover;
84
#if AP_ROVER_ADVANCED_FAILSAFE_ENABLED
85
friend class AP_AdvancedFailsafe_Rover;
86
#endif
87
#if AP_EXTERNAL_CONTROL_ENABLED
88
friend class AP_ExternalControl_Rover;
89
#endif
90
friend class GCS_Rover;
91
friend class Mode;
92
friend class ModeAcro;
93
friend class ModeAuto;
94
friend class ModeCircle;
95
friend class ModeGuided;
96
friend class ModeHold;
97
friend class ModeLoiter;
98
friend class ModeSteering;
99
friend class ModeManual;
100
friend class ModeRTL;
101
friend class ModeSmartRTL;
102
#if MODE_FOLLOW_ENABLED
103
friend class ModeFollow;
104
#endif
105
friend class ModeSimple;
106
#if MODE_DOCK_ENABLED
107
friend class ModeDock;
108
#endif
109
110
friend class RC_Channel_Rover;
111
friend class RC_Channels_Rover;
112
113
friend class Sailboat;
114
115
Rover(void);
116
117
private:
118
119
// must be the first AP_Param variable declared to ensure its
120
// constructor runs before the constructors of the other AP_Param
121
// variables
122
AP_Param param_loader;
123
124
// all settable parameters
125
Parameters g;
126
ParametersG2 g2;
127
128
// mapping between input channels
129
RCMapper rcmap;
130
131
// primary control channels
132
RC_Channel *channel_steer;
133
RC_Channel *channel_throttle;
134
RC_Channel *channel_lateral;
135
RC_Channel *channel_roll;
136
RC_Channel *channel_pitch;
137
RC_Channel *channel_walking_height;
138
139
// flight modes convenience array
140
AP_Int8 *modes;
141
const uint8_t num_modes = 6;
142
143
// Arming/Disarming management class
144
AP_Arming_Rover arming;
145
146
// external control implementation
147
#if AP_EXTERNAL_CONTROL_ENABLED
148
AP_ExternalControl_Rover external_control;
149
#endif
150
151
#if AP_OPTICALFLOW_ENABLED
152
AP_OpticalFlow optflow;
153
#endif
154
155
#if OSD_ENABLED || OSD_PARAM_ENABLED
156
AP_OSD osd;
157
#endif
158
#if AC_PRECLAND_ENABLED
159
AC_PrecLand precland;
160
#endif
161
// GCS handling
162
GCS_Rover _gcs; // avoid using this; use gcs()
163
GCS_Rover &gcs() { return _gcs; }
164
165
// RC Channels:
166
RC_Channels_Rover &rc() { return g2.rc_channels; }
167
168
// The rover's current location
169
Location current_loc;
170
171
// Camera
172
#if AP_CAMERA_ENABLED
173
AP_Camera camera{MASK_LOG_CAMERA};
174
#endif
175
176
// Camera/Antenna mount tracking and stabilisation stuff
177
#if HAL_MOUNT_ENABLED
178
AP_Mount camera_mount;
179
#endif
180
181
// true if initialisation has completed
182
bool initialised;
183
184
// This is the state of the flight control system
185
// There are multiple states defined such as MANUAL, AUTO, ...
186
Mode *control_mode;
187
188
// Used to maintain the state of the previous control switch position
189
// This is set to -1 when we need to re-read the switch
190
uint8_t oldSwitchPosition;
191
192
// structure for holding failsafe state
193
struct {
194
uint8_t bits; // bit flags of failsafes that have started (but not necessarily triggered an action)
195
uint32_t start_time; // start time of the earliest failsafe
196
uint8_t triggered; // bit flags of failsafes that have triggered an action
197
uint32_t last_valid_rc_ms; // system time of most recent RC input from pilot
198
bool ekf;
199
} failsafe;
200
201
// true if we have a position estimate from AHRS
202
bool have_position;
203
204
#if AP_RANGEFINDER_ENABLED
205
// range finder last update for each instance (used for DPTH logging)
206
uint32_t rangefinder_last_reading_ms[RANGEFINDER_MAX_INSTANCES];
207
#endif
208
209
// Ground speed
210
// The amount current ground speed is below min ground speed. meters per second
211
float ground_speed;
212
213
// Battery Sensors
214
AP_BattMonitor battery{MASK_LOG_CURRENT,
215
FUNCTOR_BIND_MEMBER(&Rover::handle_battery_failsafe, void, const char*, const int8_t),
216
_failsafe_priorities};
217
218
// flyforward timer
219
uint32_t flyforward_start_ms;
220
221
static const AP_Scheduler::Task scheduler_tasks[];
222
223
static const AP_Param::Info var_info[];
224
#if HAL_LOGGING_ENABLED
225
static const LogStructure log_structure[];
226
#endif
227
228
// latest wheel encoder values
229
float wheel_encoder_last_distance_m[WHEELENCODER_MAX_INSTANCES]; // total distance recorded by wheel encoder (for reporting to GCS)
230
bool wheel_encoder_initialised; // true once arrays below have been initialised to sensors initial values
231
float wheel_encoder_last_angle_rad[WHEELENCODER_MAX_INSTANCES]; // distance in radians at time of last update to EKF
232
uint32_t wheel_encoder_last_reading_ms[WHEELENCODER_MAX_INSTANCES]; // system time of last ping from each encoder
233
uint8_t wheel_encoder_last_index_sent; // index of the last wheel encoder sent to the EKF
234
235
// True when we are doing motor test
236
bool motor_test;
237
238
ModeInitializing mode_initializing;
239
ModeHold mode_hold;
240
ModeManual mode_manual;
241
ModeAcro mode_acro;
242
ModeGuided mode_guided;
243
ModeAuto mode_auto;
244
ModeLoiter mode_loiter;
245
ModeSteering mode_steering;
246
ModeRTL mode_rtl;
247
ModeSmartRTL mode_smartrtl;
248
#if MODE_FOLLOW_ENABLED
249
ModeFollow mode_follow;
250
#endif
251
ModeSimple mode_simple;
252
#if MODE_DOCK_ENABLED
253
ModeDock mode_dock;
254
#endif
255
256
// cruise throttle and speed learning
257
typedef struct {
258
LowPassFilterFloat speed_filt{2.0f};
259
LowPassFilterFloat throttle_filt{2.0f};
260
uint32_t learn_start_ms;
261
uint32_t log_count;
262
} cruise_learn_t;
263
cruise_learn_t cruise_learn;
264
265
// Rover.cpp
266
#if AP_SCRIPTING_ENABLED || AP_EXTERNAL_CONTROL_ENABLED
267
bool set_target_location(const Location& target_loc) override;
268
#endif
269
270
#if AP_SCRIPTING_ENABLED
271
bool set_target_velocity_NED(const Vector3f& vel_ned_ms, bool align_yaw_to_target) override;
272
bool set_steering_and_throttle(float steering, float throttle) override;
273
bool get_steering_and_throttle(float& steering, float& throttle) override;
274
// set desired turn rate (degrees/sec) and speed (m/s). Used for scripting
275
bool set_desired_turn_rate_and_speed(float turn_rate_degs, float speed_ms) override;
276
bool set_desired_speed(float speed_ms) override;
277
bool get_control_output(AP_Vehicle::ControlOutput control_output, float &control_value) override;
278
bool nav_scripting_enable(uint8_t mode) override;
279
bool nav_script_time(uint16_t &id, uint8_t &cmd, float &arg1, float &arg2, int16_t &arg3, int16_t &arg4) override;
280
void nav_script_time_done(uint16_t id) override;
281
#endif // AP_SCRIPTING_ENABLED
282
void ahrs_update();
283
void gcs_failsafe_check(void);
284
void update_logging1(void);
285
void update_logging2(void);
286
void one_second_loop(void);
287
void update_current_mode(void);
288
289
// balance_bot.cpp
290
void balancebot_pitch_control(float &throttle);
291
bool is_balancebot() const;
292
293
// commands.cpp
294
bool set_home_to_current_location(bool lock) override WARN_IF_UNUSED;
295
bool set_home(const Location& loc, bool lock) override WARN_IF_UNUSED;
296
void update_home();
297
298
// crash_check.cpp
299
void crash_check();
300
301
// cruise_learn.cpp
302
void cruise_learn_start();
303
void cruise_learn_update();
304
void cruise_learn_complete();
305
void log_write_cruise_learn() const;
306
307
// ekf_check.cpp
308
void ekf_check();
309
bool ekf_over_threshold();
310
bool ekf_position_ok();
311
void failsafe_ekf_event();
312
void failsafe_ekf_off_event(void);
313
314
// failsafe.cpp
315
void failsafe_trigger(uint8_t failsafe_type, const char* type_str, bool on);
316
void handle_battery_failsafe(const char* type_str, const int8_t action);
317
#if AP_ROVER_ADVANCED_FAILSAFE_ENABLED
318
void afs_fs_check(void);
319
#endif
320
#if AP_FENCE_ENABLED
321
// fence.cpp
322
void fence_checks_async() override;
323
void fence_check();
324
#endif
325
// GCS_Mavlink.cpp
326
void send_wheel_encoder_distance(mavlink_channel_t chan);
327
328
#if HAL_LOGGING_ENABLED
329
// methods for AP_Vehicle:
330
const AP_Int32 &get_log_bitmask() override { return g.log_bitmask; }
331
const struct LogStructure *get_log_structures() const override {
332
return log_structure;
333
}
334
uint8_t get_num_log_structures() const override;
335
336
// Log.cpp
337
void Log_Write_Attitude();
338
void Log_Write_Depth();
339
void Log_Write_GuidedTarget(uint8_t target_type, const Vector3f& pos_target, const Vector3f& vel_target);
340
void Log_Write_Nav_Tuning();
341
void Log_Write_Sail();
342
void Log_Write_Steering();
343
void Log_Write_Throttle();
344
void Log_Write_RC(void);
345
void Log_Write_Vehicle_Startup_Messages();
346
void Log_Read(uint16_t log_num, uint16_t start_page, uint16_t end_page);
347
#endif
348
349
// mode.cpp
350
Mode *mode_from_mode_num(enum Mode::Number num);
351
352
// Parameters.cpp
353
void load_parameters(void) override;
354
355
// precision_landing.cpp
356
void init_precland();
357
void update_precland();
358
359
// radio.cpp
360
void set_control_channels(void) override;
361
void init_rc_in();
362
void read_radio();
363
void radio_failsafe_check(uint16_t pwm);
364
365
// sensors.cpp
366
void update_compass(void);
367
void update_wheel_encoder();
368
#if AP_RANGEFINDER_ENABLED
369
void read_rangefinders(void);
370
#endif
371
372
// Steering.cpp
373
void set_servos(void);
374
375
// Rover.cpp
376
void get_scheduler_tasks(const AP_Scheduler::Task *&tasks,
377
uint8_t &task_count,
378
uint32_t &log_bit) override;
379
380
// system.cpp
381
void init_ardupilot() override;
382
void startup_ground(void);
383
void update_ahrs_flyforward();
384
bool gcs_mode_enabled(const Mode::Number mode_num) const;
385
bool set_mode(Mode &new_mode, ModeReason reason);
386
bool set_mode(const uint8_t new_mode, ModeReason reason) override;
387
bool set_mode(Mode::Number new_mode, ModeReason reason);
388
uint8_t get_mode() const override { return (uint8_t)control_mode->mode_number(); }
389
bool current_mode_requires_mission() const override {
390
return control_mode == &mode_auto;
391
}
392
393
void startup_INS(void);
394
void notify_mode(const Mode *new_mode);
395
uint8_t check_digital_pin(uint8_t pin);
396
bool should_log(uint32_t mask);
397
bool is_boat() const;
398
399
// vehicle specific waypoint info helpers
400
bool get_wp_distance_m(float &distance) const override;
401
bool get_wp_bearing_deg(float &bearing) const override;
402
bool get_wp_crosstrack_error_m(float &xtrack_error) const override;
403
404
enum class FailsafeAction: int8_t {
405
None = 0,
406
RTL = 1,
407
Hold = 2,
408
SmartRTL = 3,
409
SmartRTL_Hold = 4,
410
Terminate = 5,
411
Loiter_Hold = 6,
412
};
413
414
enum class Failsafe_Options : uint32_t {
415
Failsafe_Option_Active_In_Hold = (1<<0)
416
};
417
418
static constexpr int8_t _failsafe_priorities[] = {
419
(int8_t)FailsafeAction::Terminate,
420
(int8_t)FailsafeAction::Hold,
421
(int8_t)FailsafeAction::RTL,
422
(int8_t)FailsafeAction::SmartRTL_Hold,
423
(int8_t)FailsafeAction::SmartRTL,
424
(int8_t)FailsafeAction::None,
425
-1 // the priority list must end with a sentinel of -1
426
};
427
static_assert(_failsafe_priorities[ARRAY_SIZE(_failsafe_priorities) - 1] == -1,
428
"_failsafe_priorities is missing the sentinel");
429
430
431
public:
432
void failsafe_check();
433
// Motor test
434
void motor_test_output();
435
bool mavlink_motor_test_check(const GCS_MAVLINK &gcs_chan, bool check_rc, AP_MotorsUGV::motor_test_order motor_instance, uint8_t throttle_type, int16_t throttle_value);
436
MAV_RESULT mavlink_motor_test_start(const GCS_MAVLINK &gcs_chan, AP_MotorsUGV::motor_test_order motor_instance, uint8_t throttle_type, int16_t throttle_value, float timeout_sec);
437
void motor_test_stop();
438
439
// frame type
440
uint8_t get_frame_type() const { return g2.frame_type.get(); }
441
AP_WheelRateControl& get_wheel_rate_control() { return g2.wheel_rate_control; }
442
443
// Simple mode
444
float simple_sin_yaw;
445
446
#if AP_ROVER_AUTO_ARM_ONCE_ENABLED
447
struct {
448
uint32_t last_arm_attempt_ms;
449
bool done;
450
} auto_arm_once;
451
void handle_auto_arm_once();
452
#endif // AP_ROVER_AUTO_ARM_ONCE_ENABLED
453
};
454
455
extern Rover rover;
456
457
using AP_HAL::millis;
458
using AP_HAL::micros;
459
460