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/zeroing/zeroing.h" |
| 7 | #include "y2018/constants.h" |
| 8 | #include "y2018/control_loops/superstructure/intake/intake_delayed_plant.h" |
| 9 | #include "y2018/control_loops/superstructure/intake/intake_plant.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame^] | 10 | #include "y2018/control_loops/superstructure/superstructure_output_generated.h" |
| 11 | #include "y2018/control_loops/superstructure/superstructure_position_generated.h" |
| 12 | #include "y2018/control_loops/superstructure/superstructure_status_generated.h" |
Sabina Davis | 8d20ca8 | 2018-02-19 13:17:45 -0800 | [diff] [blame] | 13 | |
| 14 | namespace y2018 { |
| 15 | namespace control_loops { |
| 16 | namespace superstructure { |
| 17 | namespace intake { |
| 18 | |
| 19 | class IntakeController { |
| 20 | public: |
| 21 | IntakeController(); |
| 22 | |
| 23 | // Sets the current encoder position in radians |
| 24 | void set_position(double spring_angle, double output_position); |
| 25 | |
| 26 | // Populates the status structure. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame^] | 27 | void SetStatus(superstructure::IntakeSideStatus::Builder *status, |
Sabina Davis | 8d20ca8 | 2018-02-19 13:17:45 -0800 | [diff] [blame] | 28 | const double *unsafe_goal); |
| 29 | |
| 30 | // Returns the control loop calculated voltage. |
| 31 | double voltage() const; |
| 32 | |
Austin Schuh | 9634153 | 2018-03-09 21:17:24 -0800 | [diff] [blame] | 33 | double output_position() const { return loop_->X_hat(0); } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame^] | 34 | double motor_position() const { return loop_->X_hat(2); } |
Austin Schuh | 9634153 | 2018-03-09 21:17:24 -0800 | [diff] [blame] | 35 | |
Sabina Davis | 8d20ca8 | 2018-02-19 13:17:45 -0800 | [diff] [blame] | 36 | // Executes the control loop for a cycle. |
| 37 | void Update(bool disabled, const double *unsafe_goal); |
| 38 | |
| 39 | // Resets the kalman filter and any other internal state. |
| 40 | void Reset(); |
| 41 | |
| 42 | // Sets the goal angle from unsafe_goal. |
| 43 | double goal_angle(const double *unsafe_goal); |
| 44 | |
Sabina Davis | 8d20ca8 | 2018-02-19 13:17:45 -0800 | [diff] [blame] | 45 | constexpr static double kDt = |
James Kuszmaul | 651fc3f | 2019-05-15 21:14:25 -0700 | [diff] [blame] | 46 | ::aos::time::DurationInSeconds(::aos::controls::kLoopFrequency); |
Sabina Davis | 8d20ca8 | 2018-02-19 13:17:45 -0800 | [diff] [blame] | 47 | |
| 48 | // Sets the offset of the controller to be the zeroing estimator offset when |
| 49 | // possible otherwise zero. |
| 50 | void UpdateOffset(double offset); |
| 51 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame^] | 52 | const ::frc971::constants::Range intake_range() const { |
| 53 | return intake_range_; |
| 54 | } |
| 55 | |
| 56 | private: |
| 57 | // The control loop. |
| 58 | ::std::unique_ptr< |
| 59 | StateFeedbackLoop<5, 1, 2, double, StateFeedbackPlant<5, 1, 2>, |
| 60 | StateFeedbackObserver<5, 1, 2>>> |
| 61 | loop_; |
| 62 | |
Sabina Davis | 8d20ca8 | 2018-02-19 13:17:45 -0800 | [diff] [blame] | 63 | const ::frc971::constants::Range intake_range_; |
| 64 | |
| 65 | // Stores the current zeroing estimator offset. |
| 66 | double offset_ = 0.0; |
| 67 | |
Sabina Davis | 8d20ca8 | 2018-02-19 13:17:45 -0800 | [diff] [blame] | 68 | bool reset_ = true; |
| 69 | |
| 70 | // The current sensor measurement. |
| 71 | Eigen::Matrix<double, 2, 1> Y_; |
| 72 | |
| 73 | DISALLOW_COPY_AND_ASSIGN(IntakeController); |
| 74 | }; |
| 75 | |
| 76 | class IntakeSide { |
| 77 | public: |
| 78 | IntakeSide(const ::frc971::constants::PotAndAbsoluteEncoderZeroingConstants |
| 79 | &zeroing_constants); |
| 80 | |
| 81 | // The operating voltage. |
| 82 | static constexpr double kOperatingVoltage() { return 12.0; } |
| 83 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame^] | 84 | flatbuffers::Offset<superstructure::IntakeSideStatus> Iterate( |
| 85 | const double *unsafe_goal, |
| 86 | const superstructure::IntakeElasticSensors *position, |
| 87 | superstructure::IntakeVoltageT *output, |
| 88 | flatbuffers::FlatBufferBuilder *fbb); |
Sabina Davis | 8d20ca8 | 2018-02-19 13:17:45 -0800 | [diff] [blame] | 89 | |
| 90 | void Reset(); |
| 91 | |
| 92 | enum class State : int32_t { |
| 93 | UNINITIALIZED, |
| 94 | ZEROING, |
| 95 | RUNNING, |
| 96 | ESTOP, |
| 97 | }; |
| 98 | |
| 99 | State state() const { return state_; } |
| 100 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame^] | 101 | bool estopped() const { return state_ == State::ESTOP; } |
| 102 | bool zeroed() const { return zeroing_estimator_.zeroed(); } |
| 103 | |
James Kuszmaul | 651fc3f | 2019-05-15 21:14:25 -0700 | [diff] [blame] | 104 | bool clear_of_box() const { return controller_.output_position() < -0.1; } |
Austin Schuh | 9634153 | 2018-03-09 21:17:24 -0800 | [diff] [blame] | 105 | |
Neil Balch | ba9cbba | 2018-04-06 22:26:38 -0700 | [diff] [blame] | 106 | double output_position() const { return controller_.output_position(); } |
| 107 | |
Sabina Davis | 8d20ca8 | 2018-02-19 13:17:45 -0800 | [diff] [blame] | 108 | private: |
| 109 | IntakeController controller_; |
| 110 | |
| 111 | State state_ = State::UNINITIALIZED; |
| 112 | |
Austin Schuh | 72db9a1 | 2019-01-21 18:02:51 -0800 | [diff] [blame] | 113 | ::frc971::zeroing::PotAndAbsoluteEncoderZeroingEstimator zeroing_estimator_; |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 114 | |
| 115 | double intake_last_position_ = 0.0; |
Sabina Davis | 8d20ca8 | 2018-02-19 13:17:45 -0800 | [diff] [blame] | 116 | }; |
| 117 | |
| 118 | } // namespace intake |
| 119 | } // namespace superstructure |
| 120 | } // namespace control_loops |
| 121 | } // namespace y2018 |
| 122 | |
| 123 | #endif // Y2018_CONTROL_LOOPS_SUPERSTRUCTURE_INTAKE_INTAKE_H_ |