Sabina Davis | 8d20ca8 | 2018-02-19 13:17:45 -0800 | [diff] [blame^] | 1 | #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 | |
| 13 | namespace y2018 { |
| 14 | namespace control_loops { |
| 15 | namespace superstructure { |
| 16 | namespace intake { |
| 17 | |
| 18 | class 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 | |
| 32 | // Executes the control loop for a cycle. |
| 33 | void Update(bool disabled, const double *unsafe_goal); |
| 34 | |
| 35 | // Resets the kalman filter and any other internal state. |
| 36 | void Reset(); |
| 37 | |
| 38 | // Sets the goal angle from unsafe_goal. |
| 39 | double goal_angle(const double *unsafe_goal); |
| 40 | |
| 41 | // The control loop. |
| 42 | ::std::unique_ptr< |
| 43 | StateFeedbackLoop<5, 1, 2, double, StateFeedbackPlant<5, 1, 2>, |
| 44 | StateFeedbackObserver<5, 1, 2>>> |
| 45 | loop_; |
| 46 | |
| 47 | constexpr static double kDt = |
| 48 | ::std::chrono::duration_cast<::std::chrono::duration<double>>( |
| 49 | ::aos::controls::kLoopFrequency) |
| 50 | .count(); |
| 51 | |
| 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 | |
| 70 | class 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 | |
| 94 | private: |
| 95 | IntakeController controller_; |
| 96 | |
| 97 | State state_ = State::UNINITIALIZED; |
| 98 | |
| 99 | ::frc971::zeroing::PotAndAbsEncoderZeroingEstimator zeroing_estimator_; |
| 100 | }; |
| 101 | |
| 102 | } // namespace intake |
| 103 | } // namespace superstructure |
| 104 | } // namespace control_loops |
| 105 | } // namespace y2018 |
| 106 | |
| 107 | #endif // Y2018_CONTROL_LOOPS_SUPERSTRUCTURE_INTAKE_INTAKE_H_ |