blob: 083b5b836ddbe530b219e61b4a9ac7fd04a111a1 [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
6#include "aos/common/controls/control_loop.h"
Austin Schuh2fc10fa2016-02-08 00:44:34 -08007#include "aos/common/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 // TODO(phil): Verify that these numbers actually make sense. Someone needs
65 // to verify these either on the CAD or the real robot.
66
67 // 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.
74 static constexpr double kMinShoulderAngleForIntakeInterference = 1.3;
Philipp Schrader0119eb12016-02-15 18:16:21 +000075
76 // The intake angle (in radians) above which the intake can interfere (i.e.
77 // collide) with the arm and/or shooter.
Campbell Crowley1836a432016-03-05 15:16:51 -080078 static constexpr double kMaxIntakeAngleBeforeArmInterference = 1.12;
Philipp Schrader0119eb12016-02-15 18:16:21 +000079
80 // The maximum absolute angle (in radians) that the wrist must be below in
81 // order for the shouler to be allowed to move below
82 // kMinShoulderAngleForHorizontalShooter. In other words, only allow the arm
83 // to move down into the belly pan if the shooter is horizontal, ready to
84 // also be placed into the belly pan.
Austin Schuh2f5bfc82016-02-27 14:47:37 -080085 static constexpr double kMaxWristAngleForSafeArmStowing = 0.05;
Philipp Schrader0119eb12016-02-15 18:16:21 +000086
87 // The shoulder angle (in radians) below which the intake can safely move
88 // into the collision zone. This is necessary when the robot wants to fold up
89 // completely (i.e. stow the arm, shooter, and intake).
Austin Schuh2f5bfc82016-02-27 14:47:37 -080090 static constexpr double kMaxShoulderAngleUntilSafeIntakeStowing = 0.2;
Philipp Schrader0119eb12016-02-15 18:16:21 +000091
92 private:
93 Intake *intake_;
94 Arm *arm_;
95};
96
Comran Morshed25f81a02016-01-23 13:40:10 +000097class Superstructure
98 : public ::aos::controls::ControlLoop<control_loops::SuperstructureQueue> {
99 public:
100 explicit Superstructure(
101 control_loops::SuperstructureQueue *my_superstructure =
102 &control_loops::superstructure_queue);
Adam Snaider06779722016-02-14 15:26:22 -0800103
Philipp Schrader3da48ac2016-03-06 23:03:44 +0000104 static constexpr double kZeroingVoltage = 5.0;
105 static constexpr double kOperatingVoltage = 12.0;
106
Adam Snaider06779722016-02-14 15:26:22 -0800107 // This is the angle above which we will do a HIGH_ARM_ZERO, and below which
108 // we will do a LOW_ARM_ZERO.
109 static constexpr double kShoulderMiddleAngle = M_PI / 4.0;
110 // This is the large scale movement tolerance.
111 static constexpr double kLooseTolerance = 0.05;
112
113 // This is the small scale movement tolerance.
114 static constexpr double kTightTolerance = 0.01;
115
116 // This is the angle such that the intake will clear the arm when the shooter
117 // is level.
Austin Schuh2f5bfc82016-02-27 14:47:37 -0800118 static constexpr double kIntakeUpperClear = 1.2;
Adam Snaider06779722016-02-14 15:26:22 -0800119 // This is the angle such that the intake will clear the arm when the shooter
120 // is at almost any position.
Austin Schuh2f5bfc82016-02-27 14:47:37 -0800121 static constexpr double kIntakeLowerClear = 0.4;
Adam Snaider06779722016-02-14 15:26:22 -0800122
123 // This is the angle that the shoulder will go to when doing the
124 // HIGH_ARM_ZERO.
125 static constexpr double kShoulderUpAngle = M_PI / 2.0;
126
127 // This is the angle that the shoulder will go down to when landing in the
128 // bellypan.
129 static constexpr double kShoulderLanded = -0.02;
130
Austin Schuhb1d682b2016-02-16 13:07:44 -0800131 // This is the angle below which the shoulder will slowly profile down and
132 // land.
133 static constexpr double kShoulderTransitionToLanded = 0.10;
134
Adam Snaider06779722016-02-14 15:26:22 -0800135 // This is the angle below which we consider the wrist close enough to level
136 // that we should move it to level before doing anything.
137 static constexpr double kWristAlmostLevel = 0.10;
138
139 // This is the angle that the shoulder will go down to when raising up before
140 // leveling the shooter for calibration.
Austin Schuhb1d682b2016-02-16 13:07:44 -0800141 static constexpr double kShoulderWristClearAngle =
142 CollisionAvoidance::kMinShoulderAngleForHorizontalShooter;
Adam Snaider06779722016-02-14 15:26:22 -0800143
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800144 enum State {
Adam Snaider06779722016-02-14 15:26:22 -0800145 // Wait for all the filters to be ready before starting the initialization
146 // process.
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800147 UNINITIALIZED = 0,
Adam Snaider06779722016-02-14 15:26:22 -0800148
149 // We now are ready to decide how to zero. Decide what to do once we are
150 // enabled.
151 DISABLED_INITIALIZED = 1,
152
153 // Lift the arm up out of the way.
154 HIGH_ARM_ZERO_LIFT_ARM = 2,
155
156 HIGH_ARM_ZERO_LEVEL_SHOOTER = 3,
157
158 HIGH_ARM_ZERO_MOVE_INTAKE_OUT = 4,
159
160 HIGH_ARM_ZERO_LOWER_ARM = 6,
161
162 LOW_ARM_ZERO_LOWER_INTAKE = 7,
163 LOW_ARM_ZERO_MAYBE_LEVEL_SHOOTER = 8,
164 LOW_ARM_ZERO_LIFT_SHOULDER = 9,
165 LOW_ARM_ZERO_LEVEL_SHOOTER = 11,
166 // Run, but limit power to zeroing voltages.
167 SLOW_RUNNING = 12,
168 // Run with full power.
169 RUNNING = 13,
Austin Schuhb1d682b2016-02-16 13:07:44 -0800170 // Run, but limit power to zeroing voltages while landing.
171 LANDING_SLOW_RUNNING = 14,
172 // Run with full power while landing.
173 LANDING_RUNNING = 15,
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800174 // Internal error caused the superstructure to abort.
Austin Schuhb1d682b2016-02-16 13:07:44 -0800175 ESTOP = 16,
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800176 };
177
Austin Schuh9f4e8a72016-02-16 15:28:47 -0800178 bool IsRunning() const {
179 return (state_ == SLOW_RUNNING || state_ == RUNNING ||
180 state_ == LANDING_SLOW_RUNNING || state_ == LANDING_RUNNING);
181 }
182
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800183 State state() const { return state_; }
Comran Morshed25f81a02016-01-23 13:40:10 +0000184
Adam Snaider06779722016-02-14 15:26:22 -0800185 // Returns the value to move the joint to such that it will stay below
186 // reference_angle starting at current_angle, but move at least move_distance
187 static double MoveButKeepBelow(double reference_angle, double current_angle,
188 double move_distance);
189 // Returns the value to move the joint to such that it will stay above
190 // reference_angle starting at current_angle, but move at least move_distance
191 static double MoveButKeepAbove(double reference_angle, double current_angle,
192 double move_distance);
193
Philipp Schrader07147532016-02-16 01:23:07 +0000194 // Returns true if anything is currently considered "collided".
195 bool collided() const { return collision_avoidance_.collided(); }
Philipp Schrader0119eb12016-02-15 18:16:21 +0000196
Comran Morshed25f81a02016-01-23 13:40:10 +0000197 protected:
198 virtual void RunIteration(
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800199 const control_loops::SuperstructureQueue::Goal *unsafe_goal,
Comran Morshed25f81a02016-01-23 13:40:10 +0000200 const control_loops::SuperstructureQueue::Position *position,
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800201 control_loops::SuperstructureQueue::Output *output,
Comran Morshed25f81a02016-01-23 13:40:10 +0000202 control_loops::SuperstructureQueue::Status *status) override;
203
204 private:
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800205 friend class testing::SuperstructureTest_DisabledGoalTest_Test;
Diana Vandenberge2843c62016-02-13 17:44:20 -0800206 friend class testing::SuperstructureTest_ArmZeroingErrorTest_Test;
207 friend class testing::SuperstructureTest_IntakeZeroingErrorTest_Test;
Philipp Schrader0119eb12016-02-15 18:16:21 +0000208 friend class testing::SuperstructureTest_RespectsRange_Test;
209 friend class testing::SuperstructureTest_UpperHardstopStartup_Test;
Campbell Crowley152c7cf2016-02-14 21:20:50 -0800210 friend class testing::SuperstructureTest_DisabledWhileZeroingHigh_Test;
211 friend class testing::SuperstructureTest_DisabledWhileZeroingLow_Test;
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800212 Intake intake_;
213 Arm arm_;
214
Philipp Schrader0119eb12016-02-15 18:16:21 +0000215 CollisionAvoidance collision_avoidance_;
216
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800217 State state_ = UNINITIALIZED;
218 State last_state_ = UNINITIALIZED;
219
Austin Schuhbe041152016-02-28 22:01:52 -0800220 float last_shoulder_angle_ = 0.0;
221 float last_wrist_angle_ = 0.0;
222 float last_intake_angle_ = 0.0;
223
Adam Snaider06779722016-02-14 15:26:22 -0800224 // Returns true if the profile has finished, and the joint is within the
225 // specified tolerance.
226 bool IsArmNear(double tolerance);
227 bool IsArmNear(double shoulder_tolerance, double wrist_tolerance);
228 bool IsIntakeNear(double tolerance);
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800229
Comran Morshed25f81a02016-01-23 13:40:10 +0000230 DISALLOW_COPY_AND_ASSIGN(Superstructure);
231};
232
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800233} // namespace superstructure
Comran Morshed25f81a02016-01-23 13:40:10 +0000234} // namespace control_loops
235} // namespace y2016
236
237#endif // Y2016_CONTROL_LOOPS_SUPERSTRUCTURE_SUPERSTRUCTURE_H_