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