blob: ae9f887d395aa6994ed47a4e688b05cd2e1194dd [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"
James Kuszmaul958b21e2020-02-26 21:51:40 -08007#include "aos/network/message_bridge_server_generated.h"
James Kuszmaul5398fae2020-02-17 16:44:03 -08008#include "aos/network/team_number.h"
9#include "frc971/control_loops/drivetrain/drivetrain.h"
10#include "frc971/control_loops/drivetrain/drivetrain_test_lib.h"
11#include "frc971/control_loops/team_number_test_environment.h"
12#include "y2020/control_loops/drivetrain/drivetrain_base.h"
13#include "y2020/control_loops/drivetrain/localizer.h"
James Kuszmaul688a2b02020-02-19 18:26:33 -080014#include "y2020/control_loops/superstructure/superstructure_status_generated.h"
James Kuszmaul5398fae2020-02-17 16:44:03 -080015
16DEFINE_string(output_file, "",
17 "If set, logs all channels to the provided logfile.");
18
19// This file tests that the full 2020 localizer behaves sanely.
20
21namespace y2020 {
22namespace control_loops {
23namespace drivetrain {
24namespace testing {
25
26using frc971::control_loops::drivetrain::DrivetrainConfig;
27using frc971::control_loops::drivetrain::Goal;
28using frc971::control_loops::drivetrain::LocalizerControl;
29using frc971::vision::sift::ImageMatchResult;
30using frc971::vision::sift::ImageMatchResultT;
31using frc971::vision::sift::CameraPoseT;
32using frc971::vision::sift::CameraCalibrationT;
33using frc971::vision::sift::TransformationMatrixT;
34
35namespace {
36DrivetrainConfig<double> GetTest2020DrivetrainConfig() {
37 DrivetrainConfig<double> config = GetDrivetrainConfig();
38 return config;
39}
40
41// Copies an Eigen matrix into a row-major vector of the data.
42std::vector<float> MatrixToVector(const Eigen::Matrix<double, 4, 4> &H) {
43 std::vector<float> data;
44 for (int row = 0; row < 4; ++row) {
45 for (int col = 0; col < 4; ++col) {
46 data.push_back(H(row, col));
47 }
48 }
49 return data;
50}
51
52// Provides the location of the turret to use for simulation. Mostly we care
53// about providing a location that is not perfectly aligned with the robot's
54// origin.
55Eigen::Matrix<double, 4, 4> TurretRobotTransformation() {
56 Eigen::Matrix<double, 4, 4> H;
57 H.setIdentity();
James Kuszmaul688a2b02020-02-19 18:26:33 -080058 H.block<3, 1>(0, 3) << 1, 1.1, 0.9;
James Kuszmaul5398fae2020-02-17 16:44:03 -080059 return H;
60}
61
62// Provides the location of the camera on the turret.
63// TODO(james): Also simulate a fixed camera that is *not* on the turret.
64Eigen::Matrix<double, 4, 4> CameraTurretTransformation() {
65 Eigen::Matrix<double, 4, 4> H;
66 H.setIdentity();
67 H.block<3, 1>(0, 3) << 0.1, 0, 0;
68 // Introduce a bit of pitch to make sure that we're exercising all the code.
69 H.block<3, 3>(0, 0) =
James Kuszmaul688a2b02020-02-19 18:26:33 -080070 Eigen::AngleAxis<double>(0.1, Eigen::Vector3d::UnitY()) *
James Kuszmaul5398fae2020-02-17 16:44:03 -080071 H.block<3, 3>(0, 0);
72 return H;
73}
74
75// The absolute target location to use. Not meant to correspond with a
76// particular field target.
77// TODO(james): Make more targets.
James Kuszmaul688a2b02020-02-19 18:26:33 -080078std::vector<Eigen::Matrix<double, 4, 4>> TargetLocations() {
79 std::vector<Eigen::Matrix<double, 4, 4>> locations;
James Kuszmaul5398fae2020-02-17 16:44:03 -080080 Eigen::Matrix<double, 4, 4> H;
81 H.setIdentity();
82 H.block<3, 1>(0, 3) << 10.0, 0, 0;
James Kuszmaul688a2b02020-02-19 18:26:33 -080083 locations.push_back(H);
84 H.block<3, 1>(0, 3) << -10.0, 0, 0;
85 locations.push_back(H);
86 return locations;
James Kuszmaul5398fae2020-02-17 16:44:03 -080087}
James Kuszmaul958b21e2020-02-26 21:51:40 -080088
Austin Schuhbe69cf32020-08-27 11:38:33 -070089constexpr std::chrono::seconds kPiTimeOffset(-10);
James Kuszmaul5398fae2020-02-17 16:44:03 -080090} // namespace
91
92namespace chrono = std::chrono;
93using frc971::control_loops::drivetrain::testing::DrivetrainSimulation;
94using frc971::control_loops::drivetrain::DrivetrainLoop;
James Kuszmaul5398fae2020-02-17 16:44:03 -080095using aos::monotonic_clock;
96
James Kuszmaul688a2b02020-02-19 18:26:33 -080097class LocalizedDrivetrainTest : public aos::testing::ControlLoopTest {
James Kuszmaul5398fae2020-02-17 16:44:03 -080098 protected:
99 // We must use the 2020 drivetrain config so that we don't have to deal
100 // with shifting:
101 LocalizedDrivetrainTest()
102 : aos::testing::ControlLoopTest(
103 aos::configuration::ReadConfig(
104 "y2020/control_loops/drivetrain/simulation_config.json"),
105 GetTest2020DrivetrainConfig().dt),
Austin Schuh6aa77be2020-02-22 21:06:40 -0800106 roborio_(aos::configuration::GetNode(configuration(), "roborio")),
107 pi1_(aos::configuration::GetNode(configuration(), "pi1")),
108 test_event_loop_(MakeEventLoop("test", roborio_)),
James Kuszmaul5398fae2020-02-17 16:44:03 -0800109 drivetrain_goal_sender_(
110 test_event_loop_->MakeSender<Goal>("/drivetrain")),
111 drivetrain_goal_fetcher_(
112 test_event_loop_->MakeFetcher<Goal>("/drivetrain")),
113 localizer_control_sender_(
114 test_event_loop_->MakeSender<LocalizerControl>("/drivetrain")),
James Kuszmaul688a2b02020-02-19 18:26:33 -0800115 superstructure_status_sender_(
116 test_event_loop_->MakeSender<superstructure::Status>(
117 "/superstructure")),
James Kuszmaul958b21e2020-02-26 21:51:40 -0800118 server_statistics_sender_(
119 test_event_loop_->MakeSender<aos::message_bridge::ServerStatistics>(
120 "/aos")),
Austin Schuh6aa77be2020-02-22 21:06:40 -0800121 drivetrain_event_loop_(MakeEventLoop("drivetrain", roborio_)),
James Kuszmaul5398fae2020-02-17 16:44:03 -0800122 dt_config_(GetTest2020DrivetrainConfig()),
Austin Schuh6aa77be2020-02-22 21:06:40 -0800123 pi1_event_loop_(MakeEventLoop("test", pi1_)),
James Kuszmaul5398fae2020-02-17 16:44:03 -0800124 camera_sender_(
Austin Schuh6aa77be2020-02-22 21:06:40 -0800125 pi1_event_loop_->MakeSender<ImageMatchResult>("/pi1/camera")),
James Kuszmaul5398fae2020-02-17 16:44:03 -0800126 localizer_(drivetrain_event_loop_.get(), dt_config_),
127 drivetrain_(dt_config_, drivetrain_event_loop_.get(), &localizer_),
Austin Schuh6aa77be2020-02-22 21:06:40 -0800128 drivetrain_plant_event_loop_(MakeEventLoop("plant", roborio_)),
James Kuszmaul5398fae2020-02-17 16:44:03 -0800129 drivetrain_plant_(drivetrain_plant_event_loop_.get(), dt_config_),
130 last_frame_(monotonic_now()) {
James Kuszmaul958b21e2020-02-26 21:51:40 -0800131 event_loop_factory()->GetNodeEventLoopFactory(pi1_)->SetDistributedOffset(
Austin Schuhbe69cf32020-08-27 11:38:33 -0700132 kPiTimeOffset, 1.0);
James Kuszmaul958b21e2020-02-26 21:51:40 -0800133
James Kuszmaul5398fae2020-02-17 16:44:03 -0800134 set_team_id(frc971::control_loops::testing::kTeamNumber);
James Kuszmaul5398fae2020-02-17 16:44:03 -0800135 set_battery_voltage(12.0);
136
137 if (!FLAGS_output_file.empty()) {
Austin Schuh6aa77be2020-02-22 21:06:40 -0800138 logger_event_loop_ = MakeEventLoop("logger", roborio_);
Austin Schuh2f8fd752020-09-01 22:38:28 -0700139 logger_ = std::make_unique<aos::logger::Logger>(FLAGS_output_file,
James Kuszmaul5398fae2020-02-17 16:44:03 -0800140 logger_event_loop_.get());
141 }
142
143 test_event_loop_->MakeWatcher(
144 "/drivetrain",
145 [this](const frc971::control_loops::drivetrain::Status &) {
146 // Needs to do camera updates right after we run the control loop.
147 if (enable_cameras_) {
148 SendDelayedFrames();
149 if (last_frame_ + std::chrono::milliseconds(100) <
150 monotonic_now()) {
151 CaptureFrames();
152 last_frame_ = monotonic_now();
153 }
154 }
James Kuszmaul688a2b02020-02-19 18:26:33 -0800155 });
156
157 test_event_loop_->AddPhasedLoop(
158 [this](int) {
James Kuszmaul958b21e2020-02-26 21:51:40 -0800159 auto builder = server_statistics_sender_.MakeBuilder();
160 auto name_offset = builder.fbb()->CreateString("pi1");
161 auto node_builder = builder.MakeBuilder<aos::Node>();
162 node_builder.add_name(name_offset);
163 auto node_offset = node_builder.Finish();
164 auto connection_builder =
165 builder.MakeBuilder<aos::message_bridge::ServerConnection>();
166 connection_builder.add_node(node_offset);
167 connection_builder.add_monotonic_offset(
Austin Schuhbe69cf32020-08-27 11:38:33 -0700168 chrono::duration_cast<chrono::nanoseconds>(kPiTimeOffset)
James Kuszmaul958b21e2020-02-26 21:51:40 -0800169 .count());
170 auto connection_offset = connection_builder.Finish();
171 auto connections_offset =
172 builder.fbb()->CreateVector(&connection_offset, 1);
173 auto statistics_builder =
174 builder.MakeBuilder<aos::message_bridge::ServerStatistics>();
175 statistics_builder.add_connections(connections_offset);
176 builder.Send(statistics_builder.Finish());
177 },
178 chrono::milliseconds(500));
179
180 test_event_loop_->AddPhasedLoop(
181 [this](int) {
James Kuszmaul688a2b02020-02-19 18:26:33 -0800182 // Also use the opportunity to send out turret messages.
183 UpdateTurretPosition();
184 auto builder = superstructure_status_sender_.MakeBuilder();
185 auto turret_builder =
186 builder
187 .MakeBuilder<frc971::control_loops::
188 PotAndAbsoluteEncoderProfiledJointStatus>();
189 turret_builder.add_position(turret_position_);
190 turret_builder.add_velocity(turret_velocity_);
191 auto turret_offset = turret_builder.Finish();
192 auto status_builder = builder.MakeBuilder<superstructure::Status>();
193 status_builder.add_turret(turret_offset);
194 builder.Send(status_builder.Finish());
195 },
196 chrono::milliseconds(5));
James Kuszmaul5398fae2020-02-17 16:44:03 -0800197
James Kuszmaul286b4282020-02-26 20:29:32 -0800198 test_event_loop_->OnRun([this]() { SetStartingPosition({3.0, 2.0, 0.0}); });
199
James Kuszmaul5398fae2020-02-17 16:44:03 -0800200 // Run for enough time to allow the gyro/imu zeroing code to run.
201 RunFor(std::chrono::seconds(10));
202 }
203
204 virtual ~LocalizedDrivetrainTest() override {}
205
206 void SetStartingPosition(const Eigen::Matrix<double, 3, 1> &xytheta) {
207 *drivetrain_plant_.mutable_state() << xytheta.x(), xytheta.y(),
208 xytheta(2, 0), 0.0, 0.0;
James Kuszmauld478f872020-03-16 20:54:27 -0700209 Eigen::Matrix<float, Localizer::HybridEkf::kNStates, 1> localizer_state;
James Kuszmaul5398fae2020-02-17 16:44:03 -0800210 localizer_state.setZero();
James Kuszmauld478f872020-03-16 20:54:27 -0700211 localizer_state.block<3, 1>(0, 0) = xytheta.cast<float>();
James Kuszmaul5398fae2020-02-17 16:44:03 -0800212 localizer_.Reset(monotonic_now(), localizer_state);
213 }
214
215 void VerifyNearGoal(double eps = 1e-3) {
216 drivetrain_goal_fetcher_.Fetch();
217 EXPECT_NEAR(drivetrain_goal_fetcher_->left_goal(),
218 drivetrain_plant_.GetLeftPosition(), eps);
219 EXPECT_NEAR(drivetrain_goal_fetcher_->right_goal(),
220 drivetrain_plant_.GetRightPosition(), eps);
221 }
222
James Kuszmaul688a2b02020-02-19 18:26:33 -0800223 ::testing::AssertionResult IsNear(double expected, double actual,
224 double epsilon) {
225 if (std::abs(expected - actual) < epsilon) {
226 return ::testing::AssertionSuccess();
227 } else {
228 return ::testing::AssertionFailure()
229 << "Expected " << expected << " but got " << actual
230 << " with a max difference of " << epsilon
231 << " and an actual difference of " << std::abs(expected - actual);
232 }
233 }
234 ::testing::AssertionResult VerifyEstimatorAccurate(double eps) {
James Kuszmaul5398fae2020-02-17 16:44:03 -0800235 const Eigen::Matrix<double, 5, 1> true_state = drivetrain_plant_.state();
James Kuszmaul688a2b02020-02-19 18:26:33 -0800236 ::testing::AssertionResult result(true);
237 if (!(result = IsNear(localizer_.x(), true_state(0), eps))) {
238 return result;
239 }
240 if (!(result = IsNear(localizer_.y(), true_state(1), eps))) {
241 return result;
242 }
243 if (!(result = IsNear(localizer_.theta(), true_state(2), eps))) {
244 return result;
245 }
246 if (!(result = IsNear(localizer_.left_velocity(), true_state(3), eps))) {
247 return result;
248 }
249 if (!(result = IsNear(localizer_.right_velocity(), true_state(4), eps))) {
250 return result;
251 }
252 return result;
James Kuszmaul5398fae2020-02-17 16:44:03 -0800253 }
254
255 // Goes through and captures frames on the camera(s), queueing them up to be
256 // sent by SendDelayedFrames().
257 void CaptureFrames() {
258 const frc971::control_loops::Pose robot_pose(
259 {drivetrain_plant_.GetPosition().x(),
260 drivetrain_plant_.GetPosition().y(), 0.0},
261 drivetrain_plant_.state()(2, 0));
262 std::unique_ptr<ImageMatchResultT> frame(new ImageMatchResultT());
263
James Kuszmaulc51dbfe2020-02-23 15:39:00 -0800264 for (const auto &H_field_target : TargetLocations()) {
James Kuszmaul5398fae2020-02-17 16:44:03 -0800265 std::unique_ptr<CameraPoseT> camera_target(new CameraPoseT());
266
267 camera_target->field_to_target.reset(new TransformationMatrixT());
James Kuszmaulc51dbfe2020-02-23 15:39:00 -0800268 camera_target->field_to_target->data = MatrixToVector(H_field_target);
James Kuszmaul688a2b02020-02-19 18:26:33 -0800269
James Kuszmaulc51dbfe2020-02-23 15:39:00 -0800270 Eigen::Matrix<double, 4, 4> H_turret_camera =
James Kuszmaul688a2b02020-02-19 18:26:33 -0800271 Eigen::Matrix<double, 4, 4>::Identity();
272 if (is_turreted_) {
James Kuszmaulc51dbfe2020-02-23 15:39:00 -0800273 H_turret_camera = frc971::control_loops::TransformationMatrixForYaw(
James Kuszmaul688a2b02020-02-19 18:26:33 -0800274 turret_position_) *
275 CameraTurretTransformation();
276 }
James Kuszmaul5398fae2020-02-17 16:44:03 -0800277
278 // TODO(james): Use non-zero turret angles.
279 camera_target->camera_to_target.reset(new TransformationMatrixT());
James Kuszmaulc51dbfe2020-02-23 15:39:00 -0800280 camera_target->camera_to_target->data =
281 MatrixToVector((robot_pose.AsTransformationMatrix() *
282 TurretRobotTransformation() * H_turret_camera)
283 .inverse() *
284 H_field_target);
James Kuszmaul5398fae2020-02-17 16:44:03 -0800285
286 frame->camera_poses.emplace_back(std::move(camera_target));
287 }
288
289 frame->image_monotonic_timestamp_ns =
290 chrono::duration_cast<chrono::nanoseconds>(
James Kuszmaul958b21e2020-02-26 21:51:40 -0800291 event_loop_factory()
292 ->GetNodeEventLoopFactory(pi1_)
293 ->monotonic_now()
294 .time_since_epoch())
James Kuszmaul5398fae2020-02-17 16:44:03 -0800295 .count();
296 frame->camera_calibration.reset(new CameraCalibrationT());
297 {
298 frame->camera_calibration->fixed_extrinsics.reset(
299 new TransformationMatrixT());
James Kuszmaulc51dbfe2020-02-23 15:39:00 -0800300 TransformationMatrixT *H_robot_turret =
James Kuszmaul5398fae2020-02-17 16:44:03 -0800301 frame->camera_calibration->fixed_extrinsics.get();
James Kuszmaulc51dbfe2020-02-23 15:39:00 -0800302 H_robot_turret->data = MatrixToVector(TurretRobotTransformation());
James Kuszmaul5398fae2020-02-17 16:44:03 -0800303 }
James Kuszmaul688a2b02020-02-19 18:26:33 -0800304
305 if (is_turreted_) {
James Kuszmaul5398fae2020-02-17 16:44:03 -0800306 frame->camera_calibration->turret_extrinsics.reset(
307 new TransformationMatrixT());
James Kuszmaulc51dbfe2020-02-23 15:39:00 -0800308 TransformationMatrixT *H_turret_camera =
James Kuszmaul5398fae2020-02-17 16:44:03 -0800309 frame->camera_calibration->turret_extrinsics.get();
James Kuszmaulc51dbfe2020-02-23 15:39:00 -0800310 H_turret_camera->data = MatrixToVector(CameraTurretTransformation());
James Kuszmaul5398fae2020-02-17 16:44:03 -0800311 }
312
313 camera_delay_queue_.emplace(monotonic_now(), std::move(frame));
314 }
315
316 // Actually sends out all the camera frames.
317 void SendDelayedFrames() {
318 const std::chrono::milliseconds camera_latency(150);
319 while (!camera_delay_queue_.empty() &&
320 std::get<0>(camera_delay_queue_.front()) <
321 monotonic_now() - camera_latency) {
322 auto builder = camera_sender_.MakeBuilder();
323 ASSERT_TRUE(builder.Send(ImageMatchResult::Pack(
324 *builder.fbb(), std::get<1>(camera_delay_queue_.front()).get())));
325 camera_delay_queue_.pop();
326 }
327 }
328
Austin Schuh6aa77be2020-02-22 21:06:40 -0800329 const aos::Node *const roborio_;
330 const aos::Node *const pi1_;
331
James Kuszmaul5398fae2020-02-17 16:44:03 -0800332 std::unique_ptr<aos::EventLoop> test_event_loop_;
333 aos::Sender<Goal> drivetrain_goal_sender_;
334 aos::Fetcher<Goal> drivetrain_goal_fetcher_;
335 aos::Sender<LocalizerControl> localizer_control_sender_;
James Kuszmaul688a2b02020-02-19 18:26:33 -0800336 aos::Sender<superstructure::Status> superstructure_status_sender_;
James Kuszmaul958b21e2020-02-26 21:51:40 -0800337 aos::Sender<aos::message_bridge::ServerStatistics> server_statistics_sender_;
James Kuszmaul5398fae2020-02-17 16:44:03 -0800338
339 std::unique_ptr<aos::EventLoop> drivetrain_event_loop_;
340 const frc971::control_loops::drivetrain::DrivetrainConfig<double>
341 dt_config_;
342
Austin Schuh6aa77be2020-02-22 21:06:40 -0800343 std::unique_ptr<aos::EventLoop> pi1_event_loop_;
James Kuszmaul5398fae2020-02-17 16:44:03 -0800344 aos::Sender<ImageMatchResult> camera_sender_;
345
346 Localizer localizer_;
347 DrivetrainLoop drivetrain_;
348
349 std::unique_ptr<aos::EventLoop> drivetrain_plant_event_loop_;
350 DrivetrainSimulation drivetrain_plant_;
351 monotonic_clock::time_point last_frame_;
352
353 // A queue of camera frames so that we can add a time delay to the data
354 // coming from the cameras.
355 std::queue<std::tuple<aos::monotonic_clock::time_point,
356 std::unique_ptr<ImageMatchResultT>>>
357 camera_delay_queue_;
358
359 void set_enable_cameras(bool enable) { enable_cameras_ = enable; }
James Kuszmaul688a2b02020-02-19 18:26:33 -0800360 void set_camera_is_turreted(bool turreted) { is_turreted_ = turreted; }
361
362 void set_turret(double position, double velocity) {
363 turret_position_ = position;
364 turret_velocity_ = velocity;
365 }
366
367 void SendGoal(double left, double right) {
368 auto builder = drivetrain_goal_sender_.MakeBuilder();
369
370 Goal::Builder drivetrain_builder = builder.MakeBuilder<Goal>();
371 drivetrain_builder.add_controller_type(
372 frc971::control_loops::drivetrain::ControllerType::MOTION_PROFILE);
373 drivetrain_builder.add_left_goal(left);
374 drivetrain_builder.add_right_goal(right);
375
376 EXPECT_TRUE(builder.Send(drivetrain_builder.Finish()));
377 }
James Kuszmaul5398fae2020-02-17 16:44:03 -0800378
379 private:
James Kuszmaul688a2b02020-02-19 18:26:33 -0800380 void UpdateTurretPosition() {
381 turret_position_ +=
382 turret_velocity_ *
383 aos::time::DurationInSeconds(monotonic_now() - last_turret_update_);
384 last_turret_update_ = monotonic_now();
385 }
386
James Kuszmaul5398fae2020-02-17 16:44:03 -0800387 bool enable_cameras_ = false;
James Kuszmaul688a2b02020-02-19 18:26:33 -0800388 // Whether to make the camera be on the turret or not.
389 bool is_turreted_ = true;
390
391 // The time at which we last incremented turret_position_.
392 monotonic_clock::time_point last_turret_update_ = monotonic_clock::min_time;
393 // Current turret position and velocity. These are set directly by the user in
394 // the test, and if velocity is non-zero, then we will automatically increment
395 // turret_position_ with every timestep.
396 double turret_position_ = 0.0; // rad
397 double turret_velocity_ = 0.0; // rad / sec
James Kuszmaul5398fae2020-02-17 16:44:03 -0800398
399 std::unique_ptr<aos::EventLoop> logger_event_loop_;
James Kuszmaul5398fae2020-02-17 16:44:03 -0800400 std::unique_ptr<aos::logger::Logger> logger_;
401};
402
403// Tests that no camera updates, combined with a perfect model, results in no
404// error.
405TEST_F(LocalizedDrivetrainTest, NoCameraUpdate) {
406 SetEnabled(true);
407 set_enable_cameras(false);
James Kuszmaul688a2b02020-02-19 18:26:33 -0800408 EXPECT_TRUE(VerifyEstimatorAccurate(1e-7));
James Kuszmaul5398fae2020-02-17 16:44:03 -0800409
James Kuszmaul688a2b02020-02-19 18:26:33 -0800410 SendGoal(-1.0, 1.0);
James Kuszmaul5398fae2020-02-17 16:44:03 -0800411
James Kuszmaul5398fae2020-02-17 16:44:03 -0800412 RunFor(chrono::seconds(3));
413 VerifyNearGoal();
James Kuszmaul688a2b02020-02-19 18:26:33 -0800414 EXPECT_TRUE(VerifyEstimatorAccurate(5e-4));
James Kuszmaul5398fae2020-02-17 16:44:03 -0800415}
416
James Kuszmaul7f55f072020-03-01 10:21:26 -0800417// Tests that we can drive in a straight line and have things estimate
418// correctly.
419TEST_F(LocalizedDrivetrainTest, NoCameraUpdateStraightLine) {
420 SetEnabled(true);
421 set_enable_cameras(false);
422 EXPECT_TRUE(VerifyEstimatorAccurate(1e-7));
423
424 SendGoal(1.0, 1.0);
425
426 RunFor(chrono::seconds(1));
427 VerifyNearGoal();
428 // Due to accelerometer drift, the straight-line driving tends to be less
429 // accurate...
430 EXPECT_TRUE(VerifyEstimatorAccurate(0.1));
431}
432
James Kuszmaul688a2b02020-02-19 18:26:33 -0800433// Tests that camera updates with a perfect models results in no errors.
James Kuszmaul5398fae2020-02-17 16:44:03 -0800434TEST_F(LocalizedDrivetrainTest, PerfectCameraUpdate) {
435 SetEnabled(true);
436 set_enable_cameras(true);
James Kuszmaul688a2b02020-02-19 18:26:33 -0800437 set_camera_is_turreted(true);
James Kuszmaul5398fae2020-02-17 16:44:03 -0800438
James Kuszmaul688a2b02020-02-19 18:26:33 -0800439 SendGoal(-1.0, 1.0);
James Kuszmaul5398fae2020-02-17 16:44:03 -0800440
James Kuszmaul5398fae2020-02-17 16:44:03 -0800441 RunFor(chrono::seconds(3));
442 VerifyNearGoal();
James Kuszmaul66efe832020-03-16 19:38:33 -0700443 EXPECT_TRUE(VerifyEstimatorAccurate(2e-3));
James Kuszmaul5398fae2020-02-17 16:44:03 -0800444}
445
James Kuszmaul688a2b02020-02-19 18:26:33 -0800446// Tests that camera updates with a constant initial error in the position
James Kuszmaul5398fae2020-02-17 16:44:03 -0800447// results in convergence.
448TEST_F(LocalizedDrivetrainTest, InitialPositionError) {
449 SetEnabled(true);
450 set_enable_cameras(true);
James Kuszmaul688a2b02020-02-19 18:26:33 -0800451 set_camera_is_turreted(true);
James Kuszmaul5398fae2020-02-17 16:44:03 -0800452 drivetrain_plant_.mutable_state()->topRows(3) +=
453 Eigen::Vector3d(0.1, 0.1, 0.01);
James Kuszmaul5398fae2020-02-17 16:44:03 -0800454
James Kuszmaul688a2b02020-02-19 18:26:33 -0800455 // Confirm that some translational movement does get handled correctly.
456 SendGoal(-0.9, 1.0);
James Kuszmaul5398fae2020-02-17 16:44:03 -0800457
James Kuszmaul5398fae2020-02-17 16:44:03 -0800458 // Give the filters enough time to converge.
459 RunFor(chrono::seconds(10));
460 VerifyNearGoal(5e-3);
James Kuszmaul58cb1fe2020-03-07 16:18:59 -0800461 EXPECT_TRUE(VerifyEstimatorAccurate(4e-2));
James Kuszmaul688a2b02020-02-19 18:26:33 -0800462}
463
464// Tests that camera updates using a non-turreted camera work.
465TEST_F(LocalizedDrivetrainTest, InitialPositionErrorNoTurret) {
466 SetEnabled(true);
467 set_enable_cameras(true);
468 set_camera_is_turreted(false);
469 drivetrain_plant_.mutable_state()->topRows(3) +=
470 Eigen::Vector3d(0.1, 0.1, 0.01);
471
472 SendGoal(-1.0, 1.0);
473
474 // Give the filters enough time to converge.
475 RunFor(chrono::seconds(10));
476 VerifyNearGoal(5e-3);
James Kuszmaul58cb1fe2020-03-07 16:18:59 -0800477 EXPECT_TRUE(VerifyEstimatorAccurate(4e-2));
James Kuszmaul688a2b02020-02-19 18:26:33 -0800478}
479
480// Tests that we are able to handle a constant, non-zero turret angle.
481TEST_F(LocalizedDrivetrainTest, NonZeroTurret) {
482 SetEnabled(true);
483 set_enable_cameras(true);
484 set_camera_is_turreted(true);
485 set_turret(1.0, 0.0);
486 drivetrain_plant_.mutable_state()->topRows(3) +=
487 Eigen::Vector3d(0.1, 0.1, 0.0);
488
489 SendGoal(-1.0, 1.0);
490
491 // Give the filters enough time to converge.
492 RunFor(chrono::seconds(10));
493 VerifyNearGoal(5e-3);
James Kuszmaul58cb1fe2020-03-07 16:18:59 -0800494 EXPECT_TRUE(VerifyEstimatorAccurate(1e-2));
James Kuszmaul688a2b02020-02-19 18:26:33 -0800495}
496
497// Tests that we are able to handle a constant velocity turret.
498TEST_F(LocalizedDrivetrainTest, MovingTurret) {
499 SetEnabled(true);
500 set_enable_cameras(true);
501 set_camera_is_turreted(true);
502 set_turret(0.0, 0.2);
503 drivetrain_plant_.mutable_state()->topRows(3) +=
504 Eigen::Vector3d(0.1, 0.1, 0.0);
505
506 SendGoal(-1.0, 1.0);
507
508 // Give the filters enough time to converge.
509 RunFor(chrono::seconds(10));
510 VerifyNearGoal(5e-3);
James Kuszmaul58cb1fe2020-03-07 16:18:59 -0800511 EXPECT_TRUE(VerifyEstimatorAccurate(1e-2));
James Kuszmaul688a2b02020-02-19 18:26:33 -0800512}
513
514// Tests that we reject camera measurements when the turret is spinning too
515// fast.
516TEST_F(LocalizedDrivetrainTest, TooFastTurret) {
517 SetEnabled(true);
518 set_enable_cameras(true);
519 set_camera_is_turreted(true);
520 set_turret(0.0, -10.0);
521 const Eigen::Vector3d disturbance(0.1, 0.1, 0.0);
522 drivetrain_plant_.mutable_state()->topRows(3) += disturbance;
523
524 SendGoal(-1.0, 1.0);
525
526 RunFor(chrono::seconds(10));
527 EXPECT_FALSE(VerifyEstimatorAccurate(1e-3));
528 // If we remove the disturbance, we should now be correct.
529 drivetrain_plant_.mutable_state()->topRows(3) -= disturbance;
530 VerifyNearGoal(5e-3);
531 EXPECT_TRUE(VerifyEstimatorAccurate(1e-3));
532}
533
534// Tests that we don't reject camera measurements when the turret is spinning
535// too fast but we aren't using a camera attached to the turret.
536TEST_F(LocalizedDrivetrainTest, TooFastTurretDoesntAffectFixedCamera) {
537 SetEnabled(true);
538 set_enable_cameras(true);
539 set_camera_is_turreted(false);
540 set_turret(0.0, -10.0);
541 const Eigen::Vector3d disturbance(0.1, 0.1, 0.0);
542 drivetrain_plant_.mutable_state()->topRows(3) += disturbance;
543
544 SendGoal(-1.0, 1.0);
545
546 RunFor(chrono::seconds(10));
547 VerifyNearGoal(5e-3);
James Kuszmaul58cb1fe2020-03-07 16:18:59 -0800548 EXPECT_TRUE(VerifyEstimatorAccurate(1e-2));
James Kuszmaul5398fae2020-02-17 16:44:03 -0800549}
550
551} // namespace testing
552} // namespace drivetrain
553} // namespace control_loops
554} // namespace y2020