123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662 |
- #include "AC_Avoid.h"
- #include <AP_AHRS/AP_AHRS.h> // AHRS library
- #include <AC_Fence/AC_Fence.h> // Failsafe fence library
- #include <AP_Proximity/AP_Proximity.h>
- #include <AP_Beacon/AP_Beacon.h>
- #if APM_BUILD_TYPE(APM_BUILD_APMrover2)
- # define AP_AVOID_BEHAVE_DEFAULT AC_Avoid::BehaviourType::BEHAVIOR_STOP
- #else
- # define AP_AVOID_BEHAVE_DEFAULT AC_Avoid::BehaviourType::BEHAVIOR_SLIDE
- #endif
- const AP_Param::GroupInfo AC_Avoid::var_info[] = {
-
-
-
-
-
-
- AP_GROUPINFO("ENABLE", 1, AC_Avoid, _enabled, AC_AVOID_DEFAULT),
-
-
-
-
-
-
- AP_GROUPINFO("ANGLE_MAX", 2, AC_Avoid, _angle_max, 1000),
-
-
-
-
-
-
- AP_GROUPINFO("DIST_MAX", 3, AC_Avoid, _dist_max, AC_AVOID_NONGPS_DIST_MAX_DEFAULT),
-
-
-
-
-
-
- AP_GROUPINFO("MARGIN", 4, AC_Avoid, _margin, 2.0f),
-
-
-
-
-
- AP_GROUPINFO("BEHAVE", 5, AC_Avoid, _behavior, AP_AVOID_BEHAVE_DEFAULT),
- AP_GROUPEND
- };
- AC_Avoid::AC_Avoid()
- {
- _singleton = this;
- AP_Param::setup_object_defaults(this, var_info);
- }
- void AC_Avoid::adjust_velocity(float kP, float accel_cmss, Vector2f &desired_vel_cms, float dt)
- {
-
- if (_enabled == AC_AVOID_DISABLED) {
- return;
- }
-
- const float accel_cmss_limited = MIN(accel_cmss, AC_AVOID_ACCEL_CMSS_MAX);
- if ((_enabled & AC_AVOID_STOP_AT_FENCE) > 0) {
- adjust_velocity_circle_fence(kP, accel_cmss_limited, desired_vel_cms, dt);
- adjust_velocity_polygon_fence(kP, accel_cmss_limited, desired_vel_cms, dt);
- }
- if ((_enabled & AC_AVOID_STOP_AT_BEACON_FENCE) > 0) {
- adjust_velocity_beacon_fence(kP, accel_cmss_limited, desired_vel_cms, dt);
- }
- if ((_enabled & AC_AVOID_USE_PROXIMITY_SENSOR) > 0 && _proximity_enabled) {
- adjust_velocity_proximity(kP, accel_cmss_limited, desired_vel_cms, dt);
- }
- }
- void AC_Avoid::adjust_velocity(float kP, float accel_cmss, Vector3f &desired_vel_cms, float dt)
- {
- Vector2f des_vel_xy(desired_vel_cms.x, desired_vel_cms.y);
- adjust_velocity(kP, accel_cmss, des_vel_xy, dt);
- desired_vel_cms.x = des_vel_xy.x;
- desired_vel_cms.y = des_vel_xy.y;
- }
- void AC_Avoid::adjust_speed(float kP, float accel, float heading, float &speed, float dt)
- {
-
- Vector2f vel_xy;
- vel_xy.x = cosf(heading) * speed * 100.0f;
- vel_xy.y = sinf(heading) * speed * 100.0f;
- adjust_velocity(kP, accel * 100.0f, vel_xy, dt);
-
- if (is_negative(speed)) {
- speed = -vel_xy.length() * 0.01f;
- } else {
- speed = vel_xy.length() * 0.01f;
- }
- }
- void AC_Avoid::adjust_velocity_z(float kP, float accel_cmss, float& climb_rate_cms, float dt)
- {
-
- if (_enabled == AC_AVOID_DISABLED) {
- return;
- }
-
- if (climb_rate_cms <= 0.0f) {
- return;
- }
-
- const float accel_cmss_limited = MIN(accel_cmss, AC_AVOID_ACCEL_CMSS_MAX);
- bool limit_alt = false;
- float alt_diff = 0.0f;
- const AP_AHRS &_ahrs = AP::ahrs();
-
- AC_Fence *fence = AP::fence();
- if ((_enabled & AC_AVOID_STOP_AT_FENCE) > 0 && fence && (fence->get_enabled_fences() & AC_FENCE_TYPE_ALT_MAX) > 0) {
-
- float veh_alt;
- _ahrs.get_relative_position_D_home(veh_alt);
-
- alt_diff = fence->get_safe_alt_max() + veh_alt;
- limit_alt = true;
- }
-
-
- float alt_limit;
- float curr_alt;
- if (_ahrs.get_hgt_ctrl_limit(alt_limit) &&
- _ahrs.get_relative_position_D_origin(curr_alt)) {
-
- const float ctrl_alt_diff = alt_limit + curr_alt;
- if (!limit_alt || ctrl_alt_diff < alt_diff) {
- alt_diff = ctrl_alt_diff;
- limit_alt = true;
- }
- }
-
- float proximity_alt_diff;
- AP_Proximity *proximity = AP::proximity();
- if (proximity && proximity->get_upward_distance(proximity_alt_diff)) {
- proximity_alt_diff -= _margin;
- if (!limit_alt || proximity_alt_diff < alt_diff) {
- alt_diff = proximity_alt_diff;
- limit_alt = true;
- }
- }
-
- if (limit_alt) {
-
- if (alt_diff <= 0.0f) {
- climb_rate_cms = MIN(climb_rate_cms, 0.0f);
- return;
- }
-
- const float max_speed = get_max_speed(kP, accel_cmss_limited, alt_diff*100.0f, dt);
- climb_rate_cms = MIN(max_speed, climb_rate_cms);
- }
- }
- void AC_Avoid::adjust_roll_pitch(float &roll, float &pitch, float veh_angle_max)
- {
-
- if ((_enabled & AC_AVOID_USE_PROXIMITY_SENSOR) == 0 || !_proximity_enabled) {
- return;
- }
-
- if (_angle_max <= 0.0f || veh_angle_max <= 0.0f) {
- return;
- }
- float roll_positive = 0.0f;
- float roll_negative = 0.0f;
- float pitch_positive = 0.0f;
- float pitch_negative = 0.0f;
-
- get_proximity_roll_pitch_pct(roll_positive, roll_negative, pitch_positive, pitch_negative);
-
- Vector2f rp_out((roll_positive + roll_negative) * 4500.0f, (pitch_positive + pitch_negative) * 4500.0f);
-
-
- const float angle_limit = constrain_float(_angle_max, 0.0f, veh_angle_max * AC_AVOID_ANGLE_MAX_PERCENT);
- float vec_len = rp_out.length();
- if (vec_len > angle_limit) {
- rp_out *= (angle_limit / vec_len);
- }
-
- rp_out.x += roll;
- rp_out.y += pitch;
-
- vec_len = rp_out.length();
- if (vec_len > veh_angle_max) {
- rp_out *= (veh_angle_max / vec_len);
- }
-
- roll = rp_out.x;
- pitch = rp_out.y;
- }
- void AC_Avoid::limit_velocity(float kP, float accel_cmss, Vector2f &desired_vel_cms, const Vector2f& limit_direction, float limit_distance_cm, float dt) const
- {
- const float max_speed = get_max_speed(kP, accel_cmss, limit_distance_cm, dt);
-
- const float speed = desired_vel_cms * limit_direction;
- if (speed > max_speed) {
-
- desired_vel_cms += limit_direction*(max_speed - speed);
- }
- }
- float AC_Avoid::get_max_speed(float kP, float accel_cmss, float distance_cm, float dt) const
- {
- if (is_zero(kP)) {
- return safe_sqrt(2.0f * distance_cm * accel_cmss);
- } else {
- return AC_AttitudeControl::sqrt_controller(distance_cm, kP, accel_cmss, dt);
- }
- }
- void AC_Avoid::adjust_velocity_circle_fence(float kP, float accel_cmss, Vector2f &desired_vel_cms, float dt)
- {
- AC_Fence *fence = AP::fence();
- if (fence == nullptr) {
- return;
- }
- AC_Fence &_fence = *fence;
-
- if ((_fence.get_enabled_fences() & AC_FENCE_TYPE_CIRCLE) == 0) {
- return;
- }
-
- if ((_fence.get_breaches() & AC_FENCE_TYPE_CIRCLE) != 0) {
- return;
- }
-
- const float desired_speed = desired_vel_cms.length();
- if (is_zero(desired_speed)) {
-
- return;
- }
- const AP_AHRS &_ahrs = AP::ahrs();
-
- Vector2f position_xy;
- if (!_ahrs.get_relative_position_NE_home(position_xy)) {
-
- return;
- }
- position_xy *= 100.0f;
-
- const float fence_radius = _fence.get_radius() * 100.0f;
-
- const float margin_cm = _fence.get_margin() * 100.0f;
-
- const float dist_from_home = position_xy.length();
- if (dist_from_home > fence_radius) {
-
- return;
- }
-
- if ((AC_Avoid::BehaviourType)_behavior.get() == BEHAVIOR_SLIDE) {
-
- const Vector2f stopping_point = position_xy + desired_vel_cms*(get_stopping_distance(kP, accel_cmss, desired_speed)/desired_speed);
- const float stopping_point_dist_from_home = stopping_point.length();
- if (stopping_point_dist_from_home <= fence_radius - margin_cm) {
-
- return;
- }
-
-
-
- const Vector2f target_offset = stopping_point * ((fence_radius - margin_cm) / stopping_point_dist_from_home);
- const Vector2f target_direction = target_offset - position_xy;
- const float distance_to_target = target_direction.length();
- const float max_speed = get_max_speed(kP, accel_cmss, distance_to_target, dt);
- desired_vel_cms = target_direction * (MIN(desired_speed,max_speed) / distance_to_target);
- } else {
-
-
- const Vector2f stopping_point_plus_margin = position_xy + desired_vel_cms*((2.0f + margin_cm + get_stopping_distance(kP, accel_cmss, desired_speed))/desired_speed);
- const float stopping_point_plus_margin_dist_from_home = stopping_point_plus_margin.length();
- if (dist_from_home >= fence_radius - margin_cm) {
-
-
-
- if (stopping_point_plus_margin_dist_from_home >= dist_from_home) {
- desired_vel_cms.zero();
- }
- } else {
-
- Vector2f intersection;
- if (Vector2f::circle_segment_intersection(position_xy, stopping_point_plus_margin, Vector2f(0.0f,0.0f), fence_radius - margin_cm, intersection)) {
- const float distance_to_target = MAX((intersection - position_xy).length() - margin_cm, 0.0f);
- const float max_speed = get_max_speed(kP, accel_cmss, distance_to_target, dt);
- if (max_speed < desired_speed) {
- desired_vel_cms *= MAX(max_speed, 0.0f) / desired_speed;
- }
- }
- }
- }
- }
- void AC_Avoid::adjust_velocity_polygon_fence(float kP, float accel_cmss, Vector2f &desired_vel_cms, float dt)
- {
- AC_Fence *fence = AP::fence();
- if (fence == nullptr) {
- return;
- }
- AC_Fence &_fence = *fence;
-
- if ((_fence.get_enabled_fences() & AC_FENCE_TYPE_POLYGON) == 0) {
- return;
- }
-
- if ((_fence.get_breaches() & AC_FENCE_TYPE_POLYGON) != 0) {
- return;
- }
-
- uint16_t num_points;
- const Vector2f* boundary = _fence.get_boundary_points(num_points);
-
- adjust_velocity_polygon(kP, accel_cmss, desired_vel_cms, boundary, num_points, true, _fence.get_margin(), dt);
- }
- void AC_Avoid::adjust_velocity_beacon_fence(float kP, float accel_cmss, Vector2f &desired_vel_cms, float dt)
- {
- AP_Beacon *_beacon = AP::beacon();
-
- if (_beacon == nullptr) {
- return;
- }
-
- uint16_t num_points;
- const Vector2f* boundary = _beacon->get_boundary_points(num_points);
- if (boundary == nullptr || num_points == 0) {
- return;
- }
-
- float margin = 0;
- if (AP::fence()) {
- margin = AP::fence()->get_margin();
- }
- adjust_velocity_polygon(kP, accel_cmss, desired_vel_cms, boundary, num_points, true, margin, dt);
- }
- void AC_Avoid::adjust_velocity_proximity(float kP, float accel_cmss, Vector2f &desired_vel_cms, float dt)
- {
-
- AP_Proximity *proximity = AP::proximity();
- if (!proximity) {
- return;
- }
- AP_Proximity &_proximity = *proximity;
- if (_proximity.get_status() != AP_Proximity::Proximity_Good) {
- return;
- }
-
- uint16_t num_points;
- const Vector2f *boundary = _proximity.get_boundary_points(num_points);
- adjust_velocity_polygon(kP, accel_cmss, desired_vel_cms, boundary, num_points, false, _margin, dt);
- }
- void AC_Avoid::adjust_velocity_polygon(float kP, float accel_cmss, Vector2f &desired_vel_cms, const Vector2f* boundary, uint16_t num_points, bool earth_frame, float margin, float dt)
- {
-
- if (boundary == nullptr || num_points == 0) {
- return;
- }
-
- if (desired_vel_cms.is_zero()) {
- return;
- }
- const AP_AHRS &_ahrs = AP::ahrs();
-
- Vector2f position_xy;
- if (earth_frame) {
- if (!_ahrs.get_relative_position_NE_origin(position_xy)) {
-
-
- return;
- }
- position_xy = position_xy * 100.0f;
- }
- if (Polygon_outside(position_xy, boundary, num_points)) {
- return;
- }
-
-
-
- Vector2f safe_vel(desired_vel_cms);
-
- if (!earth_frame) {
- safe_vel.x = desired_vel_cms.y * _ahrs.sin_yaw() + desired_vel_cms.x * _ahrs.cos_yaw();
- safe_vel.y = desired_vel_cms.y * _ahrs.cos_yaw() - desired_vel_cms.x * _ahrs.sin_yaw();
- }
-
- const float margin_cm = MAX(margin * 100.0f, 0.0f);
-
- const float speed = safe_vel.length();
- const Vector2f stopping_point_plus_margin = position_xy + safe_vel*((2.0f + margin_cm + get_stopping_distance(kP, accel_cmss, speed))/speed);
- for (uint16_t i=0; i<num_points; i++) {
- uint16_t j = i+1;
- if (j >= num_points) {
- j = 0;
- }
-
- Vector2f start = boundary[j];
- Vector2f end = boundary[i];
- if ((AC_Avoid::BehaviourType)_behavior.get() == BEHAVIOR_SLIDE) {
-
- Vector2f limit_direction = Vector2f::closest_point(position_xy, start, end) - position_xy;
-
- const float limit_distance_cm = limit_direction.length();
- if (!is_zero(limit_distance_cm)) {
-
-
- limit_direction /= limit_distance_cm;
- limit_velocity(kP, accel_cmss, safe_vel, limit_direction, MAX(limit_distance_cm - margin_cm, 0.0f), dt);
- } else {
-
-
- return;
- }
- } else {
-
- Vector2f intersection;
- if (Vector2f::segment_intersection(position_xy, stopping_point_plus_margin, start, end, intersection)) {
-
- Vector2f limit_direction = intersection - position_xy;
- const float limit_distance_cm = limit_direction.length();
- if (!is_zero(limit_distance_cm)) {
- if (limit_distance_cm <= margin_cm) {
-
- safe_vel.zero();
- } else {
-
- limit_direction /= limit_distance_cm;
- limit_velocity(kP, accel_cmss, safe_vel, limit_direction, MAX(limit_distance_cm - margin_cm, 0.0f), dt);
- }
- } else {
-
-
- return;
- }
- }
- }
- }
-
- if (earth_frame) {
- desired_vel_cms = safe_vel;
- } else {
-
- desired_vel_cms.x = safe_vel.x * _ahrs.cos_yaw() - safe_vel.y * _ahrs.sin_yaw();
- desired_vel_cms.y = safe_vel.x * _ahrs.sin_yaw() + safe_vel.y * _ahrs.cos_yaw();
- }
- }
- float AC_Avoid::get_stopping_distance(float kP, float accel_cmss, float speed_cms) const
- {
-
- if (accel_cmss <= 0.0f || is_zero(speed_cms)) {
- return 0.0f;
- }
-
- if (kP <= 0.0f) {
- return 0.5f * sq(speed_cms) / accel_cmss;
- }
-
-
- if (speed_cms < accel_cmss/kP) {
- return speed_cms/kP;
- } else {
-
- return accel_cmss/(2.0f*kP*kP) + (speed_cms*speed_cms)/(2.0f*accel_cmss);
- }
- }
- float AC_Avoid::distance_to_lean_pct(float dist_m)
- {
-
- if (dist_m < 0.0f || dist_m >= _dist_max || _dist_max <= 0.0f) {
- return 0.0f;
- }
-
- return 1.0f - (dist_m / _dist_max);
- }
- void AC_Avoid::get_proximity_roll_pitch_pct(float &roll_positive, float &roll_negative, float &pitch_positive, float &pitch_negative)
- {
- AP_Proximity *proximity = AP::proximity();
- if (proximity == nullptr) {
- return;
- }
- AP_Proximity &_proximity = *proximity;
-
- if (_proximity.get_status() != AP_Proximity::Proximity_Good) {
- return;
- }
- const uint8_t obj_count = _proximity.get_object_count();
-
- if (obj_count == 0) {
- return;
- }
-
- for (uint8_t i=0; i<obj_count; i++) {
- float ang_deg, dist_m;
- if (_proximity.get_object_angle_and_distance(i, ang_deg, dist_m)) {
- if (dist_m < _dist_max) {
-
- const float lean_pct = distance_to_lean_pct(dist_m);
-
- const float angle_rad = radians(ang_deg);
- const float roll_pct = -sinf(angle_rad) * lean_pct;
- const float pitch_pct = cosf(angle_rad) * lean_pct;
-
- if (roll_pct > 0.0f) {
- roll_positive = MAX(roll_positive, roll_pct);
- } else if (roll_pct < 0.0f) {
- roll_negative = MIN(roll_negative, roll_pct);
- }
- if (pitch_pct > 0.0f) {
- pitch_positive = MAX(pitch_positive, pitch_pct);
- } else if (pitch_pct < 0.0f) {
- pitch_negative = MIN(pitch_negative, pitch_pct);
- }
- }
- }
- }
- }
- AC_Avoid *AC_Avoid::_singleton;
- namespace AP {
- AC_Avoid *ac_avoid()
- {
- return AC_Avoid::get_singleton();
- }
- }
|