Milind Upadhyay | 225156b | 2022-02-25 22:42:12 -0800 | [diff] [blame] | 1 | #include "y2022/control_loops/superstructure/collision_avoidance.h" |
| 2 | |
| 3 | #include <cmath> |
| 4 | |
| 5 | #include "absl/functional/bind_front.h" |
| 6 | #include "glog/logging.h" |
| 7 | |
| 8 | namespace y2022 { |
| 9 | namespace control_loops { |
| 10 | namespace superstructure { |
| 11 | |
| 12 | CollisionAvoidance::CollisionAvoidance() { |
| 13 | clear_min_intake_front_goal(); |
| 14 | clear_max_intake_front_goal(); |
| 15 | clear_min_intake_back_goal(); |
| 16 | clear_max_intake_back_goal(); |
| 17 | clear_min_turret_goal(); |
| 18 | clear_max_turret_goal(); |
| 19 | } |
| 20 | |
| 21 | bool CollisionAvoidance::IsCollided(const CollisionAvoidance::Status &status) { |
| 22 | // Checks if intake front is collided. |
| 23 | if (TurretCollided(status.intake_front_position, status.turret_position, |
| 24 | kMinCollisionZoneFrontTurret, |
| 25 | kMaxCollisionZoneFrontTurret)) { |
| 26 | return true; |
| 27 | } |
| 28 | |
| 29 | // Checks if intake back is collided. |
| 30 | if (TurretCollided(status.intake_back_position, status.turret_position, |
| 31 | kMinCollisionZoneBackTurret, |
| 32 | kMaxCollisionZoneBackTurret)) { |
| 33 | return true; |
| 34 | } |
| 35 | |
| 36 | return false; |
| 37 | } |
| 38 | |
| 39 | std::pair<double, int> WrapTurretAngle(double turret_angle) { |
| 40 | double wrapped = std::remainder(turret_angle - M_PI, 2 * M_PI) + M_PI; |
| 41 | int wraps = |
| 42 | static_cast<int>(std::round((turret_angle - wrapped) / (2 * M_PI))); |
| 43 | return {wrapped, wraps}; |
| 44 | } |
| 45 | |
| 46 | double UnwrapTurretAngle(double wrapped, int wraps) { |
| 47 | return wrapped + 2.0 * M_PI * wraps; |
| 48 | } |
| 49 | |
| 50 | bool CollisionAvoidance::TurretCollided(double intake_position, |
| 51 | double turret_position, |
| 52 | double min_turret_collision_position, |
| 53 | double max_turret_collision_position) { |
| 54 | const auto turret_position_wrapped_pair = WrapTurretAngle(turret_position); |
| 55 | const double turret_position_wrapped = turret_position_wrapped_pair.first; |
| 56 | |
| 57 | // Checks if turret is in the collision area. |
| 58 | if (turret_position_wrapped >= min_turret_collision_position && |
| 59 | turret_position_wrapped <= max_turret_collision_position) { |
| 60 | // Reterns true if the intake is raised. |
| 61 | if (intake_position <= kCollisionZoneIntake) { |
| 62 | return true; |
| 63 | } |
| 64 | } else { |
| 65 | return false; |
| 66 | } |
| 67 | return false; |
| 68 | } |
| 69 | |
| 70 | void CollisionAvoidance::UpdateGoal(const CollisionAvoidance::Status &status, |
| 71 | const Goal *unsafe_goal) { |
| 72 | // Start with our constraints being wide open. |
| 73 | clear_max_turret_goal(); |
| 74 | clear_min_turret_goal(); |
| 75 | clear_max_intake_front_goal(); |
| 76 | clear_min_intake_front_goal(); |
| 77 | clear_max_intake_back_goal(); |
| 78 | clear_min_intake_back_goal(); |
| 79 | |
| 80 | const double intake_front_position = status.intake_front_position; |
| 81 | const double intake_back_position = status.intake_back_position; |
| 82 | const double turret_position = status.turret_position; |
| 83 | |
| 84 | const double turret_goal = |
| 85 | (unsafe_goal != nullptr && unsafe_goal->turret() != nullptr |
| 86 | ? unsafe_goal->turret()->unsafe_goal() |
| 87 | : std::numeric_limits<double>::quiet_NaN()); |
| 88 | |
| 89 | // Calculating the avoidance with either intake, and when the turret is |
| 90 | // wrapped. |
| 91 | |
| 92 | CalculateAvoidance(true, intake_front_position, turret_goal, |
| 93 | kMinCollisionZoneFrontTurret, kMaxCollisionZoneFrontTurret, |
| 94 | turret_position); |
| 95 | CalculateAvoidance(false, intake_back_position, turret_goal, |
| 96 | kMinCollisionZoneBackTurret, kMaxCollisionZoneBackTurret, |
| 97 | turret_position); |
| 98 | } |
| 99 | |
| 100 | void CollisionAvoidance::CalculateAvoidance(bool intake_front, |
| 101 | double intake_position, |
| 102 | double turret_goal, |
| 103 | double min_turret_collision_goal, |
| 104 | double max_turret_collision_goal, |
| 105 | double turret_position) { |
| 106 | auto [turret_position_wrapped, turret_position_wraps] = |
| 107 | WrapTurretAngle(turret_position); |
| 108 | |
| 109 | // If the turret goal is in a collison zone or moving through one, limit |
| 110 | // intake. |
| 111 | const bool turret_pos_unsafe = |
| 112 | (turret_position_wrapped >= min_turret_collision_goal && |
| 113 | turret_position_wrapped <= max_turret_collision_goal); |
| 114 | |
| 115 | const bool turret_moving_forward = (turret_goal > turret_position); |
| 116 | |
| 117 | // To figure out if we are moving past an intake, find the unwrapped min/max |
| 118 | // angles closest to the turret position on the journey. |
| 119 | int bounds_wraps = turret_position_wraps; |
| 120 | double min_turret_collision_goal_unwrapped = |
| 121 | UnwrapTurretAngle(min_turret_collision_goal, bounds_wraps); |
| 122 | if (turret_moving_forward && |
| 123 | min_turret_collision_goal_unwrapped < turret_position) { |
| 124 | bounds_wraps++; |
| 125 | } else if (!turret_moving_forward && |
| 126 | min_turret_collision_goal_unwrapped > turret_position) { |
| 127 | bounds_wraps--; |
| 128 | } |
| 129 | min_turret_collision_goal_unwrapped = |
| 130 | UnwrapTurretAngle(min_turret_collision_goal, bounds_wraps); |
| 131 | const double max_turret_collision_goal_unwrapped = |
| 132 | UnwrapTurretAngle(max_turret_collision_goal, bounds_wraps); |
| 133 | |
| 134 | // Check if the closest unwrapped angles are going to be passed |
| 135 | const bool turret_moving_past_intake = |
| 136 | ((turret_moving_forward && |
| 137 | (turret_position <= max_turret_collision_goal_unwrapped && |
| 138 | turret_goal >= min_turret_collision_goal_unwrapped)) || |
| 139 | (!turret_moving_forward && |
| 140 | (turret_position >= min_turret_collision_goal_unwrapped && |
| 141 | turret_goal <= max_turret_collision_goal_unwrapped))); |
| 142 | |
| 143 | if (turret_pos_unsafe || turret_moving_past_intake) { |
| 144 | // If the turret is unsafe, limit the intake |
| 145 | if (intake_front) { |
| 146 | update_min_intake_front_goal(kCollisionZoneIntake + kEpsIntake); |
| 147 | } else { |
| 148 | update_min_intake_back_goal(kCollisionZoneIntake + kEpsIntake); |
| 149 | } |
| 150 | |
| 151 | // If the intake is in the way, limit the turret until moved. Otherwise, |
| 152 | // let'errip! |
| 153 | if (!turret_pos_unsafe && (intake_position <= kCollisionZoneIntake)) { |
| 154 | if (turret_position < min_turret_collision_goal_unwrapped) { |
| 155 | update_max_turret_goal(min_turret_collision_goal_unwrapped - |
| 156 | kEpsTurret); |
| 157 | } else { |
| 158 | update_min_turret_goal(max_turret_collision_goal_unwrapped + |
| 159 | kEpsTurret); |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | } // namespace superstructure |
| 166 | } // namespace control_loops |
| 167 | } // namespace y2022 |