blob: 49a4385896815c4a3220f4f63fc77fd4e6c61978 [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
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -080012namespace frc971::control_loops::flywheel {
Sabina Davisedf89472020-02-17 15:27:37 -080013
Austin Schuh80476772021-03-06 20:17:36 -080014class CurrentLimitedStateFeedbackController;
15
Sabina Davisedf89472020-02-17 15:27:37 -080016// Handles the velocity control of each flywheel.
17class FlywheelController {
18 public:
Austin Schuh43b9ae92020-02-29 23:08:38 -080019 FlywheelController(
20 StateFeedbackLoop<3, 1, 1, double, StateFeedbackHybridPlant<3, 1, 1>,
Austin Schuhe8ca06a2020-03-07 22:27:39 -080021 HybridKalman<3, 1, 1>> &&loop,
22 double bemf, double resistance);
Sabina Davisedf89472020-02-17 15:27:37 -080023
Austin Schuh80476772021-03-06 20:17:36 -080024 ~FlywheelController();
25
Sabina Davisedf89472020-02-17 15:27:37 -080026 // Sets the velocity goal in radians/sec
27 void set_goal(double angular_velocity_goal);
Austin Schuh263dead2021-04-04 21:19:19 -070028 double goal() const { return last_goal_; }
Sabina Davisedf89472020-02-17 15:27:37 -080029 // Sets the current encoder position in radians
Sabina Davis0f31d3f2020-02-20 20:41:00 -080030 void set_position(double current_position,
31 const aos::monotonic_clock::time_point position_timestamp);
Sabina Davisedf89472020-02-17 15:27:37 -080032
33 // Populates the status structure.
34 flatbuffers::Offset<FlywheelControllerStatus> SetStatus(
35 flatbuffers::FlatBufferBuilder *fbb);
36
37 // Returns the control loop calculated voltage.
38 double voltage() const;
39
Austin Schuh80476772021-03-06 20:17:36 -080040 // Returns the expected battery current for the last U.
41 double current() const;
42
Sabina Davisedf89472020-02-17 15:27:37 -080043 // Returns the instantaneous velocity.
Austin Schuh80476772021-03-06 20:17:36 -080044 double velocity() const;
Sabina Davisedf89472020-02-17 15:27:37 -080045
46 // Executes the control loop for a cycle.
47 void Update(bool disabled);
48
Sabina Davis0f31d3f2020-02-20 20:41:00 -080049 double avg_angular_velocity() { return avg_angular_velocity_; }
50
Sabina Davisedf89472020-02-17 15:27:37 -080051 private:
52 // The current sensor measurement.
53 Eigen::Matrix<double, 1, 1> Y_;
54 // The control loop.
Austin Schuh80476772021-03-06 20:17:36 -080055 ::std::unique_ptr<CurrentLimitedStateFeedbackController> loop_;
Sabina Davisedf89472020-02-17 15:27:37 -080056
57 // History array for calculating a filtered angular velocity.
58 static constexpr int kHistoryLength = 10;
Sabina Davis0f31d3f2020-02-20 20:41:00 -080059 ::std::array<std::pair<double, ::aos::monotonic_clock::time_point>,
60 kHistoryLength>
61 history_;
Sabina Davisedf89472020-02-17 15:27:37 -080062 ptrdiff_t history_position_ = 0;
Sabina Davis0f31d3f2020-02-20 20:41:00 -080063
64 // Average velocity logging.
milind-ub26973a2021-10-23 17:02:08 -070065 double avg_angular_velocity_ = 0;
Sabina Davis0f31d3f2020-02-20 20:41:00 -080066
Sabina Davisedf89472020-02-17 15:27:37 -080067 double last_goal_ = 0;
68
Austin Schuh43b9ae92020-02-29 23:08:38 -080069 bool first_ = true;
70
Sabina Davisedf89472020-02-17 15:27:37 -080071 DISALLOW_COPY_AND_ASSIGN(FlywheelController);
72};
73
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -080074} // namespace frc971::control_loops::flywheel
Sabina Davisedf89472020-02-17 15:27:37 -080075
Maxwell Henderson34242992024-01-07 12:39:11 -080076#endif // FRC971_CONTROL_LOOPS_SHOOTER_FLYWHEEL_CONTROLLER_H_