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