blob: c3d9772d5c0650752a16c5fc785b393b6e7d6fc1 [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
John Park33858a32018-09-28 23:05:48 -07004#include "aos/commonmath.h"
5#include "aos/controls/control_loop.h"
Sabina Davis8d20ca82018-02-19 13:17:45 -08006#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 =
James Kuszmaul651fc3f2019-05-15 21:14:25 -070050 ::aos::time::DurationInSeconds(::aos::controls::kLoopFrequency);
Sabina Davis8d20ca82018-02-19 13:17:45 -080051
52 // Sets the offset of the controller to be the zeroing estimator offset when
53 // possible otherwise zero.
54 void UpdateOffset(double offset);
55
56 const ::frc971::constants::Range intake_range_;
57
58 // Stores the current zeroing estimator offset.
59 double offset_ = 0.0;
60
61 private:
62 bool reset_ = true;
63
64 // The current sensor measurement.
65 Eigen::Matrix<double, 2, 1> Y_;
66
67 DISALLOW_COPY_AND_ASSIGN(IntakeController);
68};
69
70class IntakeSide {
71 public:
72 IntakeSide(const ::frc971::constants::PotAndAbsoluteEncoderZeroingConstants
73 &zeroing_constants);
74
75 // The operating voltage.
76 static constexpr double kOperatingVoltage() { return 12.0; }
77
78 void Iterate(const double *unsafe_goal,
79 const control_loops::IntakeElasticSensors *position,
80 control_loops::IntakeVoltage *output,
81 control_loops::IntakeSideStatus *status);
82
83 void Reset();
84
85 enum class State : int32_t {
86 UNINITIALIZED,
87 ZEROING,
88 RUNNING,
89 ESTOP,
90 };
91
92 State state() const { return state_; }
93
James Kuszmaul651fc3f2019-05-15 21:14:25 -070094 bool clear_of_box() const { return controller_.output_position() < -0.1; }
Austin Schuh96341532018-03-09 21:17:24 -080095
Neil Balchba9cbba2018-04-06 22:26:38 -070096 double output_position() const { return controller_.output_position(); }
97
Sabina Davis8d20ca82018-02-19 13:17:45 -080098 private:
99 IntakeController controller_;
100
101 State state_ = State::UNINITIALIZED;
102
Austin Schuh72db9a12019-01-21 18:02:51 -0800103 ::frc971::zeroing::PotAndAbsoluteEncoderZeroingEstimator zeroing_estimator_;
Austin Schuh17dd0892018-03-02 20:06:31 -0800104
105 double intake_last_position_ = 0.0;
Sabina Davis8d20ca82018-02-19 13:17:45 -0800106};
107
108} // namespace intake
109} // namespace superstructure
110} // namespace control_loops
111} // namespace y2018
112
113#endif // Y2018_CONTROL_LOOPS_SUPERSTRUCTURE_INTAKE_INTAKE_H_