123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- #pragma once
- #include <AP_HAL/AP_HAL.h>
- #include <AP_Common/Bitmask.h>
- #include <AP_Math/AP_Math.h>
- #define SMARTRTL_ACCURACY_DEFAULT 2.0f
- #define SMARTRTL_POINTS_DEFAULT 300
- #define SMARTRTL_POINTS_MAX 500
- #define SMARTRTL_TIMEOUT 15000
- #define SMARTRTL_CLEANUP_POINT_TRIGGER 50
- #define SMARTRTL_CLEANUP_START_MARGIN 10
- #define SMARTRTL_CLEANUP_POINT_MIN 10
- #define SMARTRTL_SIMPLIFY_EPSILON (_accuracy * 0.5f)
- #define SMARTRTL_SIMPLIFY_STACK_LEN_MULT (2.0f/3.0f)+1
-
-
- #define SMARTRTL_SIMPLIFY_TIME_US 200
- #define SMARTRTL_PRUNING_DELTA (_accuracy * 0.99)
- #define SMARTRTL_PRUNING_LOOP_BUFFER_LEN_MULT 0.25f
- #define SMARTRTL_PRUNING_LOOP_TIME_US 200
- class AP_SmartRTL {
- public:
-
- AP_SmartRTL(bool example_mode = false);
-
- void init();
-
- bool is_active() const { return _active; }
-
- uint16_t get_num_points() const;
-
- const Vector3f& get_point(uint16_t index) const { return _path[index]; }
-
- bool pop_point(Vector3f& point);
-
-
-
- void set_home(bool position_ok);
- void set_home(bool position_ok, const Vector3f& current_pos);
-
-
- void update(bool position_ok, bool save_position);
- void update(bool position_ok, const Vector3f& current_pos);
-
- enum ThoroughCleanupType {
- THOROUGH_CLEAN_DEFAULT = 0,
- THOROUGH_CLEAN_ALL,
- THOROUGH_CLEAN_SIMPLIFY_ONLY,
- THOROUGH_CLEAN_PRUNE_ONLY,
- };
-
-
-
-
- bool request_thorough_cleanup(ThoroughCleanupType clean_type = THOROUGH_CLEAN_DEFAULT);
-
- void cancel_request_for_thorough_cleanup();
-
- void run_background_cleanup();
-
- static const struct AP_Param::GroupInfo var_info[];
- private:
-
- enum SRTL_Actions {
- SRTL_POINT_ADD,
- SRTL_POINT_PRUNE,
- SRTL_POINT_SIMPLIFY,
- SRTL_ADD_FAILED_NO_SEMAPHORE,
- SRTL_ADD_FAILED_PATH_FULL,
- SRTL_POP_FAILED_NO_SEMAPHORE,
- SRTL_DEACTIVATED_INIT_FAILED,
- SRTL_DEACTIVATED_BAD_POSITION,
- SRTL_DEACTIVATED_BAD_POSITION_TIMEOUT,
- SRTL_DEACTIVATED_PATH_FULL_TIMEOUT,
- SRTL_DEACTIVATED_PROGRAM_ERROR,
- };
-
- bool add_point(const Vector3f& point);
-
- void routine_cleanup(uint16_t path_points_count, uint16_t path_points_complete_limit);
-
-
- bool thorough_cleanup(uint16_t path_points_count, ThoroughCleanupType clean_type);
-
-
- void detect_simplifications();
- void detect_loops();
-
-
- void restart_simplify_if_new_points(uint16_t path_points_count);
-
- void restart_pruning_if_new_points();
-
-
-
- void restart_simplification(uint16_t path_points_count);
-
- void reset_simplification();
-
-
-
- void restart_pruning(uint16_t path_points_count);
-
- void reset_pruning();
-
- void remove_points_by_simplify_bitmask();
-
-
-
- bool remove_points_by_loops(uint16_t num_points_to_remove);
-
-
-
-
- bool add_loop(uint16_t start_index, uint16_t end_index, const Vector3f& midpoint);
-
- typedef struct {
- float distance;
- Vector3f midpoint;
- } dist_point;
-
- static dist_point segment_segment_dist(const Vector3f& p1, const Vector3f& p2, const Vector3f& p3, const Vector3f& p4);
-
- void deactivate(SRTL_Actions action, const char *reason);
-
- void log_action(SRTL_Actions action, const Vector3f &point = Vector3f());
-
- AP_Float _accuracy;
- AP_Int16 _points_max;
-
- bool _active;
- bool _example_mode;
- bool _home_saved;
- uint32_t _last_good_position_ms;
- uint32_t _last_position_save_ms;
- uint32_t _thorough_clean_request_ms;
- uint32_t _thorough_clean_complete_ms;
- ThoroughCleanupType _thorough_clean_type;
-
- Vector3f* _path;
- uint16_t _path_points_max;
- uint16_t _path_points_count;
- uint16_t _path_points_completed_limit;
- HAL_Semaphore _path_sem;
-
-
- typedef struct {
- uint16_t start;
- uint16_t finish;
- } simplify_start_finish_t;
- struct {
- bool complete;
- bool removal_required;
- uint16_t path_points_count;
- uint16_t path_points_completed = SMARTRTL_POINTS_MAX;
- simplify_start_finish_t* stack;
- uint16_t stack_max;
- uint16_t stack_count;
- Bitmask<SMARTRTL_POINTS_MAX> bitmask;
- } _simplify;
-
- typedef struct {
- uint16_t start_index;
- uint16_t end_index;
- Vector3f midpoint;
- float length_squared;
- } prune_loop_t;
- struct {
- bool complete;
- uint16_t path_points_count;
- uint16_t path_points_completed;
- uint16_t i;
- uint16_t j;
- prune_loop_t* loops;
- uint16_t loops_max;
- uint16_t loops_count;
- } _prune;
-
- bool loops_overlap(const prune_loop_t& loop1, const prune_loop_t& loop2) const;
- };
|