blob: b83ed24ee2c77176dd2bbf258fddd06fca925708 [file] [log] [blame]
Comran Morshed2a97bc82016-01-16 17:27:01 +00001#ifndef Y2016_CONTROL_LOOPS_SHOOTER_SHOOTER_H_
2#define Y2016_CONTROL_LOOPS_SHOOTER_SHOOTER_H_
3
4#include <memory>
5
Alex Perrycb7da4b2019-08-28 19:35:56 -07006#include "aos/events/event_loop.h"
John Park33858a32018-09-28 23:05:48 -07007#include "aos/time/time.h"
Philipp Schrader790cb542023-07-05 21:06:52 -07008#include "frc971/control_loops/control_loop.h"
Comran Morshed2a97bc82016-01-16 17:27:01 +00009#include "frc971/control_loops/state_feedback_loop.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070010#include "y2016/control_loops/shooter/shooter_goal_generated.h"
Austin Schuh55a13dc2019-01-27 22:39:03 -080011#include "y2016/control_loops/shooter/shooter_integral_plant.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070012#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 Morshed2a97bc82016-01-16 17:27:01 +000015
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -080016namespace y2016::control_loops::shooter {
Comran Morshed2a97bc82016-01-16 17:27:01 +000017
Comran Morshedcde50322016-01-18 15:10:36 +000018namespace {
Austin Schuh09c2b0b2016-02-13 15:53:16 -080019constexpr double kTolerance = 10.0;
Comran Morshedcde50322016-01-18 15:10:36 +000020} // namespace
21
Comran Morshedcde50322016-01-18 15:10:36 +000022class ShooterSide {
Comran Morshed2a97bc82016-01-16 17:27:01 +000023 public:
Comran Morshedcde50322016-01-18 15:10:36 +000024 ShooterSide();
Comran Morshed2a97bc82016-01-16 17:27:01 +000025
Austin Schuh09c2b0b2016-02-13 15:53:16 -080026 // 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 Perrycb7da4b2019-08-28 19:35:56 -070032 flatbuffers::Offset<ShooterSideStatus> SetStatus(
33 flatbuffers::FlatBufferBuilder *fbb);
Austin Schuh09c2b0b2016-02-13 15:53:16 -080034
35 // Returns the control loop calculated voltage.
36 double voltage() const;
37
Austin Schuh7eecd7c2016-02-28 21:59:05 -080038 // Returns the instantaneous velocity.
39 double velocity() const { return loop_->X_hat(1, 0); }
40
Austin Schuh09c2b0b2016-02-13 15:53:16 -080041 // Executes the control loop for a cycle.
42 void Update(bool disabled);
Comran Morshed2a97bc82016-01-16 17:27:01 +000043
44 private:
Austin Schuh09c2b0b2016-02-13 15:53:16 -080045 // 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 Morshedcde50322016-01-18 15:10:36 +000049
50 // History array for calculating a filtered angular velocity.
Austin Schuh09c2b0b2016-02-13 15:53:16 -080051 static constexpr int kHistoryLength = 10;
52 ::std::array<double, kHistoryLength> history_;
Austin Schuh7eecd7c2016-02-28 21:59:05 -080053 ptrdiff_t history_position_ = 0;
Comran Morshed2a97bc82016-01-16 17:27:01 +000054
Comran Morshedcde50322016-01-18 15:10:36 +000055 DISALLOW_COPY_AND_ASSIGN(ShooterSide);
56};
Comran Morshed2a97bc82016-01-16 17:27:01 +000057
Alex Perrycb7da4b2019-08-28 19:35:56 -070058class Shooter
James Kuszmaul61750662021-06-21 21:32:33 -070059 : public ::frc971::controls::ControlLoop<Goal, Position, Status, Output> {
Comran Morshedcde50322016-01-18 15:10:36 +000060 public:
Alex Perrycb7da4b2019-08-28 19:35:56 -070061 explicit Shooter(::aos::EventLoop *event_loop,
62 const ::std::string &name = "/shooter");
Comran Morshedcde50322016-01-18 15:10:36 +000063
Austin Schuh7eecd7c2016-02-28 21:59:05 -080064 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 Schuhf59b8ee2016-03-19 21:31:36 -070071 // Increment the shot count for the Status.
72 INCREMENT_SHOT_COUNT = 3,
Austin Schuh7eecd7c2016-02-28 21:59:05 -080073 // Wait until the button is released.
Austin Schuhf59b8ee2016-03-19 21:31:36 -070074 WAITING_FOR_SHOT_NEGEDGE = 4,
Austin Schuh7eecd7c2016-02-28 21:59:05 -080075 };
76
Comran Morshedcde50322016-01-18 15:10:36 +000077 protected:
Alex Perrycb7da4b2019-08-28 19:35:56 -070078 void RunIteration(const Goal *goal, const Position *position,
79 aos::Sender<Output>::Builder *output,
80 aos::Sender<Status>::Builder *status) override;
Comran Morshedcde50322016-01-18 15:10:36 +000081
82 private:
83 ShooterSide left_, right_;
Comran Morshed2a97bc82016-01-16 17:27:01 +000084
Austin Schuhf59b8ee2016-03-19 21:31:36 -070085 // The number of shots since starting the Shooter.
86 uint32_t shots_;
87
Austin Schuh7eecd7c2016-02-28 21:59:05 -080088 // Current state.
89 ShooterLatchState state_ = ShooterLatchState::PASS_THROUGH;
Austin Schuh94a54102016-11-26 15:14:35 -080090 ::aos::monotonic_clock::time_point last_pre_shot_timeout_;
Austin Schuh7eecd7c2016-02-28 21:59:05 -080091
Comran Morshed2a97bc82016-01-16 17:27:01 +000092 DISALLOW_COPY_AND_ASSIGN(Shooter);
93};
94
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -080095} // namespace y2016::control_loops::shooter
Comran Morshed2a97bc82016-01-16 17:27:01 +000096
97#endif // Y2016_CONTROL_LOOPS_SHOOTER_SHOOTER_H_