123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855 |
- #include "AP_SmartRTL.h"
- #include <AP_AHRS/AP_AHRS.h>
- #include <AP_Logger/AP_Logger.h>
- #include <GCS_MAVLink/GCS.h>
- extern const AP_HAL::HAL& hal;
- const AP_Param::GroupInfo AP_SmartRTL::var_info[] = {
-
-
-
-
-
-
- AP_GROUPINFO("ACCURACY", 0, AP_SmartRTL, _accuracy, SMARTRTL_ACCURACY_DEFAULT),
-
-
-
-
-
-
- AP_GROUPINFO("POINTS", 1, AP_SmartRTL, _points_max, SMARTRTL_POINTS_DEFAULT),
- AP_GROUPEND
- };
- AP_SmartRTL::AP_SmartRTL(bool example_mode) :
- _example_mode(example_mode)
- {
- AP_Param::setup_object_defaults(this, var_info);
- _simplify.bitmask.setall();
- }
- void AP_SmartRTL::init()
- {
-
- if (_path != nullptr) {
- return;
- }
-
- _points_max = constrain_int16(_points_max, 0, SMARTRTL_POINTS_MAX);
-
- if (_points_max == 0 || !is_positive(_accuracy)) {
- return;
- }
-
- _path = (Vector3f*)calloc(_points_max, sizeof(Vector3f));
- _prune.loops_max = _points_max * SMARTRTL_PRUNING_LOOP_BUFFER_LEN_MULT;
- _prune.loops = (prune_loop_t*)calloc(_prune.loops_max, sizeof(prune_loop_t));
- _simplify.stack_max = _points_max * SMARTRTL_SIMPLIFY_STACK_LEN_MULT;
- _simplify.stack = (simplify_start_finish_t*)calloc(_simplify.stack_max, sizeof(simplify_start_finish_t));
-
- if (_path == nullptr || _prune.loops == nullptr || _simplify.stack == nullptr) {
- log_action(SRTL_DEACTIVATED_INIT_FAILED);
- gcs().send_text(MAV_SEVERITY_WARNING, "SmartRTL deactivated: init failed");
- free(_path);
- free(_prune.loops);
- free(_simplify.stack);
- return;
- }
- _path_points_max = _points_max;
-
- if (!_example_mode){
-
- hal.scheduler->register_io_process(FUNCTOR_BIND_MEMBER(&AP_SmartRTL::run_background_cleanup, void));
- }
- }
- uint16_t AP_SmartRTL::get_num_points() const
- {
- return _path_points_count;
- }
- bool AP_SmartRTL::pop_point(Vector3f& point)
- {
-
- if (!_active) {
- return false;
- }
-
- if (!_path_sem.take_nonblocking()) {
- log_action(SRTL_POP_FAILED_NO_SEMAPHORE);
- return false;
- }
-
- if (_path_points_count == 0) {
- _path_sem.give();
- return false;
- }
-
- point = _path[--_path_points_count];
-
- _path_points_completed_limit = _path_points_count;
- _path_sem.give();
- return true;
- }
- void AP_SmartRTL::set_home(bool position_ok)
- {
- Vector3f current_pos;
- position_ok &= AP::ahrs().get_relative_position_NED_origin(current_pos);
- set_home(position_ok, current_pos);
- }
- void AP_SmartRTL::set_home(bool position_ok, const Vector3f& current_pos)
- {
- if (_path == nullptr) {
- return;
- }
-
- _path_points_count = 0;
-
-
- reset_simplification();
- reset_pruning();
-
- if (!position_ok) {
- return;
- }
-
- if (!add_point(current_pos)) {
- return;
- }
-
- _last_good_position_ms = AP_HAL::millis();
- _active = true;
- _home_saved = true;
- }
- void AP_SmartRTL::update(bool position_ok, bool save_position)
- {
-
- if (position_ok && !_home_saved) {
- set_home(true);
- }
- if (!_active || !save_position) {
- return;
- }
- Vector3f current_pos;
- position_ok &= AP::ahrs().get_relative_position_NED_origin(current_pos);
- update(position_ok, current_pos);
- }
- void AP_SmartRTL::update(bool position_ok, const Vector3f& current_pos)
- {
- if (!_active) {
- return;
- }
- if (position_ok) {
- const uint32_t now = AP_HAL::millis();
- _last_good_position_ms = now;
-
- if (add_point(current_pos)) {
- _last_position_save_ms = now;
- } else if (AP_HAL::millis() - _last_position_save_ms > SMARTRTL_TIMEOUT) {
-
- deactivate(SRTL_DEACTIVATED_PATH_FULL_TIMEOUT, "buffer full");
- }
- } else {
-
- if (AP_HAL::millis() - _last_good_position_ms > SMARTRTL_TIMEOUT) {
- deactivate(SRTL_DEACTIVATED_BAD_POSITION_TIMEOUT, "bad position");
- return;
- }
- }
- }
- bool AP_SmartRTL::request_thorough_cleanup(ThoroughCleanupType clean_type)
- {
-
- if (!_active) {
- return false;
- }
-
- if (_thorough_clean_request_ms == 0) {
- _thorough_clean_request_ms = AP_HAL::millis();
- if (clean_type != THOROUGH_CLEAN_DEFAULT) {
- _thorough_clean_type = clean_type;
- }
- return false;
- }
-
- if (_thorough_clean_complete_ms == _thorough_clean_request_ms) {
- _thorough_clean_request_ms = 0;
- return true;
- }
- return false;
- }
- void AP_SmartRTL::cancel_request_for_thorough_cleanup()
- {
- _thorough_clean_request_ms = 0;
- }
- bool AP_SmartRTL::add_point(const Vector3f& point)
- {
-
- if (!_path_sem.take_nonblocking()) {
- log_action(SRTL_ADD_FAILED_NO_SEMAPHORE, point);
- return false;
- }
-
- if (_path_points_count > 0) {
- const Vector3f& last_pos = _path[_path_points_count-1];
- if (last_pos.distance_squared(point) < sq(_accuracy.get())) {
- _path_sem.give();
- return true;
- }
- }
-
- if (_path_points_count >= _path_points_max) {
- _path_sem.give();
- log_action(SRTL_ADD_FAILED_PATH_FULL, point);
- return false;
- }
-
- _path[_path_points_count++] = point;
- log_action(SRTL_POINT_ADD, point);
- _path_sem.give();
- return true;
- }
- void AP_SmartRTL::run_background_cleanup()
- {
- if (!_active) {
- return;
- }
-
- if (!_path_sem.take_nonblocking()) {
- return;
- }
-
- const uint16_t path_points_count = _path_points_count;
- const uint16_t path_points_completed_limit = _path_points_completed_limit;
- _path_points_completed_limit = SMARTRTL_POINTS_MAX;
- _path_sem.give();
-
- if (_thorough_clean_request_ms > 0) {
-
- if (_thorough_clean_complete_ms != _thorough_clean_request_ms) {
- if (thorough_cleanup(path_points_count, _thorough_clean_type)) {
-
- _thorough_clean_complete_ms = _thorough_clean_request_ms;
- }
- }
-
-
- return;
- }
-
- _thorough_clean_complete_ms = 0;
-
- routine_cleanup(path_points_count, path_points_completed_limit);
- }
- void AP_SmartRTL::routine_cleanup(uint16_t path_points_count, uint16_t path_points_completed_limit)
- {
-
- if (!_simplify.complete) {
- detect_simplifications();
- return;
- }
-
- if (_simplify.removal_required) {
- remove_points_by_simplify_bitmask();
- return;
- }
-
- if (_prune.complete) {
- restart_pruning_if_new_points();
- }
-
- if (!_prune.complete) {
- detect_loops();
- return;
- }
-
- if (_simplify.path_points_completed > path_points_completed_limit) {
- _simplify.path_points_completed = path_points_completed_limit;
- }
- if (_prune.path_points_completed > path_points_completed_limit) {
- _prune.path_points_completed = path_points_completed_limit;
- }
-
- const uint16_t points_to_simplify = (path_points_count > _simplify.path_points_completed) ? (path_points_count - _simplify.path_points_completed) : 0 ;
- const bool low_on_space = (_path_points_max - path_points_count) <= SMARTRTL_CLEANUP_START_MARGIN;
-
- if ((points_to_simplify >= SMARTRTL_CLEANUP_POINT_TRIGGER) || (low_on_space && (points_to_simplify >= SMARTRTL_CLEANUP_POINT_MIN))) {
- restart_simplification(path_points_count);
- return;
- }
-
- if (low_on_space) {
-
- remove_points_by_loops(SMARTRTL_CLEANUP_POINT_MIN);
- }
- }
- bool AP_SmartRTL::thorough_cleanup(uint16_t path_points_count, ThoroughCleanupType clean_type)
- {
- if (clean_type != THOROUGH_CLEAN_PRUNE_ONLY) {
-
- if (_simplify.complete) {
- restart_simplify_if_new_points(path_points_count);
- }
-
- if (!_simplify.complete) {
- detect_simplifications();
- return false;
- }
-
- if (_simplify.removal_required) {
- remove_points_by_simplify_bitmask();
- return false;
- }
- }
- if (clean_type != THOROUGH_CLEAN_SIMPLIFY_ONLY) {
-
- if (_prune.complete) {
- restart_pruning_if_new_points();
- }
-
- if (!_prune.complete) {
- detect_loops();
- return false;
- }
-
- if (!remove_points_by_loops(SMARTRTL_POINTS_MAX)) {
- return false;
- }
- }
- return true;
- }
- void AP_SmartRTL::detect_simplifications()
- {
-
- if (_simplify.path_points_count < 3) {
- _simplify.complete = true;
- return;
- }
-
- if (_simplify.stack_count == 0) {
-
-
-
- _simplify.stack[0].start = (_simplify.path_points_completed > 0) ? _simplify.path_points_completed - 1 : 0;
- _simplify.stack[0].finish = _simplify.path_points_count-1;
- _simplify.stack_count++;
- }
- const uint32_t start_time_us = AP_HAL::micros();
- while (_simplify.stack_count > 0) {
-
- if (AP_HAL::micros() - start_time_us > SMARTRTL_SIMPLIFY_TIME_US) {
- return;
- }
-
- const simplify_start_finish_t tmp = _simplify.stack[--_simplify.stack_count];
- const uint16_t start_index = tmp.start;
- const uint16_t end_index = tmp.finish;
-
- float max_dist = 0.0f;
- uint16_t farthest_point_index = start_index;
- for (uint16_t i = start_index + 1; i < end_index; i++) {
-
- if (_simplify.bitmask.get(i)) {
- const float dist = _path[i].distance_to_segment(_path[start_index], _path[end_index]);
- if (dist > max_dist) {
- farthest_point_index = i;
- max_dist = dist;
- }
- }
- }
-
-
- if (max_dist > SMARTRTL_SIMPLIFY_EPSILON) {
-
- if (_simplify.stack_count >= _simplify.stack_max) {
- _simplify.complete = true;
- return;
- }
- _simplify.stack[_simplify.stack_count++] = simplify_start_finish_t {start_index, farthest_point_index};
- _simplify.stack[_simplify.stack_count++] = simplify_start_finish_t {farthest_point_index, end_index};
- } else {
-
- for (uint16_t i = start_index + 1; i < end_index; i++) {
- _simplify.bitmask.clear(i);
- _simplify.removal_required = true;
- }
- }
- }
- _simplify.path_points_completed = _simplify.path_points_count;
- _simplify.complete = true;
- }
- void AP_SmartRTL::detect_loops()
- {
-
- if (_prune.path_points_count < 4) {
- _prune.complete = true;
- return;
- }
-
- const uint32_t start_time_us = AP_HAL::micros();
-
- while (AP_HAL::micros() - start_time_us < SMARTRTL_PRUNING_LOOP_TIME_US) {
-
- _prune.j++;
- if (_prune.j > _prune.i - 2) {
-
- _prune.j = 1;
-
- _prune.i--;
-
- if (_prune.i < 4 || _prune.i < _prune.path_points_completed) {
- _prune.complete = true;
- _prune.path_points_completed = _prune.path_points_count;
- return;
- }
- }
-
- dist_point dp = segment_segment_dist(_path[_prune.i], _path[_prune.i-1], _path[_prune.j-1], _path[_prune.j]);
- if (dp.distance < SMARTRTL_PRUNING_DELTA) {
-
- if (!add_loop(_prune.j, _prune.i-1, dp.midpoint)) {
-
- _prune.complete = true;
- }
-
- _prune.j = _prune.i;
- }
- }
- }
- void AP_SmartRTL::restart_simplify_if_new_points(uint16_t path_points_count)
- {
-
- if (_simplify.path_points_count != path_points_count) {
- restart_simplification(path_points_count);
- }
- }
- void AP_SmartRTL::restart_pruning_if_new_points()
- {
-
- if (_prune.path_points_count != _simplify.path_points_completed) {
- restart_pruning(_simplify.path_points_completed);
- }
- }
- void AP_SmartRTL::restart_simplification(uint16_t path_points_count)
- {
- _simplify.complete = false;
- _simplify.removal_required = false;
- _simplify.bitmask.setall();
- _simplify.stack_count = 0;
- _simplify.path_points_count = path_points_count;
- }
- void AP_SmartRTL::reset_simplification()
- {
- restart_simplification(0);
- _simplify.path_points_completed = 0;
- }
- void AP_SmartRTL::restart_pruning(uint16_t path_points_count)
- {
- _prune.complete = false;
- _prune.i = (path_points_count > 0) ? path_points_count - 1 : 0;
- _prune.j = 0;
- _prune.path_points_count = path_points_count;
- }
- void AP_SmartRTL::reset_pruning()
- {
- restart_pruning(0);
- _prune.loops_count = 0;
- _prune.path_points_completed = 0;
- }
- void AP_SmartRTL::remove_points_by_simplify_bitmask()
- {
-
- if (!_path_sem.take_nonblocking()) {
- return;
- }
- uint16_t dest = 1;
- uint16_t removed = 0;
- for (uint16_t src = 1; src < _path_points_count; src++) {
- if (!_simplify.bitmask.get(src)) {
- log_action(SRTL_POINT_SIMPLIFY, _path[src]);
- removed++;
- } else {
- _path[dest] = _path[src];
- dest++;
- }
- }
-
- if (_path_points_count > removed && _simplify.path_points_count > removed) {
- _path_points_count -= removed;
- _simplify.path_points_count -= removed;
- _simplify.path_points_completed = _simplify.path_points_count;
- } else {
-
- deactivate(SRTL_DEACTIVATED_PROGRAM_ERROR, "program error");
- }
- _path_sem.give();
-
- _simplify.bitmask.setall();
- _simplify.removal_required = false;
- }
- bool AP_SmartRTL::remove_points_by_loops(uint16_t num_points_to_remove)
- {
-
- if (_prune.loops_count == 0) {
- return true;
- }
-
- if (!_path_sem.take_nonblocking()) {
- return false;
- }
- uint16_t removed_points = 0;
- uint16_t i = _prune.loops_count;
- while ((i > 0) && (removed_points < num_points_to_remove)) {
- i--;
- prune_loop_t loop = _prune.loops[i];
-
- _path[loop.start_index] = loop.midpoint;
-
- uint16_t loop_num_points_to_remove = loop.end_index - loop.start_index;
- for (uint16_t dest = loop.start_index + 1; dest < _path_points_count - loop_num_points_to_remove; dest++) {
- log_action(SRTL_POINT_PRUNE, _path[dest]);
- _path[dest] = _path[dest + loop_num_points_to_remove];
- }
- if (_path_points_count > loop_num_points_to_remove) {
- _path_points_count -= loop_num_points_to_remove;
- removed_points += loop_num_points_to_remove;
- } else {
-
- deactivate(SRTL_DEACTIVATED_PROGRAM_ERROR, "program error");
- _path_sem.give();
-
- return true;
- }
-
-
- for (uint16_t loop_cnt = 0; loop_cnt < i; loop_cnt++) {
- if (_prune.loops[loop_cnt].start_index >= loop.end_index) {
- _prune.loops[loop_cnt].start_index -= loop_num_points_to_remove;
- }
- if (_prune.loops[loop_cnt].end_index >= loop.end_index) {
- _prune.loops[loop_cnt].end_index -= loop_num_points_to_remove;
- }
- }
-
- _prune.loops_count--;
- }
- _path_sem.give();
- return true;
- }
- bool AP_SmartRTL::add_loop(uint16_t start_index, uint16_t end_index, const Vector3f& midpoint)
- {
-
- if (_prune.loops_count >= _prune.loops_max) {
- return false;
- }
-
- if (end_index <= start_index) {
- return false;
- }
-
- prune_loop_t new_loop = {start_index, end_index, midpoint, 0.0f};
- new_loop.length_squared = midpoint.distance_squared(_path[start_index]) + midpoint.distance_squared(_path[end_index]);
- for (uint16_t i = start_index; i < end_index; i++) {
- new_loop.length_squared += _path[i].distance_squared(_path[i+1]);
- }
-
- bool overlapping_loops = false;
- float overlapping_loop_length = 0.0f;
- for (uint16_t loop_idx = 0; loop_idx < _prune.loops_count; loop_idx++) {
- if (loops_overlap(_prune.loops[loop_idx], new_loop)) {
- overlapping_loops = true;
- overlapping_loop_length += _prune.loops[loop_idx].length_squared;
- }
- }
-
- if (overlapping_loops) {
-
- if (overlapping_loop_length > new_loop.length_squared) {
- return true;
- }
-
- uint16_t dest_idx = 0;
- uint16_t removed = 0;
- for (uint16_t src_idx = 0; src_idx < _prune.loops_count; src_idx++) {
- if (loops_overlap(_prune.loops[src_idx], new_loop)) {
- removed++;
- } else {
- _prune.loops[dest_idx] = _prune.loops[src_idx];
- dest_idx++;
- }
- }
- _prune.loops_count -= removed;
- }
-
- _prune.loops[_prune.loops_count] = new_loop;
- _prune.loops_count++;
- return true;
- }
- AP_SmartRTL::dist_point AP_SmartRTL::segment_segment_dist(const Vector3f &p1, const Vector3f &p2, const Vector3f &p3, const Vector3f &p4)
- {
- const Vector3f line1 = p2-p1;
- const Vector3f line2 = p4-p3;
- const Vector3f line_start_diff = p1-p3;
-
- const float a = line1*line1;
- const float b = line1*line2;
- const float c = line2*line2;
- const float d = line1*line_start_diff;
- const float e = line2*line_start_diff;
-
- float t1 = 0.0f;
- float t2 = 0.0f;
-
-
- if (is_zero((a*c)-(b*b))) {
- return {FLT_MAX, Vector3f(0.0f, 0.0f, 0.0f)};
- }
- t1 = (b*e-c*d)/(a*c-b*b);
- t2 = (a*e-b*d)/(a*c-b*b);
-
- t1 = constrain_float(t1, 0.0f, 1.0f);
- t2 = constrain_float(t2, 0.0f, 1.0f);
-
- const Vector3f dP = line_start_diff+line1*t1-line2*t2;
- const Vector3f midpoint = (p1+line1*t1 + p3+line2*t2)/2.0f;
- return {dP.length(), midpoint};
- }
- void AP_SmartRTL::deactivate(SRTL_Actions action, const char *reason)
- {
- _active = false;
- log_action(action);
- gcs().send_text(MAV_SEVERITY_WARNING, "SmartRTL deactivated: %s", reason);
- }
- void AP_SmartRTL::log_action(SRTL_Actions action, const Vector3f &point)
- {
- if (!_example_mode) {
- AP::logger().Write_SRTL(_active, _path_points_count, _path_points_max, action, point);
- }
- }
- bool AP_SmartRTL::loops_overlap(const prune_loop_t &loop1, const prune_loop_t &loop2) const
- {
-
- if (loop1.start_index >= loop2.start_index && loop1.end_index <= loop2.end_index) {
- return true;
- }
-
- if (loop2.start_index >= loop1.start_index && loop2.end_index <= loop1.end_index) {
- return true;
- }
-
- const bool loop1_start_in_loop2 = (loop1.start_index >= loop2.start_index) && (loop1.start_index <= loop2.end_index);
- const bool loop1_end_in_loop2 = (loop1.end_index >= loop2.start_index) && (loop1.end_index <= loop2.end_index);
- if (loop1_start_in_loop2 != loop1_end_in_loop2) {
- return true;
- }
-
- return false;
- }
|