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