blob: 7f11267376fafc0552d5625c94d3064038b90646 [file] [log] [blame]
Tyler Chatow2737d2a2017-02-08 21:20:51 -08001#ifndef Y2017_CONTROL_LOOPS_SHOOTER_SHOOTER_H_
2#define Y2017_CONTROL_LOOPS_SHOOTER_SHOOTER_H_
3
4#include <memory>
5
6#include "aos/common/controls/control_loop.h"
7#include "aos/common/time.h"
8#include "frc971/control_loops/state_feedback_loop.h"
9
10#include "y2017/control_loops/superstructure/shooter/shooter_integral_plant.h"
11#include "y2017/control_loops/superstructure/superstructure.q.h"
12
13namespace y2017 {
14namespace control_loops {
15namespace superstructure {
16namespace shooter {
17
18class ShooterController {
19 public:
20 ShooterController();
21
22 // Sets the velocity goal in radians/sec
23 void set_goal(double angular_velocity_goal);
24 // Sets the current encoder position in radians
25 void set_position(double current_position);
26
27 // Populates the status structure.
28 void SetStatus(control_loops::ShooterStatus *status);
29
30 // Returns the control loop calculated voltage.
31 double voltage() const;
32
33 // Returns the instantaneous velocity.
34 double velocity() const { return loop_->X_hat(1, 0); }
35
36 double dt_velocity() const { return dt_velocity_; }
37
38 double error() const { return error_; }
39
40 // Executes the control loop for a cycle.
41 void Update(bool disabled);
42
43 // Resets the kalman filter and any other internal state.
44 void Reset();
45
46 private:
47 // The current sensor measurement.
48 Eigen::Matrix<double, 1, 1> Y_;
49 // The control loop.
50 ::std::unique_ptr<StateFeedbackLoop<3, 1, 1>> loop_;
51
52 // History array for calculating a filtered angular velocity.
53 static constexpr int kHistoryLength = 5;
54 ::std::array<double, kHistoryLength> history_;
55 ptrdiff_t history_position_ = 0;
56
57 double error_ = 0.0;
58 double dt_velocity_ = 0.0;
59 double last_position_ = 0.0;
60 double average_angular_velocity_ = 0.0;
61 double min_velocity_ = 0.0;
62 double position_error_ = 0.0;
63
64 Eigen::Matrix<double, 3, 1> X_hat_current_;
65
66 bool ready_ = false;
67 bool needs_reset_ = false;
68 bool reset_ = false;
69
70 bool last_ready_ = false;
71 DISALLOW_COPY_AND_ASSIGN(ShooterController);
72};
73
74class Shooter {
75 public:
76 Shooter() {}
77
78 // Iterates the shooter control loop one cycle. position and status must
79 // never be nullptr. goal can be nullptr if no goal exists, and output should
80 // be nullptr if disabled.
81 void Iterate(const control_loops::ShooterGoal *goal, const double *position,
82 double *output, control_loops::ShooterStatus *status);
83
84 // Sets the shooter up to reset the kalman filter next time Iterate is called.
85 void Reset();
86
87 private:
88 ShooterController wheel_;
89
90 bool last_ready_ = false;
91 double min_ = 0.0;
92
93 DISALLOW_COPY_AND_ASSIGN(Shooter);
94};
95
96} // namespace shooter
97} // namespace superstructure
98} // namespace control_loops
99} // namespace y2017
100
101#endif // Y2017_CONTROL_LOOPS_SHOOTER_SHOOTER_H_