Path: blob/master/libraries/AC_PrecLand/AC_PrecLand.h
9711 views
#pragma once12#include "AC_PrecLand_config.h"34#if AC_PRECLAND_ENABLED56#include <GCS_MAVLink/GCS_MAVLink.h>7#include <AP_Math/AP_Math.h>8#include <stdint.h>9#include "PosVelEKF.h"10#include <AP_HAL/utility/RingBuffer.h>11#include <AC_PrecLand/AC_PrecLand_StateMachine.h>1213// declare backend classes14class AC_PrecLand_Backend;15class AC_PrecLand_MAVLink;16class AC_PrecLand_IRLock;17class AC_PrecLand_SITL_Gazebo;18class AC_PrecLand_SITL;19class Location;2021class AC_PrecLand22{23// declare backends as friends24friend class AC_PrecLand_Backend;25friend class AC_PrecLand_MAVLink;26friend class AC_PrecLand_IRLock;27friend class AC_PrecLand_SITL_Gazebo;28friend class AC_PrecLand_SITL;2930public:31AC_PrecLand();3233/* Do not allow copies */34CLASS_NO_COPY(AC_PrecLand);3536// return singleton37static AC_PrecLand *get_singleton() {38return _singleton;39}4041// perform any required initialisation of landing controllers42// update_rate_hz should be the rate at which the update method will be called in hz43void init(uint16_t update_rate_hz);4445// returns true if precision landing is healthy46bool healthy() const { return _backend_state.healthy; }4748// returns true if precision landing is enabled (used only for logging)49bool enabled() const { return _enabled.get(); }5051// returns time of last update52uint32_t last_update_ms() const { return _last_update_ms; }5354// returns time of last time target was seen55uint32_t last_backend_los_meas_ms() const { return _last_backend_los_meas_ms; }5657// vehicle has to be closer than this many m's to the target before descending towards target58float get_max_xy_error_before_descending_m() const { return _xy_max_dist_desc_m; }5960// returns orientation of sensor61Rotation get_orient() const { return _orient; }6263// returns ekf outlier count64uint32_t ekf_outlier_count() const { return _outlier_reject_count; }6566// give chance to driver to get updates from sensor, should be called at 400hz67void update(float rangefinder_alt_m, bool rangefinder_alt_valid);6869// returns target position relative to the EKF origin70bool get_target_position_m(Vector2p& ret);7172// returns target relative position as 3D vector73void get_target_position_measurement_NED_m(Vector3f& ret);7475// returns target position relative to vehicle76bool get_target_position_relative_NE_m(Vector2f& ret);7778// returns target velocity relative to vehicle79bool get_target_velocity_relative_NE_ms(Vector2f& ret);8081// get the absolute velocity of the vehicle82void get_target_velocity_ms(const Vector2f& vehicle_velocity_ms, Vector2f& target_vel_ms);8384// returns true when the landing target has been detected85bool target_acquired();8687// process a LANDING_TARGET mavlink message88void handle_msg(const mavlink_landing_target_t &packet, uint32_t timestamp_ms);8990// State of the Landing Target Location91enum class TargetState: uint8_t {92TARGET_NEVER_SEEN = 0,93TARGET_OUT_OF_RANGE,94TARGET_RECENTLY_LOST,95TARGET_FOUND96};9798// return the last time PrecLand library had a output of the landing target position99uint32_t get_last_valid_target_ms() const { return _last_valid_target_ms; }100101// return the current state of the location of the target102TargetState get_target_state() const { return _current_target_state; }103104// return the last known landing position in Earth Frame NED meters.105void get_last_detected_landing_pos_NED_m(Vector3p &pos) const { pos = _last_target_pos_rel_origin_ned_m; }106107// return the last known position of the vehicle when the target was detected in Earth Frame NED meters.108void get_last_vehicle_pos_when_target_detected_NED_m(Vector3p &pos) const { pos = _last_vehicle_pos_ned_m; }109110// Parameter getters111AC_PrecLand_StateMachine::RetryStrictness get_retry_strictness() const { return static_cast<AC_PrecLand_StateMachine::RetryStrictness>(_strict.get()); }112uint8_t get_max_retry_allowed() const { return _retry_max; }113float get_min_retry_time_sec() const { return _retry_timeout_s; }114AC_PrecLand_StateMachine::RetryAction get_retry_behaviour() const { return static_cast<AC_PrecLand_StateMachine::RetryAction>(_retry_behave.get()); }115116bool allow_precland_after_reposition() const { return _options & PLND_OPTION_PRECLAND_AFTER_REPOSITION; }117bool do_fast_descend() const { return _options & PLND_OPTION_FAST_DESCEND; }118119/*120get target location lat/lon. Note that altitude in returned121location is not reliable122*/123bool get_target_location(Location &loc);124125/*126get the absolute velocity of the target in m/s.127return false if we cannot estimate target velocity or if the target is not acquired128*/129bool get_target_velocity(Vector2f& ret);130131// parameter var table132static const struct AP_Param::GroupInfo var_info[];133134private:135enum class EstimatorType : uint8_t {136RAW_SENSOR = 0,137KALMAN_FILTER = 1,138};139140// types of precision landing (used for PRECLAND_TYPE parameter)141enum class Type : uint8_t {142NONE = 0,143#if AC_PRECLAND_MAVLINK_ENABLED144MAVLINK = 1,145#endif146#if AC_PRECLAND_IRLOCK_ENABLED147IRLOCK = 2,148#endif149#if AC_PRECLAND_SITL_GAZEBO_ENABLED150SITL_GAZEBO = 3,151#endif152#if AC_PRECLAND_SITL_ENABLED153SITL = 4,154#endif155};156157enum PLndOptions {158PLND_OPTION_DISABLED = 0,159PLND_OPTION_MOVING_TARGET = (1 << 0),160PLND_OPTION_PRECLAND_AFTER_REPOSITION = (1 << 1),161PLND_OPTION_FAST_DESCEND = (1 << 2),162};163164// frames for vectors from vehicle to target165enum class VectorFrame : uint8_t {166BODY_FRD = 0, // body frame, forward-right-down relative to the vehicle's attitude167LOCAL_FRD = 1, // forward-right-down where forward is aligned with front of the vehicle in the horizontal plane168};169170// check the status of the target171void check_target_status(float rangefinder_alt_m, bool rangefinder_alt_valid);172173// Check if the landing target is supposed to be in sight based on the height of the vehicle from the ground174// This needs a valid rangefinder to work, if the min/max parameters are non zero175bool check_if_sensor_in_range(float rangefinder_alt_m, bool rangefinder_alt_valid) const;176177// check if EKF got the time to initialize when the landing target was first detected178// Expects sensor to update within EKF_INIT_SENSOR_MIN_UPDATE_MS milliseconds till EKF_INIT_TIME_MS milliseconds have passed179// after this period landing target estimates can be used by vehicle180void check_ekf_init_timeout();181182// run target position estimator183void run_estimator(float rangefinder_alt_m, bool rangefinder_alt_valid);184185// If a new measurement was retrieved, sets _target_pos_rel_meas_ned_m and returns true186bool construct_pos_meas_using_rangefinder(float rangefinder_alt_m, bool rangefinder_alt_valid);187188// get 3D vector from vehicle to target and frame. returns true on success, false on failure189bool retrieve_los_meas(Vector3f& target_vec_unit, VectorFrame& frame);190191// calculate target's position and velocity relative to the vehicle (used as input to position controller)192// results are stored in_target_pos_rel_out_NE, _target_vel_rel_out_ne_ms193void run_output_prediction();194195// parameters196AP_Int8 _enabled; // enabled/disabled197AP_Enum<Type> _type; // precision landing sensor type198AP_Int8 _bus; // which sensor bus199AP_Enum<EstimatorType> _estimator_type; // precision landing estimator type200AP_Float _lag_s; // sensor lag in seconds201AP_Float _yaw_align_cd; // Yaw angle from body x-axis to sensor x-axis.202AP_Float _land_ofs_cm_x; // Desired landing position of the camera forward of the target in vehicle body frame203AP_Float _land_ofs_cm_y; // Desired landing position of the camera right of the target in vehicle body frame204AP_Float _accel_noise; // accelerometer process noise205AP_Vector3f _cam_offset_m; // Position of the camera relative to the CG206AP_Float _xy_max_dist_desc_m; // Vehicle doing prec land will only descent vertically when horizontal error (in m) is below this limit207AP_Int8 _strict; // PrecLand strictness208AP_Int8 _retry_max; // PrecLand Maximum number of retires to a failed landing209AP_Float _retry_timeout_s; // 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.210AP_Int8 _retry_behave; // Action to do when trying a landing retry211AP_Float _sensor_min_alt_m; // PrecLand minimum height required for detecting target212AP_Float _sensor_max_alt_m; // PrecLand maximum height the sensor can detect target213AP_Int16 _options; // Bitmask for extra options214AP_Enum<Rotation> _orient; // Orientation of camera/sensor215216uint32_t _last_update_ms; // system time in millisecond when update was last called217bool _target_acquired; // true if target has been seen recently after estimator is initialized218bool _estimator_initialized; // true if estimator has been initialized after few seconds of the target being detected by sensor219uint32_t _estimator_init_ms; // system time in millisecond when EKF was init220uint32_t _last_backend_los_meas_ms; // system time target was last seen221uint32_t _last_valid_target_ms; // last time PrecLand library had a output of the landing target position222223PosVelEKF _ekf_x, _ekf_y; // Kalman Filter for x and y axis224uint32_t _outlier_reject_count; // mini-EKF's outlier counter (3 consecutive outliers lead to EKF accepting updates)225226Vector3f _target_pos_rel_meas_ned_m; // target's relative position as 3D vector227Vector3f _approach_vector_body; // unit vector in landing approach direction (in body frame)228229Vector3p _last_target_pos_rel_origin_ned_m; // stores the last known location of the target horizontally, and the height of the vehicle where it detected this target in meters NED230Vector3p _last_vehicle_pos_ned_m; // stores the position of the vehicle when landing target was last detected in m and NED231Vector2f _target_pos_rel_est_ne_m; // target's position relative to the IMU, not compensated for lag232Vector2f _target_vel_rel_est_ne_ms; // target's velocity relative to the IMU, not compensated for lag233234Vector2f _target_pos_rel_out_ne_m; // target's position relative to the camera, fed into position controller235Vector2f _target_vel_rel_out_ne_ms; // target's velocity relative to the CG, fed into position controller236Vector3f _last_veh_velocity_NED_ms; // AHRS velocity at last estimate237238TargetState _current_target_state; // Current status of the landing target239240// structure and buffer to hold a history of vehicle velocity241struct inertial_data_frame_s {242Matrix3f Tbn; // dcm rotation matrix to rotate body frame to north243Vector3f correctedVehicleDeltaVelocityNED;244Vector3f inertialNavVelocity;245bool inertialNavVelocityValid;246float dt;247uint64_t time_usec;248};249ObjectArray<inertial_data_frame_s> *_inertial_history;250struct inertial_data_frame_s *_inertial_data_delayed;251252// backend state253struct precland_state {254bool healthy;255} _backend_state;256AC_PrecLand_Backend *_backend; // pointers to backend precision landing driver257258// write out PREC message to log:259void Write_Precland();260uint32_t _last_log_ms; // last time we logged261262static AC_PrecLand *_singleton; //singleton263};264265namespace AP {266AC_PrecLand *ac_precland();267};268269#endif // AC_PRECLAND_ENABLED270271272