blob: fe360906f643d57006f65f7787eb7045ea787ec9 [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;
26
27 bool operator==(const Status &s) const {
28 return (intake_pivot_position == s.intake_pivot_position &&
29 turret_position == s.turret_position);
30 }
31 bool operator!=(const Status &s) const { return !(*this == s); }
32 };
33
Niko Sohmersac4d8872024-02-23 13:55:47 -080034 // For the turret, 0 rad is pointing straight forwards
Maxwell Henderson3d68e142024-02-25 09:58:11 -080035 static constexpr double kMinCollisionZoneTurret = 0.15;
36 static constexpr double kMaxCollisionZoneTurret = 1.15;
Niko Sohmersac4d8872024-02-23 13:55:47 -080037
38 // Maximum position of the intake to avoid collisions
39 static constexpr double kCollisionZoneIntake = 1.6;
40
41 // Tolerances for the subsystems
42 static constexpr double kEpsTurret = 0.05;
43 static constexpr double kEpsIntake = 0.05;
44
45 CollisionAvoidance();
46
47 // Reports if the superstructure is collided.
48 bool IsCollided(const Status &status);
49 // Checks if there is a collision on either intake.
50 bool TurretCollided(double intake_position, double turret_position,
51 double min_turret_collision_position,
52 double max_turret_collision_position);
53 // Checks and alters goals to make sure they're safe.
54 void UpdateGoal(const CollisionAvoidance::Status &status,
55 const double &turret_goal_position);
56 // Limits if goal is in collision spots.
57 void CalculateAvoidance(bool intake_pivot, double intake_position,
58 double turret_position, double turret_goal,
59 double min_turret_collision_goal,
60 double max_turret_collision_goal);
61
62 // Returns the goals to give to the respective control loops in
63 // superstructure.
64 double min_turret_goal() const { return min_turret_goal_; }
65 double max_turret_goal() const { return max_turret_goal_; }
66 double min_intake_pivot_goal() const { return min_intake_pivot_goal_; }
67 double max_intake_pivot_goal() const { return max_intake_pivot_goal_; }
68
69 void update_max_turret_goal(double max_turret_goal) {
70 max_turret_goal_ = ::std::min(max_turret_goal, max_turret_goal_);
71 }
72 void update_min_turret_goal(double min_turret_goal) {
73 min_turret_goal_ = ::std::max(min_turret_goal, min_turret_goal_);
74 }
75 void update_max_intake_pivot_goal(double max_intake_pivot_goal) {
76 max_intake_pivot_goal_ =
77 ::std::min(max_intake_pivot_goal, max_intake_pivot_goal_);
78 }
79 void update_min_intake_pivot_goal(double min_intake_pivot_goal) {
80 min_intake_pivot_goal_ =
81 ::std::max(min_intake_pivot_goal, min_intake_pivot_goal_);
82 }
83
84 private:
85 void clear_min_intake_pivot_goal() {
86 min_intake_pivot_goal_ = -::std::numeric_limits<double>::infinity();
87 }
88 void clear_max_intake_pivot_goal() {
89 max_intake_pivot_goal_ = ::std::numeric_limits<double>::infinity();
90 }
91 void clear_min_turret_goal() {
92 min_turret_goal_ = -::std::numeric_limits<double>::infinity();
93 }
94 void clear_max_turret_goal() {
95 max_turret_goal_ = ::std::numeric_limits<double>::infinity();
96 }
97
98 double min_intake_pivot_goal_;
99 double max_intake_pivot_goal_;
100 double min_turret_goal_;
101 double max_turret_goal_;
102};
103
104} // namespace y2024::control_loops::superstructure
105
106#endif