blob: 9e6968fe236d245060ab09923c170bd50c33a3fe [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
John Park33858a32018-09-28 23:05:48 -07006#include "aos/controls/control_loop.h"
Austin Schuh55a13dc2019-01-27 22:39:03 -08007#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"
Comran Morshed2a97bc82016-01-16 17:27:01 +000010#include "y2016/control_loops/shooter/shooter.q.h"
Austin Schuh55a13dc2019-01-27 22:39:03 -080011#include "y2016/control_loops/shooter/shooter_integral_plant.h"
Comran Morshed2a97bc82016-01-16 17:27:01 +000012
13namespace y2016 {
14namespace control_loops {
Austin Schuh09c2b0b2016-02-13 15:53:16 -080015namespace shooter {
Comran Morshed2a97bc82016-01-16 17:27:01 +000016
Comran Morshedcde50322016-01-18 15:10:36 +000017namespace {
Austin Schuh09c2b0b2016-02-13 15:53:16 -080018constexpr double kTolerance = 10.0;
Comran Morshedcde50322016-01-18 15:10:36 +000019} // namespace
20
Comran Morshedcde50322016-01-18 15:10:36 +000021class ShooterSide {
Comran Morshed2a97bc82016-01-16 17:27:01 +000022 public:
Comran Morshedcde50322016-01-18 15:10:36 +000023 ShooterSide();
Comran Morshed2a97bc82016-01-16 17:27:01 +000024
Austin Schuh09c2b0b2016-02-13 15:53:16 -080025 // 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 Schuh7eecd7c2016-02-28 21:59:05 -080036 // Returns the instantaneous velocity.
37 double velocity() const { return loop_->X_hat(1, 0); }
38
Austin Schuh09c2b0b2016-02-13 15:53:16 -080039 // Executes the control loop for a cycle.
40 void Update(bool disabled);
Comran Morshed2a97bc82016-01-16 17:27:01 +000041
42 private:
Austin Schuh09c2b0b2016-02-13 15:53:16 -080043 // 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 Morshedcde50322016-01-18 15:10:36 +000047
48 // History array for calculating a filtered angular velocity.
Austin Schuh09c2b0b2016-02-13 15:53:16 -080049 static constexpr int kHistoryLength = 10;
50 ::std::array<double, kHistoryLength> history_;
Austin Schuh7eecd7c2016-02-28 21:59:05 -080051 ptrdiff_t history_position_ = 0;
Comran Morshed2a97bc82016-01-16 17:27:01 +000052
Comran Morshedcde50322016-01-18 15:10:36 +000053 DISALLOW_COPY_AND_ASSIGN(ShooterSide);
54};
Comran Morshed2a97bc82016-01-16 17:27:01 +000055
Austin Schuh09c2b0b2016-02-13 15:53:16 -080056class Shooter : public ::aos::controls::ControlLoop<ShooterQueue> {
Comran Morshedcde50322016-01-18 15:10:36 +000057 public:
Austin Schuh09c2b0b2016-02-13 15:53:16 -080058 explicit Shooter(
Austin Schuh55a13dc2019-01-27 22:39:03 -080059 ::aos::EventLoop *event_loop,
60 const ::std::string &name = ".y2016.control_loops.shooter.shooter_queue");
Comran Morshedcde50322016-01-18 15:10:36 +000061
Austin Schuh7eecd7c2016-02-28 21:59:05 -080062 enum class ShooterLatchState {
63 // Any shoot commands will be passed through without modification.
64 PASS_THROUGH = 0,
65 // We are latched shooting waiting for the wheel to loose RPM.
66 WAITING_FOR_SPINDOWN = 1,
67 // We are latched shooting waiting for the wheel to spin back up.
68 WAITING_FOR_SPINUP = 2,
Austin Schuhf59b8ee2016-03-19 21:31:36 -070069 // Increment the shot count for the Status.
70 INCREMENT_SHOT_COUNT = 3,
Austin Schuh7eecd7c2016-02-28 21:59:05 -080071 // Wait until the button is released.
Austin Schuhf59b8ee2016-03-19 21:31:36 -070072 WAITING_FOR_SHOT_NEGEDGE = 4,
Austin Schuh7eecd7c2016-02-28 21:59:05 -080073 };
74
Comran Morshedcde50322016-01-18 15:10:36 +000075 protected:
Austin Schuh09c2b0b2016-02-13 15:53:16 -080076 void RunIteration(const ShooterQueue::Goal *goal,
77 const ShooterQueue::Position *position,
78 ShooterQueue::Output *output,
79 ShooterQueue::Status *status) override;
Comran Morshedcde50322016-01-18 15:10:36 +000080
81 private:
82 ShooterSide left_, right_;
Comran Morshed2a97bc82016-01-16 17:27:01 +000083
Austin Schuhf59b8ee2016-03-19 21:31:36 -070084 // The number of shots since starting the Shooter.
85 uint32_t shots_;
86
Austin Schuh7eecd7c2016-02-28 21:59:05 -080087 // Current state.
88 ShooterLatchState state_ = ShooterLatchState::PASS_THROUGH;
Austin Schuh94a54102016-11-26 15:14:35 -080089 ::aos::monotonic_clock::time_point last_pre_shot_timeout_;
Austin Schuh7eecd7c2016-02-28 21:59:05 -080090
Comran Morshed2a97bc82016-01-16 17:27:01 +000091 DISALLOW_COPY_AND_ASSIGN(Shooter);
92};
93
Austin Schuh09c2b0b2016-02-13 15:53:16 -080094} // namespace shooter
Comran Morshed2a97bc82016-01-16 17:27:01 +000095} // namespace control_loops
96} // namespace y2016
97
98#endif // Y2016_CONTROL_LOOPS_SHOOTER_SHOOTER_H_