blob: c29869c7d8ef29df0fce324db8b34e2e283d3855 [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
James Kuszmaul61750662021-06-21 21:32:33 -07006#include "frc971/control_loops/control_loop.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -07007#include "aos/events/event_loop.h"
John Park33858a32018-09-28 23:05:48 -07008#include "aos/time/time.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
16namespace y2016 {
17namespace control_loops {
Austin Schuh09c2b0b2016-02-13 15:53:16 -080018namespace shooter {
Comran Morshed2a97bc82016-01-16 17:27:01 +000019
Comran Morshedcde50322016-01-18 15:10:36 +000020namespace {
Austin Schuh09c2b0b2016-02-13 15:53:16 -080021constexpr double kTolerance = 10.0;
Comran Morshedcde50322016-01-18 15:10:36 +000022} // namespace
23
Comran Morshedcde50322016-01-18 15:10:36 +000024class ShooterSide {
Comran Morshed2a97bc82016-01-16 17:27:01 +000025 public:
Comran Morshedcde50322016-01-18 15:10:36 +000026 ShooterSide();
Comran Morshed2a97bc82016-01-16 17:27:01 +000027
Austin Schuh09c2b0b2016-02-13 15:53:16 -080028 // 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 Perrycb7da4b2019-08-28 19:35:56 -070034 flatbuffers::Offset<ShooterSideStatus> SetStatus(
35 flatbuffers::FlatBufferBuilder *fbb);
Austin Schuh09c2b0b2016-02-13 15:53:16 -080036
37 // Returns the control loop calculated voltage.
38 double voltage() const;
39
Austin Schuh7eecd7c2016-02-28 21:59:05 -080040 // Returns the instantaneous velocity.
41 double velocity() const { return loop_->X_hat(1, 0); }
42
Austin Schuh09c2b0b2016-02-13 15:53:16 -080043 // Executes the control loop for a cycle.
44 void Update(bool disabled);
Comran Morshed2a97bc82016-01-16 17:27:01 +000045
46 private:
Austin Schuh09c2b0b2016-02-13 15:53:16 -080047 // 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 Morshedcde50322016-01-18 15:10:36 +000051
52 // History array for calculating a filtered angular velocity.
Austin Schuh09c2b0b2016-02-13 15:53:16 -080053 static constexpr int kHistoryLength = 10;
54 ::std::array<double, kHistoryLength> history_;
Austin Schuh7eecd7c2016-02-28 21:59:05 -080055 ptrdiff_t history_position_ = 0;
Comran Morshed2a97bc82016-01-16 17:27:01 +000056
Comran Morshedcde50322016-01-18 15:10:36 +000057 DISALLOW_COPY_AND_ASSIGN(ShooterSide);
58};
Comran Morshed2a97bc82016-01-16 17:27:01 +000059
Alex Perrycb7da4b2019-08-28 19:35:56 -070060class Shooter
James Kuszmaul61750662021-06-21 21:32:33 -070061 : public ::frc971::controls::ControlLoop<Goal, Position, Status, Output> {
Comran Morshedcde50322016-01-18 15:10:36 +000062 public:
Alex Perrycb7da4b2019-08-28 19:35:56 -070063 explicit Shooter(::aos::EventLoop *event_loop,
64 const ::std::string &name = "/shooter");
Comran Morshedcde50322016-01-18 15:10:36 +000065
Austin Schuh7eecd7c2016-02-28 21:59:05 -080066 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 Schuhf59b8ee2016-03-19 21:31:36 -070073 // Increment the shot count for the Status.
74 INCREMENT_SHOT_COUNT = 3,
Austin Schuh7eecd7c2016-02-28 21:59:05 -080075 // Wait until the button is released.
Austin Schuhf59b8ee2016-03-19 21:31:36 -070076 WAITING_FOR_SHOT_NEGEDGE = 4,
Austin Schuh7eecd7c2016-02-28 21:59:05 -080077 };
78
Comran Morshedcde50322016-01-18 15:10:36 +000079 protected:
Alex Perrycb7da4b2019-08-28 19:35:56 -070080 void RunIteration(const Goal *goal, const Position *position,
81 aos::Sender<Output>::Builder *output,
82 aos::Sender<Status>::Builder *status) override;
Comran Morshedcde50322016-01-18 15:10:36 +000083
84 private:
85 ShooterSide left_, right_;
Comran Morshed2a97bc82016-01-16 17:27:01 +000086
Austin Schuhf59b8ee2016-03-19 21:31:36 -070087 // The number of shots since starting the Shooter.
88 uint32_t shots_;
89
Austin Schuh7eecd7c2016-02-28 21:59:05 -080090 // Current state.
91 ShooterLatchState state_ = ShooterLatchState::PASS_THROUGH;
Austin Schuh94a54102016-11-26 15:14:35 -080092 ::aos::monotonic_clock::time_point last_pre_shot_timeout_;
Austin Schuh7eecd7c2016-02-28 21:59:05 -080093
Comran Morshed2a97bc82016-01-16 17:27:01 +000094 DISALLOW_COPY_AND_ASSIGN(Shooter);
95};
96
Austin Schuh09c2b0b2016-02-13 15:53:16 -080097} // namespace shooter
Comran Morshed2a97bc82016-01-16 17:27:01 +000098} // namespace control_loops
99} // namespace y2016
100
101#endif // Y2016_CONTROL_LOOPS_SHOOTER_SHOOTER_H_