blob: e81691c2a8827695ea8ad304fbcb945f145c9811 [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);
James Kuszmaul5398fae2020-02-17 16:44:03 -0800127 set_battery_voltage(12.0);
128
129 if (!FLAGS_output_file.empty()) {
130 log_buffer_writer_ = std::make_unique<aos::logger::DetachedBufferWriter>(
131 FLAGS_output_file);
Austin Schuh6aa77be2020-02-22 21:06:40 -0800132 logger_event_loop_ = MakeEventLoop("logger", roborio_);
James Kuszmaul5398fae2020-02-17 16:44:03 -0800133 logger_ = std::make_unique<aos::logger::Logger>(log_buffer_writer_.get(),
134 logger_event_loop_.get());
135 }
136
137 test_event_loop_->MakeWatcher(
138 "/drivetrain",
139 [this](const frc971::control_loops::drivetrain::Status &) {
140 // Needs to do camera updates right after we run the control loop.
141 if (enable_cameras_) {
142 SendDelayedFrames();
143 if (last_frame_ + std::chrono::milliseconds(100) <
144 monotonic_now()) {
145 CaptureFrames();
146 last_frame_ = monotonic_now();
147 }
148 }
James Kuszmaul688a2b02020-02-19 18:26:33 -0800149 });
150
151 test_event_loop_->AddPhasedLoop(
152 [this](int) {
153 // Also use the opportunity to send out turret messages.
154 UpdateTurretPosition();
155 auto builder = superstructure_status_sender_.MakeBuilder();
156 auto turret_builder =
157 builder
158 .MakeBuilder<frc971::control_loops::
159 PotAndAbsoluteEncoderProfiledJointStatus>();
160 turret_builder.add_position(turret_position_);
161 turret_builder.add_velocity(turret_velocity_);
162 auto turret_offset = turret_builder.Finish();
163 auto status_builder = builder.MakeBuilder<superstructure::Status>();
164 status_builder.add_turret(turret_offset);
165 builder.Send(status_builder.Finish());
166 },
167 chrono::milliseconds(5));
James Kuszmaul5398fae2020-02-17 16:44:03 -0800168
James Kuszmaul286b4282020-02-26 20:29:32 -0800169 test_event_loop_->OnRun([this]() { SetStartingPosition({3.0, 2.0, 0.0}); });
170
James Kuszmaul5398fae2020-02-17 16:44:03 -0800171 // Run for enough time to allow the gyro/imu zeroing code to run.
172 RunFor(std::chrono::seconds(10));
173 }
174
175 virtual ~LocalizedDrivetrainTest() override {}
176
177 void SetStartingPosition(const Eigen::Matrix<double, 3, 1> &xytheta) {
178 *drivetrain_plant_.mutable_state() << xytheta.x(), xytheta.y(),
179 xytheta(2, 0), 0.0, 0.0;
180 Eigen::Matrix<double, Localizer::HybridEkf::kNStates, 1> localizer_state;
181 localizer_state.setZero();
182 localizer_state.block<3, 1>(0, 0) = xytheta;
183 localizer_.Reset(monotonic_now(), localizer_state);
184 }
185
186 void VerifyNearGoal(double eps = 1e-3) {
187 drivetrain_goal_fetcher_.Fetch();
188 EXPECT_NEAR(drivetrain_goal_fetcher_->left_goal(),
189 drivetrain_plant_.GetLeftPosition(), eps);
190 EXPECT_NEAR(drivetrain_goal_fetcher_->right_goal(),
191 drivetrain_plant_.GetRightPosition(), eps);
192 }
193
James Kuszmaul688a2b02020-02-19 18:26:33 -0800194 ::testing::AssertionResult IsNear(double expected, double actual,
195 double epsilon) {
196 if (std::abs(expected - actual) < epsilon) {
197 return ::testing::AssertionSuccess();
198 } else {
199 return ::testing::AssertionFailure()
200 << "Expected " << expected << " but got " << actual
201 << " with a max difference of " << epsilon
202 << " and an actual difference of " << std::abs(expected - actual);
203 }
204 }
205 ::testing::AssertionResult VerifyEstimatorAccurate(double eps) {
James Kuszmaul5398fae2020-02-17 16:44:03 -0800206 const Eigen::Matrix<double, 5, 1> true_state = drivetrain_plant_.state();
James Kuszmaul688a2b02020-02-19 18:26:33 -0800207 ::testing::AssertionResult result(true);
208 if (!(result = IsNear(localizer_.x(), true_state(0), eps))) {
209 return result;
210 }
211 if (!(result = IsNear(localizer_.y(), true_state(1), eps))) {
212 return result;
213 }
214 if (!(result = IsNear(localizer_.theta(), true_state(2), eps))) {
215 return result;
216 }
217 if (!(result = IsNear(localizer_.left_velocity(), true_state(3), eps))) {
218 return result;
219 }
220 if (!(result = IsNear(localizer_.right_velocity(), true_state(4), eps))) {
221 return result;
222 }
223 return result;
James Kuszmaul5398fae2020-02-17 16:44:03 -0800224 }
225
226 // Goes through and captures frames on the camera(s), queueing them up to be
227 // sent by SendDelayedFrames().
228 void CaptureFrames() {
229 const frc971::control_loops::Pose robot_pose(
230 {drivetrain_plant_.GetPosition().x(),
231 drivetrain_plant_.GetPosition().y(), 0.0},
232 drivetrain_plant_.state()(2, 0));
233 std::unique_ptr<ImageMatchResultT> frame(new ImageMatchResultT());
234
James Kuszmaulc51dbfe2020-02-23 15:39:00 -0800235 for (const auto &H_field_target : TargetLocations()) {
James Kuszmaul5398fae2020-02-17 16:44:03 -0800236 std::unique_ptr<CameraPoseT> camera_target(new CameraPoseT());
237
238 camera_target->field_to_target.reset(new TransformationMatrixT());
James Kuszmaulc51dbfe2020-02-23 15:39:00 -0800239 camera_target->field_to_target->data = MatrixToVector(H_field_target);
James Kuszmaul688a2b02020-02-19 18:26:33 -0800240
James Kuszmaulc51dbfe2020-02-23 15:39:00 -0800241 Eigen::Matrix<double, 4, 4> H_turret_camera =
James Kuszmaul688a2b02020-02-19 18:26:33 -0800242 Eigen::Matrix<double, 4, 4>::Identity();
243 if (is_turreted_) {
James Kuszmaulc51dbfe2020-02-23 15:39:00 -0800244 H_turret_camera = frc971::control_loops::TransformationMatrixForYaw(
James Kuszmaul688a2b02020-02-19 18:26:33 -0800245 turret_position_) *
246 CameraTurretTransformation();
247 }
James Kuszmaul5398fae2020-02-17 16:44:03 -0800248
249 // TODO(james): Use non-zero turret angles.
250 camera_target->camera_to_target.reset(new TransformationMatrixT());
James Kuszmaulc51dbfe2020-02-23 15:39:00 -0800251 camera_target->camera_to_target->data =
252 MatrixToVector((robot_pose.AsTransformationMatrix() *
253 TurretRobotTransformation() * H_turret_camera)
254 .inverse() *
255 H_field_target);
James Kuszmaul5398fae2020-02-17 16:44:03 -0800256
257 frame->camera_poses.emplace_back(std::move(camera_target));
258 }
259
260 frame->image_monotonic_timestamp_ns =
261 chrono::duration_cast<chrono::nanoseconds>(
262 monotonic_now().time_since_epoch())
263 .count();
264 frame->camera_calibration.reset(new CameraCalibrationT());
265 {
266 frame->camera_calibration->fixed_extrinsics.reset(
267 new TransformationMatrixT());
James Kuszmaulc51dbfe2020-02-23 15:39:00 -0800268 TransformationMatrixT *H_robot_turret =
James Kuszmaul5398fae2020-02-17 16:44:03 -0800269 frame->camera_calibration->fixed_extrinsics.get();
James Kuszmaulc51dbfe2020-02-23 15:39:00 -0800270 H_robot_turret->data = MatrixToVector(TurretRobotTransformation());
James Kuszmaul5398fae2020-02-17 16:44:03 -0800271 }
James Kuszmaul688a2b02020-02-19 18:26:33 -0800272
273 if (is_turreted_) {
James Kuszmaul5398fae2020-02-17 16:44:03 -0800274 frame->camera_calibration->turret_extrinsics.reset(
275 new TransformationMatrixT());
James Kuszmaulc51dbfe2020-02-23 15:39:00 -0800276 TransformationMatrixT *H_turret_camera =
James Kuszmaul5398fae2020-02-17 16:44:03 -0800277 frame->camera_calibration->turret_extrinsics.get();
James Kuszmaulc51dbfe2020-02-23 15:39:00 -0800278 H_turret_camera->data = MatrixToVector(CameraTurretTransformation());
James Kuszmaul5398fae2020-02-17 16:44:03 -0800279 }
280
281 camera_delay_queue_.emplace(monotonic_now(), std::move(frame));
282 }
283
284 // Actually sends out all the camera frames.
285 void SendDelayedFrames() {
286 const std::chrono::milliseconds camera_latency(150);
287 while (!camera_delay_queue_.empty() &&
288 std::get<0>(camera_delay_queue_.front()) <
289 monotonic_now() - camera_latency) {
290 auto builder = camera_sender_.MakeBuilder();
291 ASSERT_TRUE(builder.Send(ImageMatchResult::Pack(
292 *builder.fbb(), std::get<1>(camera_delay_queue_.front()).get())));
293 camera_delay_queue_.pop();
294 }
295 }
296
Austin Schuh6aa77be2020-02-22 21:06:40 -0800297 const aos::Node *const roborio_;
298 const aos::Node *const pi1_;
299
James Kuszmaul5398fae2020-02-17 16:44:03 -0800300 std::unique_ptr<aos::EventLoop> test_event_loop_;
301 aos::Sender<Goal> drivetrain_goal_sender_;
302 aos::Fetcher<Goal> drivetrain_goal_fetcher_;
303 aos::Sender<LocalizerControl> localizer_control_sender_;
James Kuszmaul688a2b02020-02-19 18:26:33 -0800304 aos::Sender<superstructure::Status> superstructure_status_sender_;
James Kuszmaul5398fae2020-02-17 16:44:03 -0800305
306 std::unique_ptr<aos::EventLoop> drivetrain_event_loop_;
307 const frc971::control_loops::drivetrain::DrivetrainConfig<double>
308 dt_config_;
309
Austin Schuh6aa77be2020-02-22 21:06:40 -0800310 std::unique_ptr<aos::EventLoop> pi1_event_loop_;
James Kuszmaul5398fae2020-02-17 16:44:03 -0800311 aos::Sender<ImageMatchResult> camera_sender_;
312
313 Localizer localizer_;
314 DrivetrainLoop drivetrain_;
315
316 std::unique_ptr<aos::EventLoop> drivetrain_plant_event_loop_;
317 DrivetrainSimulation drivetrain_plant_;
318 monotonic_clock::time_point last_frame_;
319
320 // A queue of camera frames so that we can add a time delay to the data
321 // coming from the cameras.
322 std::queue<std::tuple<aos::monotonic_clock::time_point,
323 std::unique_ptr<ImageMatchResultT>>>
324 camera_delay_queue_;
325
326 void set_enable_cameras(bool enable) { enable_cameras_ = enable; }
James Kuszmaul688a2b02020-02-19 18:26:33 -0800327 void set_camera_is_turreted(bool turreted) { is_turreted_ = turreted; }
328
329 void set_turret(double position, double velocity) {
330 turret_position_ = position;
331 turret_velocity_ = velocity;
332 }
333
334 void SendGoal(double left, double right) {
335 auto builder = drivetrain_goal_sender_.MakeBuilder();
336
337 Goal::Builder drivetrain_builder = builder.MakeBuilder<Goal>();
338 drivetrain_builder.add_controller_type(
339 frc971::control_loops::drivetrain::ControllerType::MOTION_PROFILE);
340 drivetrain_builder.add_left_goal(left);
341 drivetrain_builder.add_right_goal(right);
342
343 EXPECT_TRUE(builder.Send(drivetrain_builder.Finish()));
344 }
James Kuszmaul5398fae2020-02-17 16:44:03 -0800345
346 private:
James Kuszmaul688a2b02020-02-19 18:26:33 -0800347 void UpdateTurretPosition() {
348 turret_position_ +=
349 turret_velocity_ *
350 aos::time::DurationInSeconds(monotonic_now() - last_turret_update_);
351 last_turret_update_ = monotonic_now();
352 }
353
James Kuszmaul5398fae2020-02-17 16:44:03 -0800354 bool enable_cameras_ = false;
James Kuszmaul688a2b02020-02-19 18:26:33 -0800355 // Whether to make the camera be on the turret or not.
356 bool is_turreted_ = true;
357
358 // The time at which we last incremented turret_position_.
359 monotonic_clock::time_point last_turret_update_ = monotonic_clock::min_time;
360 // Current turret position and velocity. These are set directly by the user in
361 // the test, and if velocity is non-zero, then we will automatically increment
362 // turret_position_ with every timestep.
363 double turret_position_ = 0.0; // rad
364 double turret_velocity_ = 0.0; // rad / sec
James Kuszmaul5398fae2020-02-17 16:44:03 -0800365
366 std::unique_ptr<aos::EventLoop> logger_event_loop_;
367 std::unique_ptr<aos::logger::DetachedBufferWriter> log_buffer_writer_;
368 std::unique_ptr<aos::logger::Logger> logger_;
369};
370
371// Tests that no camera updates, combined with a perfect model, results in no
372// error.
373TEST_F(LocalizedDrivetrainTest, NoCameraUpdate) {
374 SetEnabled(true);
375 set_enable_cameras(false);
James Kuszmaul688a2b02020-02-19 18:26:33 -0800376 EXPECT_TRUE(VerifyEstimatorAccurate(1e-7));
James Kuszmaul5398fae2020-02-17 16:44:03 -0800377
James Kuszmaul688a2b02020-02-19 18:26:33 -0800378 SendGoal(-1.0, 1.0);
James Kuszmaul5398fae2020-02-17 16:44:03 -0800379
James Kuszmaul5398fae2020-02-17 16:44:03 -0800380 RunFor(chrono::seconds(3));
381 VerifyNearGoal();
James Kuszmaul688a2b02020-02-19 18:26:33 -0800382 EXPECT_TRUE(VerifyEstimatorAccurate(5e-4));
James Kuszmaul5398fae2020-02-17 16:44:03 -0800383}
384
James Kuszmaul688a2b02020-02-19 18:26:33 -0800385// Tests that camera updates with a perfect models results in no errors.
James Kuszmaul5398fae2020-02-17 16:44:03 -0800386TEST_F(LocalizedDrivetrainTest, PerfectCameraUpdate) {
387 SetEnabled(true);
388 set_enable_cameras(true);
James Kuszmaul688a2b02020-02-19 18:26:33 -0800389 set_camera_is_turreted(true);
James Kuszmaul5398fae2020-02-17 16:44:03 -0800390
James Kuszmaul688a2b02020-02-19 18:26:33 -0800391 SendGoal(-1.0, 1.0);
James Kuszmaul5398fae2020-02-17 16:44:03 -0800392
James Kuszmaul5398fae2020-02-17 16:44:03 -0800393 RunFor(chrono::seconds(3));
394 VerifyNearGoal();
James Kuszmaul688a2b02020-02-19 18:26:33 -0800395 EXPECT_TRUE(VerifyEstimatorAccurate(5e-4));
James Kuszmaul5398fae2020-02-17 16:44:03 -0800396}
397
James Kuszmaul688a2b02020-02-19 18:26:33 -0800398// Tests that camera updates with a constant initial error in the position
James Kuszmaul5398fae2020-02-17 16:44:03 -0800399// results in convergence.
400TEST_F(LocalizedDrivetrainTest, InitialPositionError) {
401 SetEnabled(true);
402 set_enable_cameras(true);
James Kuszmaul688a2b02020-02-19 18:26:33 -0800403 set_camera_is_turreted(true);
James Kuszmaul5398fae2020-02-17 16:44:03 -0800404 drivetrain_plant_.mutable_state()->topRows(3) +=
405 Eigen::Vector3d(0.1, 0.1, 0.01);
James Kuszmaul5398fae2020-02-17 16:44:03 -0800406
James Kuszmaul688a2b02020-02-19 18:26:33 -0800407 // Confirm that some translational movement does get handled correctly.
408 SendGoal(-0.9, 1.0);
James Kuszmaul5398fae2020-02-17 16:44:03 -0800409
James Kuszmaul5398fae2020-02-17 16:44:03 -0800410 // Give the filters enough time to converge.
411 RunFor(chrono::seconds(10));
412 VerifyNearGoal(5e-3);
James Kuszmaul688a2b02020-02-19 18:26:33 -0800413 EXPECT_TRUE(VerifyEstimatorAccurate(1e-2));
414}
415
416// Tests that camera updates using a non-turreted camera work.
417TEST_F(LocalizedDrivetrainTest, InitialPositionErrorNoTurret) {
418 SetEnabled(true);
419 set_enable_cameras(true);
420 set_camera_is_turreted(false);
421 drivetrain_plant_.mutable_state()->topRows(3) +=
422 Eigen::Vector3d(0.1, 0.1, 0.01);
423
424 SendGoal(-1.0, 1.0);
425
426 // Give the filters enough time to converge.
427 RunFor(chrono::seconds(10));
428 VerifyNearGoal(5e-3);
429 EXPECT_TRUE(VerifyEstimatorAccurate(1e-2));
430}
431
432// Tests that we are able to handle a constant, non-zero turret angle.
433TEST_F(LocalizedDrivetrainTest, NonZeroTurret) {
434 SetEnabled(true);
435 set_enable_cameras(true);
436 set_camera_is_turreted(true);
437 set_turret(1.0, 0.0);
438 drivetrain_plant_.mutable_state()->topRows(3) +=
439 Eigen::Vector3d(0.1, 0.1, 0.0);
440
441 SendGoal(-1.0, 1.0);
442
443 // Give the filters enough time to converge.
444 RunFor(chrono::seconds(10));
445 VerifyNearGoal(5e-3);
446 EXPECT_TRUE(VerifyEstimatorAccurate(1e-3));
447}
448
449// Tests that we are able to handle a constant velocity turret.
450TEST_F(LocalizedDrivetrainTest, MovingTurret) {
451 SetEnabled(true);
452 set_enable_cameras(true);
453 set_camera_is_turreted(true);
454 set_turret(0.0, 0.2);
455 drivetrain_plant_.mutable_state()->topRows(3) +=
456 Eigen::Vector3d(0.1, 0.1, 0.0);
457
458 SendGoal(-1.0, 1.0);
459
460 // Give the filters enough time to converge.
461 RunFor(chrono::seconds(10));
462 VerifyNearGoal(5e-3);
463 EXPECT_TRUE(VerifyEstimatorAccurate(1e-3));
464}
465
466// Tests that we reject camera measurements when the turret is spinning too
467// fast.
468TEST_F(LocalizedDrivetrainTest, TooFastTurret) {
469 SetEnabled(true);
470 set_enable_cameras(true);
471 set_camera_is_turreted(true);
472 set_turret(0.0, -10.0);
473 const Eigen::Vector3d disturbance(0.1, 0.1, 0.0);
474 drivetrain_plant_.mutable_state()->topRows(3) += disturbance;
475
476 SendGoal(-1.0, 1.0);
477
478 RunFor(chrono::seconds(10));
479 EXPECT_FALSE(VerifyEstimatorAccurate(1e-3));
480 // If we remove the disturbance, we should now be correct.
481 drivetrain_plant_.mutable_state()->topRows(3) -= disturbance;
482 VerifyNearGoal(5e-3);
483 EXPECT_TRUE(VerifyEstimatorAccurate(1e-3));
484}
485
486// Tests that we don't reject camera measurements when the turret is spinning
487// too fast but we aren't using a camera attached to the turret.
488TEST_F(LocalizedDrivetrainTest, TooFastTurretDoesntAffectFixedCamera) {
489 SetEnabled(true);
490 set_enable_cameras(true);
491 set_camera_is_turreted(false);
492 set_turret(0.0, -10.0);
493 const Eigen::Vector3d disturbance(0.1, 0.1, 0.0);
494 drivetrain_plant_.mutable_state()->topRows(3) += disturbance;
495
496 SendGoal(-1.0, 1.0);
497
498 RunFor(chrono::seconds(10));
499 VerifyNearGoal(5e-3);
500 EXPECT_TRUE(VerifyEstimatorAccurate(1e-3));
James Kuszmaul5398fae2020-02-17 16:44:03 -0800501}
502
503} // namespace testing
504} // namespace drivetrain
505} // namespace control_loops
506} // namespace y2020