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