blob: d22e885dc84b30c067af6bdde19248c571c995a0 [file] [log] [blame]
Comran Morshed25f81a02016-01-23 13:40:10 +00001#ifndef Y2016_CONTROL_LOOPS_SUPERSTRUCTURE_SUPERSTRUCTURE_H_
2#define Y2016_CONTROL_LOOPS_SUPERSTRUCTURE_SUPERSTRUCTURE_H_
3
4#include <memory>
5
John Park33858a32018-09-28 23:05:48 -07006#include "aos/util/trapezoid_profile.h"
Philipp Schrader790cb542023-07-05 21:06:52 -07007#include "frc971/control_loops/control_loop.h"
Austin Schuh10c2d112016-02-14 13:42:28 -08008#include "frc971/control_loops/state_feedback_loop.h"
Austin Schuh2fc10fa2016-02-08 00:44:34 -08009#include "frc971/zeroing/zeroing.h"
Austin Schuh10c2d112016-02-14 13:42:28 -080010#include "y2016/control_loops/superstructure/superstructure_controls.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070011#include "y2016/control_loops/superstructure/superstructure_goal_generated.h"
12#include "y2016/control_loops/superstructure/superstructure_output_generated.h"
13#include "y2016/control_loops/superstructure/superstructure_position_generated.h"
14#include "y2016/control_loops/superstructure/superstructure_status_generated.h"
15#include "y2016/queues/ball_detector_generated.h"
Comran Morshed25f81a02016-01-23 13:40:10 +000016
17namespace y2016 {
18namespace control_loops {
Austin Schuh2fc10fa2016-02-08 00:44:34 -080019namespace superstructure {
20namespace testing {
Philipp Schrader0119eb12016-02-15 18:16:21 +000021class SuperstructureTest_RespectsRange_Test;
Austin Schuh2fc10fa2016-02-08 00:44:34 -080022class SuperstructureTest_DisabledGoalTest_Test;
Diana Vandenberge2843c62016-02-13 17:44:20 -080023class SuperstructureTest_ArmZeroingErrorTest_Test;
24class SuperstructureTest_IntakeZeroingErrorTest_Test;
Philipp Schrader0119eb12016-02-15 18:16:21 +000025class SuperstructureTest_UpperHardstopStartup_Test;
Campbell Crowley152c7cf2016-02-14 21:20:50 -080026class SuperstructureTest_DisabledWhileZeroingHigh_Test;
27class SuperstructureTest_DisabledWhileZeroingLow_Test;
Austin Schuh2fc10fa2016-02-08 00:44:34 -080028} // namespace testing
29
Philipp Schrader0119eb12016-02-15 18:16:21 +000030// Helper class to prevent parts from crashing into each other. The parts in
31// question here are: the frame, the arm (plus shooter), and the intake.
32// Assumptions:
33// - The shoulder, the wrist, and intake are horizontal when at angle 0.
34// - The arm (i.e. shoulder) and shooter (i.e. wrist) are stored when they are
35// both at zero degrees.
36// - The intake at angle 0 is in a position to help get a ball in the robot.
37// - The intake at angle PI is in a "stowed" position. In other words, it is
38// folded over the arm and shooter when they are also in a stowed position.
39// - The shooter must remain horizontal when the arm is folding into the robot.
40// Otherwise, the shooter will collide with the frame.
41// - The arm has priority over the intake. If the arm wants to move in such a
42// way that interferes with the intake's movement then the intake must move
43// out of the way.
44class CollisionAvoidance {
45 public:
46 // Set up collision avoidance for an arm and intake.
47 CollisionAvoidance(Intake *intake, Arm *arm) : intake_(intake), arm_(arm) {}
48
49 // This function accepts goals for the intake and the arm and modifies them
50 // in such a way that collisions between all the different parts of the robot
51 // are avoided. The modified goals are then sent to the arm and intake as
52 // unprofiled goals.
53 void UpdateGoal(double shoulder_angle_goal, double wrist_angle_goal,
54 double intake_angle_goal);
55
Philipp Schrader07147532016-02-16 01:23:07 +000056 // Returns true if any of the limbs and frame are somehow currently
57 // interfering with one another. This is based purely on the angles that the
58 // limbs are reporting.
59 bool collided() const;
60
61 // Detects collision with the specified angles. This is especially useful for
62 // unit testing where we have proper ground truth for all the angles.
63 static bool collided_with_given_angles(double shoulder_angle,
64 double wrist_angle,
65 double intake_angle);
66
Philipp Schrader0119eb12016-02-15 18:16:21 +000067 // The shoulder angle (in radians) below which the shooter must be in a
68 // stowing position. In other words the wrist must be at angle zero if the
69 // shoulder is below this angle.
Austin Schuhb1d682b2016-02-16 13:07:44 -080070 static constexpr double kMinShoulderAngleForHorizontalShooter = 0.6;
Philipp Schrader0119eb12016-02-15 18:16:21 +000071
Campbell Crowley1836a432016-03-05 15:16:51 -080072 // The shoulder angle (in radians) below which the arm and the shooter have
73 // the potential to interfere with the intake.
Austin Schuh6ca0f792016-03-12 14:06:14 -080074 static constexpr double kMinShoulderAngleForIntakeUpInterference = 1.3;
75
76 // The shoulder angle (in radians) below which the shooter should be closer to
77 // level to fix the inverted case.
78 // TODO(austin): Verify
79 static constexpr double kMinShoulderAngleForIntakeInterference = 1.1;
Philipp Schrader0119eb12016-02-15 18:16:21 +000080
81 // The intake angle (in radians) above which the intake can interfere (i.e.
82 // collide) with the arm and/or shooter.
Austin Schuh2c717862016-03-13 15:32:53 -070083 static constexpr double kMaxIntakeAngleBeforeArmInterference = 1.05;
Philipp Schrader0119eb12016-02-15 18:16:21 +000084
85 // The maximum absolute angle (in radians) that the wrist must be below in
86 // order for the shouler to be allowed to move below
87 // kMinShoulderAngleForHorizontalShooter. In other words, only allow the arm
88 // to move down into the belly pan if the shooter is horizontal, ready to
89 // also be placed into the belly pan.
Austin Schuh2f5bfc82016-02-27 14:47:37 -080090 static constexpr double kMaxWristAngleForSafeArmStowing = 0.05;
Philipp Schrader0119eb12016-02-15 18:16:21 +000091
Austin Schuh2c717862016-03-13 15:32:53 -070092 // The maximum angle in radians that the wrist can be from horizontal
Austin Schuh6ca0f792016-03-12 14:06:14 -080093 // while it is near the intake.
Austin Schuh6802a9d2016-03-12 21:34:53 -080094 static constexpr double kMaxWristAngleForMovingByIntake = 0.50;
Austin Schuh2c717862016-03-13 15:32:53 -070095 // The minimum angle in radians that the wrist can be from horizontal
96 // while it is near the intake.
Austin Schuh214164b2016-03-20 16:50:09 -070097 static constexpr double kMinWristAngleForMovingByIntake = -1.50;
Austin Schuh6ca0f792016-03-12 14:06:14 -080098
Philipp Schrader0119eb12016-02-15 18:16:21 +000099 // The shoulder angle (in radians) below which the intake can safely move
100 // into the collision zone. This is necessary when the robot wants to fold up
101 // completely (i.e. stow the arm, shooter, and intake).
Austin Schuh6ca0f792016-03-12 14:06:14 -0800102 static constexpr double kMaxShoulderAngleUntilSafeIntakeStowing = 0.19;
Philipp Schrader0119eb12016-02-15 18:16:21 +0000103
104 private:
105 Intake *intake_;
106 Arm *arm_;
107};
108
Comran Morshed25f81a02016-01-23 13:40:10 +0000109class Superstructure
James Kuszmaul61750662021-06-21 21:32:33 -0700110 : public ::frc971::controls::ControlLoop<Goal, Position, Status, Output> {
Comran Morshed25f81a02016-01-23 13:40:10 +0000111 public:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700112 explicit Superstructure(::aos::EventLoop *event_loop,
113 const ::std::string &name = "/superstructure");
Adam Snaider06779722016-02-14 15:26:22 -0800114
Austin Schuhf132dc52016-03-13 19:43:15 -0700115 static constexpr double kZeroingVoltage = 6.0;
Austin Schuhfef64ac2016-04-24 19:08:01 -0700116 static constexpr double kShooterHangingVoltage = 6.0;
Austin Schuh0c001a82016-05-01 12:30:09 -0700117 static constexpr double kShooterHangingLowVoltage = 2.0;
Philipp Schrader3da48ac2016-03-06 23:03:44 +0000118 static constexpr double kOperatingVoltage = 12.0;
Austin Schuh2c717862016-03-13 15:32:53 -0700119 static constexpr double kLandingShoulderDownVoltage = -1.5;
Philipp Schrader3da48ac2016-03-06 23:03:44 +0000120
Adam Snaider06779722016-02-14 15:26:22 -0800121 // This is the angle above which we will do a HIGH_ARM_ZERO, and below which
122 // we will do a LOW_ARM_ZERO.
123 static constexpr double kShoulderMiddleAngle = M_PI / 4.0;
124 // This is the large scale movement tolerance.
125 static constexpr double kLooseTolerance = 0.05;
126
127 // This is the small scale movement tolerance.
Austin Schuhf132dc52016-03-13 19:43:15 -0700128 static constexpr double kTightTolerance = 0.03;
Adam Snaider06779722016-02-14 15:26:22 -0800129
130 // This is the angle such that the intake will clear the arm when the shooter
131 // is level.
Austin Schuh6ca0f792016-03-12 14:06:14 -0800132 static constexpr double kIntakeUpperClear =
133 CollisionAvoidance::kMaxIntakeAngleBeforeArmInterference;
Adam Snaider06779722016-02-14 15:26:22 -0800134 // This is the angle such that the intake will clear the arm when the shooter
135 // is at almost any position.
Austin Schuh2f5bfc82016-02-27 14:47:37 -0800136 static constexpr double kIntakeLowerClear = 0.4;
Adam Snaider06779722016-02-14 15:26:22 -0800137
138 // This is the angle that the shoulder will go to when doing the
139 // HIGH_ARM_ZERO.
140 static constexpr double kShoulderUpAngle = M_PI / 2.0;
141
142 // This is the angle that the shoulder will go down to when landing in the
143 // bellypan.
144 static constexpr double kShoulderLanded = -0.02;
145
Austin Schuhb1d682b2016-02-16 13:07:44 -0800146 // This is the angle below which the shoulder will slowly profile down and
147 // land.
148 static constexpr double kShoulderTransitionToLanded = 0.10;
149
Adam Snaider06779722016-02-14 15:26:22 -0800150 // This is the angle below which we consider the wrist close enough to level
151 // that we should move it to level before doing anything.
152 static constexpr double kWristAlmostLevel = 0.10;
153
154 // This is the angle that the shoulder will go down to when raising up before
155 // leveling the shooter for calibration.
Austin Schuhb1d682b2016-02-16 13:07:44 -0800156 static constexpr double kShoulderWristClearAngle =
157 CollisionAvoidance::kMinShoulderAngleForHorizontalShooter;
Adam Snaider06779722016-02-14 15:26:22 -0800158
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800159 enum State {
Adam Snaider06779722016-02-14 15:26:22 -0800160 // Wait for all the filters to be ready before starting the initialization
161 // process.
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800162 UNINITIALIZED = 0,
Adam Snaider06779722016-02-14 15:26:22 -0800163
164 // We now are ready to decide how to zero. Decide what to do once we are
165 // enabled.
166 DISABLED_INITIALIZED = 1,
167
168 // Lift the arm up out of the way.
169 HIGH_ARM_ZERO_LIFT_ARM = 2,
170
171 HIGH_ARM_ZERO_LEVEL_SHOOTER = 3,
172
173 HIGH_ARM_ZERO_MOVE_INTAKE_OUT = 4,
174
175 HIGH_ARM_ZERO_LOWER_ARM = 6,
176
177 LOW_ARM_ZERO_LOWER_INTAKE = 7,
178 LOW_ARM_ZERO_MAYBE_LEVEL_SHOOTER = 8,
179 LOW_ARM_ZERO_LIFT_SHOULDER = 9,
180 LOW_ARM_ZERO_LEVEL_SHOOTER = 11,
181 // Run, but limit power to zeroing voltages.
182 SLOW_RUNNING = 12,
183 // Run with full power.
184 RUNNING = 13,
Austin Schuhb1d682b2016-02-16 13:07:44 -0800185 // Run, but limit power to zeroing voltages while landing.
186 LANDING_SLOW_RUNNING = 14,
187 // Run with full power while landing.
188 LANDING_RUNNING = 15,
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800189 // Internal error caused the superstructure to abort.
Austin Schuhb1d682b2016-02-16 13:07:44 -0800190 ESTOP = 16,
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800191 };
192
Austin Schuh9f4e8a72016-02-16 15:28:47 -0800193 bool IsRunning() const {
194 return (state_ == SLOW_RUNNING || state_ == RUNNING ||
195 state_ == LANDING_SLOW_RUNNING || state_ == LANDING_RUNNING);
196 }
197
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800198 State state() const { return state_; }
Comran Morshed25f81a02016-01-23 13:40:10 +0000199
Adam Snaider06779722016-02-14 15:26:22 -0800200 // Returns the value to move the joint to such that it will stay below
201 // reference_angle starting at current_angle, but move at least move_distance
202 static double MoveButKeepBelow(double reference_angle, double current_angle,
203 double move_distance);
204 // Returns the value to move the joint to such that it will stay above
205 // reference_angle starting at current_angle, but move at least move_distance
206 static double MoveButKeepAbove(double reference_angle, double current_angle,
207 double move_distance);
208
Philipp Schrader07147532016-02-16 01:23:07 +0000209 // Returns true if anything is currently considered "collided".
210 bool collided() const { return collision_avoidance_.collided(); }
Philipp Schrader0119eb12016-02-15 18:16:21 +0000211
Comran Morshed25f81a02016-01-23 13:40:10 +0000212 protected:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700213 virtual void RunIteration(const Goal *unsafe_goal, const Position *position,
214 aos::Sender<Output>::Builder *output,
215 aos::Sender<Status>::Builder *status) override;
Comran Morshed25f81a02016-01-23 13:40:10 +0000216
217 private:
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800218 friend class testing::SuperstructureTest_DisabledGoalTest_Test;
Diana Vandenberge2843c62016-02-13 17:44:20 -0800219 friend class testing::SuperstructureTest_ArmZeroingErrorTest_Test;
220 friend class testing::SuperstructureTest_IntakeZeroingErrorTest_Test;
Philipp Schrader0119eb12016-02-15 18:16:21 +0000221 friend class testing::SuperstructureTest_RespectsRange_Test;
222 friend class testing::SuperstructureTest_UpperHardstopStartup_Test;
Campbell Crowley152c7cf2016-02-14 21:20:50 -0800223 friend class testing::SuperstructureTest_DisabledWhileZeroingHigh_Test;
224 friend class testing::SuperstructureTest_DisabledWhileZeroingLow_Test;
Austin Schuh4b652c92019-05-27 13:22:27 -0700225
226 ::aos::Fetcher<::y2016::sensors::BallDetector> ball_detector_fetcher_;
227
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800228 Intake intake_;
229 Arm arm_;
230
Philipp Schrader0119eb12016-02-15 18:16:21 +0000231 CollisionAvoidance collision_avoidance_;
232
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800233 State state_ = UNINITIALIZED;
234 State last_state_ = UNINITIALIZED;
235
Austin Schuhbe041152016-02-28 22:01:52 -0800236 float last_shoulder_angle_ = 0.0;
237 float last_wrist_angle_ = 0.0;
238 float last_intake_angle_ = 0.0;
239
Comran Morshed71466fe2016-04-21 20:21:14 -0700240 double kill_shoulder_accumulator_ = 0.0;
241 bool kill_shoulder_ = false;
242
Adam Snaider06779722016-02-14 15:26:22 -0800243 // Returns true if the profile has finished, and the joint is within the
244 // specified tolerance.
245 bool IsArmNear(double tolerance);
246 bool IsArmNear(double shoulder_tolerance, double wrist_tolerance);
247 bool IsIntakeNear(double tolerance);
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800248
Comran Morshed25f81a02016-01-23 13:40:10 +0000249 DISALLOW_COPY_AND_ASSIGN(Superstructure);
250};
251
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800252} // namespace superstructure
Comran Morshed25f81a02016-01-23 13:40:10 +0000253} // namespace control_loops
254} // namespace y2016
255
256#endif // Y2016_CONTROL_LOOPS_SUPERSTRUCTURE_SUPERSTRUCTURE_H_