Niko Sohmers | ac4d887 | 2024-02-23 13:55:47 -0800 | [diff] [blame] | 1 | #ifndef Y2024_CONTROL_LOOPS_SUPERSTRUCTURE_COLLISION_AVOIDENCE_H_ |
| 2 | #define Y2024_CONTROL_LOOPS_SUPERSTRUCTURE_COLLISION_AVOIDENCE_H_ |
| 3 | |
| 4 | #include <cmath> |
| 5 | |
| 6 | #include "frc971/control_loops/control_loops_generated.h" |
| 7 | #include "frc971/control_loops/profiled_subsystem_generated.h" |
| 8 | |
| 9 | namespace y2024::control_loops::superstructure { |
| 10 | |
| 11 | // Checks if theta is between theta_min and theta_max. Expects all angles to be |
| 12 | // wrapped from 0 to 2pi |
| 13 | bool AngleInRange(double theta, double theta_min, double theta_max); |
| 14 | |
| 15 | // 1. Prevent the turret from moving if the intake is up |
| 16 | // and prevent the back of the turret from colliding with the intake when it's |
| 17 | // up. |
| 18 | // 2. If the intake is up, drop it so it is not in the way |
| 19 | // 3. Move the turret to the desired position. |
| 20 | // 4. When the turret moves away, if the intake is down, move it back up. |
| 21 | class CollisionAvoidance { |
| 22 | public: |
| 23 | struct Status { |
| 24 | double intake_pivot_position; |
| 25 | double turret_position; |
Austin Schuh | 027fd62 | 2024-03-01 21:26:07 -0800 | [diff] [blame] | 26 | double extend_position; |
Niko Sohmers | ac4d887 | 2024-02-23 13:55:47 -0800 | [diff] [blame] | 27 | |
| 28 | bool operator==(const Status &s) const { |
| 29 | return (intake_pivot_position == s.intake_pivot_position && |
Austin Schuh | 027fd62 | 2024-03-01 21:26:07 -0800 | [diff] [blame] | 30 | turret_position == s.turret_position && |
| 31 | extend_position == s.extend_position); |
Niko Sohmers | ac4d887 | 2024-02-23 13:55:47 -0800 | [diff] [blame] | 32 | } |
| 33 | bool operator!=(const Status &s) const { return !(*this == s); } |
| 34 | }; |
| 35 | |
Niko Sohmers | ac4d887 | 2024-02-23 13:55:47 -0800 | [diff] [blame] | 36 | // For the turret, 0 rad is pointing straight forwards |
Maxwell Henderson | 3d68e14 | 2024-02-25 09:58:11 -0800 | [diff] [blame] | 37 | static constexpr double kMinCollisionZoneTurret = 0.15; |
| 38 | static constexpr double kMaxCollisionZoneTurret = 1.15; |
Niko Sohmers | ac4d887 | 2024-02-23 13:55:47 -0800 | [diff] [blame] | 39 | |
Austin Schuh | 027fd62 | 2024-03-01 21:26:07 -0800 | [diff] [blame] | 40 | static constexpr double kSafeTurretExtendedPosition = 0.0; |
| 41 | |
Niko Sohmers | ac4d887 | 2024-02-23 13:55:47 -0800 | [diff] [blame] | 42 | // Maximum position of the intake to avoid collisions |
| 43 | static constexpr double kCollisionZoneIntake = 1.6; |
| 44 | |
Austin Schuh | 027fd62 | 2024-03-01 21:26:07 -0800 | [diff] [blame] | 45 | static constexpr double kMinCollisionZoneExtend = 0.03; |
| 46 | |
Niko Sohmers | ac4d887 | 2024-02-23 13:55:47 -0800 | [diff] [blame] | 47 | // Tolerances for the subsystems |
| 48 | static constexpr double kEpsTurret = 0.05; |
| 49 | static constexpr double kEpsIntake = 0.05; |
Austin Schuh | 79903e8 | 2024-04-07 23:12:00 -0700 | [diff] [blame] | 50 | static constexpr double kEpsExtend = 0.005; |
Niko Sohmers | ac4d887 | 2024-02-23 13:55:47 -0800 | [diff] [blame] | 51 | |
| 52 | CollisionAvoidance(); |
| 53 | |
| 54 | // Reports if the superstructure is collided. |
| 55 | bool IsCollided(const Status &status); |
Austin Schuh | 027fd62 | 2024-03-01 21:26:07 -0800 | [diff] [blame] | 56 | // Checks if there is a collision with the intake. |
Niko Sohmers | ac4d887 | 2024-02-23 13:55:47 -0800 | [diff] [blame] | 57 | bool TurretCollided(double intake_position, double turret_position, |
Austin Schuh | 027fd62 | 2024-03-01 21:26:07 -0800 | [diff] [blame] | 58 | double extend_position); |
| 59 | // Checks if there is a collision with the extend. |
| 60 | bool ExtendCollided(double intake_position, double turret_position, |
| 61 | double extend_position); |
Niko Sohmers | ac4d887 | 2024-02-23 13:55:47 -0800 | [diff] [blame] | 62 | // Checks and alters goals to make sure they're safe. |
| 63 | void UpdateGoal(const CollisionAvoidance::Status &status, |
Austin Schuh | 027fd62 | 2024-03-01 21:26:07 -0800 | [diff] [blame] | 64 | double turret_goal_position, double extend_goal_position); |
Niko Sohmers | ac4d887 | 2024-02-23 13:55:47 -0800 | [diff] [blame] | 65 | // Limits if goal is in collision spots. |
Austin Schuh | 027fd62 | 2024-03-01 21:26:07 -0800 | [diff] [blame] | 66 | void CalculateAvoidance(double intake_position, double turret_position, |
| 67 | double extend_position, double turret_goal, |
| 68 | double extend_goal); |
Niko Sohmers | ac4d887 | 2024-02-23 13:55:47 -0800 | [diff] [blame] | 69 | |
| 70 | // Returns the goals to give to the respective control loops in |
| 71 | // superstructure. |
Austin Schuh | 027fd62 | 2024-03-01 21:26:07 -0800 | [diff] [blame] | 72 | double min_extend_goal() const { return min_extend_goal_; } |
| 73 | double max_extend_goal() const { return max_extend_goal_; } |
Niko Sohmers | ac4d887 | 2024-02-23 13:55:47 -0800 | [diff] [blame] | 74 | double min_turret_goal() const { return min_turret_goal_; } |
| 75 | double max_turret_goal() const { return max_turret_goal_; } |
| 76 | double min_intake_pivot_goal() const { return min_intake_pivot_goal_; } |
| 77 | double max_intake_pivot_goal() const { return max_intake_pivot_goal_; } |
| 78 | |
| 79 | void update_max_turret_goal(double max_turret_goal) { |
| 80 | max_turret_goal_ = ::std::min(max_turret_goal, max_turret_goal_); |
| 81 | } |
| 82 | void update_min_turret_goal(double min_turret_goal) { |
| 83 | min_turret_goal_ = ::std::max(min_turret_goal, min_turret_goal_); |
| 84 | } |
| 85 | void update_max_intake_pivot_goal(double max_intake_pivot_goal) { |
| 86 | max_intake_pivot_goal_ = |
| 87 | ::std::min(max_intake_pivot_goal, max_intake_pivot_goal_); |
| 88 | } |
| 89 | void update_min_intake_pivot_goal(double min_intake_pivot_goal) { |
| 90 | min_intake_pivot_goal_ = |
| 91 | ::std::max(min_intake_pivot_goal, min_intake_pivot_goal_); |
| 92 | } |
Austin Schuh | 027fd62 | 2024-03-01 21:26:07 -0800 | [diff] [blame] | 93 | void update_min_extend_goal(double min_extend_goal) { |
| 94 | min_extend_goal_ = ::std::max(min_extend_goal, min_extend_goal_); |
| 95 | } |
| 96 | void update_max_extend_goal(double max_extend_goal) { |
| 97 | max_extend_goal_ = ::std::min(max_extend_goal, max_extend_goal_); |
| 98 | } |
Niko Sohmers | ac4d887 | 2024-02-23 13:55:47 -0800 | [diff] [blame] | 99 | |
| 100 | private: |
| 101 | void clear_min_intake_pivot_goal() { |
| 102 | min_intake_pivot_goal_ = -::std::numeric_limits<double>::infinity(); |
| 103 | } |
| 104 | void clear_max_intake_pivot_goal() { |
| 105 | max_intake_pivot_goal_ = ::std::numeric_limits<double>::infinity(); |
| 106 | } |
| 107 | void clear_min_turret_goal() { |
| 108 | min_turret_goal_ = -::std::numeric_limits<double>::infinity(); |
| 109 | } |
| 110 | void clear_max_turret_goal() { |
| 111 | max_turret_goal_ = ::std::numeric_limits<double>::infinity(); |
| 112 | } |
Austin Schuh | 027fd62 | 2024-03-01 21:26:07 -0800 | [diff] [blame] | 113 | void clear_min_extend_goal() { |
| 114 | min_extend_goal_ = -::std::numeric_limits<double>::infinity(); |
| 115 | } |
| 116 | void clear_max_extend_goal() { |
| 117 | max_extend_goal_ = ::std::numeric_limits<double>::infinity(); |
| 118 | } |
Niko Sohmers | ac4d887 | 2024-02-23 13:55:47 -0800 | [diff] [blame] | 119 | |
| 120 | double min_intake_pivot_goal_; |
| 121 | double max_intake_pivot_goal_; |
| 122 | double min_turret_goal_; |
| 123 | double max_turret_goal_; |
Austin Schuh | 027fd62 | 2024-03-01 21:26:07 -0800 | [diff] [blame] | 124 | double min_extend_goal_; |
| 125 | double max_extend_goal_; |
Niko Sohmers | ac4d887 | 2024-02-23 13:55:47 -0800 | [diff] [blame] | 126 | }; |
| 127 | |
| 128 | } // namespace y2024::control_loops::superstructure |
| 129 | |
| 130 | #endif |