blob: 7dc44bf0c293e2cf9254e7eb2b8a39a5d0b40fe1 [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.
Austin Schuh3ad5ed82017-02-25 21:36:19 -080052 ::std::unique_ptr<StateFeedbackLoop<
53 3, 1, 1, StateFeedbackHybridPlant<3, 1, 1>, HybridKalman<3, 1, 1>>>
54 loop_;
Tyler Chatow2737d2a2017-02-08 21:20:51 -080055
56 // History array for calculating a filtered angular velocity.
57 static constexpr int kHistoryLength = 5;
58 ::std::array<double, kHistoryLength> history_;
59 ptrdiff_t history_position_ = 0;
60
61 double error_ = 0.0;
62 double dt_velocity_ = 0.0;
63 double last_position_ = 0.0;
64 double average_angular_velocity_ = 0.0;
65 double min_velocity_ = 0.0;
66 double position_error_ = 0.0;
67
68 Eigen::Matrix<double, 3, 1> X_hat_current_;
69
70 bool ready_ = false;
71 bool needs_reset_ = false;
72 bool reset_ = false;
73
74 bool last_ready_ = false;
75 DISALLOW_COPY_AND_ASSIGN(ShooterController);
76};
77
78class Shooter {
79 public:
80 Shooter() {}
81
82 // Iterates the shooter control loop one cycle. position and status must
83 // never be nullptr. goal can be nullptr if no goal exists, and output should
84 // be nullptr if disabled.
85 void Iterate(const control_loops::ShooterGoal *goal, const double *position,
86 double *output, control_loops::ShooterStatus *status);
87
88 // Sets the shooter up to reset the kalman filter next time Iterate is called.
89 void Reset();
90
91 private:
92 ShooterController wheel_;
93
94 bool last_ready_ = false;
95 double min_ = 0.0;
96
97 DISALLOW_COPY_AND_ASSIGN(Shooter);
98};
99
100} // namespace shooter
101} // namespace superstructure
102} // namespace control_loops
103} // namespace y2017
104
105#endif // Y2017_CONTROL_LOOPS_SHOOTER_SHOOTER_H_