blob: 8fd14d7fcaec380ac6ee81e12edfcaf845bfd041 [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
34 // Reference angles between which the turret will be careful
35 static constexpr double kCollisionZoneTurret = M_PI * 7.0 / 18.0;
36
37 // For the turret, 0 rad is pointing straight forwards
38 static constexpr double kMinCollisionZoneTurret = 0.07;
39 static constexpr double kMaxCollisionZoneTurret = 2.1;
40
41 // Maximum position of the intake to avoid collisions
42 static constexpr double kCollisionZoneIntake = 1.6;
43
44 // Tolerances for the subsystems
45 static constexpr double kEpsTurret = 0.05;
46 static constexpr double kEpsIntake = 0.05;
47
48 CollisionAvoidance();
49
50 // Reports if the superstructure is collided.
51 bool IsCollided(const Status &status);
52 // Checks if there is a collision on either intake.
53 bool TurretCollided(double intake_position, double turret_position,
54 double min_turret_collision_position,
55 double max_turret_collision_position);
56 // Checks and alters goals to make sure they're safe.
57 void UpdateGoal(const CollisionAvoidance::Status &status,
58 const double &turret_goal_position);
59 // Limits if goal is in collision spots.
60 void CalculateAvoidance(bool intake_pivot, double intake_position,
61 double turret_position, double turret_goal,
62 double min_turret_collision_goal,
63 double max_turret_collision_goal);
64
65 // Returns the goals to give to the respective control loops in
66 // superstructure.
67 double min_turret_goal() const { return min_turret_goal_; }
68 double max_turret_goal() const { return max_turret_goal_; }
69 double min_intake_pivot_goal() const { return min_intake_pivot_goal_; }
70 double max_intake_pivot_goal() const { return max_intake_pivot_goal_; }
71
72 void update_max_turret_goal(double max_turret_goal) {
73 max_turret_goal_ = ::std::min(max_turret_goal, max_turret_goal_);
74 }
75 void update_min_turret_goal(double min_turret_goal) {
76 min_turret_goal_ = ::std::max(min_turret_goal, min_turret_goal_);
77 }
78 void update_max_intake_pivot_goal(double max_intake_pivot_goal) {
79 max_intake_pivot_goal_ =
80 ::std::min(max_intake_pivot_goal, max_intake_pivot_goal_);
81 }
82 void update_min_intake_pivot_goal(double min_intake_pivot_goal) {
83 min_intake_pivot_goal_ =
84 ::std::max(min_intake_pivot_goal, min_intake_pivot_goal_);
85 }
86
87 private:
88 void clear_min_intake_pivot_goal() {
89 min_intake_pivot_goal_ = -::std::numeric_limits<double>::infinity();
90 }
91 void clear_max_intake_pivot_goal() {
92 max_intake_pivot_goal_ = ::std::numeric_limits<double>::infinity();
93 }
94 void clear_min_turret_goal() {
95 min_turret_goal_ = -::std::numeric_limits<double>::infinity();
96 }
97 void clear_max_turret_goal() {
98 max_turret_goal_ = ::std::numeric_limits<double>::infinity();
99 }
100
101 double min_intake_pivot_goal_;
102 double max_intake_pivot_goal_;
103 double min_turret_goal_;
104 double max_turret_goal_;
105};
106
107} // namespace y2024::control_loops::superstructure
108
109#endif