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/libraries/AC_PrecLand/AC_PrecLand.h
Views: 1798
1
#pragma once
2
3
#include "AC_PrecLand_config.h"
4
5
#if AC_PRECLAND_ENABLED
6
7
#include <GCS_MAVLink/GCS_MAVLink.h>
8
#include <AP_Math/AP_Math.h>
9
#include <stdint.h>
10
#include "PosVelEKF.h"
11
#include <AP_HAL/utility/RingBuffer.h>
12
#include <AC_PrecLand/AC_PrecLand_StateMachine.h>
13
14
// declare backend classes
15
class AC_PrecLand_Backend;
16
class AC_PrecLand_Companion;
17
class AC_PrecLand_IRLock;
18
class AC_PrecLand_SITL_Gazebo;
19
class AC_PrecLand_SITL;
20
class Location;
21
22
class AC_PrecLand
23
{
24
// declare backends as friends
25
friend class AC_PrecLand_Backend;
26
friend class AC_PrecLand_Companion;
27
friend class AC_PrecLand_IRLock;
28
friend class AC_PrecLand_SITL_Gazebo;
29
friend class AC_PrecLand_SITL;
30
31
public:
32
AC_PrecLand();
33
34
/* Do not allow copies */
35
CLASS_NO_COPY(AC_PrecLand);
36
37
// return singleton
38
static AC_PrecLand *get_singleton() {
39
return _singleton;
40
}
41
42
// perform any required initialisation of landing controllers
43
// update_rate_hz should be the rate at which the update method will be called in hz
44
void init(uint16_t update_rate_hz);
45
46
// returns true if precision landing is healthy
47
bool healthy() const { return _backend_state.healthy; }
48
49
// returns true if precision landing is enabled (used only for logging)
50
bool enabled() const { return _enabled.get(); }
51
52
// returns time of last update
53
uint32_t last_update_ms() const { return _last_update_ms; }
54
55
// returns time of last time target was seen
56
uint32_t last_backend_los_meas_ms() const { return _last_backend_los_meas_ms; }
57
58
// vehicle has to be closer than this many cm's to the target before descending towards target
59
float get_max_xy_error_before_descending_cm() const { return _xy_max_dist_desc * 100.0f; }
60
61
// returns orientation of sensor
62
Rotation get_orient() const { return _orient; }
63
64
// returns ekf outlier count
65
uint32_t ekf_outlier_count() const { return _outlier_reject_count; }
66
67
// give chance to driver to get updates from sensor, should be called at 400hz
68
void update(float rangefinder_alt_cm, bool rangefinder_alt_valid);
69
70
// returns target position relative to the EKF origin
71
bool get_target_position_cm(Vector2f& ret);
72
73
// returns target relative position as 3D vector
74
void get_target_position_measurement_cm(Vector3f& ret);
75
76
// returns target position relative to vehicle
77
bool get_target_position_relative_cm(Vector2f& ret);
78
79
// returns target velocity relative to vehicle
80
bool get_target_velocity_relative_cms(Vector2f& ret);
81
82
// get the absolute velocity of the vehicle
83
void get_target_velocity_cms(const Vector2f& vehicle_velocity_cms, Vector2f& target_vel_cms);
84
85
// returns true when the landing target has been detected
86
bool target_acquired();
87
88
// process a LANDING_TARGET mavlink message
89
void handle_msg(const mavlink_landing_target_t &packet, uint32_t timestamp_ms);
90
91
// State of the Landing Target Location
92
enum class TargetState: uint8_t {
93
TARGET_NEVER_SEEN = 0,
94
TARGET_OUT_OF_RANGE,
95
TARGET_RECENTLY_LOST,
96
TARGET_FOUND
97
};
98
99
// return the last time PrecLand library had a output of the landing target position
100
uint32_t get_last_valid_target_ms() const { return _last_valid_target_ms; }
101
102
// return the current state of the location of the target
103
TargetState get_target_state() const { return _current_target_state; }
104
105
// return the last known landing position in Earth Frame NED meters.
106
void get_last_detected_landing_pos(Vector3f &pos) const { pos = _last_target_pos_rel_origin_NED; }
107
108
// return the last known postion of the vehicle when the target was detected in Earth Frame NED meters.
109
void get_last_vehicle_pos_when_target_detected(Vector3f &pos) const { pos = _last_vehicle_pos_NED; }
110
111
// Parameter getters
112
AC_PrecLand_StateMachine::RetryStrictness get_retry_strictness() const { return static_cast<AC_PrecLand_StateMachine::RetryStrictness>(_strict.get()); }
113
uint8_t get_max_retry_allowed() const { return _retry_max; }
114
float get_min_retry_time_sec() const { return _retry_timeout_sec; }
115
AC_PrecLand_StateMachine::RetryAction get_retry_behaviour() const { return static_cast<AC_PrecLand_StateMachine::RetryAction>(_retry_behave.get()); }
116
117
bool allow_precland_after_reposition() const { return _options & PLND_OPTION_PRECLAND_AFTER_REPOSITION; }
118
bool do_fast_descend() const { return _options & PLND_OPTION_FAST_DESCEND; }
119
120
/*
121
get target location lat/lon. Note that altitude in returned
122
location is not reliable
123
*/
124
bool get_target_location(Location &loc);
125
126
/*
127
get the absolute velocity of the target in m/s.
128
return false if we cannot estimate target velocity or if the target is not acquired
129
*/
130
bool get_target_velocity(Vector2f& ret);
131
132
// parameter var table
133
static const struct AP_Param::GroupInfo var_info[];
134
135
private:
136
enum class EstimatorType : uint8_t {
137
RAW_SENSOR = 0,
138
KALMAN_FILTER = 1,
139
};
140
141
// types of precision landing (used for PRECLAND_TYPE parameter)
142
enum class Type : uint8_t {
143
NONE = 0,
144
#if AC_PRECLAND_COMPANION_ENABLED
145
COMPANION = 1,
146
#endif
147
#if AC_PRECLAND_IRLOCK_ENABLED
148
IRLOCK = 2,
149
#endif
150
#if AC_PRECLAND_SITL_GAZEBO_ENABLED
151
SITL_GAZEBO = 3,
152
#endif
153
#if AC_PRECLAND_SITL_ENABLED
154
SITL = 4,
155
#endif
156
};
157
158
enum PLndOptions {
159
PLND_OPTION_DISABLED = 0,
160
PLND_OPTION_MOVING_TARGET = (1 << 0),
161
PLND_OPTION_PRECLAND_AFTER_REPOSITION = (1 << 1),
162
PLND_OPTION_FAST_DESCEND = (1 << 2),
163
};
164
165
// check the status of the target
166
void check_target_status(float rangefinder_alt_m, bool rangefinder_alt_valid);
167
168
// Check if the landing target is supposed to be in sight based on the height of the vehicle from the ground
169
// This needs a valid rangefinder to work, if the min/max parameters are non zero
170
bool check_if_sensor_in_range(float rangefinder_alt_m, bool rangefinder_alt_valid) const;
171
172
// check if EKF got the time to initialize when the landing target was first detected
173
// Expects sensor to update within EKF_INIT_SENSOR_MIN_UPDATE_MS milliseconds till EKF_INIT_TIME_MS milliseconds have passed
174
// after this period landing target estimates can be used by vehicle
175
void check_ekf_init_timeout();
176
177
// run target position estimator
178
void run_estimator(float rangefinder_alt_m, bool rangefinder_alt_valid);
179
180
// If a new measurement was retrieved, sets _target_pos_rel_meas_NED and returns true
181
bool construct_pos_meas_using_rangefinder(float rangefinder_alt_m, bool rangefinder_alt_valid);
182
183
// get vehicle body frame 3D vector from vehicle to target. returns true on success, false on failure
184
bool retrieve_los_meas(Vector3f& target_vec_unit_body);
185
186
// calculate target's position and velocity relative to the vehicle (used as input to position controller)
187
// results are stored in_target_pos_rel_out_NE, _target_vel_rel_out_NE
188
void run_output_prediction();
189
190
// parameters
191
AP_Int8 _enabled; // enabled/disabled
192
AP_Enum<Type> _type; // precision landing sensor type
193
AP_Int8 _bus; // which sensor bus
194
AP_Enum<EstimatorType> _estimator_type; // precision landing estimator type
195
AP_Float _lag; // sensor lag in seconds
196
AP_Float _yaw_align; // Yaw angle from body x-axis to sensor x-axis.
197
AP_Float _land_ofs_cm_x; // Desired landing position of the camera forward of the target in vehicle body frame
198
AP_Float _land_ofs_cm_y; // Desired landing position of the camera right of the target in vehicle body frame
199
AP_Float _accel_noise; // accelerometer process noise
200
AP_Vector3f _cam_offset; // Position of the camera relative to the CG
201
AP_Float _xy_max_dist_desc; // Vehicle doing prec land will only descent vertically when horizontal error (in m) is below this limit
202
AP_Int8 _strict; // PrecLand strictness
203
AP_Int8 _retry_max; // PrecLand Maximum number of retires to a failed landing
204
AP_Float _retry_timeout_sec; // Time for which vehicle continues descend even if target is lost. After this time period, vehicle will attempt a landing retry depending on PLND_STRICT param.
205
AP_Int8 _retry_behave; // Action to do when trying a landing retry
206
AP_Float _sensor_min_alt; // PrecLand minimum height required for detecting target
207
AP_Float _sensor_max_alt; // PrecLand maximum height the sensor can detect target
208
AP_Int16 _options; // Bitmask for extra options
209
AP_Enum<Rotation> _orient; // Orientation of camera/sensor
210
211
uint32_t _last_update_ms; // system time in millisecond when update was last called
212
bool _target_acquired; // true if target has been seen recently after estimator is initialized
213
bool _estimator_initialized; // true if estimator has been initialized after few seconds of the target being detected by sensor
214
uint32_t _estimator_init_ms; // system time in millisecond when EKF was init
215
uint32_t _last_backend_los_meas_ms; // system time target was last seen
216
uint32_t _last_valid_target_ms; // last time PrecLand library had a output of the landing target position
217
218
PosVelEKF _ekf_x, _ekf_y; // Kalman Filter for x and y axis
219
uint32_t _outlier_reject_count; // mini-EKF's outlier counter (3 consecutive outliers lead to EKF accepting updates)
220
221
Vector3f _target_pos_rel_meas_NED; // target's relative position as 3D vector
222
Vector3f _approach_vector_body; // unit vector in landing approach direction (in body frame)
223
224
Vector3f _last_target_pos_rel_origin_NED; // stores the last known location of the target horizontally, and the height of the vehicle where it detected this target in meters NED
225
Vector3f _last_vehicle_pos_NED; // stores the position of the vehicle when landing target was last detected in m and NED
226
Vector2f _target_pos_rel_est_NE; // target's position relative to the IMU, not compensated for lag
227
Vector2f _target_vel_rel_est_NE; // target's velocity relative to the IMU, not compensated for lag
228
229
Vector2f _target_pos_rel_out_NE; // target's position relative to the camera, fed into position controller
230
Vector2f _target_vel_rel_out_NE; // target's velocity relative to the CG, fed into position controller
231
Vector3f _last_veh_velocity_NED_ms; // AHRS velocity at last estimate
232
233
TargetState _current_target_state; // Current status of the landing target
234
235
// structure and buffer to hold a history of vehicle velocity
236
struct inertial_data_frame_s {
237
Matrix3f Tbn; // dcm rotation matrix to rotate body frame to north
238
Vector3f correctedVehicleDeltaVelocityNED;
239
Vector3f inertialNavVelocity;
240
bool inertialNavVelocityValid;
241
float dt;
242
uint64_t time_usec;
243
};
244
ObjectArray<inertial_data_frame_s> *_inertial_history;
245
struct inertial_data_frame_s *_inertial_data_delayed;
246
247
// backend state
248
struct precland_state {
249
bool healthy;
250
} _backend_state;
251
AC_PrecLand_Backend *_backend; // pointers to backend precision landing driver
252
253
// write out PREC message to log:
254
void Write_Precland();
255
uint32_t _last_log_ms; // last time we logged
256
257
static AC_PrecLand *_singleton; //singleton
258
};
259
260
namespace AP {
261
AC_PrecLand *ac_precland();
262
};
263
264
#endif // AC_PRECLAND_ENABLED
265
266