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