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