blob: dcc26d45ec12fce44cc6231d619669e3ebd9658f [file] [log] [blame]
Niko Sohmersac4d8872024-02-23 13:55:47 -08001#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
9namespace 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
13bool 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.
21class CollisionAvoidance {
22 public:
23 struct Status {
24 double intake_pivot_position;
25 double turret_position;
Austin Schuh027fd622024-03-01 21:26:07 -080026 double extend_position;
Niko Sohmersac4d8872024-02-23 13:55:47 -080027
28 bool operator==(const Status &s) const {
29 return (intake_pivot_position == s.intake_pivot_position &&
Austin Schuh027fd622024-03-01 21:26:07 -080030 turret_position == s.turret_position &&
31 extend_position == s.extend_position);
Niko Sohmersac4d8872024-02-23 13:55:47 -080032 }
33 bool operator!=(const Status &s) const { return !(*this == s); }
34 };
35
Niko Sohmersac4d8872024-02-23 13:55:47 -080036 // For the turret, 0 rad is pointing straight forwards
Maxwell Henderson3d68e142024-02-25 09:58:11 -080037 static constexpr double kMinCollisionZoneTurret = 0.15;
38 static constexpr double kMaxCollisionZoneTurret = 1.15;
Niko Sohmersac4d8872024-02-23 13:55:47 -080039
Austin Schuh027fd622024-03-01 21:26:07 -080040 static constexpr double kSafeTurretExtendedPosition = 0.0;
41
Niko Sohmersac4d8872024-02-23 13:55:47 -080042 // Maximum position of the intake to avoid collisions
43 static constexpr double kCollisionZoneIntake = 1.6;
44
Austin Schuh027fd622024-03-01 21:26:07 -080045 static constexpr double kMinCollisionZoneExtend = 0.03;
46
Niko Sohmersac4d8872024-02-23 13:55:47 -080047 // Tolerances for the subsystems
48 static constexpr double kEpsTurret = 0.05;
49 static constexpr double kEpsIntake = 0.05;
Austin Schuh79903e82024-04-07 23:12:00 -070050 static constexpr double kEpsExtend = 0.005;
Niko Sohmersac4d8872024-02-23 13:55:47 -080051
52 CollisionAvoidance();
53
54 // Reports if the superstructure is collided.
55 bool IsCollided(const Status &status);
Austin Schuh027fd622024-03-01 21:26:07 -080056 // Checks if there is a collision with the intake.
Niko Sohmersac4d8872024-02-23 13:55:47 -080057 bool TurretCollided(double intake_position, double turret_position,
Austin Schuh027fd622024-03-01 21:26:07 -080058 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 Sohmersac4d8872024-02-23 13:55:47 -080062 // Checks and alters goals to make sure they're safe.
63 void UpdateGoal(const CollisionAvoidance::Status &status,
Austin Schuh027fd622024-03-01 21:26:07 -080064 double turret_goal_position, double extend_goal_position);
Niko Sohmersac4d8872024-02-23 13:55:47 -080065 // Limits if goal is in collision spots.
Austin Schuh027fd622024-03-01 21:26:07 -080066 void CalculateAvoidance(double intake_position, double turret_position,
67 double extend_position, double turret_goal,
68 double extend_goal);
Niko Sohmersac4d8872024-02-23 13:55:47 -080069
70 // Returns the goals to give to the respective control loops in
71 // superstructure.
Austin Schuh027fd622024-03-01 21:26:07 -080072 double min_extend_goal() const { return min_extend_goal_; }
73 double max_extend_goal() const { return max_extend_goal_; }
Niko Sohmersac4d8872024-02-23 13:55:47 -080074 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 Schuh027fd622024-03-01 21:26:07 -080093 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 Sohmersac4d8872024-02-23 13:55:47 -080099
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 Schuh027fd622024-03-01 21:26:07 -0800113 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 Sohmersac4d8872024-02-23 13:55:47 -0800119
120 double min_intake_pivot_goal_;
121 double max_intake_pivot_goal_;
122 double min_turret_goal_;
123 double max_turret_goal_;
Austin Schuh027fd622024-03-01 21:26:07 -0800124 double min_extend_goal_;
125 double max_extend_goal_;
Niko Sohmersac4d8872024-02-23 13:55:47 -0800126};
127
128} // namespace y2024::control_loops::superstructure
129
130#endif