123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- #pragma once
- #include <AP_ADSB/AP_ADSB.h>
- #define AP_AVOIDANCE_RECOVERY_REMAIN_IN_AVOID_ADSB 0
- #define AP_AVOIDANCE_RECOVERY_RESUME_PREVIOUS_FLIGHTMODE 1
- #define AP_AVOIDANCE_RECOVERY_RTL 2
- #define AP_AVOIDANCE_RECOVERY_RESUME_IF_AUTO_ELSE_LOITER 3
- #define AP_AVOIDANCE_STATE_RECOVERY_TIME_MS 2000
- #define AP_AVOIDANCE_ESCAPE_TIME_SEC 2
- class AP_Avoidance {
- public:
-
- AP_Avoidance(class AP_ADSB &adsb);
-
- class Obstacle {
- public:
- MAV_COLLISION_SRC src;
- uint32_t src_id;
- uint32_t timestamp_ms;
- Location _location;
- Vector3f _velocity;
-
- MAV_COLLISION_THREAT_LEVEL threat_level;
- float closest_approach_xy;
- float closest_approach_z;
- float time_to_closest_approach;
- float distance_to_closest_approach;
- uint32_t last_gcs_report_time;
- };
-
- void add_obstacle(uint32_t obstacle_timestamp_ms,
- const MAV_COLLISION_SRC src,
- uint32_t src_id,
- const Location &loc,
- const Vector3f &vel_ned);
- void add_obstacle(uint32_t obstacle_timestamp_ms,
- const MAV_COLLISION_SRC src,
- uint32_t src_id,
- const Location &loc,
- float cog,
- float hspeed,
- float vspeed);
-
- void update();
-
- void enable() { _enabled = true; };
- void disable() { _enabled = false; };
-
- MAV_COLLISION_THREAT_LEVEL current_threat_level() const;
-
- void handle_msg(const mavlink_message_t &msg);
-
- static const struct AP_Param::GroupInfo var_info[];
- protected:
-
- void handle_avoidance_local(AP_Avoidance::Obstacle *threat);
-
-
- virtual MAV_COLLISION_ACTION handle_avoidance(const AP_Avoidance::Obstacle *obstacle, MAV_COLLISION_ACTION requested_action) = 0;
-
-
- virtual void handle_recovery(uint8_t recovery_action) = 0;
- uint32_t _last_state_change_ms = 0;
- MAV_COLLISION_THREAT_LEVEL _threat_level = MAV_COLLISION_THREAT_LEVEL_NONE;
-
-
- static const uint8_t _gcs_cleared_messages_duration = 5;
- uint32_t _gcs_cleared_messages_first_sent;
- void handle_threat_gcs_notify(AP_Avoidance::Obstacle *threat);
- AP_Avoidance::Obstacle *most_serious_threat();
-
-
- MAV_COLLISION_ACTION mav_avoidance_action() { return _latest_action; }
-
- bool get_destination_perpendicular(const AP_Avoidance::Obstacle *obstacle, Vector3f &newdest_neu, const float wp_speed_xy, const float wp_speed_z, const uint8_t _minimum_avoid_height);
-
- bool get_vector_perpendicular(const AP_Avoidance::Obstacle *obstacle, Vector3f &vec_neu);
-
-
- static Vector3f perpendicular_xyz(const Location &p1, const Vector3f &v1, const Location &p2);
- static Vector2f perpendicular_xy(const Location &p1, const Vector3f &v1, const Location &p2);
- private:
- void send_collision_all(const AP_Avoidance::Obstacle &threat, MAV_COLLISION_ACTION behaviour) const;
-
- const uint32_t MAX_OBSTACLE_AGE_MS = 5000;
- const static uint8_t _gcs_notify_interval = 1;
-
-
- const uint8_t _low_velocity_threshold = 1;
-
- bool check_startup();
-
- void init();
-
- void deinit();
-
- uint32_t src_id_for_adsb_vehicle(const AP_ADSB::adsb_vehicle_t &vehicle) const;
- void check_for_threats();
- void update_threat_level(const Location &my_loc,
- const Vector3f &my_vel,
- AP_Avoidance::Obstacle &obstacle);
-
- void get_adsb_samples();
-
-
- bool obstacle_is_more_serious_threat(const AP_Avoidance::Obstacle &obstacle) const;
-
- AP_Avoidance::Obstacle *_obstacles;
- uint8_t _obstacles_allocated;
- uint8_t _obstacle_count;
- int8_t _current_most_serious_threat;
- MAV_COLLISION_ACTION _latest_action = MAV_COLLISION_ACTION_NONE;
-
- class AP_ADSB &_adsb;
-
- AP_Int8 _enabled;
- AP_Int8 _obstacles_max;
- AP_Int8 _fail_action;
- AP_Int8 _fail_recovery;
- AP_Int8 _fail_time_horizon;
- AP_Int16 _fail_distance_xy;
- AP_Int16 _fail_distance_z;
- AP_Int16 _fail_altitude_minimum;
- AP_Int8 _warn_action;
- AP_Int8 _warn_time_horizon;
- AP_Float _warn_distance_xy;
- AP_Float _warn_distance_z;
-
- HAL_Semaphore_Recursive _rsem;
- };
- float closest_approach_xy(const Location &my_loc,
- const Vector3f &my_vel,
- const Location &obstacle_loc,
- const Vector3f &obstacle_vel,
- uint8_t time_horizon);
- float closest_approach_z(const Location &my_loc,
- const Vector3f &my_vel,
- const Location &obstacle_loc,
- const Vector3f &obstacle_vel,
- uint8_t time_horizon);
|