initialise safe rtl including setting up background processes
| 92 | |
| 93 | // initialise safe rtl including setting up background processes |
| 94 | void AP_SmartRTL::init() |
| 95 | { |
| 96 | // protect against repeated call to init |
| 97 | if (_path != nullptr) { |
| 98 | return; |
| 99 | } |
| 100 | |
| 101 | // constrain the path length, in case the user decided to make the path unreasonably long. |
| 102 | _points_max.set(constrain_int16(_points_max, 0, SMARTRTL_POINTS_MAX)); |
| 103 | |
| 104 | // check if user has disabled SmartRTL |
| 105 | if (_points_max == 0 || !is_positive(_accuracy)) { |
| 106 | return; |
| 107 | } |
| 108 | |
| 109 | // allocate arrays |
| 110 | _path = (Vector3f*)calloc(_points_max, sizeof(Vector3f)); |
| 111 | |
| 112 | _prune.loops_max = _points_max * SMARTRTL_PRUNING_LOOP_BUFFER_LEN_MULT; |
| 113 | _prune.loops = (prune_loop_t*)calloc(_prune.loops_max, sizeof(prune_loop_t)); |
| 114 | |
| 115 | _simplify.stack_max = _points_max * SMARTRTL_SIMPLIFY_STACK_LEN_MULT; |
| 116 | _simplify.stack = (simplify_start_finish_t*)calloc(_simplify.stack_max, sizeof(simplify_start_finish_t)); |
| 117 | |
| 118 | // check if memory allocation failed |
| 119 | if (_path == nullptr || _prune.loops == nullptr || _simplify.stack == nullptr) { |
| 120 | log_action(Action::DEACTIVATED_INIT_FAILED); |
| 121 | GCS_SEND_TEXT(MAV_SEVERITY_WARNING, "SmartRTL deactivated: init failed"); |
| 122 | free(_path); |
| 123 | free(_prune.loops); |
| 124 | free(_simplify.stack); |
| 125 | return; |
| 126 | } |
| 127 | |
| 128 | _path_points_max = _points_max; |
| 129 | |
| 130 | // when running the example sketch, we want the cleanup tasks to run when we tell them to, not in the background (so that they can be timed.) |
| 131 | if (!_example_mode){ |
| 132 | // register background cleanup to run in IO thread |
| 133 | hal.scheduler->register_io_process(FUNCTOR_BIND_MEMBER(&AP_SmartRTL::run_background_cleanup, void)); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | // returns number of points on the path |
| 138 | uint16_t AP_SmartRTL::get_num_points() const |