Tyler Chatow | 2737d2a | 2017-02-08 21:20:51 -0800 | [diff] [blame] | 1 | #ifndef Y2017_CONTROL_LOOPS_SHOOTER_SHOOTER_H_ |
| 2 | #define Y2017_CONTROL_LOOPS_SHOOTER_SHOOTER_H_ |
| 3 | |
Austin Schuh | 169f5f2 | 2017-02-18 16:31:13 -0800 | [diff] [blame] | 4 | #include <array> |
Tyler Chatow | 2737d2a | 2017-02-08 21:20:51 -0800 | [diff] [blame] | 5 | #include <memory> |
| 6 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 7 | #include "Eigen/Dense" |
Tyler Chatow | 2737d2a | 2017-02-08 21:20:51 -0800 | [diff] [blame] | 8 | |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 9 | #include "aos/time/time.h" |
| 10 | #include "frc971/control_loops/control_loop.h" |
| 11 | #include "frc971/control_loops/state_feedback_loop.h" |
Tyler Chatow | 2737d2a | 2017-02-08 21:20:51 -0800 | [diff] [blame] | 12 | #include "y2017/control_loops/superstructure/shooter/shooter_integral_plant.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 13 | #include "y2017/control_loops/superstructure/superstructure_goal_generated.h" |
| 14 | #include "y2017/control_loops/superstructure/superstructure_status_generated.h" |
Tyler Chatow | 2737d2a | 2017-02-08 21:20:51 -0800 | [diff] [blame] | 15 | |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame] | 16 | namespace y2017::control_loops::superstructure::shooter { |
Tyler Chatow | 2737d2a | 2017-02-08 21:20:51 -0800 | [diff] [blame] | 17 | |
| 18 | class ShooterController { |
| 19 | public: |
| 20 | ShooterController(); |
| 21 | |
| 22 | // Sets the velocity goal in radians/sec |
| 23 | void set_goal(double angular_velocity_goal); |
| 24 | // Sets the current encoder position in radians |
| 25 | void set_position(double current_position); |
| 26 | |
| 27 | // Populates the status structure. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 28 | flatbuffers::Offset<ShooterStatus> BuildStatus( |
| 29 | flatbuffers::FlatBufferBuilder *fbb); |
Tyler Chatow | 2737d2a | 2017-02-08 21:20:51 -0800 | [diff] [blame] | 30 | |
| 31 | // Returns the control loop calculated voltage. |
| 32 | double voltage() const; |
| 33 | |
| 34 | // Returns the instantaneous velocity. |
Austin Schuh | c66b6fc | 2017-03-25 19:56:59 -0700 | [diff] [blame] | 35 | double velocity() const { return loop_->X_hat(2, 0); } |
| 36 | double voltage_error() const { return loop_->X_hat(3, 0); } |
Tyler Chatow | 2737d2a | 2017-02-08 21:20:51 -0800 | [diff] [blame] | 37 | |
| 38 | double dt_velocity() const { return dt_velocity_; } |
| 39 | |
| 40 | double error() const { return error_; } |
| 41 | |
| 42 | // Executes the control loop for a cycle. |
Austin Schuh | 932a5ce | 2017-03-05 01:04:18 -0800 | [diff] [blame] | 43 | void Update(bool disabled, ::std::chrono::nanoseconds dt); |
Tyler Chatow | 2737d2a | 2017-02-08 21:20:51 -0800 | [diff] [blame] | 44 | |
| 45 | // Resets the kalman filter and any other internal state. |
| 46 | void Reset(); |
| 47 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 48 | bool ready() { return ready_; } |
| 49 | |
Tyler Chatow | 2737d2a | 2017-02-08 21:20:51 -0800 | [diff] [blame] | 50 | private: |
| 51 | // The current sensor measurement. |
| 52 | Eigen::Matrix<double, 1, 1> Y_; |
| 53 | // The control loop. |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 54 | ::std::unique_ptr< |
| 55 | StateFeedbackLoop<4, 1, 1, double, StateFeedbackHybridPlant<4, 1, 1>, |
| 56 | HybridKalman<4, 1, 1>>> |
Austin Schuh | 3ad5ed8 | 2017-02-25 21:36:19 -0800 | [diff] [blame] | 57 | loop_; |
Tyler Chatow | 2737d2a | 2017-02-08 21:20:51 -0800 | [diff] [blame] | 58 | |
| 59 | // History array for calculating a filtered angular velocity. |
| 60 | static constexpr int kHistoryLength = 5; |
| 61 | ::std::array<double, kHistoryLength> history_; |
| 62 | ptrdiff_t history_position_ = 0; |
| 63 | |
| 64 | double error_ = 0.0; |
Austin Schuh | 932a5ce | 2017-03-05 01:04:18 -0800 | [diff] [blame] | 65 | double dt_position_ = 0.0; |
Tyler Chatow | 2737d2a | 2017-02-08 21:20:51 -0800 | [diff] [blame] | 66 | double dt_velocity_ = 0.0; |
Austin Schuh | 932a5ce | 2017-03-05 01:04:18 -0800 | [diff] [blame] | 67 | double fixed_dt_velocity_ = 0.0; |
Tyler Chatow | 2737d2a | 2017-02-08 21:20:51 -0800 | [diff] [blame] | 68 | double last_position_ = 0.0; |
| 69 | double average_angular_velocity_ = 0.0; |
| 70 | double min_velocity_ = 0.0; |
| 71 | double position_error_ = 0.0; |
| 72 | |
Austin Schuh | c66b6fc | 2017-03-25 19:56:59 -0700 | [diff] [blame] | 73 | Eigen::Matrix<double, 4, 1> X_hat_current_; |
Tyler Chatow | 2737d2a | 2017-02-08 21:20:51 -0800 | [diff] [blame] | 74 | |
| 75 | bool ready_ = false; |
| 76 | bool needs_reset_ = false; |
| 77 | bool reset_ = false; |
| 78 | |
| 79 | bool last_ready_ = false; |
| 80 | DISALLOW_COPY_AND_ASSIGN(ShooterController); |
| 81 | }; |
| 82 | |
| 83 | class Shooter { |
| 84 | public: |
| 85 | Shooter() {} |
| 86 | |
| 87 | // Iterates the shooter control loop one cycle. position and status must |
| 88 | // never be nullptr. goal can be nullptr if no goal exists, and output should |
| 89 | // be nullptr if disabled. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 90 | flatbuffers::Offset<ShooterStatus> Iterate( |
| 91 | const ShooterGoalT *goal, const double position, |
| 92 | ::aos::monotonic_clock::time_point position_time, double *output, |
| 93 | flatbuffers::FlatBufferBuilder *fbb); |
Tyler Chatow | 2737d2a | 2017-02-08 21:20:51 -0800 | [diff] [blame] | 94 | |
| 95 | // Sets the shooter up to reset the kalman filter next time Iterate is called. |
| 96 | void Reset(); |
| 97 | |
| 98 | private: |
| 99 | ShooterController wheel_; |
| 100 | |
| 101 | bool last_ready_ = false; |
| 102 | double min_ = 0.0; |
Austin Schuh | 932a5ce | 2017-03-05 01:04:18 -0800 | [diff] [blame] | 103 | ::aos::monotonic_clock::time_point last_time_ = |
| 104 | ::aos::monotonic_clock::min_time; |
Tyler Chatow | 2737d2a | 2017-02-08 21:20:51 -0800 | [diff] [blame] | 105 | |
| 106 | DISALLOW_COPY_AND_ASSIGN(Shooter); |
| 107 | }; |
| 108 | |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame] | 109 | } // namespace y2017::control_loops::superstructure::shooter |
Tyler Chatow | 2737d2a | 2017-02-08 21:20:51 -0800 | [diff] [blame] | 110 | |
| 111 | #endif // Y2017_CONTROL_LOOPS_SHOOTER_SHOOTER_H_ |