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