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 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 4 | #include "aos/commonmath.h" |
| 5 | #include "aos/controls/control_loop.h" |
Sabina Davis | 8d20ca8 | 2018-02-19 13:17:45 -0800 | [diff] [blame] | 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 | |
Austin Schuh | 9634153 | 2018-03-09 21:17:24 -0800 | [diff] [blame] | 32 | double output_position() const { return loop_->X_hat(0); } |
| 33 | |
Sabina Davis | 8d20ca8 | 2018-02-19 13:17:45 -0800 | [diff] [blame] | 34 | // 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 | |
| 72 | class 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 Schuh | 9634153 | 2018-03-09 21:17:24 -0800 | [diff] [blame] | 96 | bool clear_of_box() const { |
Austin Schuh | b874fd3 | 2018-03-05 00:27:10 -0800 | [diff] [blame] | 97 | return controller_.output_position() < -0.1; |
Austin Schuh | 9634153 | 2018-03-09 21:17:24 -0800 | [diff] [blame] | 98 | } |
| 99 | |
Neil Balch | ba9cbba | 2018-04-06 22:26:38 -0700 | [diff] [blame] | 100 | double output_position() const { return controller_.output_position(); } |
| 101 | |
Sabina Davis | 8d20ca8 | 2018-02-19 13:17:45 -0800 | [diff] [blame] | 102 | private: |
| 103 | IntakeController controller_; |
| 104 | |
| 105 | State state_ = State::UNINITIALIZED; |
| 106 | |
| 107 | ::frc971::zeroing::PotAndAbsEncoderZeroingEstimator zeroing_estimator_; |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 108 | |
| 109 | double intake_last_position_ = 0.0; |
Sabina Davis | 8d20ca8 | 2018-02-19 13:17:45 -0800 | [diff] [blame] | 110 | }; |
| 111 | |
| 112 | } // namespace intake |
| 113 | } // namespace superstructure |
| 114 | } // namespace control_loops |
| 115 | } // namespace y2018 |
| 116 | |
| 117 | #endif // Y2018_CONTROL_LOOPS_SUPERSTRUCTURE_INTAKE_INTAKE_H_ |