blob: a860f09328370c243cfb75d0ca473908090f7d85 [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 {
18class SuperstructureTest_DisabledGoalTest_Test;
Diana Vandenberge2843c62016-02-13 17:44:20 -080019class SuperstructureTest_ArmZeroingErrorTest_Test;
20class SuperstructureTest_IntakeZeroingErrorTest_Test;
Austin Schuh2fc10fa2016-02-08 00:44:34 -080021} // namespace testing
22
Comran Morshed25f81a02016-01-23 13:40:10 +000023class Superstructure
24 : public ::aos::controls::ControlLoop<control_loops::SuperstructureQueue> {
25 public:
26 explicit Superstructure(
27 control_loops::SuperstructureQueue *my_superstructure =
28 &control_loops::superstructure_queue);
Adam Snaider06779722016-02-14 15:26:22 -080029
30 // This is the angle above which we will do a HIGH_ARM_ZERO, and below which
31 // we will do a LOW_ARM_ZERO.
32 static constexpr double kShoulderMiddleAngle = M_PI / 4.0;
33 // This is the large scale movement tolerance.
34 static constexpr double kLooseTolerance = 0.05;
35
36 // This is the small scale movement tolerance.
37 static constexpr double kTightTolerance = 0.01;
38
39 // This is the angle such that the intake will clear the arm when the shooter
40 // is level.
41 static constexpr double kIntakeUpperClear = 1.1;
42 // This is the angle such that the intake will clear the arm when the shooter
43 // is at almost any position.
44 static constexpr double kIntakeLowerClear = 0.5;
45
46 // This is the angle that the shoulder will go to when doing the
47 // HIGH_ARM_ZERO.
48 static constexpr double kShoulderUpAngle = M_PI / 2.0;
49
50 // This is the angle that the shoulder will go down to when landing in the
51 // bellypan.
52 static constexpr double kShoulderLanded = -0.02;
53
54 // This is the angle below which we consider the wrist close enough to level
55 // that we should move it to level before doing anything.
56 static constexpr double kWristAlmostLevel = 0.10;
57
58 // This is the angle that the shoulder will go down to when raising up before
59 // leveling the shooter for calibration.
60 static constexpr double kShoulderWristClearAngle = 0.6;
61
Austin Schuh2fc10fa2016-02-08 00:44:34 -080062 enum State {
Adam Snaider06779722016-02-14 15:26:22 -080063 // Wait for all the filters to be ready before starting the initialization
64 // process.
Austin Schuh2fc10fa2016-02-08 00:44:34 -080065 UNINITIALIZED = 0,
Adam Snaider06779722016-02-14 15:26:22 -080066
67 // We now are ready to decide how to zero. Decide what to do once we are
68 // enabled.
69 DISABLED_INITIALIZED = 1,
70
71 // Lift the arm up out of the way.
72 HIGH_ARM_ZERO_LIFT_ARM = 2,
73
74 HIGH_ARM_ZERO_LEVEL_SHOOTER = 3,
75
76 HIGH_ARM_ZERO_MOVE_INTAKE_OUT = 4,
77
78 HIGH_ARM_ZERO_LOWER_ARM = 6,
79
80 LOW_ARM_ZERO_LOWER_INTAKE = 7,
81 LOW_ARM_ZERO_MAYBE_LEVEL_SHOOTER = 8,
82 LOW_ARM_ZERO_LIFT_SHOULDER = 9,
83 LOW_ARM_ZERO_LEVEL_SHOOTER = 11,
84 // Run, but limit power to zeroing voltages.
85 SLOW_RUNNING = 12,
86 // Run with full power.
87 RUNNING = 13,
Austin Schuh2fc10fa2016-02-08 00:44:34 -080088 // Internal error caused the superstructure to abort.
Adam Snaider06779722016-02-14 15:26:22 -080089 ESTOP = 14,
Austin Schuh2fc10fa2016-02-08 00:44:34 -080090 };
91
92 State state() const { return state_; }
Comran Morshed25f81a02016-01-23 13:40:10 +000093
Adam Snaider06779722016-02-14 15:26:22 -080094 // Returns the value to move the joint to such that it will stay below
95 // reference_angle starting at current_angle, but move at least move_distance
96 static double MoveButKeepBelow(double reference_angle, double current_angle,
97 double move_distance);
98 // Returns the value to move the joint to such that it will stay above
99 // reference_angle starting at current_angle, but move at least move_distance
100 static double MoveButKeepAbove(double reference_angle, double current_angle,
101 double move_distance);
102
Comran Morshed25f81a02016-01-23 13:40:10 +0000103 protected:
104 virtual void RunIteration(
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800105 const control_loops::SuperstructureQueue::Goal *unsafe_goal,
Comran Morshed25f81a02016-01-23 13:40:10 +0000106 const control_loops::SuperstructureQueue::Position *position,
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800107 control_loops::SuperstructureQueue::Output *output,
Comran Morshed25f81a02016-01-23 13:40:10 +0000108 control_loops::SuperstructureQueue::Status *status) override;
109
110 private:
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800111 friend class testing::SuperstructureTest_DisabledGoalTest_Test;
Diana Vandenberge2843c62016-02-13 17:44:20 -0800112 friend class testing::SuperstructureTest_ArmZeroingErrorTest_Test;
113 friend class testing::SuperstructureTest_IntakeZeroingErrorTest_Test;
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800114 Intake intake_;
115 Arm arm_;
116
117 State state_ = UNINITIALIZED;
118 State last_state_ = UNINITIALIZED;
119
Adam Snaider06779722016-02-14 15:26:22 -0800120 // Returns true if the profile has finished, and the joint is within the
121 // specified tolerance.
122 bool IsArmNear(double tolerance);
123 bool IsArmNear(double shoulder_tolerance, double wrist_tolerance);
124 bool IsIntakeNear(double tolerance);
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800125
Comran Morshed25f81a02016-01-23 13:40:10 +0000126 DISALLOW_COPY_AND_ASSIGN(Superstructure);
127};
128
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800129} // namespace superstructure
Comran Morshed25f81a02016-01-23 13:40:10 +0000130} // namespace control_loops
131} // namespace y2016
132
133#endif // Y2016_CONTROL_LOOPS_SUPERSTRUCTURE_SUPERSTRUCTURE_H_