blob: 325715f524bb84a0be35460e9224a049396dba0a [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"
5
John Park33858a32018-09-28 23:05:48 -07006#include "aos/controls/control_loop.h"
7#include "aos/controls/polytope.h"
8#include "aos/util/log_interval.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -07009#include "frc971/control_loops/control_loops_generated.h"
Austin Schuh093535c2016-03-05 23:21:00 -080010#include "frc971/control_loops/drivetrain/drivetrain_config.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070011#include "frc971/control_loops/drivetrain/drivetrain_goal_generated.h"
12#include "frc971/control_loops/drivetrain/drivetrain_output_generated.h"
13#include "frc971/control_loops/drivetrain/drivetrain_position_generated.h"
Austin Schuh95771d92021-01-23 14:42:25 -080014#include "frc971/control_loops/drivetrain/drivetrain_states.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070015#include "frc971/control_loops/drivetrain/drivetrain_status_generated.h"
Austin Schuh093535c2016-03-05 23:21:00 -080016#include "frc971/control_loops/drivetrain/gear.h"
James Kuszmaul3e1bb272020-01-17 18:38:19 -080017#include "frc971/control_loops/drivetrain/improved_down_estimator.h"
James Kuszmaule39cbcf2019-02-27 20:48:34 -080018#include "frc971/control_loops/drivetrain/line_follow_drivetrain.h"
Austin Schuh73b6e3b2019-05-27 16:37:15 -070019#include "frc971/control_loops/drivetrain/localizer.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070020#include "frc971/control_loops/drivetrain/localizer_generated.h"
Austin Schuh73b6e3b2019-05-27 16:37:15 -070021#include "frc971/control_loops/drivetrain/polydrivetrain.h"
Alex Perry731b4602019-02-02 22:13:01 -080022#include "frc971/control_loops/drivetrain/splinedrivetrain.h"
Austin Schuh73b6e3b2019-05-27 16:37:15 -070023#include "frc971/control_loops/drivetrain/ssdrivetrain.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070024#include "frc971/queues/gyro_generated.h"
Austin Schuhac17fba2020-03-28 15:55:33 -070025#include "frc971/wpilib/imu_batch_generated.h"
James Kuszmaul3e1bb272020-01-17 18:38:19 -080026#include "frc971/zeroing/imu_zeroer.h"
Comran Morshed5323ecb2015-12-26 20:50:55 +000027
28namespace frc971 {
29namespace control_loops {
30namespace drivetrain {
31
Austin Schuhfb0f35c2021-02-14 21:25:29 -080032namespace 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.
37class 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() {
71 return (Eigen::Matrix<double, 5, 1>() << localizer_->x(), localizer_->y(),
72 localizer_->theta(), localizer_->left_velocity(),
73 localizer_->right_velocity())
74 .finished();
75 }
76
77 // Resets all filters when wpilib_interface resets.
78 void Reset(aos::monotonic_clock::time_point monotonic_now,
79 const drivetrain::Position *position);
80
81 // Corrects all the filters.
82 void Correct(aos::monotonic_clock::time_point monotonic_now,
83 const drivetrain::Position *position);
84 // Runs the predict step for all filters. Should be called with the undelayed
85 // U.
86 void UpdateObserver(Eigen::Matrix<double, 2, 1> U);
87
88 // Returns the negative of the voltage error from the drivetrain controller.
89 // This can be used for integral control.
90 Eigen::Matrix<double, 2, 1> VoltageError() const;
91
92 // Returns the current drivetrain state.
93 Eigen::Matrix<double, 7, 1> DrivetrainXHat() const { return kf_.X_hat(); }
94 double DrivetrainXHat(int index) const { return kf_.X_hat(index); }
95
96 // Returns the current uncapped voltage from the kalman filter.
97 double DrivetrainUUncapped(int index) const { return kf_.U_uncapped(index); }
98
James Kuszmaulddc69702021-03-24 21:55:03 -070099 bool Ready() const { return ready_; }
100
Austin Schuhfb0f35c2021-02-14 21:25:29 -0800101 private:
102 // Returns the current controller index for the current gear.
103 int ControllerIndexFromGears() const;
104
105 // Computes which gear a shifter is in.
106 Gear ComputeGear(double shifter_position,
107 const constants::ShifterHallEffect &shifter_config,
108 bool high_requested) const;
109
110 const DrivetrainConfig<double> dt_config_;
111
112 aos::Fetcher<LocalizerControl> localizer_control_fetcher_;
113 aos::Fetcher<frc971::IMUValuesBatch> imu_values_fetcher_;
114 aos::Fetcher<frc971::sensors::GyroReading> gyro_reading_fetcher_;
115
116 zeroing::ImuZeroer imu_zeroer_;
117 DrivetrainUkf down_estimator_;
118 aos::monotonic_clock::time_point last_imu_update_ =
119 aos::monotonic_clock::min_time;
120 LocalizerInterface *localizer_;
121 StateFeedbackLoop<7, 2, 4> kf_;
122
123 // Current gears for each drive side.
124 Gear left_gear_;
125 Gear right_gear_;
126
127 // Shift request.
128 bool left_high_requested_;
129 bool right_high_requested_;
130
131 // Last acceleration and yaw rate.
132 aos::monotonic_clock::time_point last_gyro_time_ =
133 aos::monotonic_clock::min_time;
134 double last_accel_ = 0.0;
135 double last_gyro_rate_ = 0.0;
136
James Kuszmaulddc69702021-03-24 21:55:03 -0700137 bool ready_ = false;
138
Austin Schuhfb0f35c2021-02-14 21:25:29 -0800139 // Last applied voltage.
140 Eigen::Matrix<double, 2, 1> last_voltage_;
141 Eigen::Matrix<double, 2, 1> last_last_voltage_;
142};
143
Alex Perrycb7da4b2019-08-28 19:35:56 -0700144class DrivetrainLoop
145 : public aos::controls::ControlLoop<Goal, Position, Status, Output> {
Comran Morshed5323ecb2015-12-26 20:50:55 +0000146 public:
James Kuszmaul99af8b52021-03-28 10:50:15 -0700147 // Note that we only actually store N - 1 splines, since we need to keep one
148 // fetcher free to check whether there are any new splines.
149 static constexpr size_t kNumSplineFetchers = 5;
James Kuszmaul75a18c52021-03-10 22:02:07 -0800150
Comran Morshed5323ecb2015-12-26 20:50:55 +0000151 // Constructs a control loop which can take a Drivetrain or defaults to the
152 // drivetrain at frc971::control_loops::drivetrain
Alex Perrycb7da4b2019-08-28 19:35:56 -0700153 explicit DrivetrainLoop(const DrivetrainConfig<double> &dt_config,
154 ::aos::EventLoop *event_loop,
155 LocalizerInterface *localizer,
156 const ::std::string &name = "/drivetrain");
Comran Morshed5323ecb2015-12-26 20:50:55 +0000157
James Kuszmaul531609d2020-02-18 17:12:23 -0800158 virtual ~DrivetrainLoop() {}
159
Comran Morshed5323ecb2015-12-26 20:50:55 +0000160 protected:
James Kuszmaul75a18c52021-03-10 22:02:07 -0800161 struct TrajectoryFetcherState {
162 aos::Fetcher<fb::Trajectory> fetcher;
163 bool in_use = false;
164 };
165
Comran Morshed5323ecb2015-12-26 20:50:55 +0000166 // Executes one cycle of the control loop.
Adam Snaiderbc918b62016-02-27 21:03:39 -0800167 void RunIteration(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700168 const ::frc971::control_loops::drivetrain::Goal *goal,
169 const ::frc971::control_loops::drivetrain::Position *position,
170 aos::Sender<::frc971::control_loops::drivetrain::Output>::Builder *output,
171 aos::Sender<::frc971::control_loops::drivetrain::Status>::Builder *status)
172 override;
Adam Snaiderbc918b62016-02-27 21:03:39 -0800173
Alex Perrycb7da4b2019-08-28 19:35:56 -0700174 flatbuffers::Offset<drivetrain::Output> Zero(
175 aos::Sender<drivetrain::Output>::Builder *builder) override;
Comran Morshed5323ecb2015-12-26 20:50:55 +0000176
James Kuszmaul75a18c52021-03-10 22:02:07 -0800177 void UpdateTrajectoryFetchers();
178
Austin Schuhbcce26a2018-03-26 23:41:24 -0700179 const DrivetrainConfig<double> dt_config_;
Austin Schuhfb0f35c2021-02-14 21:25:29 -0800180 DrivetrainFilters filters_;
James Kuszmaul75a18c52021-03-10 22:02:07 -0800181 std::array<TrajectoryFetcherState, kNumSplineFetchers> trajectory_fetchers_;
Comran Morshed5323ecb2015-12-26 20:50:55 +0000182
Austin Schuhbcce26a2018-03-26 23:41:24 -0700183 PolyDrivetrain<double> dt_openloop_;
Austin Schuh41565602016-02-28 20:10:49 -0800184 DrivetrainMotorsSS dt_closedloop_;
Alex Perry731b4602019-02-02 22:13:01 -0800185 SplineDrivetrain dt_spline_;
James Kuszmaule39cbcf2019-02-27 20:48:34 -0800186 LineFollowDrivetrain dt_line_follow_;
Austin Schuh5900d142016-04-03 21:35:12 -0700187
188 bool has_been_enabled_ = false;
Comran Morshed5323ecb2015-12-26 20:50:55 +0000189};
190
191} // namespace drivetrain
192} // namespace control_loops
193} // namespace frc971
194
195#endif // FRC971_CONTROL_LOOPS_DRIVETRAIN_H_