Maxwell Henderson | 3424299 | 2024-01-07 12:39:11 -0800 | [diff] [blame] | 1 | #ifndef FRC971_CONTROL_LOOPS_SHOOTER_FLYWHEEL_CONTROLLER_H_ |
| 2 | #define FRC971_CONTROL_LOOPS_SHOOTER_FLYWHEEL_CONTROLLER_H_ |
Sabina Davis | edf8947 | 2020-02-17 15:27:37 -0800 | [diff] [blame] | 3 | |
| 4 | #include <memory> |
| 5 | |
Sabina Davis | edf8947 | 2020-02-17 15:27:37 -0800 | [diff] [blame] | 6 | #include "aos/time/time.h" |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 7 | #include "frc971/control_loops/control_loop.h" |
Maxwell Henderson | 3424299 | 2024-01-07 12:39:11 -0800 | [diff] [blame] | 8 | #include "frc971/control_loops/flywheel/flywheel_controller_status_generated.h" |
| 9 | #include "frc971/control_loops/hybrid_state_feedback_loop.h" |
Sabina Davis | edf8947 | 2020-02-17 15:27:37 -0800 | [diff] [blame] | 10 | #include "frc971/control_loops/state_feedback_loop.h" |
Sabina Davis | edf8947 | 2020-02-17 15:27:37 -0800 | [diff] [blame] | 11 | |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame] | 12 | namespace frc971::control_loops::flywheel { |
Sabina Davis | edf8947 | 2020-02-17 15:27:37 -0800 | [diff] [blame] | 13 | |
Austin Schuh | 8047677 | 2021-03-06 20:17:36 -0800 | [diff] [blame] | 14 | class CurrentLimitedStateFeedbackController; |
| 15 | |
Sabina Davis | edf8947 | 2020-02-17 15:27:37 -0800 | [diff] [blame] | 16 | // Handles the velocity control of each flywheel. |
| 17 | class FlywheelController { |
| 18 | public: |
Austin Schuh | 43b9ae9 | 2020-02-29 23:08:38 -0800 | [diff] [blame] | 19 | FlywheelController( |
| 20 | StateFeedbackLoop<3, 1, 1, double, StateFeedbackHybridPlant<3, 1, 1>, |
Austin Schuh | e8ca06a | 2020-03-07 22:27:39 -0800 | [diff] [blame] | 21 | HybridKalman<3, 1, 1>> &&loop, |
| 22 | double bemf, double resistance); |
Sabina Davis | edf8947 | 2020-02-17 15:27:37 -0800 | [diff] [blame] | 23 | |
Austin Schuh | 8047677 | 2021-03-06 20:17:36 -0800 | [diff] [blame] | 24 | ~FlywheelController(); |
| 25 | |
Sabina Davis | edf8947 | 2020-02-17 15:27:37 -0800 | [diff] [blame] | 26 | // Sets the velocity goal in radians/sec |
| 27 | void set_goal(double angular_velocity_goal); |
Austin Schuh | 263dead | 2021-04-04 21:19:19 -0700 | [diff] [blame] | 28 | double goal() const { return last_goal_; } |
Sabina Davis | edf8947 | 2020-02-17 15:27:37 -0800 | [diff] [blame] | 29 | // Sets the current encoder position in radians |
Sabina Davis | 0f31d3f | 2020-02-20 20:41:00 -0800 | [diff] [blame] | 30 | void set_position(double current_position, |
| 31 | const aos::monotonic_clock::time_point position_timestamp); |
Sabina Davis | edf8947 | 2020-02-17 15:27:37 -0800 | [diff] [blame] | 32 | |
| 33 | // Populates the status structure. |
| 34 | flatbuffers::Offset<FlywheelControllerStatus> SetStatus( |
| 35 | flatbuffers::FlatBufferBuilder *fbb); |
| 36 | |
| 37 | // Returns the control loop calculated voltage. |
| 38 | double voltage() const; |
| 39 | |
Austin Schuh | 8047677 | 2021-03-06 20:17:36 -0800 | [diff] [blame] | 40 | // Returns the expected battery current for the last U. |
| 41 | double current() const; |
| 42 | |
Sabina Davis | edf8947 | 2020-02-17 15:27:37 -0800 | [diff] [blame] | 43 | // Returns the instantaneous velocity. |
Austin Schuh | 8047677 | 2021-03-06 20:17:36 -0800 | [diff] [blame] | 44 | double velocity() const; |
Sabina Davis | edf8947 | 2020-02-17 15:27:37 -0800 | [diff] [blame] | 45 | |
| 46 | // Executes the control loop for a cycle. |
| 47 | void Update(bool disabled); |
| 48 | |
Sabina Davis | 0f31d3f | 2020-02-20 20:41:00 -0800 | [diff] [blame] | 49 | double avg_angular_velocity() { return avg_angular_velocity_; } |
| 50 | |
Sabina Davis | edf8947 | 2020-02-17 15:27:37 -0800 | [diff] [blame] | 51 | private: |
| 52 | // The current sensor measurement. |
| 53 | Eigen::Matrix<double, 1, 1> Y_; |
| 54 | // The control loop. |
Austin Schuh | 8047677 | 2021-03-06 20:17:36 -0800 | [diff] [blame] | 55 | ::std::unique_ptr<CurrentLimitedStateFeedbackController> loop_; |
Sabina Davis | edf8947 | 2020-02-17 15:27:37 -0800 | [diff] [blame] | 56 | |
| 57 | // History array for calculating a filtered angular velocity. |
| 58 | static constexpr int kHistoryLength = 10; |
Sabina Davis | 0f31d3f | 2020-02-20 20:41:00 -0800 | [diff] [blame] | 59 | ::std::array<std::pair<double, ::aos::monotonic_clock::time_point>, |
| 60 | kHistoryLength> |
| 61 | history_; |
Sabina Davis | edf8947 | 2020-02-17 15:27:37 -0800 | [diff] [blame] | 62 | ptrdiff_t history_position_ = 0; |
Sabina Davis | 0f31d3f | 2020-02-20 20:41:00 -0800 | [diff] [blame] | 63 | |
| 64 | // Average velocity logging. |
milind-u | b26973a | 2021-10-23 17:02:08 -0700 | [diff] [blame] | 65 | double avg_angular_velocity_ = 0; |
Sabina Davis | 0f31d3f | 2020-02-20 20:41:00 -0800 | [diff] [blame] | 66 | |
Sabina Davis | edf8947 | 2020-02-17 15:27:37 -0800 | [diff] [blame] | 67 | double last_goal_ = 0; |
| 68 | |
Austin Schuh | 43b9ae9 | 2020-02-29 23:08:38 -0800 | [diff] [blame] | 69 | bool first_ = true; |
| 70 | |
Sabina Davis | edf8947 | 2020-02-17 15:27:37 -0800 | [diff] [blame] | 71 | DISALLOW_COPY_AND_ASSIGN(FlywheelController); |
| 72 | }; |
| 73 | |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame] | 74 | } // namespace frc971::control_loops::flywheel |
Sabina Davis | edf8947 | 2020-02-17 15:27:37 -0800 | [diff] [blame] | 75 | |
Maxwell Henderson | 3424299 | 2024-01-07 12:39:11 -0800 | [diff] [blame] | 76 | #endif // FRC971_CONTROL_LOOPS_SHOOTER_FLYWHEEL_CONTROLLER_H_ |