blob: 1e72c39b63e9a1d2644ffcfb028f780c2a486cd2 [file] [log] [blame]
James Kuszmaul5398fae2020-02-17 16:44:03 -08001#include <queue>
2
3#include "gtest/gtest.h"
4
5#include "aos/controls/control_loop_test.h"
6#include "aos/events/logging/logger.h"
7#include "aos/network/team_number.h"
8#include "frc971/control_loops/drivetrain/drivetrain.h"
9#include "frc971/control_loops/drivetrain/drivetrain_test_lib.h"
10#include "frc971/control_loops/team_number_test_environment.h"
11#include "y2020/control_loops/drivetrain/drivetrain_base.h"
12#include "y2020/control_loops/drivetrain/localizer.h"
James Kuszmaul688a2b02020-02-19 18:26:33 -080013#include "y2020/control_loops/superstructure/superstructure_status_generated.h"
James Kuszmaul5398fae2020-02-17 16:44:03 -080014
15DEFINE_string(output_file, "",
16 "If set, logs all channels to the provided logfile.");
17
18// This file tests that the full 2020 localizer behaves sanely.
19
20namespace y2020 {
21namespace control_loops {
22namespace drivetrain {
23namespace testing {
24
25using frc971::control_loops::drivetrain::DrivetrainConfig;
26using frc971::control_loops::drivetrain::Goal;
27using frc971::control_loops::drivetrain::LocalizerControl;
28using frc971::vision::sift::ImageMatchResult;
29using frc971::vision::sift::ImageMatchResultT;
30using frc971::vision::sift::CameraPoseT;
31using frc971::vision::sift::CameraCalibrationT;
32using frc971::vision::sift::TransformationMatrixT;
33
34namespace {
35DrivetrainConfig<double> GetTest2020DrivetrainConfig() {
36 DrivetrainConfig<double> config = GetDrivetrainConfig();
37 return config;
38}
39
40// Copies an Eigen matrix into a row-major vector of the data.
41std::vector<float> MatrixToVector(const Eigen::Matrix<double, 4, 4> &H) {
42 std::vector<float> data;
43 for (int row = 0; row < 4; ++row) {
44 for (int col = 0; col < 4; ++col) {
45 data.push_back(H(row, col));
46 }
47 }
48 return data;
49}
50
51// Provides the location of the turret to use for simulation. Mostly we care
52// about providing a location that is not perfectly aligned with the robot's
53// origin.
54Eigen::Matrix<double, 4, 4> TurretRobotTransformation() {
55 Eigen::Matrix<double, 4, 4> H;
56 H.setIdentity();
James Kuszmaul688a2b02020-02-19 18:26:33 -080057 H.block<3, 1>(0, 3) << 1, 1.1, 0.9;
James Kuszmaul5398fae2020-02-17 16:44:03 -080058 return H;
59}
60
61// Provides the location of the camera on the turret.
62// TODO(james): Also simulate a fixed camera that is *not* on the turret.
63Eigen::Matrix<double, 4, 4> CameraTurretTransformation() {
64 Eigen::Matrix<double, 4, 4> H;
65 H.setIdentity();
66 H.block<3, 1>(0, 3) << 0.1, 0, 0;
67 // Introduce a bit of pitch to make sure that we're exercising all the code.
68 H.block<3, 3>(0, 0) =
James Kuszmaul688a2b02020-02-19 18:26:33 -080069 Eigen::AngleAxis<double>(0.1, Eigen::Vector3d::UnitY()) *
James Kuszmaul5398fae2020-02-17 16:44:03 -080070 H.block<3, 3>(0, 0);
71 return H;
72}
73
74// The absolute target location to use. Not meant to correspond with a
75// particular field target.
76// TODO(james): Make more targets.
James Kuszmaul688a2b02020-02-19 18:26:33 -080077std::vector<Eigen::Matrix<double, 4, 4>> TargetLocations() {
78 std::vector<Eigen::Matrix<double, 4, 4>> locations;
James Kuszmaul5398fae2020-02-17 16:44:03 -080079 Eigen::Matrix<double, 4, 4> H;
80 H.setIdentity();
81 H.block<3, 1>(0, 3) << 10.0, 0, 0;
James Kuszmaul688a2b02020-02-19 18:26:33 -080082 locations.push_back(H);
83 H.block<3, 1>(0, 3) << -10.0, 0, 0;
84 locations.push_back(H);
85 return locations;
James Kuszmaul5398fae2020-02-17 16:44:03 -080086}
87} // namespace
88
89namespace chrono = std::chrono;
90using frc971::control_loops::drivetrain::testing::DrivetrainSimulation;
91using frc971::control_loops::drivetrain::DrivetrainLoop;
92using frc971::control_loops::drivetrain::testing::GetTestDrivetrainConfig;
93using aos::monotonic_clock;
94
James Kuszmaul688a2b02020-02-19 18:26:33 -080095class LocalizedDrivetrainTest : public aos::testing::ControlLoopTest {
James Kuszmaul5398fae2020-02-17 16:44:03 -080096 protected:
97 // We must use the 2020 drivetrain config so that we don't have to deal
98 // with shifting:
99 LocalizedDrivetrainTest()
100 : aos::testing::ControlLoopTest(
101 aos::configuration::ReadConfig(
102 "y2020/control_loops/drivetrain/simulation_config.json"),
103 GetTest2020DrivetrainConfig().dt),
Austin Schuh6aa77be2020-02-22 21:06:40 -0800104 roborio_(aos::configuration::GetNode(configuration(), "roborio")),
105 pi1_(aos::configuration::GetNode(configuration(), "pi1")),
106 test_event_loop_(MakeEventLoop("test", roborio_)),
James Kuszmaul5398fae2020-02-17 16:44:03 -0800107 drivetrain_goal_sender_(
108 test_event_loop_->MakeSender<Goal>("/drivetrain")),
109 drivetrain_goal_fetcher_(
110 test_event_loop_->MakeFetcher<Goal>("/drivetrain")),
111 localizer_control_sender_(
112 test_event_loop_->MakeSender<LocalizerControl>("/drivetrain")),
James Kuszmaul688a2b02020-02-19 18:26:33 -0800113 superstructure_status_sender_(
114 test_event_loop_->MakeSender<superstructure::Status>(
115 "/superstructure")),
Austin Schuh6aa77be2020-02-22 21:06:40 -0800116 drivetrain_event_loop_(MakeEventLoop("drivetrain", roborio_)),
James Kuszmaul5398fae2020-02-17 16:44:03 -0800117 dt_config_(GetTest2020DrivetrainConfig()),
Austin Schuh6aa77be2020-02-22 21:06:40 -0800118 pi1_event_loop_(MakeEventLoop("test", pi1_)),
James Kuszmaul5398fae2020-02-17 16:44:03 -0800119 camera_sender_(
Austin Schuh6aa77be2020-02-22 21:06:40 -0800120 pi1_event_loop_->MakeSender<ImageMatchResult>("/pi1/camera")),
James Kuszmaul5398fae2020-02-17 16:44:03 -0800121 localizer_(drivetrain_event_loop_.get(), dt_config_),
122 drivetrain_(dt_config_, drivetrain_event_loop_.get(), &localizer_),
Austin Schuh6aa77be2020-02-22 21:06:40 -0800123 drivetrain_plant_event_loop_(MakeEventLoop("plant", roborio_)),
James Kuszmaul5398fae2020-02-17 16:44:03 -0800124 drivetrain_plant_(drivetrain_plant_event_loop_.get(), dt_config_),
125 last_frame_(monotonic_now()) {
126 set_team_id(frc971::control_loops::testing::kTeamNumber);
127 SetStartingPosition({3.0, 2.0, 0.0});
128 set_battery_voltage(12.0);
129
130 if (!FLAGS_output_file.empty()) {
131 log_buffer_writer_ = std::make_unique<aos::logger::DetachedBufferWriter>(
132 FLAGS_output_file);
Austin Schuh6aa77be2020-02-22 21:06:40 -0800133 logger_event_loop_ = MakeEventLoop("logger", roborio_);
James Kuszmaul5398fae2020-02-17 16:44:03 -0800134 logger_ = std::make_unique<aos::logger::Logger>(log_buffer_writer_.get(),
135 logger_event_loop_.get());
136 }
137
138 test_event_loop_->MakeWatcher(
139 "/drivetrain",
140 [this](const frc971::control_loops::drivetrain::Status &) {
141 // Needs to do camera updates right after we run the control loop.
142 if (enable_cameras_) {
143 SendDelayedFrames();
144 if (last_frame_ + std::chrono::milliseconds(100) <
145 monotonic_now()) {
146 CaptureFrames();
147 last_frame_ = monotonic_now();
148 }
149 }
James Kuszmaul688a2b02020-02-19 18:26:33 -0800150 });
151
152 test_event_loop_->AddPhasedLoop(
153 [this](int) {
154 // Also use the opportunity to send out turret messages.
155 UpdateTurretPosition();
156 auto builder = superstructure_status_sender_.MakeBuilder();
157 auto turret_builder =
158 builder
159 .MakeBuilder<frc971::control_loops::
160 PotAndAbsoluteEncoderProfiledJointStatus>();
161 turret_builder.add_position(turret_position_);
162 turret_builder.add_velocity(turret_velocity_);
163 auto turret_offset = turret_builder.Finish();
164 auto status_builder = builder.MakeBuilder<superstructure::Status>();
165 status_builder.add_turret(turret_offset);
166 builder.Send(status_builder.Finish());
167 },
168 chrono::milliseconds(5));
James Kuszmaul5398fae2020-02-17 16:44:03 -0800169
170 // Run for enough time to allow the gyro/imu zeroing code to run.
171 RunFor(std::chrono::seconds(10));
172 }
173
174 virtual ~LocalizedDrivetrainTest() override {}
175
176 void SetStartingPosition(const Eigen::Matrix<double, 3, 1> &xytheta) {
177 *drivetrain_plant_.mutable_state() << xytheta.x(), xytheta.y(),
178 xytheta(2, 0), 0.0, 0.0;
179 Eigen::Matrix<double, Localizer::HybridEkf::kNStates, 1> localizer_state;
180 localizer_state.setZero();
181 localizer_state.block<3, 1>(0, 0) = xytheta;
182 localizer_.Reset(monotonic_now(), localizer_state);
183 }
184
185 void VerifyNearGoal(double eps = 1e-3) {
186 drivetrain_goal_fetcher_.Fetch();
187 EXPECT_NEAR(drivetrain_goal_fetcher_->left_goal(),
188 drivetrain_plant_.GetLeftPosition(), eps);
189 EXPECT_NEAR(drivetrain_goal_fetcher_->right_goal(),
190 drivetrain_plant_.GetRightPosition(), eps);
191 }
192
James Kuszmaul688a2b02020-02-19 18:26:33 -0800193 ::testing::AssertionResult IsNear(double expected, double actual,
194 double epsilon) {
195 if (std::abs(expected - actual) < epsilon) {
196 return ::testing::AssertionSuccess();
197 } else {
198 return ::testing::AssertionFailure()
199 << "Expected " << expected << " but got " << actual
200 << " with a max difference of " << epsilon
201 << " and an actual difference of " << std::abs(expected - actual);
202 }
203 }
204 ::testing::AssertionResult VerifyEstimatorAccurate(double eps) {
James Kuszmaul5398fae2020-02-17 16:44:03 -0800205 const Eigen::Matrix<double, 5, 1> true_state = drivetrain_plant_.state();
James Kuszmaul688a2b02020-02-19 18:26:33 -0800206 ::testing::AssertionResult result(true);
207 if (!(result = IsNear(localizer_.x(), true_state(0), eps))) {
208 return result;
209 }
210 if (!(result = IsNear(localizer_.y(), true_state(1), eps))) {
211 return result;
212 }
213 if (!(result = IsNear(localizer_.theta(), true_state(2), eps))) {
214 return result;
215 }
216 if (!(result = IsNear(localizer_.left_velocity(), true_state(3), eps))) {
217 return result;
218 }
219 if (!(result = IsNear(localizer_.right_velocity(), true_state(4), eps))) {
220 return result;
221 }
222 return result;
James Kuszmaul5398fae2020-02-17 16:44:03 -0800223 }
224
225 // Goes through and captures frames on the camera(s), queueing them up to be
226 // sent by SendDelayedFrames().
227 void CaptureFrames() {
228 const frc971::control_loops::Pose robot_pose(
229 {drivetrain_plant_.GetPosition().x(),
230 drivetrain_plant_.GetPosition().y(), 0.0},
231 drivetrain_plant_.state()(2, 0));
232 std::unique_ptr<ImageMatchResultT> frame(new ImageMatchResultT());
233
James Kuszmaulc51dbfe2020-02-23 15:39:00 -0800234 for (const auto &H_field_target : TargetLocations()) {
James Kuszmaul5398fae2020-02-17 16:44:03 -0800235 std::unique_ptr<CameraPoseT> camera_target(new CameraPoseT());
236
237 camera_target->field_to_target.reset(new TransformationMatrixT());
James Kuszmaulc51dbfe2020-02-23 15:39:00 -0800238 camera_target->field_to_target->data = MatrixToVector(H_field_target);
James Kuszmaul688a2b02020-02-19 18:26:33 -0800239
James Kuszmaulc51dbfe2020-02-23 15:39:00 -0800240 Eigen::Matrix<double, 4, 4> H_turret_camera =
James Kuszmaul688a2b02020-02-19 18:26:33 -0800241 Eigen::Matrix<double, 4, 4>::Identity();
242 if (is_turreted_) {
James Kuszmaulc51dbfe2020-02-23 15:39:00 -0800243 H_turret_camera = frc971::control_loops::TransformationMatrixForYaw(
James Kuszmaul688a2b02020-02-19 18:26:33 -0800244 turret_position_) *
245 CameraTurretTransformation();
246 }
James Kuszmaul5398fae2020-02-17 16:44:03 -0800247
248 // TODO(james): Use non-zero turret angles.
249 camera_target->camera_to_target.reset(new TransformationMatrixT());
James Kuszmaulc51dbfe2020-02-23 15:39:00 -0800250 camera_target->camera_to_target->data =
251 MatrixToVector((robot_pose.AsTransformationMatrix() *
252 TurretRobotTransformation() * H_turret_camera)
253 .inverse() *
254 H_field_target);
James Kuszmaul5398fae2020-02-17 16:44:03 -0800255
256 frame->camera_poses.emplace_back(std::move(camera_target));
257 }
258
259 frame->image_monotonic_timestamp_ns =
260 chrono::duration_cast<chrono::nanoseconds>(
261 monotonic_now().time_since_epoch())
262 .count();
263 frame->camera_calibration.reset(new CameraCalibrationT());
264 {
265 frame->camera_calibration->fixed_extrinsics.reset(
266 new TransformationMatrixT());
James Kuszmaulc51dbfe2020-02-23 15:39:00 -0800267 TransformationMatrixT *H_robot_turret =
James Kuszmaul5398fae2020-02-17 16:44:03 -0800268 frame->camera_calibration->fixed_extrinsics.get();
James Kuszmaulc51dbfe2020-02-23 15:39:00 -0800269 H_robot_turret->data = MatrixToVector(TurretRobotTransformation());
James Kuszmaul5398fae2020-02-17 16:44:03 -0800270 }
James Kuszmaul688a2b02020-02-19 18:26:33 -0800271
272 if (is_turreted_) {
James Kuszmaul5398fae2020-02-17 16:44:03 -0800273 frame->camera_calibration->turret_extrinsics.reset(
274 new TransformationMatrixT());
James Kuszmaulc51dbfe2020-02-23 15:39:00 -0800275 TransformationMatrixT *H_turret_camera =
James Kuszmaul5398fae2020-02-17 16:44:03 -0800276 frame->camera_calibration->turret_extrinsics.get();
James Kuszmaulc51dbfe2020-02-23 15:39:00 -0800277 H_turret_camera->data = MatrixToVector(CameraTurretTransformation());
James Kuszmaul5398fae2020-02-17 16:44:03 -0800278 }
279
280 camera_delay_queue_.emplace(monotonic_now(), std::move(frame));
281 }
282
283 // Actually sends out all the camera frames.
284 void SendDelayedFrames() {
285 const std::chrono::milliseconds camera_latency(150);
286 while (!camera_delay_queue_.empty() &&
287 std::get<0>(camera_delay_queue_.front()) <
288 monotonic_now() - camera_latency) {
289 auto builder = camera_sender_.MakeBuilder();
290 ASSERT_TRUE(builder.Send(ImageMatchResult::Pack(
291 *builder.fbb(), std::get<1>(camera_delay_queue_.front()).get())));
292 camera_delay_queue_.pop();
293 }
294 }
295
Austin Schuh6aa77be2020-02-22 21:06:40 -0800296 const aos::Node *const roborio_;
297 const aos::Node *const pi1_;
298
James Kuszmaul5398fae2020-02-17 16:44:03 -0800299 std::unique_ptr<aos::EventLoop> test_event_loop_;
300 aos::Sender<Goal> drivetrain_goal_sender_;
301 aos::Fetcher<Goal> drivetrain_goal_fetcher_;
302 aos::Sender<LocalizerControl> localizer_control_sender_;
James Kuszmaul688a2b02020-02-19 18:26:33 -0800303 aos::Sender<superstructure::Status> superstructure_status_sender_;
James Kuszmaul5398fae2020-02-17 16:44:03 -0800304
305 std::unique_ptr<aos::EventLoop> drivetrain_event_loop_;
306 const frc971::control_loops::drivetrain::DrivetrainConfig<double>
307 dt_config_;
308
Austin Schuh6aa77be2020-02-22 21:06:40 -0800309 std::unique_ptr<aos::EventLoop> pi1_event_loop_;
James Kuszmaul5398fae2020-02-17 16:44:03 -0800310 aos::Sender<ImageMatchResult> camera_sender_;
311
312 Localizer localizer_;
313 DrivetrainLoop drivetrain_;
314
315 std::unique_ptr<aos::EventLoop> drivetrain_plant_event_loop_;
316 DrivetrainSimulation drivetrain_plant_;
317 monotonic_clock::time_point last_frame_;
318
319 // A queue of camera frames so that we can add a time delay to the data
320 // coming from the cameras.
321 std::queue<std::tuple<aos::monotonic_clock::time_point,
322 std::unique_ptr<ImageMatchResultT>>>
323 camera_delay_queue_;
324
325 void set_enable_cameras(bool enable) { enable_cameras_ = enable; }
James Kuszmaul688a2b02020-02-19 18:26:33 -0800326 void set_camera_is_turreted(bool turreted) { is_turreted_ = turreted; }
327
328 void set_turret(double position, double velocity) {
329 turret_position_ = position;
330 turret_velocity_ = velocity;
331 }
332
333 void SendGoal(double left, double right) {
334 auto builder = drivetrain_goal_sender_.MakeBuilder();
335
336 Goal::Builder drivetrain_builder = builder.MakeBuilder<Goal>();
337 drivetrain_builder.add_controller_type(
338 frc971::control_loops::drivetrain::ControllerType::MOTION_PROFILE);
339 drivetrain_builder.add_left_goal(left);
340 drivetrain_builder.add_right_goal(right);
341
342 EXPECT_TRUE(builder.Send(drivetrain_builder.Finish()));
343 }
James Kuszmaul5398fae2020-02-17 16:44:03 -0800344
345 private:
James Kuszmaul688a2b02020-02-19 18:26:33 -0800346 void UpdateTurretPosition() {
347 turret_position_ +=
348 turret_velocity_ *
349 aos::time::DurationInSeconds(monotonic_now() - last_turret_update_);
350 last_turret_update_ = monotonic_now();
351 }
352
James Kuszmaul5398fae2020-02-17 16:44:03 -0800353 bool enable_cameras_ = false;
James Kuszmaul688a2b02020-02-19 18:26:33 -0800354 // Whether to make the camera be on the turret or not.
355 bool is_turreted_ = true;
356
357 // The time at which we last incremented turret_position_.
358 monotonic_clock::time_point last_turret_update_ = monotonic_clock::min_time;
359 // Current turret position and velocity. These are set directly by the user in
360 // the test, and if velocity is non-zero, then we will automatically increment
361 // turret_position_ with every timestep.
362 double turret_position_ = 0.0; // rad
363 double turret_velocity_ = 0.0; // rad / sec
James Kuszmaul5398fae2020-02-17 16:44:03 -0800364
365 std::unique_ptr<aos::EventLoop> logger_event_loop_;
366 std::unique_ptr<aos::logger::DetachedBufferWriter> log_buffer_writer_;
367 std::unique_ptr<aos::logger::Logger> logger_;
368};
369
370// Tests that no camera updates, combined with a perfect model, results in no
371// error.
372TEST_F(LocalizedDrivetrainTest, NoCameraUpdate) {
373 SetEnabled(true);
374 set_enable_cameras(false);
James Kuszmaul688a2b02020-02-19 18:26:33 -0800375 EXPECT_TRUE(VerifyEstimatorAccurate(1e-7));
James Kuszmaul5398fae2020-02-17 16:44:03 -0800376
James Kuszmaul688a2b02020-02-19 18:26:33 -0800377 SendGoal(-1.0, 1.0);
James Kuszmaul5398fae2020-02-17 16:44:03 -0800378
James Kuszmaul5398fae2020-02-17 16:44:03 -0800379 RunFor(chrono::seconds(3));
380 VerifyNearGoal();
James Kuszmaul688a2b02020-02-19 18:26:33 -0800381 EXPECT_TRUE(VerifyEstimatorAccurate(5e-4));
James Kuszmaul5398fae2020-02-17 16:44:03 -0800382}
383
James Kuszmaul688a2b02020-02-19 18:26:33 -0800384// Tests that camera updates with a perfect models results in no errors.
James Kuszmaul5398fae2020-02-17 16:44:03 -0800385TEST_F(LocalizedDrivetrainTest, PerfectCameraUpdate) {
386 SetEnabled(true);
387 set_enable_cameras(true);
James Kuszmaul688a2b02020-02-19 18:26:33 -0800388 set_camera_is_turreted(true);
James Kuszmaul5398fae2020-02-17 16:44:03 -0800389
James Kuszmaul688a2b02020-02-19 18:26:33 -0800390 SendGoal(-1.0, 1.0);
James Kuszmaul5398fae2020-02-17 16:44:03 -0800391
James Kuszmaul5398fae2020-02-17 16:44:03 -0800392 RunFor(chrono::seconds(3));
393 VerifyNearGoal();
James Kuszmaul688a2b02020-02-19 18:26:33 -0800394 EXPECT_TRUE(VerifyEstimatorAccurate(5e-4));
James Kuszmaul5398fae2020-02-17 16:44:03 -0800395}
396
James Kuszmaul688a2b02020-02-19 18:26:33 -0800397// Tests that camera updates with a constant initial error in the position
James Kuszmaul5398fae2020-02-17 16:44:03 -0800398// results in convergence.
399TEST_F(LocalizedDrivetrainTest, InitialPositionError) {
400 SetEnabled(true);
401 set_enable_cameras(true);
James Kuszmaul688a2b02020-02-19 18:26:33 -0800402 set_camera_is_turreted(true);
James Kuszmaul5398fae2020-02-17 16:44:03 -0800403 drivetrain_plant_.mutable_state()->topRows(3) +=
404 Eigen::Vector3d(0.1, 0.1, 0.01);
James Kuszmaul5398fae2020-02-17 16:44:03 -0800405
James Kuszmaul688a2b02020-02-19 18:26:33 -0800406 // Confirm that some translational movement does get handled correctly.
407 SendGoal(-0.9, 1.0);
James Kuszmaul5398fae2020-02-17 16:44:03 -0800408
James Kuszmaul5398fae2020-02-17 16:44:03 -0800409 // Give the filters enough time to converge.
410 RunFor(chrono::seconds(10));
411 VerifyNearGoal(5e-3);
James Kuszmaul688a2b02020-02-19 18:26:33 -0800412 EXPECT_TRUE(VerifyEstimatorAccurate(1e-2));
413}
414
415// Tests that camera updates using a non-turreted camera work.
416TEST_F(LocalizedDrivetrainTest, InitialPositionErrorNoTurret) {
417 SetEnabled(true);
418 set_enable_cameras(true);
419 set_camera_is_turreted(false);
420 drivetrain_plant_.mutable_state()->topRows(3) +=
421 Eigen::Vector3d(0.1, 0.1, 0.01);
422
423 SendGoal(-1.0, 1.0);
424
425 // Give the filters enough time to converge.
426 RunFor(chrono::seconds(10));
427 VerifyNearGoal(5e-3);
428 EXPECT_TRUE(VerifyEstimatorAccurate(1e-2));
429}
430
431// Tests that we are able to handle a constant, non-zero turret angle.
432TEST_F(LocalizedDrivetrainTest, NonZeroTurret) {
433 SetEnabled(true);
434 set_enable_cameras(true);
435 set_camera_is_turreted(true);
436 set_turret(1.0, 0.0);
437 drivetrain_plant_.mutable_state()->topRows(3) +=
438 Eigen::Vector3d(0.1, 0.1, 0.0);
439
440 SendGoal(-1.0, 1.0);
441
442 // Give the filters enough time to converge.
443 RunFor(chrono::seconds(10));
444 VerifyNearGoal(5e-3);
445 EXPECT_TRUE(VerifyEstimatorAccurate(1e-3));
446}
447
448// Tests that we are able to handle a constant velocity turret.
449TEST_F(LocalizedDrivetrainTest, MovingTurret) {
450 SetEnabled(true);
451 set_enable_cameras(true);
452 set_camera_is_turreted(true);
453 set_turret(0.0, 0.2);
454 drivetrain_plant_.mutable_state()->topRows(3) +=
455 Eigen::Vector3d(0.1, 0.1, 0.0);
456
457 SendGoal(-1.0, 1.0);
458
459 // Give the filters enough time to converge.
460 RunFor(chrono::seconds(10));
461 VerifyNearGoal(5e-3);
462 EXPECT_TRUE(VerifyEstimatorAccurate(1e-3));
463}
464
465// Tests that we reject camera measurements when the turret is spinning too
466// fast.
467TEST_F(LocalizedDrivetrainTest, TooFastTurret) {
468 SetEnabled(true);
469 set_enable_cameras(true);
470 set_camera_is_turreted(true);
471 set_turret(0.0, -10.0);
472 const Eigen::Vector3d disturbance(0.1, 0.1, 0.0);
473 drivetrain_plant_.mutable_state()->topRows(3) += disturbance;
474
475 SendGoal(-1.0, 1.0);
476
477 RunFor(chrono::seconds(10));
478 EXPECT_FALSE(VerifyEstimatorAccurate(1e-3));
479 // If we remove the disturbance, we should now be correct.
480 drivetrain_plant_.mutable_state()->topRows(3) -= disturbance;
481 VerifyNearGoal(5e-3);
482 EXPECT_TRUE(VerifyEstimatorAccurate(1e-3));
483}
484
485// Tests that we don't reject camera measurements when the turret is spinning
486// too fast but we aren't using a camera attached to the turret.
487TEST_F(LocalizedDrivetrainTest, TooFastTurretDoesntAffectFixedCamera) {
488 SetEnabled(true);
489 set_enable_cameras(true);
490 set_camera_is_turreted(false);
491 set_turret(0.0, -10.0);
492 const Eigen::Vector3d disturbance(0.1, 0.1, 0.0);
493 drivetrain_plant_.mutable_state()->topRows(3) += disturbance;
494
495 SendGoal(-1.0, 1.0);
496
497 RunFor(chrono::seconds(10));
498 VerifyNearGoal(5e-3);
499 EXPECT_TRUE(VerifyEstimatorAccurate(1e-3));
James Kuszmaul5398fae2020-02-17 16:44:03 -0800500}
501
502} // namespace testing
503} // namespace drivetrain
504} // namespace control_loops
505} // namespace y2020