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, |
| 68 | // Wait until the button is released. |
| 69 | WAITING_FOR_SHOT_NEGEDGE = 3 |
| 70 | }; |
| 71 | |
Comran Morshed | cde5032 | 2016-01-18 15:10:36 +0000 | [diff] [blame] | 72 | protected: |
Austin Schuh | 09c2b0b | 2016-02-13 15:53:16 -0800 | [diff] [blame] | 73 | void RunIteration(const ShooterQueue::Goal *goal, |
| 74 | const ShooterQueue::Position *position, |
| 75 | ShooterQueue::Output *output, |
| 76 | ShooterQueue::Status *status) override; |
Comran Morshed | cde5032 | 2016-01-18 15:10:36 +0000 | [diff] [blame] | 77 | |
| 78 | private: |
| 79 | ShooterSide left_, right_; |
Comran Morshed | 2a97bc8 | 2016-01-16 17:27:01 +0000 | [diff] [blame] | 80 | |
Austin Schuh | 7eecd7c | 2016-02-28 21:59:05 -0800 | [diff] [blame^] | 81 | // Current state. |
| 82 | ShooterLatchState state_ = ShooterLatchState::PASS_THROUGH; |
| 83 | ::aos::time::Time last_pre_shot_timeout_; |
| 84 | |
Comran Morshed | 2a97bc8 | 2016-01-16 17:27:01 +0000 | [diff] [blame] | 85 | DISALLOW_COPY_AND_ASSIGN(Shooter); |
| 86 | }; |
| 87 | |
Austin Schuh | 09c2b0b | 2016-02-13 15:53:16 -0800 | [diff] [blame] | 88 | } // namespace shooter |
Comran Morshed | 2a97bc8 | 2016-01-16 17:27:01 +0000 | [diff] [blame] | 89 | } // namespace control_loops |
| 90 | } // namespace y2016 |
| 91 | |
| 92 | #endif // Y2016_CONTROL_LOOPS_SHOOTER_SHOOTER_H_ |