blob: 0b515f19768481730ed11e25863476891a88e38a [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,
Austin Schuhf59b8ee2016-03-19 21:31:36 -070068 // Increment the shot count for the Status.
69 INCREMENT_SHOT_COUNT = 3,
Austin Schuh7eecd7c2016-02-28 21:59:05 -080070 // Wait until the button is released.
Austin Schuhf59b8ee2016-03-19 21:31:36 -070071 WAITING_FOR_SHOT_NEGEDGE = 4,
Austin Schuh7eecd7c2016-02-28 21:59:05 -080072 };
73
Comran Morshedcde50322016-01-18 15:10:36 +000074 protected:
Austin Schuh09c2b0b2016-02-13 15:53:16 -080075 void RunIteration(const ShooterQueue::Goal *goal,
76 const ShooterQueue::Position *position,
77 ShooterQueue::Output *output,
78 ShooterQueue::Status *status) override;
Comran Morshedcde50322016-01-18 15:10:36 +000079
80 private:
81 ShooterSide left_, right_;
Comran Morshed2a97bc82016-01-16 17:27:01 +000082
Austin Schuhf59b8ee2016-03-19 21:31:36 -070083 // The number of shots since starting the Shooter.
84 uint32_t shots_;
85
Austin Schuh7eecd7c2016-02-28 21:59:05 -080086 // Current state.
87 ShooterLatchState state_ = ShooterLatchState::PASS_THROUGH;
88 ::aos::time::Time last_pre_shot_timeout_;
89
Comran Morshed2a97bc82016-01-16 17:27:01 +000090 DISALLOW_COPY_AND_ASSIGN(Shooter);
91};
92
Austin Schuh09c2b0b2016-02-13 15:53:16 -080093} // namespace shooter
Comran Morshed2a97bc82016-01-16 17:27:01 +000094} // namespace control_loops
95} // namespace y2016
96
97#endif // Y2016_CONTROL_LOOPS_SHOOTER_SHOOTER_H_