blob: 3a09b28a95889b71c9e58a2a3fa14def21b789e5 [file] [log] [blame]
James Kuszmaul29c59522022-02-12 16:44:26 -08001#ifndef Y2022_CONTROL_LOOPS_LOCALIZER_LOCALIZER_H_
2#define Y2022_CONTROL_LOOPS_LOCALIZER_LOCALIZER_H_
3
4#include "Eigen/Dense"
5#include "Eigen/Geometry"
6
7#include "aos/events/event_loop.h"
8#include "aos/containers/ring_buffer.h"
9#include "aos/time/time.h"
10#include "y2020/vision/sift/sift_generated.h"
11#include "y2022/control_loops/localizer/localizer_status_generated.h"
12#include "y2022/control_loops/localizer/localizer_output_generated.h"
13#include "frc971/control_loops/drivetrain/improved_down_estimator.h"
James Kuszmaul29c59522022-02-12 16:44:26 -080014#include "frc971/control_loops/drivetrain/drivetrain_output_generated.h"
15#include "frc971/control_loops/drivetrain/localizer_generated.h"
16#include "frc971/zeroing/imu_zeroer.h"
17
18namespace frc971::controls {
19
20namespace testing {
21class LocalizerTest;
22}
23
24// Localizer implementation that makes use of a 6-axis IMU, encoder readings,
25// drivetrain voltages, and camera returns to localize the robot. Meant to
26// be run on a raspberry pi.
27//
28// This operates on the principle that the drivetrain can be in one of two
29// modes:
30// 1) A "normal" mode where it is obeying the regular drivetrain model, with
31// minimal lateral motion and no major external disturbances. This is
32// referred to as the "model" mode in the code/variable names.
33// 2) An non-holonomic mode where the robot is just flying around on a 2-D
34// plane with no meaningful constraints (referred to as an "accel" model
35// in the code, because we rely primarily on the accelerometer readings).
36//
37// In order to determine which mode to be in, we attempt to track whether the
38// two models are diverging substantially. In order to do this, we maintain a
39// 1-second long queue of "branches". A branch is generated every X iterations
40// and contains a model state and an accel state. When the branch starts, the
41// two will have identical states. For the remaining 1 second, the model state
42// will evolve purely according to the drivetrian model, and the accel state
43// will evolve purely using IMU readings.
44//
45// When the branch reaches 1 second in age, we calculate a residual associated
46// with how much the accel and model based states diverged. If they have
47// diverged substantially, that implies that the model is a poor match for
48// whatever has been happening to the robot in the past second, so if we were
49// previously relying on the model, we will override the current "actual"
50// state with the branched accel state, and then continue to update the accel
51// state based on IMU readings.
52// If we are currently in the accel state, we will continue generating branches
53// until the branches stop diverging--this will indicate that the model
54// matches the accelerometer readings again, and so we will swap back to
55// the model-based state.
56//
57// TODO:
58// * Implement paying attention to camera readings.
59// * Tune for ADIS16505/real robot.
60// * Tune down CPU usage to run on a pi.
61class ModelBasedLocalizer {
62 public:
63 static constexpr size_t kX = 0;
64 static constexpr size_t kY = 1;
65 static constexpr size_t kTheta = 2;
66
67 static constexpr size_t kVelocityX = 3;
68 static constexpr size_t kVelocityY = 4;
69 static constexpr size_t kNAccelStates = 5;
70
71 static constexpr size_t kLeftEncoder = 3;
72 static constexpr size_t kLeftVelocity = 4;
73 static constexpr size_t kLeftVoltageError = 5;
74 static constexpr size_t kRightEncoder = 6;
75 static constexpr size_t kRightVelocity = 7;
76 static constexpr size_t kRightVoltageError = 8;
77 static constexpr size_t kNModelStates = 9;
78
79 static constexpr size_t kNModelOutputs = 3;
80
81 static constexpr size_t kNAccelOuputs = 1;
82
83 static constexpr size_t kAccelX = 0;
84 static constexpr size_t kAccelY = 1;
85 static constexpr size_t kThetaRate = 2;
86 static constexpr size_t kNAccelInputs = 3;
87
88 static constexpr size_t kLeftVoltage = 0;
89 static constexpr size_t kRightVoltage = 1;
90 static constexpr size_t kNModelInputs = 2;
91
92 typedef Eigen::Matrix<double, kNModelStates, 1> ModelState;
93 typedef Eigen::Matrix<double, kNAccelStates, 1> AccelState;
94 typedef Eigen::Matrix<double, kNModelInputs, 1> ModelInput;
95 typedef Eigen::Matrix<double, kNAccelInputs, 1> AccelInput;
96
97 ModelBasedLocalizer(
98 const control_loops::drivetrain::DrivetrainConfig<double> &dt_config);
99 void HandleImu(aos::monotonic_clock::time_point t,
100 const Eigen::Vector3d &gyro, const Eigen::Vector3d &accel,
101 const Eigen::Vector2d encoders, const Eigen::Vector2d voltage);
102 void HandleImageMatch(aos::monotonic_clock::time_point,
103 const vision::sift::ImageMatchResult *) {
104 LOG(FATAL) << "Unimplemented.";
105 }
106 void HandleReset(aos::monotonic_clock::time_point,
107 const Eigen::Vector3d &xytheta);
108
109 flatbuffers::Offset<ModelBasedStatus> PopulateStatus(
110 flatbuffers::FlatBufferBuilder *fbb);
111
112 Eigen::Vector3d xytheta() const {
113 if (using_model_) {
114 return current_state_.model_state.block<3, 1>(0, 0);
115 } else {
116 return current_state_.accel_state.block<3, 1>(0, 0);
117 }
118 }
119
120 AccelState accel_state() const { return current_state_.accel_state; };
121
122 private:
123 struct CombinedState {
124 AccelState accel_state;
125 ModelState model_state;
126 aos::monotonic_clock::time_point branch_time;
127 double accumulated_divergence;
128 };
129
130 static flatbuffers::Offset<AccelBasedState> BuildAccelState(
131 flatbuffers::FlatBufferBuilder *fbb, const AccelState &state);
132
133 static flatbuffers::Offset<ModelBasedState> BuildModelState(
134 flatbuffers::FlatBufferBuilder *fbb, const ModelState &state);
135
136 Eigen::Matrix<double, kNModelStates, kNModelStates> AModel(
137 const ModelState &state) const;
138 Eigen::Matrix<double, kNAccelStates, kNAccelStates> AAccel() const;
139 ModelState DiffModel(const ModelState &state, const ModelInput &U) const;
140 AccelState DiffAccel(const AccelState &state, const AccelInput &U) const;
141
142 ModelState UpdateModel(const ModelState &model, const ModelInput &input,
143 aos::monotonic_clock::duration dt) const;
144 AccelState UpdateAccel(const AccelState &accel, const AccelInput &input,
145 aos::monotonic_clock::duration dt) const;
146
147 AccelState AccelStateForModelState(const ModelState &state) const;
148 ModelState ModelStateForAccelState(const AccelState &state,
149 const Eigen::Vector2d &encoders,
150 const double yaw_rate) const;
151 double ModelDivergence(const CombinedState &state,
152 const AccelInput &accel_inputs,
153 const Eigen::Vector2d &filtered_accel,
154 const ModelInput &model_inputs);
155
156 const control_loops::drivetrain::DrivetrainConfig<double> dt_config_;
157 const StateFeedbackHybridPlantCoefficients<2, 2, 2, double>
158 velocity_drivetrain_coefficients_;
159 Eigen::Matrix<double, kNModelStates, kNModelStates> A_continuous_model_;
160 Eigen::Matrix<double, kNAccelStates, kNAccelStates> A_continuous_accel_;
161 Eigen::Matrix<double, kNModelStates, kNModelInputs> B_continuous_model_;
162 Eigen::Matrix<double, kNAccelStates, kNAccelInputs> B_continuous_accel_;
163
164 Eigen::Matrix<double, kNModelStates, kNModelStates> Q_continuous_model_;
165
166 Eigen::Matrix<double, kNModelStates, kNModelStates> P_model_;
167 // When we are following the model, we will, on each iteration:
168 // 1) Perform a model-based update of a single state.
169 // 2) Add a hypothetical non-model-based entry based on the current state.
170 // 3) Evict too-old non-model-based entries.
171 control_loops::drivetrain::DrivetrainUkf down_estimator_;
172
173 // Buffer of old branches these are all created by initializing a new
174 // model-based state based on the current state, and then initializing a new
175 // accel-based state on top of that new model-based state (to eliminate the
176 // impact of any lateral motion).
177 // We then integrate up all of these states and observe how much the model and
178 // accel based states of each branch compare to one another.
179 aos::RingBuffer<CombinedState, 2000> branches_;
180
181 CombinedState current_state_;
182 aos::monotonic_clock::time_point t_ = aos::monotonic_clock::min_time;
183 bool using_model_;
184
185 double last_residual_ = 0.0;
186 double filtered_residual_ = 0.0;
187 Eigen::Vector2d filtered_residual_accel_ = Eigen::Vector2d::Zero();
188 Eigen::Vector3d abs_accel_ = Eigen::Vector3d::Zero();
189 double velocity_residual_ = 0.0;
190 double accel_residual_ = 0.0;
191 double theta_rate_residual_ = 0.0;
192 int hysteresis_count_ = 0;
193
194 int clock_resets_ = 0;
195
196 friend class testing::LocalizerTest;
197};
198
199class EventLoopLocalizer {
200 public:
201 EventLoopLocalizer(
202 aos::EventLoop *event_loop,
203 const control_loops::drivetrain::DrivetrainConfig<double> &dt_config);
204
205 private:
206 aos::EventLoop *event_loop_;
207 ModelBasedLocalizer model_based_;
208 aos::Sender<LocalizerStatus> status_sender_;
209 aos::Sender<LocalizerOutput> output_sender_;
James Kuszmaul29c59522022-02-12 16:44:26 -0800210 aos::Fetcher<frc971::control_loops::drivetrain::Output> output_fetcher_;
211 zeroing::ImuZeroer zeroer_;
212 aos::monotonic_clock::time_point last_output_send_ =
213 aos::monotonic_clock::min_time;
214};
215} // namespace frc971::controls
216#endif // Y2022_CONTROL_LOOPS_LOCALIZER_LOCALIZER_H_