blob: 2c36efac85259de296329461019be5a40819ed56 [file] [log] [blame]
Sabina Davis8d20ca82018-02-19 13:17:45 -08001#ifndef Y2018_CONTROL_LOOPS_SUPERSTRUCTURE_INTAKE_INTAKE_H_
2#define Y2018_CONTROL_LOOPS_SUPERSTRUCTURE_INTAKE_INTAKE_H_
3
4#include "aos/common/commonmath.h"
5#include "aos/common/controls/control_loop.h"
6#include "frc971/control_loops/control_loops.q.h"
7#include "frc971/zeroing/zeroing.h"
8#include "y2018/constants.h"
9#include "y2018/control_loops/superstructure/intake/intake_delayed_plant.h"
10#include "y2018/control_loops/superstructure/intake/intake_plant.h"
11#include "y2018/control_loops/superstructure/superstructure.q.h"
12
13namespace y2018 {
14namespace control_loops {
15namespace superstructure {
16namespace intake {
17
18class IntakeController {
19 public:
20 IntakeController();
21
22 // Sets the current encoder position in radians
23 void set_position(double spring_angle, double output_position);
24
25 // Populates the status structure.
26 void SetStatus(control_loops::IntakeSideStatus *status,
27 const double *unsafe_goal);
28
29 // Returns the control loop calculated voltage.
30 double voltage() const;
31
Austin Schuh96341532018-03-09 21:17:24 -080032 double output_position() const { return loop_->X_hat(0); }
33
Sabina Davis8d20ca82018-02-19 13:17:45 -080034 // Executes the control loop for a cycle.
35 void Update(bool disabled, const double *unsafe_goal);
36
37 // Resets the kalman filter and any other internal state.
38 void Reset();
39
40 // Sets the goal angle from unsafe_goal.
41 double goal_angle(const double *unsafe_goal);
42
43 // The control loop.
44 ::std::unique_ptr<
45 StateFeedbackLoop<5, 1, 2, double, StateFeedbackPlant<5, 1, 2>,
46 StateFeedbackObserver<5, 1, 2>>>
47 loop_;
48
49 constexpr static double kDt =
50 ::std::chrono::duration_cast<::std::chrono::duration<double>>(
51 ::aos::controls::kLoopFrequency)
52 .count();
53
54 // Sets the offset of the controller to be the zeroing estimator offset when
55 // possible otherwise zero.
56 void UpdateOffset(double offset);
57
58 const ::frc971::constants::Range intake_range_;
59
60 // Stores the current zeroing estimator offset.
61 double offset_ = 0.0;
62
63 private:
64 bool reset_ = true;
65
66 // The current sensor measurement.
67 Eigen::Matrix<double, 2, 1> Y_;
68
69 DISALLOW_COPY_AND_ASSIGN(IntakeController);
70};
71
72class IntakeSide {
73 public:
74 IntakeSide(const ::frc971::constants::PotAndAbsoluteEncoderZeroingConstants
75 &zeroing_constants);
76
77 // The operating voltage.
78 static constexpr double kOperatingVoltage() { return 12.0; }
79
80 void Iterate(const double *unsafe_goal,
81 const control_loops::IntakeElasticSensors *position,
82 control_loops::IntakeVoltage *output,
83 control_loops::IntakeSideStatus *status);
84
85 void Reset();
86
87 enum class State : int32_t {
88 UNINITIALIZED,
89 ZEROING,
90 RUNNING,
91 ESTOP,
92 };
93
94 State state() const { return state_; }
95
Austin Schuh96341532018-03-09 21:17:24 -080096 bool clear_of_box() const {
Austin Schuhb874fd32018-03-05 00:27:10 -080097 return controller_.output_position() < -0.1;
Austin Schuh96341532018-03-09 21:17:24 -080098 }
99
Sabina Davis8d20ca82018-02-19 13:17:45 -0800100 private:
101 IntakeController controller_;
102
103 State state_ = State::UNINITIALIZED;
104
105 ::frc971::zeroing::PotAndAbsEncoderZeroingEstimator zeroing_estimator_;
Austin Schuh17dd0892018-03-02 20:06:31 -0800106
107 double intake_last_position_ = 0.0;
Sabina Davis8d20ca82018-02-19 13:17:45 -0800108};
109
110} // namespace intake
111} // namespace superstructure
112} // namespace control_loops
113} // namespace y2018
114
115#endif // Y2018_CONTROL_LOOPS_SUPERSTRUCTURE_INTAKE_INTAKE_H_