blob: 17a15478a9cdc053ed7cdbe0ebbfcd1cc1155b27 [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/controls/control_loop.h"
7#include "aos/util/trapezoid_profile.h"
Austin Schuh10c2d112016-02-14 13:42:28 -08008#include "frc971/control_loops/state_feedback_loop.h"
Comran Morshed25f81a02016-01-23 13:40:10 +00009
Austin Schuh2fc10fa2016-02-08 00:44:34 -080010#include "frc971/zeroing/zeroing.h"
Comran Morshed25f81a02016-01-23 13:40:10 +000011#include "y2016/control_loops/superstructure/superstructure.q.h"
Austin Schuh10c2d112016-02-14 13:42:28 -080012#include "y2016/control_loops/superstructure/superstructure_controls.h"
Comran Morshed25f81a02016-01-23 13:40:10 +000013
14namespace y2016 {
15namespace control_loops {
Austin Schuh2fc10fa2016-02-08 00:44:34 -080016namespace superstructure {
17namespace testing {
Philipp Schrader0119eb12016-02-15 18:16:21 +000018class SuperstructureTest_RespectsRange_Test;
Austin Schuh2fc10fa2016-02-08 00:44:34 -080019class SuperstructureTest_DisabledGoalTest_Test;
Diana Vandenberge2843c62016-02-13 17:44:20 -080020class SuperstructureTest_ArmZeroingErrorTest_Test;
21class SuperstructureTest_IntakeZeroingErrorTest_Test;
Philipp Schrader0119eb12016-02-15 18:16:21 +000022class SuperstructureTest_UpperHardstopStartup_Test;
Campbell Crowley152c7cf2016-02-14 21:20:50 -080023class SuperstructureTest_DisabledWhileZeroingHigh_Test;
24class SuperstructureTest_DisabledWhileZeroingLow_Test;
Austin Schuh2fc10fa2016-02-08 00:44:34 -080025} // namespace testing
26
Philipp Schrader0119eb12016-02-15 18:16:21 +000027// Helper class to prevent parts from crashing into each other. The parts in
28// question here are: the frame, the arm (plus shooter), and the intake.
29// Assumptions:
30// - The shoulder, the wrist, and intake are horizontal when at angle 0.
31// - The arm (i.e. shoulder) and shooter (i.e. wrist) are stored when they are
32// both at zero degrees.
33// - The intake at angle 0 is in a position to help get a ball in the robot.
34// - The intake at angle PI is in a "stowed" position. In other words, it is
35// folded over the arm and shooter when they are also in a stowed position.
36// - The shooter must remain horizontal when the arm is folding into the robot.
37// Otherwise, the shooter will collide with the frame.
38// - The arm has priority over the intake. If the arm wants to move in such a
39// way that interferes with the intake's movement then the intake must move
40// out of the way.
41class CollisionAvoidance {
42 public:
43 // Set up collision avoidance for an arm and intake.
44 CollisionAvoidance(Intake *intake, Arm *arm) : intake_(intake), arm_(arm) {}
45
46 // This function accepts goals for the intake and the arm and modifies them
47 // in such a way that collisions between all the different parts of the robot
48 // are avoided. The modified goals are then sent to the arm and intake as
49 // unprofiled goals.
50 void UpdateGoal(double shoulder_angle_goal, double wrist_angle_goal,
51 double intake_angle_goal);
52
Philipp Schrader07147532016-02-16 01:23:07 +000053 // Returns true if any of the limbs and frame are somehow currently
54 // interfering with one another. This is based purely on the angles that the
55 // limbs are reporting.
56 bool collided() const;
57
58 // Detects collision with the specified angles. This is especially useful for
59 // unit testing where we have proper ground truth for all the angles.
60 static bool collided_with_given_angles(double shoulder_angle,
61 double wrist_angle,
62 double intake_angle);
63
Philipp Schrader0119eb12016-02-15 18:16:21 +000064 // The shoulder angle (in radians) below which the shooter must be in a
65 // stowing position. In other words the wrist must be at angle zero if the
66 // shoulder is below this angle.
Austin Schuhb1d682b2016-02-16 13:07:44 -080067 static constexpr double kMinShoulderAngleForHorizontalShooter = 0.6;
Philipp Schrader0119eb12016-02-15 18:16:21 +000068
Campbell Crowley1836a432016-03-05 15:16:51 -080069 // The shoulder angle (in radians) below which the arm and the shooter have
70 // the potential to interfere with the intake.
Austin Schuh6ca0f792016-03-12 14:06:14 -080071 static constexpr double kMinShoulderAngleForIntakeUpInterference = 1.3;
72
73 // The shoulder angle (in radians) below which the shooter should be closer to
74 // level to fix the inverted case.
75 // TODO(austin): Verify
76 static constexpr double kMinShoulderAngleForIntakeInterference = 1.1;
Philipp Schrader0119eb12016-02-15 18:16:21 +000077
78 // The intake angle (in radians) above which the intake can interfere (i.e.
79 // collide) with the arm and/or shooter.
Austin Schuh2c717862016-03-13 15:32:53 -070080 static constexpr double kMaxIntakeAngleBeforeArmInterference = 1.05;
Philipp Schrader0119eb12016-02-15 18:16:21 +000081
82 // The maximum absolute angle (in radians) that the wrist must be below in
83 // order for the shouler to be allowed to move below
84 // kMinShoulderAngleForHorizontalShooter. In other words, only allow the arm
85 // to move down into the belly pan if the shooter is horizontal, ready to
86 // also be placed into the belly pan.
Austin Schuh2f5bfc82016-02-27 14:47:37 -080087 static constexpr double kMaxWristAngleForSafeArmStowing = 0.05;
Philipp Schrader0119eb12016-02-15 18:16:21 +000088
Austin Schuh2c717862016-03-13 15:32:53 -070089 // The maximum angle in radians that the wrist can be from horizontal
Austin Schuh6ca0f792016-03-12 14:06:14 -080090 // while it is near the intake.
Austin Schuh6802a9d2016-03-12 21:34:53 -080091 static constexpr double kMaxWristAngleForMovingByIntake = 0.50;
Austin Schuh2c717862016-03-13 15:32:53 -070092 // The minimum angle in radians that the wrist can be from horizontal
93 // while it is near the intake.
Austin Schuh214164b2016-03-20 16:50:09 -070094 static constexpr double kMinWristAngleForMovingByIntake = -1.50;
Austin Schuh6ca0f792016-03-12 14:06:14 -080095
Philipp Schrader0119eb12016-02-15 18:16:21 +000096 // The shoulder angle (in radians) below which the intake can safely move
97 // into the collision zone. This is necessary when the robot wants to fold up
98 // completely (i.e. stow the arm, shooter, and intake).
Austin Schuh6ca0f792016-03-12 14:06:14 -080099 static constexpr double kMaxShoulderAngleUntilSafeIntakeStowing = 0.19;
Philipp Schrader0119eb12016-02-15 18:16:21 +0000100
101 private:
102 Intake *intake_;
103 Arm *arm_;
104};
105
Comran Morshed25f81a02016-01-23 13:40:10 +0000106class Superstructure
107 : public ::aos::controls::ControlLoop<control_loops::SuperstructureQueue> {
108 public:
109 explicit Superstructure(
110 control_loops::SuperstructureQueue *my_superstructure =
111 &control_loops::superstructure_queue);
Adam Snaider06779722016-02-14 15:26:22 -0800112
Austin Schuhf132dc52016-03-13 19:43:15 -0700113 static constexpr double kZeroingVoltage = 6.0;
Austin Schuhfef64ac2016-04-24 19:08:01 -0700114 static constexpr double kShooterHangingVoltage = 6.0;
Austin Schuh0c001a82016-05-01 12:30:09 -0700115 static constexpr double kShooterHangingLowVoltage = 2.0;
Philipp Schrader3da48ac2016-03-06 23:03:44 +0000116 static constexpr double kOperatingVoltage = 12.0;
Austin Schuh2c717862016-03-13 15:32:53 -0700117 static constexpr double kLandingShoulderDownVoltage = -1.5;
Philipp Schrader3da48ac2016-03-06 23:03:44 +0000118
Adam Snaider06779722016-02-14 15:26:22 -0800119 // This is the angle above which we will do a HIGH_ARM_ZERO, and below which
120 // we will do a LOW_ARM_ZERO.
121 static constexpr double kShoulderMiddleAngle = M_PI / 4.0;
122 // This is the large scale movement tolerance.
123 static constexpr double kLooseTolerance = 0.05;
124
125 // This is the small scale movement tolerance.
Austin Schuhf132dc52016-03-13 19:43:15 -0700126 static constexpr double kTightTolerance = 0.03;
Adam Snaider06779722016-02-14 15:26:22 -0800127
128 // This is the angle such that the intake will clear the arm when the shooter
129 // is level.
Austin Schuh6ca0f792016-03-12 14:06:14 -0800130 static constexpr double kIntakeUpperClear =
131 CollisionAvoidance::kMaxIntakeAngleBeforeArmInterference;
Adam Snaider06779722016-02-14 15:26:22 -0800132 // This is the angle such that the intake will clear the arm when the shooter
133 // is at almost any position.
Austin Schuh2f5bfc82016-02-27 14:47:37 -0800134 static constexpr double kIntakeLowerClear = 0.4;
Adam Snaider06779722016-02-14 15:26:22 -0800135
136 // This is the angle that the shoulder will go to when doing the
137 // HIGH_ARM_ZERO.
138 static constexpr double kShoulderUpAngle = M_PI / 2.0;
139
140 // This is the angle that the shoulder will go down to when landing in the
141 // bellypan.
142 static constexpr double kShoulderLanded = -0.02;
143
Austin Schuhb1d682b2016-02-16 13:07:44 -0800144 // This is the angle below which the shoulder will slowly profile down and
145 // land.
146 static constexpr double kShoulderTransitionToLanded = 0.10;
147
Adam Snaider06779722016-02-14 15:26:22 -0800148 // This is the angle below which we consider the wrist close enough to level
149 // that we should move it to level before doing anything.
150 static constexpr double kWristAlmostLevel = 0.10;
151
152 // This is the angle that the shoulder will go down to when raising up before
153 // leveling the shooter for calibration.
Austin Schuhb1d682b2016-02-16 13:07:44 -0800154 static constexpr double kShoulderWristClearAngle =
155 CollisionAvoidance::kMinShoulderAngleForHorizontalShooter;
Adam Snaider06779722016-02-14 15:26:22 -0800156
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800157 enum State {
Adam Snaider06779722016-02-14 15:26:22 -0800158 // Wait for all the filters to be ready before starting the initialization
159 // process.
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800160 UNINITIALIZED = 0,
Adam Snaider06779722016-02-14 15:26:22 -0800161
162 // We now are ready to decide how to zero. Decide what to do once we are
163 // enabled.
164 DISABLED_INITIALIZED = 1,
165
166 // Lift the arm up out of the way.
167 HIGH_ARM_ZERO_LIFT_ARM = 2,
168
169 HIGH_ARM_ZERO_LEVEL_SHOOTER = 3,
170
171 HIGH_ARM_ZERO_MOVE_INTAKE_OUT = 4,
172
173 HIGH_ARM_ZERO_LOWER_ARM = 6,
174
175 LOW_ARM_ZERO_LOWER_INTAKE = 7,
176 LOW_ARM_ZERO_MAYBE_LEVEL_SHOOTER = 8,
177 LOW_ARM_ZERO_LIFT_SHOULDER = 9,
178 LOW_ARM_ZERO_LEVEL_SHOOTER = 11,
179 // Run, but limit power to zeroing voltages.
180 SLOW_RUNNING = 12,
181 // Run with full power.
182 RUNNING = 13,
Austin Schuhb1d682b2016-02-16 13:07:44 -0800183 // Run, but limit power to zeroing voltages while landing.
184 LANDING_SLOW_RUNNING = 14,
185 // Run with full power while landing.
186 LANDING_RUNNING = 15,
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800187 // Internal error caused the superstructure to abort.
Austin Schuhb1d682b2016-02-16 13:07:44 -0800188 ESTOP = 16,
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800189 };
190
Austin Schuh9f4e8a72016-02-16 15:28:47 -0800191 bool IsRunning() const {
192 return (state_ == SLOW_RUNNING || state_ == RUNNING ||
193 state_ == LANDING_SLOW_RUNNING || state_ == LANDING_RUNNING);
194 }
195
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800196 State state() const { return state_; }
Comran Morshed25f81a02016-01-23 13:40:10 +0000197
Adam Snaider06779722016-02-14 15:26:22 -0800198 // Returns the value to move the joint to such that it will stay below
199 // reference_angle starting at current_angle, but move at least move_distance
200 static double MoveButKeepBelow(double reference_angle, double current_angle,
201 double move_distance);
202 // Returns the value to move the joint to such that it will stay above
203 // reference_angle starting at current_angle, but move at least move_distance
204 static double MoveButKeepAbove(double reference_angle, double current_angle,
205 double move_distance);
206
Philipp Schrader07147532016-02-16 01:23:07 +0000207 // Returns true if anything is currently considered "collided".
208 bool collided() const { return collision_avoidance_.collided(); }
Philipp Schrader0119eb12016-02-15 18:16:21 +0000209
Comran Morshed25f81a02016-01-23 13:40:10 +0000210 protected:
211 virtual void RunIteration(
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800212 const control_loops::SuperstructureQueue::Goal *unsafe_goal,
Comran Morshed25f81a02016-01-23 13:40:10 +0000213 const control_loops::SuperstructureQueue::Position *position,
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800214 control_loops::SuperstructureQueue::Output *output,
Comran Morshed25f81a02016-01-23 13:40:10 +0000215 control_loops::SuperstructureQueue::Status *status) override;
216
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 Schuh2fc10fa2016-02-08 00:44:34 -0800225 Intake intake_;
226 Arm arm_;
227
Philipp Schrader0119eb12016-02-15 18:16:21 +0000228 CollisionAvoidance collision_avoidance_;
229
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800230 State state_ = UNINITIALIZED;
231 State last_state_ = UNINITIALIZED;
232
Austin Schuhbe041152016-02-28 22:01:52 -0800233 float last_shoulder_angle_ = 0.0;
234 float last_wrist_angle_ = 0.0;
235 float last_intake_angle_ = 0.0;
236
Comran Morshed71466fe2016-04-21 20:21:14 -0700237 double kill_shoulder_accumulator_ = 0.0;
238 bool kill_shoulder_ = false;
239
Adam Snaider06779722016-02-14 15:26:22 -0800240 // Returns true if the profile has finished, and the joint is within the
241 // specified tolerance.
242 bool IsArmNear(double tolerance);
243 bool IsArmNear(double shoulder_tolerance, double wrist_tolerance);
244 bool IsIntakeNear(double tolerance);
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800245
Comran Morshed25f81a02016-01-23 13:40:10 +0000246 DISALLOW_COPY_AND_ASSIGN(Superstructure);
247};
248
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800249} // namespace superstructure
Comran Morshed25f81a02016-01-23 13:40:10 +0000250} // namespace control_loops
251} // namespace y2016
252
253#endif // Y2016_CONTROL_LOOPS_SUPERSTRUCTURE_SUPERSTRUCTURE_H_