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