blob: d13d94c8efef5fe9549a261b2ace9d799403acaf [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.
Austin Schuhc66b6fc2017-03-25 19:56:59 -070036 double velocity() const { return loop_->X_hat(2, 0); }
37 double voltage_error() const { return loop_->X_hat(3, 0); }
Tyler Chatow2737d2a2017-02-08 21:20:51 -080038
39 double dt_velocity() const { return dt_velocity_; }
40
41 double error() const { return error_; }
42
43 // Executes the control loop for a cycle.
Austin Schuh932a5ce2017-03-05 01:04:18 -080044 void Update(bool disabled, ::std::chrono::nanoseconds dt);
Tyler Chatow2737d2a2017-02-08 21:20:51 -080045
46 // Resets the kalman filter and any other internal state.
47 void Reset();
48
49 private:
50 // The current sensor measurement.
51 Eigen::Matrix<double, 1, 1> Y_;
52 // The control loop.
Austin Schuh3ad5ed82017-02-25 21:36:19 -080053 ::std::unique_ptr<StateFeedbackLoop<
Austin Schuhc66b6fc2017-03-25 19:56:59 -070054 4, 1, 1, StateFeedbackHybridPlant<4, 1, 1>, HybridKalman<4, 1, 1>>>
Austin Schuh3ad5ed82017-02-25 21:36:19 -080055 loop_;
Tyler Chatow2737d2a2017-02-08 21:20:51 -080056
57 // History array for calculating a filtered angular velocity.
58 static constexpr int kHistoryLength = 5;
59 ::std::array<double, kHistoryLength> history_;
60 ptrdiff_t history_position_ = 0;
61
62 double error_ = 0.0;
Austin Schuh932a5ce2017-03-05 01:04:18 -080063 double dt_position_ = 0.0;
Tyler Chatow2737d2a2017-02-08 21:20:51 -080064 double dt_velocity_ = 0.0;
Austin Schuh932a5ce2017-03-05 01:04:18 -080065 double fixed_dt_velocity_ = 0.0;
Tyler Chatow2737d2a2017-02-08 21:20:51 -080066 double last_position_ = 0.0;
67 double average_angular_velocity_ = 0.0;
68 double min_velocity_ = 0.0;
69 double position_error_ = 0.0;
70
Austin Schuhc66b6fc2017-03-25 19:56:59 -070071 Eigen::Matrix<double, 4, 1> X_hat_current_;
Tyler Chatow2737d2a2017-02-08 21:20:51 -080072
73 bool ready_ = false;
74 bool needs_reset_ = false;
75 bool reset_ = false;
76
77 bool last_ready_ = false;
78 DISALLOW_COPY_AND_ASSIGN(ShooterController);
79};
80
81class Shooter {
82 public:
83 Shooter() {}
84
85 // Iterates the shooter control loop one cycle. position and status must
86 // never be nullptr. goal can be nullptr if no goal exists, and output should
87 // be nullptr if disabled.
88 void Iterate(const control_loops::ShooterGoal *goal, const double *position,
Austin Schuh932a5ce2017-03-05 01:04:18 -080089 ::aos::monotonic_clock::time_point position_time, double *output,
90 control_loops::ShooterStatus *status);
Tyler Chatow2737d2a2017-02-08 21:20:51 -080091
92 // Sets the shooter up to reset the kalman filter next time Iterate is called.
93 void Reset();
94
95 private:
96 ShooterController wheel_;
97
98 bool last_ready_ = false;
99 double min_ = 0.0;
Austin Schuh932a5ce2017-03-05 01:04:18 -0800100 ::aos::monotonic_clock::time_point last_time_ =
101 ::aos::monotonic_clock::min_time;
Tyler Chatow2737d2a2017-02-08 21:20:51 -0800102
103 DISALLOW_COPY_AND_ASSIGN(Shooter);
104};
105
106} // namespace shooter
107} // namespace superstructure
108} // namespace control_loops
109} // namespace y2017
110
111#endif // Y2017_CONTROL_LOOPS_SHOOTER_SHOOTER_H_