Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/libraries/AC_PrecLand/AC_PrecLand.h
Views: 1798
#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_Companion;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_Companion;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 cm's to the target before descending towards target58float get_max_xy_error_before_descending_cm() const { return _xy_max_dist_desc * 100.0f; }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_cm, bool rangefinder_alt_valid);6869// returns target position relative to the EKF origin70bool get_target_position_cm(Vector2f& ret);7172// returns target relative position as 3D vector73void get_target_position_measurement_cm(Vector3f& ret);7475// returns target position relative to vehicle76bool get_target_position_relative_cm(Vector2f& ret);7778// returns target velocity relative to vehicle79bool get_target_velocity_relative_cms(Vector2f& ret);8081// get the absolute velocity of the vehicle82void get_target_velocity_cms(const Vector2f& vehicle_velocity_cms, Vector2f& target_vel_cms);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(Vector3f &pos) const { pos = _last_target_pos_rel_origin_NED; }106107// return the last known postion of the vehicle when the target was detected in Earth Frame NED meters.108void get_last_vehicle_pos_when_target_detected(Vector3f &pos) const { pos = _last_vehicle_pos_NED; }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_sec; }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_COMPANION_ENABLED144COMPANION = 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// check the status of the target165void check_target_status(float rangefinder_alt_m, bool rangefinder_alt_valid);166167// Check if the landing target is supposed to be in sight based on the height of the vehicle from the ground168// This needs a valid rangefinder to work, if the min/max parameters are non zero169bool check_if_sensor_in_range(float rangefinder_alt_m, bool rangefinder_alt_valid) const;170171// check if EKF got the time to initialize when the landing target was first detected172// Expects sensor to update within EKF_INIT_SENSOR_MIN_UPDATE_MS milliseconds till EKF_INIT_TIME_MS milliseconds have passed173// after this period landing target estimates can be used by vehicle174void check_ekf_init_timeout();175176// run target position estimator177void run_estimator(float rangefinder_alt_m, bool rangefinder_alt_valid);178179// If a new measurement was retrieved, sets _target_pos_rel_meas_NED and returns true180bool construct_pos_meas_using_rangefinder(float rangefinder_alt_m, bool rangefinder_alt_valid);181182// get vehicle body frame 3D vector from vehicle to target. returns true on success, false on failure183bool retrieve_los_meas(Vector3f& target_vec_unit_body);184185// calculate target's position and velocity relative to the vehicle (used as input to position controller)186// results are stored in_target_pos_rel_out_NE, _target_vel_rel_out_NE187void run_output_prediction();188189// parameters190AP_Int8 _enabled; // enabled/disabled191AP_Enum<Type> _type; // precision landing sensor type192AP_Int8 _bus; // which sensor bus193AP_Enum<EstimatorType> _estimator_type; // precision landing estimator type194AP_Float _lag; // sensor lag in seconds195AP_Float _yaw_align; // Yaw angle from body x-axis to sensor x-axis.196AP_Float _land_ofs_cm_x; // Desired landing position of the camera forward of the target in vehicle body frame197AP_Float _land_ofs_cm_y; // Desired landing position of the camera right of the target in vehicle body frame198AP_Float _accel_noise; // accelerometer process noise199AP_Vector3f _cam_offset; // Position of the camera relative to the CG200AP_Float _xy_max_dist_desc; // Vehicle doing prec land will only descent vertically when horizontal error (in m) is below this limit201AP_Int8 _strict; // PrecLand strictness202AP_Int8 _retry_max; // PrecLand Maximum number of retires to a failed landing203AP_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.204AP_Int8 _retry_behave; // Action to do when trying a landing retry205AP_Float _sensor_min_alt; // PrecLand minimum height required for detecting target206AP_Float _sensor_max_alt; // PrecLand maximum height the sensor can detect target207AP_Int16 _options; // Bitmask for extra options208AP_Enum<Rotation> _orient; // Orientation of camera/sensor209210uint32_t _last_update_ms; // system time in millisecond when update was last called211bool _target_acquired; // true if target has been seen recently after estimator is initialized212bool _estimator_initialized; // true if estimator has been initialized after few seconds of the target being detected by sensor213uint32_t _estimator_init_ms; // system time in millisecond when EKF was init214uint32_t _last_backend_los_meas_ms; // system time target was last seen215uint32_t _last_valid_target_ms; // last time PrecLand library had a output of the landing target position216217PosVelEKF _ekf_x, _ekf_y; // Kalman Filter for x and y axis218uint32_t _outlier_reject_count; // mini-EKF's outlier counter (3 consecutive outliers lead to EKF accepting updates)219220Vector3f _target_pos_rel_meas_NED; // target's relative position as 3D vector221Vector3f _approach_vector_body; // unit vector in landing approach direction (in body frame)222223Vector3f _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 NED224Vector3f _last_vehicle_pos_NED; // stores the position of the vehicle when landing target was last detected in m and NED225Vector2f _target_pos_rel_est_NE; // target's position relative to the IMU, not compensated for lag226Vector2f _target_vel_rel_est_NE; // target's velocity relative to the IMU, not compensated for lag227228Vector2f _target_pos_rel_out_NE; // target's position relative to the camera, fed into position controller229Vector2f _target_vel_rel_out_NE; // target's velocity relative to the CG, fed into position controller230Vector3f _last_veh_velocity_NED_ms; // AHRS velocity at last estimate231232TargetState _current_target_state; // Current status of the landing target233234// structure and buffer to hold a history of vehicle velocity235struct inertial_data_frame_s {236Matrix3f Tbn; // dcm rotation matrix to rotate body frame to north237Vector3f correctedVehicleDeltaVelocityNED;238Vector3f inertialNavVelocity;239bool inertialNavVelocityValid;240float dt;241uint64_t time_usec;242};243ObjectArray<inertial_data_frame_s> *_inertial_history;244struct inertial_data_frame_s *_inertial_data_delayed;245246// backend state247struct precland_state {248bool healthy;249} _backend_state;250AC_PrecLand_Backend *_backend; // pointers to backend precision landing driver251252// write out PREC message to log:253void Write_Precland();254uint32_t _last_log_ms; // last time we logged255256static AC_PrecLand *_singleton; //singleton257};258259namespace AP {260AC_PrecLand *ac_precland();261};262263#endif // AC_PRECLAND_ENABLED264265266