blob: 1ad9582b29c647a5f65de1b08d4c77837f367316 [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
53 // TODO(sabina): set all the numbers to correctly match the robot.
54
55 // Height above which we can move the wrist freely.
56 static constexpr double kElevatorClearHeight = 0.5;
57
58 // Height above which we can move the wrist down.
59 static constexpr double kElevatorClearWristDownHeight = 0.3;
60 // Height the carriage needs to be above to move the intake.
61 static constexpr double kElevatorClearIntakeHeight = 0.4;
62
63 // Angle constraints for the wrist when below kElevatorClearDownHeight
64 static constexpr double kWristMaxAngle = M_PI / 2.0;
65 static constexpr double kWristMinAngle = -M_PI / 2.0;
66
67 // Angles outside of which the intake is fully clear of the wrist.
68 static constexpr double kIntakeOutAngle = M_PI / 6.0;
69 static constexpr double kIntakeInAngle = -M_PI / 3.0;
70
71 // Angles within which we will crash the wrist into the elevator if the
72 // elevator is below kElevatorClearHeight.
73 static constexpr double kWristElevatorCollisionMinAngle = -M_PI / 4.0;
74 static constexpr double kWristElevatorCollisionMaxAngle = M_PI / 4.0;
75
76 // Tolerance for the elevator.
77 static constexpr double kEps = 0.02;
78 // Tolerance for the intake.
79 static constexpr double kEpsIntake = 0.05;
80 // Tolerance for the wrist.
81 static constexpr double kEpsWrist = 0.05;
82
83 private:
84 void clear_min_wrist_goal() {
85 min_wrist_goal_ = -::std::numeric_limits<double>::infinity();
86 }
87 void clear_max_wrist_goal() {
88 max_wrist_goal_ = ::std::numeric_limits<double>::infinity();
89 }
90 void clear_min_elevator_goal() {
91 min_elevator_goal_ = -::std::numeric_limits<double>::infinity();
92 }
93 void clear_min_intake_goal() {
94 min_intake_goal_ = -::std::numeric_limits<double>::infinity();
95 }
96 void clear_max_intake_goal() {
97 max_intake_goal_ = ::std::numeric_limits<double>::infinity();
98 }
99
100 double min_wrist_goal_;
101 double max_wrist_goal_;
102 double min_elevator_goal_;
103 double min_intake_goal_;
104 double max_intake_goal_;
105};
106
107} // namespace superstructure
108} // namespace control_loops
109} // namespace y2019
110
111#endif // Y2019_CONTROL_LOOPS_SUPERSTRUCTURE_COLLISION_AVOIDANCE_H_