blob: d3a2f862375abf6fe8f4d0748075f64a4acafaf9 [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
6#include "aos/common/controls/control_loop.h"
Austin Schuh7eecd7c2016-02-28 21:59:05 -08007#include "aos/common/time.h"
Comran Morshed2a97bc82016-01-16 17:27:01 +00008#include "frc971/control_loops/state_feedback_loop.h"
9
Austin Schuh09c2b0b2016-02-13 15:53:16 -080010#include "y2016/control_loops/shooter/shooter_integral_plant.h"
Comran Morshed2a97bc82016-01-16 17:27:01 +000011#include "y2016/control_loops/shooter/shooter.q.h"
12
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(
59 ShooterQueue *shooter_queue = &control_loops::shooter::shooter_queue);
Comran Morshedcde50322016-01-18 15:10:36 +000060
Austin Schuh7eecd7c2016-02-28 21:59:05 -080061 enum class ShooterLatchState {
62 // Any shoot commands will be passed through without modification.
63 PASS_THROUGH = 0,
64 // We are latched shooting waiting for the wheel to loose RPM.
65 WAITING_FOR_SPINDOWN = 1,
66 // We are latched shooting waiting for the wheel to spin back up.
67 WAITING_FOR_SPINUP = 2,
68 // Wait until the button is released.
69 WAITING_FOR_SHOT_NEGEDGE = 3
70 };
71
Comran Morshedcde50322016-01-18 15:10:36 +000072 protected:
Austin Schuh09c2b0b2016-02-13 15:53:16 -080073 void RunIteration(const ShooterQueue::Goal *goal,
74 const ShooterQueue::Position *position,
75 ShooterQueue::Output *output,
76 ShooterQueue::Status *status) override;
Comran Morshedcde50322016-01-18 15:10:36 +000077
78 private:
79 ShooterSide left_, right_;
Comran Morshed2a97bc82016-01-16 17:27:01 +000080
Austin Schuh7eecd7c2016-02-28 21:59:05 -080081 // Current state.
82 ShooterLatchState state_ = ShooterLatchState::PASS_THROUGH;
83 ::aos::time::Time last_pre_shot_timeout_;
84
Comran Morshed2a97bc82016-01-16 17:27:01 +000085 DISALLOW_COPY_AND_ASSIGN(Shooter);
86};
87
Austin Schuh09c2b0b2016-02-13 15:53:16 -080088} // namespace shooter
Comran Morshed2a97bc82016-01-16 17:27:01 +000089} // namespace control_loops
90} // namespace y2016
91
92#endif // Y2016_CONTROL_LOOPS_SHOOTER_SHOOTER_H_