Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 1 | #ifndef FRC971_CONTROL_LOOPS_DRIVETRAIN_H_ |
| 2 | #define FRC971_CONTROL_LOOPS_DRIVETRAIN_H_ |
| 3 | |
| 4 | #include "Eigen/Dense" |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 5 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 6 | #include "aos/util/log_interval.h" |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 7 | #include "frc971/control_loops/control_loop.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 8 | #include "frc971/control_loops/control_loops_generated.h" |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 9 | #include "frc971/control_loops/drivetrain/drivetrain_config.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 10 | #include "frc971/control_loops/drivetrain/drivetrain_goal_generated.h" |
| 11 | #include "frc971/control_loops/drivetrain/drivetrain_output_generated.h" |
| 12 | #include "frc971/control_loops/drivetrain/drivetrain_position_generated.h" |
Austin Schuh | 95771d9 | 2021-01-23 14:42:25 -0800 | [diff] [blame] | 13 | #include "frc971/control_loops/drivetrain/drivetrain_states.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 14 | #include "frc971/control_loops/drivetrain/drivetrain_status_generated.h" |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 15 | #include "frc971/control_loops/drivetrain/gear.h" |
James Kuszmaul | 3e1bb27 | 2020-01-17 18:38:19 -0800 | [diff] [blame] | 16 | #include "frc971/control_loops/drivetrain/improved_down_estimator.h" |
James Kuszmaul | e39cbcf | 2019-02-27 20:48:34 -0800 | [diff] [blame] | 17 | #include "frc971/control_loops/drivetrain/line_follow_drivetrain.h" |
Austin Schuh | 73b6e3b | 2019-05-27 16:37:15 -0700 | [diff] [blame] | 18 | #include "frc971/control_loops/drivetrain/localizer.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 19 | #include "frc971/control_loops/drivetrain/localizer_generated.h" |
Austin Schuh | 73b6e3b | 2019-05-27 16:37:15 -0700 | [diff] [blame] | 20 | #include "frc971/control_loops/drivetrain/polydrivetrain.h" |
Alex Perry | 731b460 | 2019-02-02 22:13:01 -0800 | [diff] [blame] | 21 | #include "frc971/control_loops/drivetrain/splinedrivetrain.h" |
Austin Schuh | 73b6e3b | 2019-05-27 16:37:15 -0700 | [diff] [blame] | 22 | #include "frc971/control_loops/drivetrain/ssdrivetrain.h" |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 23 | #include "frc971/control_loops/polytope.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 24 | #include "frc971/queues/gyro_generated.h" |
Austin Schuh | ac17fba | 2020-03-28 15:55:33 -0700 | [diff] [blame] | 25 | #include "frc971/wpilib/imu_batch_generated.h" |
James Kuszmaul | 3e1bb27 | 2020-01-17 18:38:19 -0800 | [diff] [blame] | 26 | #include "frc971/zeroing/imu_zeroer.h" |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 27 | |
| 28 | namespace frc971 { |
| 29 | namespace control_loops { |
| 30 | namespace drivetrain { |
| 31 | |
Austin Schuh | fb0f35c | 2021-02-14 21:25:29 -0800 | [diff] [blame] | 32 | namespace chrono = std::chrono; |
| 33 | |
| 34 | // A class to hold all the estimators in use in the drivetrain. This lets us |
| 35 | // run them on a log file without running the controllers, making them easier to |
| 36 | // tune. |
| 37 | class DrivetrainFilters { |
| 38 | public: |
| 39 | DrivetrainFilters(const DrivetrainConfig<double> &dt_config, |
| 40 | ::aos::EventLoop *event_loop, |
| 41 | LocalizerInterface *localizer); |
| 42 | |
| 43 | double localizer_theta() const { return localizer_->theta(); } |
| 44 | double x() const { return localizer_->x(); } |
| 45 | double y() const { return localizer_->y(); } |
| 46 | |
| 47 | // Returns the current gear for both sides. |
| 48 | Gear left_gear() const { return left_gear_; } |
| 49 | Gear right_gear() const { return right_gear_; } |
| 50 | |
| 51 | // Tracks the shift requests for each side. |
| 52 | void set_left_high_requested(bool value) { left_high_requested_ = value; } |
| 53 | void set_right_high_requested(bool value) { right_high_requested_ = value; } |
| 54 | |
| 55 | // Returns a pointer to the drivetrain kalman filter. |
| 56 | StateFeedbackLoop<7, 2, 4> *kf() { return &kf_; } |
| 57 | |
| 58 | // Populates the various state flatbuffers used for diagnostics. |
| 59 | flatbuffers::Offset<LocalizerState> PopulateLocalizerState( |
| 60 | flatbuffers::FlatBufferBuilder *fbb); |
| 61 | flatbuffers::Offset<ImuZeroerState> PopulateImuZeroerState( |
| 62 | flatbuffers::FlatBufferBuilder *fbb); |
| 63 | flatbuffers::Offset<DownEstimatorState> PopulateDownEstimatorState( |
| 64 | flatbuffers::FlatBufferBuilder *fbb, |
| 65 | aos::monotonic_clock::time_point monotonic_now); |
| 66 | flatbuffers::Offset<GearLogging> CreateGearLogging( |
| 67 | flatbuffers::FlatBufferBuilder *fbb) const; |
| 68 | |
| 69 | // Returns the current localizer state. |
| 70 | Eigen::Matrix<double, 5, 1> trajectory_state() { |
James Kuszmaul | 49c9320 | 2023-03-23 20:44:03 -0700 | [diff] [blame] | 71 | // Use the regular kalman filter's left/right velocity because they are |
| 72 | // generally smoother. |
Austin Schuh | fb0f35c | 2021-02-14 21:25:29 -0800 | [diff] [blame] | 73 | return (Eigen::Matrix<double, 5, 1>() << localizer_->x(), localizer_->y(), |
James Kuszmaul | 49c9320 | 2023-03-23 20:44:03 -0700 | [diff] [blame] | 74 | localizer_->theta(), DrivetrainXHat()(1), DrivetrainXHat()(3)) |
Austin Schuh | fb0f35c | 2021-02-14 21:25:29 -0800 | [diff] [blame] | 75 | .finished(); |
| 76 | } |
| 77 | |
| 78 | // Resets all filters when wpilib_interface resets. |
| 79 | void Reset(aos::monotonic_clock::time_point monotonic_now, |
| 80 | const drivetrain::Position *position); |
| 81 | |
| 82 | // Corrects all the filters. |
| 83 | void Correct(aos::monotonic_clock::time_point monotonic_now, |
| 84 | const drivetrain::Position *position); |
| 85 | // Runs the predict step for all filters. Should be called with the undelayed |
| 86 | // U. |
| 87 | void UpdateObserver(Eigen::Matrix<double, 2, 1> U); |
| 88 | |
| 89 | // Returns the negative of the voltage error from the drivetrain controller. |
| 90 | // This can be used for integral control. |
| 91 | Eigen::Matrix<double, 2, 1> VoltageError() const; |
| 92 | |
| 93 | // Returns the current drivetrain state. |
| 94 | Eigen::Matrix<double, 7, 1> DrivetrainXHat() const { return kf_.X_hat(); } |
| 95 | double DrivetrainXHat(int index) const { return kf_.X_hat(index); } |
| 96 | |
| 97 | // Returns the current uncapped voltage from the kalman filter. |
| 98 | double DrivetrainUUncapped(int index) const { return kf_.U_uncapped(index); } |
| 99 | |
James Kuszmaul | ddc6970 | 2021-03-24 21:55:03 -0700 | [diff] [blame] | 100 | bool Ready() const { return ready_; } |
| 101 | |
Austin Schuh | fb0f35c | 2021-02-14 21:25:29 -0800 | [diff] [blame] | 102 | private: |
| 103 | // Returns the current controller index for the current gear. |
| 104 | int ControllerIndexFromGears() const; |
| 105 | |
| 106 | // Computes which gear a shifter is in. |
| 107 | Gear ComputeGear(double shifter_position, |
| 108 | const constants::ShifterHallEffect &shifter_config, |
| 109 | bool high_requested) const; |
| 110 | |
| 111 | const DrivetrainConfig<double> dt_config_; |
| 112 | |
| 113 | aos::Fetcher<LocalizerControl> localizer_control_fetcher_; |
| 114 | aos::Fetcher<frc971::IMUValuesBatch> imu_values_fetcher_; |
| 115 | aos::Fetcher<frc971::sensors::GyroReading> gyro_reading_fetcher_; |
| 116 | |
| 117 | zeroing::ImuZeroer imu_zeroer_; |
| 118 | DrivetrainUkf down_estimator_; |
| 119 | aos::monotonic_clock::time_point last_imu_update_ = |
| 120 | aos::monotonic_clock::min_time; |
| 121 | LocalizerInterface *localizer_; |
| 122 | StateFeedbackLoop<7, 2, 4> kf_; |
| 123 | |
| 124 | // Current gears for each drive side. |
| 125 | Gear left_gear_; |
| 126 | Gear right_gear_; |
| 127 | |
| 128 | // Shift request. |
| 129 | bool left_high_requested_; |
| 130 | bool right_high_requested_; |
| 131 | |
| 132 | // Last acceleration and yaw rate. |
| 133 | aos::monotonic_clock::time_point last_gyro_time_ = |
| 134 | aos::monotonic_clock::min_time; |
| 135 | double last_accel_ = 0.0; |
| 136 | double last_gyro_rate_ = 0.0; |
| 137 | |
James Kuszmaul | ddc6970 | 2021-03-24 21:55:03 -0700 | [diff] [blame] | 138 | bool ready_ = false; |
| 139 | |
Austin Schuh | fb0f35c | 2021-02-14 21:25:29 -0800 | [diff] [blame] | 140 | // Last applied voltage. |
| 141 | Eigen::Matrix<double, 2, 1> last_voltage_; |
| 142 | Eigen::Matrix<double, 2, 1> last_last_voltage_; |
James Kuszmaul | c53de4a | 2022-03-12 21:32:06 -0800 | [diff] [blame] | 143 | |
| 144 | std::optional<double> yaw_gyro_zero_; |
| 145 | zeroing::Averager<double, 200> yaw_gyro_zeroer_; |
Austin Schuh | fb0f35c | 2021-02-14 21:25:29 -0800 | [diff] [blame] | 146 | }; |
| 147 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 148 | class DrivetrainLoop |
James Kuszmaul | 6175066 | 2021-06-21 21:32:33 -0700 | [diff] [blame] | 149 | : public frc971::controls::ControlLoop<Goal, Position, Status, Output> { |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 150 | public: |
James Kuszmaul | dc53443 | 2023-02-05 14:51:11 -0800 | [diff] [blame] | 151 | // Note that we only actually store N - 1 splines consistently, since we need |
| 152 | // to keep one fetcher free to check whether there are any new splines. |
| 153 | static constexpr size_t kNumSplineFetchers = |
| 154 | SplineDrivetrain::kMaxTrajectories; |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 155 | |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 156 | // Constructs a control loop which can take a Drivetrain or defaults to the |
| 157 | // drivetrain at frc971::control_loops::drivetrain |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 158 | explicit DrivetrainLoop(const DrivetrainConfig<double> &dt_config, |
| 159 | ::aos::EventLoop *event_loop, |
| 160 | LocalizerInterface *localizer, |
| 161 | const ::std::string &name = "/drivetrain"); |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 162 | |
James Kuszmaul | 531609d | 2020-02-18 17:12:23 -0800 | [diff] [blame] | 163 | virtual ~DrivetrainLoop() {} |
| 164 | |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 165 | protected: |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 166 | struct TrajectoryFetcherState { |
| 167 | aos::Fetcher<fb::Trajectory> fetcher; |
| 168 | bool in_use = false; |
| 169 | }; |
| 170 | |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 171 | // Executes one cycle of the control loop. |
Adam Snaider | bc918b6 | 2016-02-27 21:03:39 -0800 | [diff] [blame] | 172 | void RunIteration( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 173 | const ::frc971::control_loops::drivetrain::Goal *goal, |
| 174 | const ::frc971::control_loops::drivetrain::Position *position, |
| 175 | aos::Sender<::frc971::control_loops::drivetrain::Output>::Builder *output, |
| 176 | aos::Sender<::frc971::control_loops::drivetrain::Status>::Builder *status) |
| 177 | override; |
Adam Snaider | bc918b6 | 2016-02-27 21:03:39 -0800 | [diff] [blame] | 178 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 179 | flatbuffers::Offset<drivetrain::Output> Zero( |
| 180 | aos::Sender<drivetrain::Output>::Builder *builder) override; |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 181 | |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 182 | void UpdateTrajectoryFetchers(); |
| 183 | |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 184 | const DrivetrainConfig<double> dt_config_; |
Austin Schuh | fb0f35c | 2021-02-14 21:25:29 -0800 | [diff] [blame] | 185 | DrivetrainFilters filters_; |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 186 | std::array<TrajectoryFetcherState, kNumSplineFetchers> trajectory_fetchers_; |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 187 | |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 188 | PolyDrivetrain<double> dt_openloop_; |
Austin Schuh | 4156560 | 2016-02-28 20:10:49 -0800 | [diff] [blame] | 189 | DrivetrainMotorsSS dt_closedloop_; |
Alex Perry | 731b460 | 2019-02-02 22:13:01 -0800 | [diff] [blame] | 190 | SplineDrivetrain dt_spline_; |
James Kuszmaul | e39cbcf | 2019-02-27 20:48:34 -0800 | [diff] [blame] | 191 | LineFollowDrivetrain dt_line_follow_; |
Austin Schuh | 5900d14 | 2016-04-03 21:35:12 -0700 | [diff] [blame] | 192 | |
| 193 | bool has_been_enabled_ = false; |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 194 | |
| 195 | aos::SendFailureCounter status_failure_counter_; |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 196 | }; |
| 197 | |
| 198 | } // namespace drivetrain |
| 199 | } // namespace control_loops |
| 200 | } // namespace frc971 |
| 201 | |
| 202 | #endif // FRC971_CONTROL_LOOPS_DRIVETRAIN_H_ |