Sabina Davis | 4b63ae5 | 2019-01-27 16:15:25 -0800 | [diff] [blame] | 1 | #include "y2019/control_loops/superstructure/collision_avoidance.h" |
| 2 | |
| 3 | #include <cmath> |
| 4 | #include "y2019/control_loops/superstructure/superstructure.q.h" |
| 5 | |
| 6 | namespace y2019 { |
| 7 | namespace control_loops { |
| 8 | namespace superstructure { |
| 9 | |
| 10 | constexpr double CollisionAvoidance::kElevatorClearHeight; |
| 11 | constexpr double CollisionAvoidance::kElevatorClearWristDownHeight; |
| 12 | constexpr double CollisionAvoidance::kElevatorClearIntakeHeight; |
| 13 | constexpr double CollisionAvoidance::kWristMaxAngle; |
| 14 | constexpr double CollisionAvoidance::kWristMinAngle; |
| 15 | constexpr double CollisionAvoidance::kIntakeOutAngle; |
| 16 | constexpr double CollisionAvoidance::kIntakeInAngle; |
| 17 | constexpr double CollisionAvoidance::kWristElevatorCollisionMinAngle; |
| 18 | constexpr double CollisionAvoidance::kWristElevatorCollisionMaxAngle; |
Tyler Chatow | d180b25 | 2019-02-23 22:00:20 -0800 | [diff] [blame^] | 19 | constexpr double |
| 20 | CollisionAvoidance::kWristElevatorCollisionMaxAngleWithoutObject; |
Sabina Davis | 4b63ae5 | 2019-01-27 16:15:25 -0800 | [diff] [blame] | 21 | constexpr double CollisionAvoidance::kEps; |
| 22 | constexpr double CollisionAvoidance::kEpsIntake; |
| 23 | constexpr double CollisionAvoidance::kEpsWrist; |
| 24 | |
| 25 | CollisionAvoidance::CollisionAvoidance() { |
| 26 | clear_min_wrist_goal(); |
| 27 | clear_max_wrist_goal(); |
| 28 | clear_min_elevator_goal(); |
| 29 | clear_min_intake_goal(); |
| 30 | clear_max_intake_goal(); |
| 31 | } |
| 32 | |
| 33 | bool CollisionAvoidance::IsCollided(const SuperstructureQueue::Status *status) { |
| 34 | const double wrist_position = status->wrist.position; |
| 35 | const double elevator_position = status->elevator.position; |
| 36 | const double intake_position = status->intake.position; |
Tyler Chatow | d180b25 | 2019-02-23 22:00:20 -0800 | [diff] [blame^] | 37 | const bool has_piece = status->has_piece; |
| 38 | |
| 39 | const double wrist_elevator_collision_max_angle = |
| 40 | has_piece ? kWristElevatorCollisionMaxAngle |
| 41 | : kWristElevatorCollisionMaxAngleWithoutObject; |
Sabina Davis | 4b63ae5 | 2019-01-27 16:15:25 -0800 | [diff] [blame] | 42 | |
| 43 | // Elevator is down, so the wrist can't be close to vertical. |
| 44 | if (elevator_position < kElevatorClearHeight) { |
Tyler Chatow | d180b25 | 2019-02-23 22:00:20 -0800 | [diff] [blame^] | 45 | if (wrist_position < wrist_elevator_collision_max_angle && |
Sabina Davis | 4b63ae5 | 2019-01-27 16:15:25 -0800 | [diff] [blame] | 46 | wrist_position > kWristElevatorCollisionMinAngle) { |
| 47 | return true; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | // Elevator is down so wrist can't go below horizontal in either direction. |
| 52 | if (elevator_position < kElevatorClearWristDownHeight) { |
| 53 | if (wrist_position > kWristMaxAngle) { |
| 54 | return true; |
| 55 | } |
| 56 | if (wrist_position < kWristMinAngle) { |
| 57 | return true; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | // Elevator is down so the intake has to be at either extreme. |
Austin Schuh | 0158e6a | 2019-02-17 15:02:26 -0800 | [diff] [blame] | 62 | if (elevator_position < kElevatorClearIntakeHeight && |
| 63 | wrist_position > kWristMaxAngle) { |
Sabina Davis | 4b63ae5 | 2019-01-27 16:15:25 -0800 | [diff] [blame] | 64 | if (intake_position < kIntakeOutAngle && intake_position > kIntakeInAngle) { |
| 65 | return true; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | // Nothing is hitting, we must be good. |
| 70 | return false; |
| 71 | } |
| 72 | |
| 73 | void CollisionAvoidance::UpdateGoal( |
| 74 | const SuperstructureQueue::Status *status, |
| 75 | const SuperstructureQueue::Goal *unsafe_goal) { |
| 76 | const double wrist_position = status->wrist.position; |
| 77 | const double elevator_position = status->elevator.position; |
| 78 | const double intake_position = status->intake.position; |
Tyler Chatow | d180b25 | 2019-02-23 22:00:20 -0800 | [diff] [blame^] | 79 | const bool has_piece = status->has_piece; |
Sabina Davis | 4b63ae5 | 2019-01-27 16:15:25 -0800 | [diff] [blame] | 80 | |
| 81 | // Start with our constraints being wide open. |
| 82 | clear_max_wrist_goal(); |
| 83 | clear_min_wrist_goal(); |
| 84 | clear_max_intake_goal(); |
| 85 | clear_min_intake_goal(); |
| 86 | |
Tyler Chatow | d180b25 | 2019-02-23 22:00:20 -0800 | [diff] [blame^] | 87 | const double wrist_elevator_collision_max_angle = |
| 88 | has_piece ? kWristElevatorCollisionMaxAngle |
| 89 | : kWristElevatorCollisionMaxAngleWithoutObject; |
| 90 | |
Sabina Davis | 4b63ae5 | 2019-01-27 16:15:25 -0800 | [diff] [blame] | 91 | // If the elevator is low enough, we also can't transition the wrist. |
| 92 | if (elevator_position < kElevatorClearHeight) { |
| 93 | // Figure out which side the wrist is on and stay there. |
Austin Schuh | 0158e6a | 2019-02-17 15:02:26 -0800 | [diff] [blame] | 94 | if (wrist_position < (2.0 * kWristElevatorCollisionMinAngle + |
| 95 | kWristElevatorCollisionMaxAngle) / |
| 96 | 3.0) { |
Sabina Davis | 4b63ae5 | 2019-01-27 16:15:25 -0800 | [diff] [blame] | 97 | update_max_wrist_goal(kWristElevatorCollisionMinAngle - kEpsWrist); |
| 98 | } else { |
Tyler Chatow | d180b25 | 2019-02-23 22:00:20 -0800 | [diff] [blame^] | 99 | update_min_wrist_goal(wrist_elevator_collision_max_angle + kEpsWrist); |
Sabina Davis | 4b63ae5 | 2019-01-27 16:15:25 -0800 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | |
| 103 | // If the elevator is too low, the wrist needs to be above the clearance |
| 104 | // angles to avoid crashing the frame. |
| 105 | if (elevator_position < kElevatorClearWristDownHeight) { |
| 106 | update_min_wrist_goal(kWristMinAngle + kEpsWrist); |
| 107 | update_max_wrist_goal(kWristMaxAngle - kEpsWrist); |
| 108 | } |
| 109 | |
| 110 | constexpr double kIntakeMiddleAngle = |
| 111 | (kIntakeOutAngle + kIntakeInAngle) / 2.0; |
| 112 | |
| 113 | // If the elevator is too low, the intake can't transition from in to out or |
| 114 | // back. |
Austin Schuh | 0158e6a | 2019-02-17 15:02:26 -0800 | [diff] [blame] | 115 | if (elevator_position < kElevatorClearIntakeHeight && |
Tyler Chatow | d180b25 | 2019-02-23 22:00:20 -0800 | [diff] [blame^] | 116 | wrist_position > wrist_elevator_collision_max_angle) { |
Sabina Davis | 4b63ae5 | 2019-01-27 16:15:25 -0800 | [diff] [blame] | 117 | // Figure out if the intake is in our out and keep it there. |
| 118 | if (intake_position < kIntakeMiddleAngle) { |
| 119 | update_max_intake_goal(kIntakeInAngle - kEpsIntake); |
| 120 | } else { |
| 121 | update_min_intake_goal(kIntakeOutAngle + kEpsIntake); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | // Start with an unconstrained elevator. |
| 126 | clear_min_elevator_goal(); |
| 127 | |
| 128 | // If the intake is within the collision range, don't let the elevator down. |
Austin Schuh | 0158e6a | 2019-02-17 15:02:26 -0800 | [diff] [blame] | 129 | if (intake_position > kIntakeInAngle && intake_position < kIntakeOutAngle && |
Tyler Chatow | d180b25 | 2019-02-23 22:00:20 -0800 | [diff] [blame^] | 130 | wrist_position > wrist_elevator_collision_max_angle) { |
Sabina Davis | 4b63ae5 | 2019-01-27 16:15:25 -0800 | [diff] [blame] | 131 | update_min_elevator_goal(kElevatorClearIntakeHeight + kEps); |
| 132 | } |
| 133 | |
Austin Schuh | 0158e6a | 2019-02-17 15:02:26 -0800 | [diff] [blame] | 134 | // If the intake is in the collision range and the elevator is down, don't let |
| 135 | // the wrist go far down. |
| 136 | if (intake_position > kIntakeInAngle && intake_position < kIntakeOutAngle && |
| 137 | elevator_position < kElevatorClearIntakeHeight) { |
| 138 | update_max_wrist_goal(kWristMaxAngle - kEpsWrist); |
| 139 | } |
| 140 | |
Sabina Davis | 4b63ae5 | 2019-01-27 16:15:25 -0800 | [diff] [blame] | 141 | // If the wrist is within the elevator collision range, don't let the elevator |
| 142 | // go down. |
| 143 | if (wrist_position > kWristElevatorCollisionMinAngle && |
Tyler Chatow | d180b25 | 2019-02-23 22:00:20 -0800 | [diff] [blame^] | 144 | wrist_position < wrist_elevator_collision_max_angle) { |
Sabina Davis | 4b63ae5 | 2019-01-27 16:15:25 -0800 | [diff] [blame] | 145 | update_min_elevator_goal(kElevatorClearHeight + kEps); |
| 146 | } |
| 147 | |
| 148 | // If the wrist is far enough down that we are going to hit the frame, don't |
| 149 | // let the elevator go too far down. |
| 150 | if (wrist_position > kWristMaxAngle || wrist_position < kWristMinAngle) { |
| 151 | update_min_elevator_goal(kElevatorClearWristDownHeight + kEps); |
| 152 | } |
| 153 | |
| 154 | if (unsafe_goal) { |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 155 | const double wrist_goal = unsafe_goal->wrist.unsafe_goal; |
| 156 | const double intake_goal = unsafe_goal->intake.unsafe_goal; |
Sabina Davis | 4b63ae5 | 2019-01-27 16:15:25 -0800 | [diff] [blame] | 157 | |
| 158 | // Compute if we need to move the intake. |
| 159 | const bool intake_needs_to_move = (intake_position < kIntakeMiddleAngle) ^ |
| 160 | (intake_goal < kIntakeMiddleAngle); |
| 161 | |
| 162 | // Compute if we need to move the wrist across 0. |
| 163 | const bool wrist_needs_to_move = |
| 164 | (wrist_position < 0.0) ^ (wrist_goal < 0.0); |
| 165 | |
| 166 | // If we need to move the intake, we've got to shove the elevator up. The |
| 167 | // intake is already constrained so it can't hit anything until it's clear. |
Austin Schuh | 0158e6a | 2019-02-17 15:02:26 -0800 | [diff] [blame] | 168 | if (intake_needs_to_move && |
Tyler Chatow | d180b25 | 2019-02-23 22:00:20 -0800 | [diff] [blame^] | 169 | wrist_position > wrist_elevator_collision_max_angle) { |
Sabina Davis | 4b63ae5 | 2019-01-27 16:15:25 -0800 | [diff] [blame] | 170 | update_min_elevator_goal(kElevatorClearIntakeHeight + kEps); |
| 171 | } |
| 172 | // If we need to move the wrist, we've got to shove the elevator up too. The |
| 173 | // wrist is already constrained so it can't hit anything until it's clear. |
| 174 | // If both the intake and wrist need to move, figure out which one will |
| 175 | // require the higher motion and move that. |
| 176 | if (wrist_needs_to_move) { |
| 177 | update_min_elevator_goal(kElevatorClearHeight + kEps); |
| 178 | } |
| 179 | |
| 180 | // TODO(austin): We won't shove the elevator up if the wrist is asked to go |
| 181 | // down below horizontal. I think that's fine. |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | } // namespace superstructure |
| 186 | } // namespace control_loops |
| 187 | } // namespace y2019 |