blob: db8baeb1c4156054025013cbfa23701ba3763408 [file] [log] [blame]
Maxwell Henderson34242992024-01-07 12:39:11 -08001#ifndef FRC971_CONTROL_LOOPS_SHOOTER_FLYWHEEL_CONTROLLER_H_
2#define FRC971_CONTROL_LOOPS_SHOOTER_FLYWHEEL_CONTROLLER_H_
Sabina Davisedf89472020-02-17 15:27:37 -08003
4#include <memory>
5
Sabina Davisedf89472020-02-17 15:27:37 -08006#include "aos/time/time.h"
Philipp Schrader790cb542023-07-05 21:06:52 -07007#include "frc971/control_loops/control_loop.h"
Maxwell Henderson34242992024-01-07 12:39:11 -08008#include "frc971/control_loops/flywheel/flywheel_controller_status_generated.h"
9#include "frc971/control_loops/hybrid_state_feedback_loop.h"
Sabina Davisedf89472020-02-17 15:27:37 -080010#include "frc971/control_loops/state_feedback_loop.h"
Sabina Davisedf89472020-02-17 15:27:37 -080011
Maxwell Henderson34242992024-01-07 12:39:11 -080012namespace frc971 {
Sabina Davisedf89472020-02-17 15:27:37 -080013namespace control_loops {
Maxwell Henderson34242992024-01-07 12:39:11 -080014namespace flywheel {
Sabina Davisedf89472020-02-17 15:27:37 -080015
Austin Schuh80476772021-03-06 20:17:36 -080016class CurrentLimitedStateFeedbackController;
17
Sabina Davisedf89472020-02-17 15:27:37 -080018// Handles the velocity control of each flywheel.
19class FlywheelController {
20 public:
Austin Schuh43b9ae92020-02-29 23:08:38 -080021 FlywheelController(
22 StateFeedbackLoop<3, 1, 1, double, StateFeedbackHybridPlant<3, 1, 1>,
Austin Schuhe8ca06a2020-03-07 22:27:39 -080023 HybridKalman<3, 1, 1>> &&loop,
24 double bemf, double resistance);
Sabina Davisedf89472020-02-17 15:27:37 -080025
Austin Schuh80476772021-03-06 20:17:36 -080026 ~FlywheelController();
27
Sabina Davisedf89472020-02-17 15:27:37 -080028 // Sets the velocity goal in radians/sec
29 void set_goal(double angular_velocity_goal);
Austin Schuh263dead2021-04-04 21:19:19 -070030 double goal() const { return last_goal_; }
Sabina Davisedf89472020-02-17 15:27:37 -080031 // Sets the current encoder position in radians
Sabina Davis0f31d3f2020-02-20 20:41:00 -080032 void set_position(double current_position,
33 const aos::monotonic_clock::time_point position_timestamp);
Sabina Davisedf89472020-02-17 15:27:37 -080034
35 // Populates the status structure.
36 flatbuffers::Offset<FlywheelControllerStatus> SetStatus(
37 flatbuffers::FlatBufferBuilder *fbb);
38
39 // Returns the control loop calculated voltage.
40 double voltage() const;
41
Austin Schuh80476772021-03-06 20:17:36 -080042 // Returns the expected battery current for the last U.
43 double current() const;
44
Sabina Davisedf89472020-02-17 15:27:37 -080045 // Returns the instantaneous velocity.
Austin Schuh80476772021-03-06 20:17:36 -080046 double velocity() const;
Sabina Davisedf89472020-02-17 15:27:37 -080047
48 // Executes the control loop for a cycle.
49 void Update(bool disabled);
50
Sabina Davis0f31d3f2020-02-20 20:41:00 -080051 double avg_angular_velocity() { return avg_angular_velocity_; }
52
Sabina Davisedf89472020-02-17 15:27:37 -080053 private:
54 // The current sensor measurement.
55 Eigen::Matrix<double, 1, 1> Y_;
56 // The control loop.
Austin Schuh80476772021-03-06 20:17:36 -080057 ::std::unique_ptr<CurrentLimitedStateFeedbackController> loop_;
Sabina Davisedf89472020-02-17 15:27:37 -080058
59 // History array for calculating a filtered angular velocity.
60 static constexpr int kHistoryLength = 10;
Sabina Davis0f31d3f2020-02-20 20:41:00 -080061 ::std::array<std::pair<double, ::aos::monotonic_clock::time_point>,
62 kHistoryLength>
63 history_;
Sabina Davisedf89472020-02-17 15:27:37 -080064 ptrdiff_t history_position_ = 0;
Sabina Davis0f31d3f2020-02-20 20:41:00 -080065
66 // Average velocity logging.
milind-ub26973a2021-10-23 17:02:08 -070067 double avg_angular_velocity_ = 0;
Sabina Davis0f31d3f2020-02-20 20:41:00 -080068
Sabina Davisedf89472020-02-17 15:27:37 -080069 double last_goal_ = 0;
70
Austin Schuh43b9ae92020-02-29 23:08:38 -080071 bool first_ = true;
72
Sabina Davisedf89472020-02-17 15:27:37 -080073 DISALLOW_COPY_AND_ASSIGN(FlywheelController);
74};
75
Maxwell Henderson34242992024-01-07 12:39:11 -080076} // namespace flywheel
Sabina Davisedf89472020-02-17 15:27:37 -080077} // namespace control_loops
Maxwell Henderson34242992024-01-07 12:39:11 -080078} // namespace frc971
Sabina Davisedf89472020-02-17 15:27:37 -080079
Maxwell Henderson34242992024-01-07 12:39:11 -080080#endif // FRC971_CONTROL_LOOPS_SHOOTER_FLYWHEEL_CONTROLLER_H_