Comran Morshed | 2a97bc8 | 2016-01-16 17:27:01 +0000 | [diff] [blame] | 1 | #ifndef Y2016_CONTROL_LOOPS_SHOOTER_SHOOTER_H_ |
| 2 | #define Y2016_CONTROL_LOOPS_SHOOTER_SHOOTER_H_ |
| 3 | |
| 4 | #include <memory> |
| 5 | |
| 6 | #include "aos/common/controls/control_loop.h" |
Austin Schuh | 7eecd7c | 2016-02-28 21:59:05 -0800 | [diff] [blame] | 7 | #include "aos/common/time.h" |
Comran Morshed | 2a97bc8 | 2016-01-16 17:27:01 +0000 | [diff] [blame] | 8 | #include "frc971/control_loops/state_feedback_loop.h" |
| 9 | |
Austin Schuh | 09c2b0b | 2016-02-13 15:53:16 -0800 | [diff] [blame] | 10 | #include "y2016/control_loops/shooter/shooter_integral_plant.h" |
Comran Morshed | 2a97bc8 | 2016-01-16 17:27:01 +0000 | [diff] [blame] | 11 | #include "y2016/control_loops/shooter/shooter.q.h" |
| 12 | |
| 13 | namespace y2016 { |
| 14 | namespace control_loops { |
Austin Schuh | 09c2b0b | 2016-02-13 15:53:16 -0800 | [diff] [blame] | 15 | namespace shooter { |
Comran Morshed | 2a97bc8 | 2016-01-16 17:27:01 +0000 | [diff] [blame] | 16 | |
Comran Morshed | cde5032 | 2016-01-18 15:10:36 +0000 | [diff] [blame] | 17 | namespace { |
Austin Schuh | 09c2b0b | 2016-02-13 15:53:16 -0800 | [diff] [blame] | 18 | constexpr double kTolerance = 10.0; |
Comran Morshed | cde5032 | 2016-01-18 15:10:36 +0000 | [diff] [blame] | 19 | } // namespace |
| 20 | |
Comran Morshed | cde5032 | 2016-01-18 15:10:36 +0000 | [diff] [blame] | 21 | class ShooterSide { |
Comran Morshed | 2a97bc8 | 2016-01-16 17:27:01 +0000 | [diff] [blame] | 22 | public: |
Comran Morshed | cde5032 | 2016-01-18 15:10:36 +0000 | [diff] [blame] | 23 | ShooterSide(); |
Comran Morshed | 2a97bc8 | 2016-01-16 17:27:01 +0000 | [diff] [blame] | 24 | |
Austin Schuh | 09c2b0b | 2016-02-13 15:53:16 -0800 | [diff] [blame] | 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. |
| 31 | void SetStatus(ShooterSideStatus *status); |
| 32 | |
| 33 | // Returns the control loop calculated voltage. |
| 34 | double voltage() const; |
| 35 | |
Austin Schuh | 7eecd7c | 2016-02-28 21:59:05 -0800 | [diff] [blame] | 36 | // Returns the instantaneous velocity. |
| 37 | double velocity() const { return loop_->X_hat(1, 0); } |
| 38 | |
Austin Schuh | 09c2b0b | 2016-02-13 15:53:16 -0800 | [diff] [blame] | 39 | // Executes the control loop for a cycle. |
| 40 | void Update(bool disabled); |
Comran Morshed | 2a97bc8 | 2016-01-16 17:27:01 +0000 | [diff] [blame] | 41 | |
| 42 | private: |
Austin Schuh | 09c2b0b | 2016-02-13 15:53:16 -0800 | [diff] [blame] | 43 | // The current sensor measurement. |
| 44 | Eigen::Matrix<double, 1, 1> Y_; |
| 45 | // The control loop. |
| 46 | ::std::unique_ptr<StateFeedbackLoop<3, 1, 1>> loop_; |
Comran Morshed | cde5032 | 2016-01-18 15:10:36 +0000 | [diff] [blame] | 47 | |
| 48 | // History array for calculating a filtered angular velocity. |
Austin Schuh | 09c2b0b | 2016-02-13 15:53:16 -0800 | [diff] [blame] | 49 | static constexpr int kHistoryLength = 10; |
| 50 | ::std::array<double, kHistoryLength> history_; |
Austin Schuh | 7eecd7c | 2016-02-28 21:59:05 -0800 | [diff] [blame] | 51 | ptrdiff_t history_position_ = 0; |
Comran Morshed | 2a97bc8 | 2016-01-16 17:27:01 +0000 | [diff] [blame] | 52 | |
Comran Morshed | cde5032 | 2016-01-18 15:10:36 +0000 | [diff] [blame] | 53 | DISALLOW_COPY_AND_ASSIGN(ShooterSide); |
| 54 | }; |
Comran Morshed | 2a97bc8 | 2016-01-16 17:27:01 +0000 | [diff] [blame] | 55 | |
Austin Schuh | 09c2b0b | 2016-02-13 15:53:16 -0800 | [diff] [blame] | 56 | class Shooter : public ::aos::controls::ControlLoop<ShooterQueue> { |
Comran Morshed | cde5032 | 2016-01-18 15:10:36 +0000 | [diff] [blame] | 57 | public: |
Austin Schuh | 09c2b0b | 2016-02-13 15:53:16 -0800 | [diff] [blame] | 58 | explicit Shooter( |
| 59 | ShooterQueue *shooter_queue = &control_loops::shooter::shooter_queue); |
Comran Morshed | cde5032 | 2016-01-18 15:10:36 +0000 | [diff] [blame] | 60 | |
Austin Schuh | 7eecd7c | 2016-02-28 21:59:05 -0800 | [diff] [blame] | 61 | enum class ShooterLatchState { |
| 62 | // Any shoot commands will be passed through without modification. |
| 63 | PASS_THROUGH = 0, |
| 64 | // We are latched shooting waiting for the wheel to loose RPM. |
| 65 | WAITING_FOR_SPINDOWN = 1, |
| 66 | // We are latched shooting waiting for the wheel to spin back up. |
| 67 | WAITING_FOR_SPINUP = 2, |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame^] | 68 | // Increment the shot count for the Status. |
| 69 | INCREMENT_SHOT_COUNT = 3, |
Austin Schuh | 7eecd7c | 2016-02-28 21:59:05 -0800 | [diff] [blame] | 70 | // Wait until the button is released. |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame^] | 71 | WAITING_FOR_SHOT_NEGEDGE = 4, |
Austin Schuh | 7eecd7c | 2016-02-28 21:59:05 -0800 | [diff] [blame] | 72 | }; |
| 73 | |
Comran Morshed | cde5032 | 2016-01-18 15:10:36 +0000 | [diff] [blame] | 74 | protected: |
Austin Schuh | 09c2b0b | 2016-02-13 15:53:16 -0800 | [diff] [blame] | 75 | void RunIteration(const ShooterQueue::Goal *goal, |
| 76 | const ShooterQueue::Position *position, |
| 77 | ShooterQueue::Output *output, |
| 78 | ShooterQueue::Status *status) override; |
Comran Morshed | cde5032 | 2016-01-18 15:10:36 +0000 | [diff] [blame] | 79 | |
| 80 | private: |
| 81 | ShooterSide left_, right_; |
Comran Morshed | 2a97bc8 | 2016-01-16 17:27:01 +0000 | [diff] [blame] | 82 | |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame^] | 83 | // The number of shots since starting the Shooter. |
| 84 | uint32_t shots_; |
| 85 | |
Austin Schuh | 7eecd7c | 2016-02-28 21:59:05 -0800 | [diff] [blame] | 86 | // Current state. |
| 87 | ShooterLatchState state_ = ShooterLatchState::PASS_THROUGH; |
| 88 | ::aos::time::Time last_pre_shot_timeout_; |
| 89 | |
Comran Morshed | 2a97bc8 | 2016-01-16 17:27:01 +0000 | [diff] [blame] | 90 | DISALLOW_COPY_AND_ASSIGN(Shooter); |
| 91 | }; |
| 92 | |
Austin Schuh | 09c2b0b | 2016-02-13 15:53:16 -0800 | [diff] [blame] | 93 | } // namespace shooter |
Comran Morshed | 2a97bc8 | 2016-01-16 17:27:01 +0000 | [diff] [blame] | 94 | } // namespace control_loops |
| 95 | } // namespace y2016 |
| 96 | |
| 97 | #endif // Y2016_CONTROL_LOOPS_SHOOTER_SHOOTER_H_ |