blob: 19fc7dd9522517917b0126b3f2f5097332b59eb2 [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
John Park33858a32018-09-28 23:05:48 -07007#include "aos/controls/control_loop.h"
8#include "aos/time/time.h"
Tyler Chatow2737d2a2017-02-08 21:20:51 -08009#include "frc971/control_loops/state_feedback_loop.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070010#include "Eigen/Dense"
Tyler Chatow2737d2a2017-02-08 21:20:51 -080011
12#include "y2017/control_loops/superstructure/shooter/shooter_integral_plant.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070013#include "y2017/control_loops/superstructure/superstructure_goal_generated.h"
14#include "y2017/control_loops/superstructure/superstructure_status_generated.h"
Tyler Chatow2737d2a2017-02-08 21:20:51 -080015
16namespace y2017 {
17namespace control_loops {
18namespace superstructure {
19namespace shooter {
20
21class ShooterController {
22 public:
23 ShooterController();
24
25 // 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.
Alex Perrycb7da4b2019-08-28 19:35:56 -070031 flatbuffers::Offset<ShooterStatus> BuildStatus(
32 flatbuffers::FlatBufferBuilder *fbb);
Tyler Chatow2737d2a2017-02-08 21:20:51 -080033
34 // Returns the control loop calculated voltage.
35 double voltage() const;
36
37 // Returns the instantaneous velocity.
Austin Schuhc66b6fc2017-03-25 19:56:59 -070038 double velocity() const { return loop_->X_hat(2, 0); }
39 double voltage_error() const { return loop_->X_hat(3, 0); }
Tyler Chatow2737d2a2017-02-08 21:20:51 -080040
41 double dt_velocity() const { return dt_velocity_; }
42
43 double error() const { return error_; }
44
45 // Executes the control loop for a cycle.
Austin Schuh932a5ce2017-03-05 01:04:18 -080046 void Update(bool disabled, ::std::chrono::nanoseconds dt);
Tyler Chatow2737d2a2017-02-08 21:20:51 -080047
48 // Resets the kalman filter and any other internal state.
49 void Reset();
50
Alex Perrycb7da4b2019-08-28 19:35:56 -070051 bool ready() { return ready_; }
52
Tyler Chatow2737d2a2017-02-08 21:20:51 -080053 private:
54 // The current sensor measurement.
55 Eigen::Matrix<double, 1, 1> Y_;
56 // The control loop.
Austin Schuh20388b62017-11-23 22:40:46 -080057 ::std::unique_ptr<
58 StateFeedbackLoop<4, 1, 1, double, StateFeedbackHybridPlant<4, 1, 1>,
59 HybridKalman<4, 1, 1>>>
Austin Schuh3ad5ed82017-02-25 21:36:19 -080060 loop_;
Tyler Chatow2737d2a2017-02-08 21:20:51 -080061
62 // History array for calculating a filtered angular velocity.
63 static constexpr int kHistoryLength = 5;
64 ::std::array<double, kHistoryLength> history_;
65 ptrdiff_t history_position_ = 0;
66
67 double error_ = 0.0;
Austin Schuh932a5ce2017-03-05 01:04:18 -080068 double dt_position_ = 0.0;
Tyler Chatow2737d2a2017-02-08 21:20:51 -080069 double dt_velocity_ = 0.0;
Austin Schuh932a5ce2017-03-05 01:04:18 -080070 double fixed_dt_velocity_ = 0.0;
Tyler Chatow2737d2a2017-02-08 21:20:51 -080071 double last_position_ = 0.0;
72 double average_angular_velocity_ = 0.0;
73 double min_velocity_ = 0.0;
74 double position_error_ = 0.0;
75
Austin Schuhc66b6fc2017-03-25 19:56:59 -070076 Eigen::Matrix<double, 4, 1> X_hat_current_;
Tyler Chatow2737d2a2017-02-08 21:20:51 -080077
78 bool ready_ = false;
79 bool needs_reset_ = false;
80 bool reset_ = false;
81
82 bool last_ready_ = false;
83 DISALLOW_COPY_AND_ASSIGN(ShooterController);
84};
85
86class Shooter {
87 public:
88 Shooter() {}
89
90 // Iterates the shooter control loop one cycle. position and status must
91 // never be nullptr. goal can be nullptr if no goal exists, and output should
92 // be nullptr if disabled.
Alex Perrycb7da4b2019-08-28 19:35:56 -070093 flatbuffers::Offset<ShooterStatus> Iterate(
94 const ShooterGoalT *goal, const double position,
95 ::aos::monotonic_clock::time_point position_time, double *output,
96 flatbuffers::FlatBufferBuilder *fbb);
Tyler Chatow2737d2a2017-02-08 21:20:51 -080097
98 // Sets the shooter up to reset the kalman filter next time Iterate is called.
99 void Reset();
100
101 private:
102 ShooterController wheel_;
103
104 bool last_ready_ = false;
105 double min_ = 0.0;
Austin Schuh932a5ce2017-03-05 01:04:18 -0800106 ::aos::monotonic_clock::time_point last_time_ =
107 ::aos::monotonic_clock::min_time;
Tyler Chatow2737d2a2017-02-08 21:20:51 -0800108
109 DISALLOW_COPY_AND_ASSIGN(Shooter);
110};
111
112} // namespace shooter
113} // namespace superstructure
114} // namespace control_loops
115} // namespace y2017
116
117#endif // Y2017_CONTROL_LOOPS_SHOOTER_SHOOTER_H_