blob: 0d100edefdfd5ce4c337d5d0f94815e4535aebb5 [file] [log] [blame]
Comran Morshed5323ecb2015-12-26 20:50:55 +00001#ifndef FRC971_CONTROL_LOOPS_DRIVETRAIN_H_
2#define FRC971_CONTROL_LOOPS_DRIVETRAIN_H_
3
4#include "Eigen/Dense"
John Park33858a32018-09-28 23:05:48 -07005#include "aos/util/log_interval.h"
milind1f1dca32021-07-03 13:50:07 -07006#include "frc971/control_loops/control_loop.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -07007#include "frc971/control_loops/control_loops_generated.h"
Austin Schuh093535c2016-03-05 23:21:00 -08008#include "frc971/control_loops/drivetrain/drivetrain_config.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -07009#include "frc971/control_loops/drivetrain/drivetrain_goal_generated.h"
10#include "frc971/control_loops/drivetrain/drivetrain_output_generated.h"
11#include "frc971/control_loops/drivetrain/drivetrain_position_generated.h"
Austin Schuh95771d92021-01-23 14:42:25 -080012#include "frc971/control_loops/drivetrain/drivetrain_states.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070013#include "frc971/control_loops/drivetrain/drivetrain_status_generated.h"
Austin Schuh093535c2016-03-05 23:21:00 -080014#include "frc971/control_loops/drivetrain/gear.h"
James Kuszmaul3e1bb272020-01-17 18:38:19 -080015#include "frc971/control_loops/drivetrain/improved_down_estimator.h"
James Kuszmaule39cbcf2019-02-27 20:48:34 -080016#include "frc971/control_loops/drivetrain/line_follow_drivetrain.h"
Austin Schuh73b6e3b2019-05-27 16:37:15 -070017#include "frc971/control_loops/drivetrain/localizer.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070018#include "frc971/control_loops/drivetrain/localizer_generated.h"
Austin Schuh73b6e3b2019-05-27 16:37:15 -070019#include "frc971/control_loops/drivetrain/polydrivetrain.h"
Alex Perry731b4602019-02-02 22:13:01 -080020#include "frc971/control_loops/drivetrain/splinedrivetrain.h"
Austin Schuh73b6e3b2019-05-27 16:37:15 -070021#include "frc971/control_loops/drivetrain/ssdrivetrain.h"
milind1f1dca32021-07-03 13:50:07 -070022#include "frc971/control_loops/polytope.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070023#include "frc971/queues/gyro_generated.h"
Austin Schuhac17fba2020-03-28 15:55:33 -070024#include "frc971/wpilib/imu_batch_generated.h"
James Kuszmaul3e1bb272020-01-17 18:38:19 -080025#include "frc971/zeroing/imu_zeroer.h"
Comran Morshed5323ecb2015-12-26 20:50:55 +000026
27namespace frc971 {
28namespace control_loops {
29namespace drivetrain {
30
Austin Schuhfb0f35c2021-02-14 21:25:29 -080031namespace chrono = std::chrono;
32
33// A class to hold all the estimators in use in the drivetrain. This lets us
34// run them on a log file without running the controllers, making them easier to
35// tune.
36class DrivetrainFilters {
37 public:
38 DrivetrainFilters(const DrivetrainConfig<double> &dt_config,
39 ::aos::EventLoop *event_loop,
40 LocalizerInterface *localizer);
41
42 double localizer_theta() const { return localizer_->theta(); }
43 double x() const { return localizer_->x(); }
44 double y() const { return localizer_->y(); }
45
46 // Returns the current gear for both sides.
47 Gear left_gear() const { return left_gear_; }
48 Gear right_gear() const { return right_gear_; }
49
50 // Tracks the shift requests for each side.
51 void set_left_high_requested(bool value) { left_high_requested_ = value; }
52 void set_right_high_requested(bool value) { right_high_requested_ = value; }
53
54 // Returns a pointer to the drivetrain kalman filter.
55 StateFeedbackLoop<7, 2, 4> *kf() { return &kf_; }
56
57 // Populates the various state flatbuffers used for diagnostics.
58 flatbuffers::Offset<LocalizerState> PopulateLocalizerState(
59 flatbuffers::FlatBufferBuilder *fbb);
60 flatbuffers::Offset<ImuZeroerState> PopulateImuZeroerState(
61 flatbuffers::FlatBufferBuilder *fbb);
62 flatbuffers::Offset<DownEstimatorState> PopulateDownEstimatorState(
63 flatbuffers::FlatBufferBuilder *fbb,
64 aos::monotonic_clock::time_point monotonic_now);
65 flatbuffers::Offset<GearLogging> CreateGearLogging(
66 flatbuffers::FlatBufferBuilder *fbb) const;
67
68 // Returns the current localizer state.
69 Eigen::Matrix<double, 5, 1> trajectory_state() {
70 return (Eigen::Matrix<double, 5, 1>() << localizer_->x(), localizer_->y(),
71 localizer_->theta(), localizer_->left_velocity(),
72 localizer_->right_velocity())
73 .finished();
74 }
75
76 // Resets all filters when wpilib_interface resets.
77 void Reset(aos::monotonic_clock::time_point monotonic_now,
78 const drivetrain::Position *position);
79
80 // Corrects all the filters.
81 void Correct(aos::monotonic_clock::time_point monotonic_now,
82 const drivetrain::Position *position);
83 // Runs the predict step for all filters. Should be called with the undelayed
84 // U.
85 void UpdateObserver(Eigen::Matrix<double, 2, 1> U);
86
87 // Returns the negative of the voltage error from the drivetrain controller.
88 // This can be used for integral control.
89 Eigen::Matrix<double, 2, 1> VoltageError() const;
90
91 // Returns the current drivetrain state.
92 Eigen::Matrix<double, 7, 1> DrivetrainXHat() const { return kf_.X_hat(); }
93 double DrivetrainXHat(int index) const { return kf_.X_hat(index); }
94
95 // Returns the current uncapped voltage from the kalman filter.
96 double DrivetrainUUncapped(int index) const { return kf_.U_uncapped(index); }
97
James Kuszmaulddc69702021-03-24 21:55:03 -070098 bool Ready() const { return ready_; }
99
Austin Schuhfb0f35c2021-02-14 21:25:29 -0800100 private:
101 // Returns the current controller index for the current gear.
102 int ControllerIndexFromGears() const;
103
104 // Computes which gear a shifter is in.
105 Gear ComputeGear(double shifter_position,
106 const constants::ShifterHallEffect &shifter_config,
107 bool high_requested) const;
108
109 const DrivetrainConfig<double> dt_config_;
110
111 aos::Fetcher<LocalizerControl> localizer_control_fetcher_;
112 aos::Fetcher<frc971::IMUValuesBatch> imu_values_fetcher_;
113 aos::Fetcher<frc971::sensors::GyroReading> gyro_reading_fetcher_;
114
115 zeroing::ImuZeroer imu_zeroer_;
116 DrivetrainUkf down_estimator_;
117 aos::monotonic_clock::time_point last_imu_update_ =
118 aos::monotonic_clock::min_time;
119 LocalizerInterface *localizer_;
120 StateFeedbackLoop<7, 2, 4> kf_;
121
122 // Current gears for each drive side.
123 Gear left_gear_;
124 Gear right_gear_;
125
126 // Shift request.
127 bool left_high_requested_;
128 bool right_high_requested_;
129
130 // Last acceleration and yaw rate.
131 aos::monotonic_clock::time_point last_gyro_time_ =
132 aos::monotonic_clock::min_time;
133 double last_accel_ = 0.0;
134 double last_gyro_rate_ = 0.0;
135
James Kuszmaulddc69702021-03-24 21:55:03 -0700136 bool ready_ = false;
137
Austin Schuhfb0f35c2021-02-14 21:25:29 -0800138 // Last applied voltage.
139 Eigen::Matrix<double, 2, 1> last_voltage_;
140 Eigen::Matrix<double, 2, 1> last_last_voltage_;
James Kuszmaulc53de4a2022-03-12 21:32:06 -0800141
142 std::optional<double> yaw_gyro_zero_;
143 zeroing::Averager<double, 200> yaw_gyro_zeroer_;
Austin Schuhfb0f35c2021-02-14 21:25:29 -0800144};
145
Alex Perrycb7da4b2019-08-28 19:35:56 -0700146class DrivetrainLoop
James Kuszmaul61750662021-06-21 21:32:33 -0700147 : public frc971::controls::ControlLoop<Goal, Position, Status, Output> {
Comran Morshed5323ecb2015-12-26 20:50:55 +0000148 public:
James Kuszmauldc534432023-02-05 14:51:11 -0800149 // Note that we only actually store N - 1 splines consistently, since we need
150 // to keep one fetcher free to check whether there are any new splines.
151 static constexpr size_t kNumSplineFetchers =
152 SplineDrivetrain::kMaxTrajectories;
James Kuszmaul75a18c52021-03-10 22:02:07 -0800153
Comran Morshed5323ecb2015-12-26 20:50:55 +0000154 // Constructs a control loop which can take a Drivetrain or defaults to the
155 // drivetrain at frc971::control_loops::drivetrain
Alex Perrycb7da4b2019-08-28 19:35:56 -0700156 explicit DrivetrainLoop(const DrivetrainConfig<double> &dt_config,
157 ::aos::EventLoop *event_loop,
158 LocalizerInterface *localizer,
159 const ::std::string &name = "/drivetrain");
Comran Morshed5323ecb2015-12-26 20:50:55 +0000160
James Kuszmaul531609d2020-02-18 17:12:23 -0800161 virtual ~DrivetrainLoop() {}
162
Comran Morshed5323ecb2015-12-26 20:50:55 +0000163 protected:
James Kuszmaul75a18c52021-03-10 22:02:07 -0800164 struct TrajectoryFetcherState {
165 aos::Fetcher<fb::Trajectory> fetcher;
166 bool in_use = false;
167 };
168
Comran Morshed5323ecb2015-12-26 20:50:55 +0000169 // Executes one cycle of the control loop.
Adam Snaiderbc918b62016-02-27 21:03:39 -0800170 void RunIteration(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700171 const ::frc971::control_loops::drivetrain::Goal *goal,
172 const ::frc971::control_loops::drivetrain::Position *position,
173 aos::Sender<::frc971::control_loops::drivetrain::Output>::Builder *output,
174 aos::Sender<::frc971::control_loops::drivetrain::Status>::Builder *status)
175 override;
Adam Snaiderbc918b62016-02-27 21:03:39 -0800176
Alex Perrycb7da4b2019-08-28 19:35:56 -0700177 flatbuffers::Offset<drivetrain::Output> Zero(
178 aos::Sender<drivetrain::Output>::Builder *builder) override;
Comran Morshed5323ecb2015-12-26 20:50:55 +0000179
James Kuszmaul75a18c52021-03-10 22:02:07 -0800180 void UpdateTrajectoryFetchers();
181
Austin Schuhbcce26a2018-03-26 23:41:24 -0700182 const DrivetrainConfig<double> dt_config_;
Austin Schuhfb0f35c2021-02-14 21:25:29 -0800183 DrivetrainFilters filters_;
James Kuszmaul75a18c52021-03-10 22:02:07 -0800184 std::array<TrajectoryFetcherState, kNumSplineFetchers> trajectory_fetchers_;
Comran Morshed5323ecb2015-12-26 20:50:55 +0000185
Austin Schuhbcce26a2018-03-26 23:41:24 -0700186 PolyDrivetrain<double> dt_openloop_;
Austin Schuh41565602016-02-28 20:10:49 -0800187 DrivetrainMotorsSS dt_closedloop_;
Alex Perry731b4602019-02-02 22:13:01 -0800188 SplineDrivetrain dt_spline_;
James Kuszmaule39cbcf2019-02-27 20:48:34 -0800189 LineFollowDrivetrain dt_line_follow_;
Austin Schuh5900d142016-04-03 21:35:12 -0700190
191 bool has_been_enabled_ = false;
milind1f1dca32021-07-03 13:50:07 -0700192
193 aos::SendFailureCounter status_failure_counter_;
Comran Morshed5323ecb2015-12-26 20:50:55 +0000194};
195
196} // namespace drivetrain
197} // namespace control_loops
198} // namespace frc971
199
200#endif // FRC971_CONTROL_LOOPS_DRIVETRAIN_H_