blob: ca29e7c1861e34b143445afdcfb5de60e21ff2b9 [file] [log] [blame]
Sabina Davis4b63ae52019-01-27 16:15:25 -08001#ifndef Y2019_CONTROL_LOOPS_SUPERSTRUCTURE_COLLISION_AVOIDANCE_H_
2#define Y2019_CONTROL_LOOPS_SUPERSTRUCTURE_COLLISION_AVOIDANCE_H_
3
4#include <cmath>
5#include "aos/controls/control_loops.q.h"
6#include "frc971/constants.h"
7#include "y2019/control_loops/superstructure/superstructure.q.h"
8
9namespace y2019 {
10namespace control_loops {
11namespace superstructure {
12
13// CollisionAvoidance computes the min and max allowable ranges for various
14// subsystems to avoid collisions. It also shoves the elevator up to let the
15// intake go in and out, and to let the wrist switch sides.
16class CollisionAvoidance {
17 public:
18 CollisionAvoidance();
19
20 // Reports if the superstructure is collided.
21 bool IsCollided(const SuperstructureQueue::Status *status);
22
23 // Checks and alters goals to make sure they're safe.
24 // TODO(austin): Either we will have a unit delay because this has to happen
25 // after the controls, or we need to be more clever about restructuring.
26 void UpdateGoal(const SuperstructureQueue::Status *status,
27 const SuperstructureQueue::Goal *unsafe_goal);
28
29 // Returns the goals to give to the respective control loops in
30 // superstructure.
31 double min_wrist_goal() const { return min_wrist_goal_; }
32 double max_wrist_goal() const { return max_wrist_goal_; }
33 double min_elevator_goal() const { return min_elevator_goal_; }
34 double min_intake_goal() const { return min_intake_goal_; }
35 double max_intake_goal() const { return max_intake_goal_; }
36
37 void update_max_wrist_goal(double max_wrist_goal) {
38 max_wrist_goal_ = ::std::min(max_wrist_goal, max_wrist_goal_);
39 }
40 void update_min_wrist_goal(double min_wrist_goal) {
41 min_wrist_goal_ = ::std::max(min_wrist_goal, min_wrist_goal_);
42 }
43 void update_max_intake_goal(double max_intake_goal) {
44 max_intake_goal_ = ::std::min(max_intake_goal, max_intake_goal_);
45 }
46 void update_min_intake_goal(double min_intake_goal) {
47 min_intake_goal_ = ::std::max(min_intake_goal, min_intake_goal_);
48 }
49 void update_min_elevator_goal(double min_elevator_goal) {
50 min_elevator_goal_ = ::std::max(min_elevator_goal, min_elevator_goal_);
51 }
52
Sabina Davis4b63ae52019-01-27 16:15:25 -080053 // Height above which we can move the wrist freely.
Austin Schuhed7f8632019-02-15 23:12:20 -080054 static constexpr double kElevatorClearHeight = 0.35;
Sabina Davis4b63ae52019-01-27 16:15:25 -080055
56 // Height above which we can move the wrist down.
Austin Schuhed7f8632019-02-15 23:12:20 -080057 static constexpr double kElevatorClearWristDownHeight = 0.25;
Sabina Davis4b63ae52019-01-27 16:15:25 -080058 // Height the carriage needs to be above to move the intake.
59 static constexpr double kElevatorClearIntakeHeight = 0.4;
60
61 // Angle constraints for the wrist when below kElevatorClearDownHeight
Austin Schuh0158e6a2019-02-17 15:02:26 -080062 static constexpr double kWristMaxAngle = M_PI / 2.0 + 0.05;
63 static constexpr double kWristMinAngle = -M_PI / 2.0 - 0.05;
Sabina Davis4b63ae52019-01-27 16:15:25 -080064
65 // Angles outside of which the intake is fully clear of the wrist.
Austin Schuhed7f8632019-02-15 23:12:20 -080066 static constexpr double kIntakeOutAngle = 0.3;
67 static constexpr double kIntakeInAngle = -1.1;
Sabina Davis4b63ae52019-01-27 16:15:25 -080068
69 // Angles within which we will crash the wrist into the elevator if the
70 // elevator is below kElevatorClearHeight.
71 static constexpr double kWristElevatorCollisionMinAngle = -M_PI / 4.0;
72 static constexpr double kWristElevatorCollisionMaxAngle = M_PI / 4.0;
73
74 // Tolerance for the elevator.
75 static constexpr double kEps = 0.02;
76 // Tolerance for the intake.
77 static constexpr double kEpsIntake = 0.05;
78 // Tolerance for the wrist.
79 static constexpr double kEpsWrist = 0.05;
80
81 private:
82 void clear_min_wrist_goal() {
83 min_wrist_goal_ = -::std::numeric_limits<double>::infinity();
84 }
85 void clear_max_wrist_goal() {
86 max_wrist_goal_ = ::std::numeric_limits<double>::infinity();
87 }
88 void clear_min_elevator_goal() {
89 min_elevator_goal_ = -::std::numeric_limits<double>::infinity();
90 }
91 void clear_min_intake_goal() {
92 min_intake_goal_ = -::std::numeric_limits<double>::infinity();
93 }
94 void clear_max_intake_goal() {
95 max_intake_goal_ = ::std::numeric_limits<double>::infinity();
96 }
97
98 double min_wrist_goal_;
99 double max_wrist_goal_;
100 double min_elevator_goal_;
101 double min_intake_goal_;
102 double max_intake_goal_;
103};
104
105} // namespace superstructure
106} // namespace control_loops
107} // namespace y2019
108
109#endif // Y2019_CONTROL_LOOPS_SUPERSTRUCTURE_COLLISION_AVOIDANCE_H_