blob: e05a8105631e3d92bdf5fee10b6d2e6a0e7a9e29 [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
89constexpr 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;
95using frc971::control_loops::drivetrain::testing::GetTestDrivetrainConfig;
96using aos::monotonic_clock;
97
James Kuszmaul688a2b02020-02-19 18:26:33 -080098class LocalizedDrivetrainTest : public aos::testing::ControlLoopTest {
James Kuszmaul5398fae2020-02-17 16:44:03 -080099 protected:
100 // We must use the 2020 drivetrain config so that we don't have to deal
101 // with shifting:
102 LocalizedDrivetrainTest()
103 : aos::testing::ControlLoopTest(
104 aos::configuration::ReadConfig(
105 "y2020/control_loops/drivetrain/simulation_config.json"),
106 GetTest2020DrivetrainConfig().dt),
Austin Schuh6aa77be2020-02-22 21:06:40 -0800107 roborio_(aos::configuration::GetNode(configuration(), "roborio")),
108 pi1_(aos::configuration::GetNode(configuration(), "pi1")),
109 test_event_loop_(MakeEventLoop("test", roborio_)),
James Kuszmaul5398fae2020-02-17 16:44:03 -0800110 drivetrain_goal_sender_(
111 test_event_loop_->MakeSender<Goal>("/drivetrain")),
112 drivetrain_goal_fetcher_(
113 test_event_loop_->MakeFetcher<Goal>("/drivetrain")),
114 localizer_control_sender_(
115 test_event_loop_->MakeSender<LocalizerControl>("/drivetrain")),
James Kuszmaul688a2b02020-02-19 18:26:33 -0800116 superstructure_status_sender_(
117 test_event_loop_->MakeSender<superstructure::Status>(
118 "/superstructure")),
James Kuszmaul958b21e2020-02-26 21:51:40 -0800119 server_statistics_sender_(
120 test_event_loop_->MakeSender<aos::message_bridge::ServerStatistics>(
121 "/aos")),
Austin Schuh6aa77be2020-02-22 21:06:40 -0800122 drivetrain_event_loop_(MakeEventLoop("drivetrain", roborio_)),
James Kuszmaul5398fae2020-02-17 16:44:03 -0800123 dt_config_(GetTest2020DrivetrainConfig()),
Austin Schuh6aa77be2020-02-22 21:06:40 -0800124 pi1_event_loop_(MakeEventLoop("test", pi1_)),
James Kuszmaul5398fae2020-02-17 16:44:03 -0800125 camera_sender_(
Austin Schuh6aa77be2020-02-22 21:06:40 -0800126 pi1_event_loop_->MakeSender<ImageMatchResult>("/pi1/camera")),
James Kuszmaul5398fae2020-02-17 16:44:03 -0800127 localizer_(drivetrain_event_loop_.get(), dt_config_),
128 drivetrain_(dt_config_, drivetrain_event_loop_.get(), &localizer_),
Austin Schuh6aa77be2020-02-22 21:06:40 -0800129 drivetrain_plant_event_loop_(MakeEventLoop("plant", roborio_)),
James Kuszmaul5398fae2020-02-17 16:44:03 -0800130 drivetrain_plant_(drivetrain_plant_event_loop_.get(), dt_config_),
131 last_frame_(monotonic_now()) {
James Kuszmaul958b21e2020-02-26 21:51:40 -0800132 event_loop_factory()->GetNodeEventLoopFactory(pi1_)->SetDistributedOffset(
133 kPiTimeOffset);
134
James Kuszmaul5398fae2020-02-17 16:44:03 -0800135 set_team_id(frc971::control_loops::testing::kTeamNumber);
136 SetStartingPosition({3.0, 2.0, 0.0});
137 set_battery_voltage(12.0);
138
139 if (!FLAGS_output_file.empty()) {
140 log_buffer_writer_ = std::make_unique<aos::logger::DetachedBufferWriter>(
141 FLAGS_output_file);
Austin Schuh6aa77be2020-02-22 21:06:40 -0800142 logger_event_loop_ = MakeEventLoop("logger", roborio_);
James Kuszmaul5398fae2020-02-17 16:44:03 -0800143 logger_ = std::make_unique<aos::logger::Logger>(log_buffer_writer_.get(),
144 logger_event_loop_.get());
145 }
146
147 test_event_loop_->MakeWatcher(
148 "/drivetrain",
149 [this](const frc971::control_loops::drivetrain::Status &) {
150 // Needs to do camera updates right after we run the control loop.
151 if (enable_cameras_) {
152 SendDelayedFrames();
153 if (last_frame_ + std::chrono::milliseconds(100) <
154 monotonic_now()) {
155 CaptureFrames();
156 last_frame_ = monotonic_now();
157 }
158 }
James Kuszmaul688a2b02020-02-19 18:26:33 -0800159 });
160
161 test_event_loop_->AddPhasedLoop(
162 [this](int) {
James Kuszmaul958b21e2020-02-26 21:51:40 -0800163 auto builder = server_statistics_sender_.MakeBuilder();
164 auto name_offset = builder.fbb()->CreateString("pi1");
165 auto node_builder = builder.MakeBuilder<aos::Node>();
166 node_builder.add_name(name_offset);
167 auto node_offset = node_builder.Finish();
168 auto connection_builder =
169 builder.MakeBuilder<aos::message_bridge::ServerConnection>();
170 connection_builder.add_node(node_offset);
171 connection_builder.add_monotonic_offset(
172 chrono::duration_cast<chrono::nanoseconds>(-kPiTimeOffset)
173 .count());
174 auto connection_offset = connection_builder.Finish();
175 auto connections_offset =
176 builder.fbb()->CreateVector(&connection_offset, 1);
177 auto statistics_builder =
178 builder.MakeBuilder<aos::message_bridge::ServerStatistics>();
179 statistics_builder.add_connections(connections_offset);
180 builder.Send(statistics_builder.Finish());
181 },
182 chrono::milliseconds(500));
183
184 test_event_loop_->AddPhasedLoop(
185 [this](int) {
James Kuszmaul688a2b02020-02-19 18:26:33 -0800186 // Also use the opportunity to send out turret messages.
187 UpdateTurretPosition();
188 auto builder = superstructure_status_sender_.MakeBuilder();
189 auto turret_builder =
190 builder
191 .MakeBuilder<frc971::control_loops::
192 PotAndAbsoluteEncoderProfiledJointStatus>();
193 turret_builder.add_position(turret_position_);
194 turret_builder.add_velocity(turret_velocity_);
195 auto turret_offset = turret_builder.Finish();
196 auto status_builder = builder.MakeBuilder<superstructure::Status>();
197 status_builder.add_turret(turret_offset);
198 builder.Send(status_builder.Finish());
199 },
200 chrono::milliseconds(5));
James Kuszmaul5398fae2020-02-17 16:44:03 -0800201
202 // Run for enough time to allow the gyro/imu zeroing code to run.
203 RunFor(std::chrono::seconds(10));
204 }
205
206 virtual ~LocalizedDrivetrainTest() override {}
207
208 void SetStartingPosition(const Eigen::Matrix<double, 3, 1> &xytheta) {
209 *drivetrain_plant_.mutable_state() << xytheta.x(), xytheta.y(),
210 xytheta(2, 0), 0.0, 0.0;
211 Eigen::Matrix<double, Localizer::HybridEkf::kNStates, 1> localizer_state;
212 localizer_state.setZero();
213 localizer_state.block<3, 1>(0, 0) = xytheta;
214 localizer_.Reset(monotonic_now(), localizer_state);
215 }
216
217 void VerifyNearGoal(double eps = 1e-3) {
218 drivetrain_goal_fetcher_.Fetch();
219 EXPECT_NEAR(drivetrain_goal_fetcher_->left_goal(),
220 drivetrain_plant_.GetLeftPosition(), eps);
221 EXPECT_NEAR(drivetrain_goal_fetcher_->right_goal(),
222 drivetrain_plant_.GetRightPosition(), eps);
223 }
224
James Kuszmaul688a2b02020-02-19 18:26:33 -0800225 ::testing::AssertionResult IsNear(double expected, double actual,
226 double epsilon) {
227 if (std::abs(expected - actual) < epsilon) {
228 return ::testing::AssertionSuccess();
229 } else {
230 return ::testing::AssertionFailure()
231 << "Expected " << expected << " but got " << actual
232 << " with a max difference of " << epsilon
233 << " and an actual difference of " << std::abs(expected - actual);
234 }
235 }
236 ::testing::AssertionResult VerifyEstimatorAccurate(double eps) {
James Kuszmaul5398fae2020-02-17 16:44:03 -0800237 const Eigen::Matrix<double, 5, 1> true_state = drivetrain_plant_.state();
James Kuszmaul688a2b02020-02-19 18:26:33 -0800238 ::testing::AssertionResult result(true);
239 if (!(result = IsNear(localizer_.x(), true_state(0), eps))) {
240 return result;
241 }
242 if (!(result = IsNear(localizer_.y(), true_state(1), eps))) {
243 return result;
244 }
245 if (!(result = IsNear(localizer_.theta(), true_state(2), eps))) {
246 return result;
247 }
248 if (!(result = IsNear(localizer_.left_velocity(), true_state(3), eps))) {
249 return result;
250 }
251 if (!(result = IsNear(localizer_.right_velocity(), true_state(4), eps))) {
252 return result;
253 }
254 return result;
James Kuszmaul5398fae2020-02-17 16:44:03 -0800255 }
256
257 // Goes through and captures frames on the camera(s), queueing them up to be
258 // sent by SendDelayedFrames().
259 void CaptureFrames() {
260 const frc971::control_loops::Pose robot_pose(
261 {drivetrain_plant_.GetPosition().x(),
262 drivetrain_plant_.GetPosition().y(), 0.0},
263 drivetrain_plant_.state()(2, 0));
264 std::unique_ptr<ImageMatchResultT> frame(new ImageMatchResultT());
265
James Kuszmaulc51dbfe2020-02-23 15:39:00 -0800266 for (const auto &H_field_target : TargetLocations()) {
James Kuszmaul5398fae2020-02-17 16:44:03 -0800267 std::unique_ptr<CameraPoseT> camera_target(new CameraPoseT());
268
269 camera_target->field_to_target.reset(new TransformationMatrixT());
James Kuszmaulc51dbfe2020-02-23 15:39:00 -0800270 camera_target->field_to_target->data = MatrixToVector(H_field_target);
James Kuszmaul688a2b02020-02-19 18:26:33 -0800271
James Kuszmaulc51dbfe2020-02-23 15:39:00 -0800272 Eigen::Matrix<double, 4, 4> H_turret_camera =
James Kuszmaul688a2b02020-02-19 18:26:33 -0800273 Eigen::Matrix<double, 4, 4>::Identity();
274 if (is_turreted_) {
James Kuszmaulc51dbfe2020-02-23 15:39:00 -0800275 H_turret_camera = frc971::control_loops::TransformationMatrixForYaw(
James Kuszmaul688a2b02020-02-19 18:26:33 -0800276 turret_position_) *
277 CameraTurretTransformation();
278 }
James Kuszmaul5398fae2020-02-17 16:44:03 -0800279
280 // TODO(james): Use non-zero turret angles.
281 camera_target->camera_to_target.reset(new TransformationMatrixT());
James Kuszmaulc51dbfe2020-02-23 15:39:00 -0800282 camera_target->camera_to_target->data =
283 MatrixToVector((robot_pose.AsTransformationMatrix() *
284 TurretRobotTransformation() * H_turret_camera)
285 .inverse() *
286 H_field_target);
James Kuszmaul5398fae2020-02-17 16:44:03 -0800287
288 frame->camera_poses.emplace_back(std::move(camera_target));
289 }
290
291 frame->image_monotonic_timestamp_ns =
292 chrono::duration_cast<chrono::nanoseconds>(
James Kuszmaul958b21e2020-02-26 21:51:40 -0800293 event_loop_factory()
294 ->GetNodeEventLoopFactory(pi1_)
295 ->monotonic_now()
296 .time_since_epoch())
James Kuszmaul5398fae2020-02-17 16:44:03 -0800297 .count();
298 frame->camera_calibration.reset(new CameraCalibrationT());
299 {
300 frame->camera_calibration->fixed_extrinsics.reset(
301 new TransformationMatrixT());
James Kuszmaulc51dbfe2020-02-23 15:39:00 -0800302 TransformationMatrixT *H_robot_turret =
James Kuszmaul5398fae2020-02-17 16:44:03 -0800303 frame->camera_calibration->fixed_extrinsics.get();
James Kuszmaulc51dbfe2020-02-23 15:39:00 -0800304 H_robot_turret->data = MatrixToVector(TurretRobotTransformation());
James Kuszmaul5398fae2020-02-17 16:44:03 -0800305 }
James Kuszmaul688a2b02020-02-19 18:26:33 -0800306
307 if (is_turreted_) {
James Kuszmaul5398fae2020-02-17 16:44:03 -0800308 frame->camera_calibration->turret_extrinsics.reset(
309 new TransformationMatrixT());
James Kuszmaulc51dbfe2020-02-23 15:39:00 -0800310 TransformationMatrixT *H_turret_camera =
James Kuszmaul5398fae2020-02-17 16:44:03 -0800311 frame->camera_calibration->turret_extrinsics.get();
James Kuszmaulc51dbfe2020-02-23 15:39:00 -0800312 H_turret_camera->data = MatrixToVector(CameraTurretTransformation());
James Kuszmaul5398fae2020-02-17 16:44:03 -0800313 }
314
315 camera_delay_queue_.emplace(monotonic_now(), std::move(frame));
316 }
317
318 // Actually sends out all the camera frames.
319 void SendDelayedFrames() {
320 const std::chrono::milliseconds camera_latency(150);
321 while (!camera_delay_queue_.empty() &&
322 std::get<0>(camera_delay_queue_.front()) <
323 monotonic_now() - camera_latency) {
324 auto builder = camera_sender_.MakeBuilder();
325 ASSERT_TRUE(builder.Send(ImageMatchResult::Pack(
326 *builder.fbb(), std::get<1>(camera_delay_queue_.front()).get())));
327 camera_delay_queue_.pop();
328 }
329 }
330
Austin Schuh6aa77be2020-02-22 21:06:40 -0800331 const aos::Node *const roborio_;
332 const aos::Node *const pi1_;
333
James Kuszmaul5398fae2020-02-17 16:44:03 -0800334 std::unique_ptr<aos::EventLoop> test_event_loop_;
335 aos::Sender<Goal> drivetrain_goal_sender_;
336 aos::Fetcher<Goal> drivetrain_goal_fetcher_;
337 aos::Sender<LocalizerControl> localizer_control_sender_;
James Kuszmaul688a2b02020-02-19 18:26:33 -0800338 aos::Sender<superstructure::Status> superstructure_status_sender_;
James Kuszmaul958b21e2020-02-26 21:51:40 -0800339 aos::Sender<aos::message_bridge::ServerStatistics> server_statistics_sender_;
James Kuszmaul5398fae2020-02-17 16:44:03 -0800340
341 std::unique_ptr<aos::EventLoop> drivetrain_event_loop_;
342 const frc971::control_loops::drivetrain::DrivetrainConfig<double>
343 dt_config_;
344
Austin Schuh6aa77be2020-02-22 21:06:40 -0800345 std::unique_ptr<aos::EventLoop> pi1_event_loop_;
James Kuszmaul5398fae2020-02-17 16:44:03 -0800346 aos::Sender<ImageMatchResult> camera_sender_;
347
348 Localizer localizer_;
349 DrivetrainLoop drivetrain_;
350
351 std::unique_ptr<aos::EventLoop> drivetrain_plant_event_loop_;
352 DrivetrainSimulation drivetrain_plant_;
353 monotonic_clock::time_point last_frame_;
354
355 // A queue of camera frames so that we can add a time delay to the data
356 // coming from the cameras.
357 std::queue<std::tuple<aos::monotonic_clock::time_point,
358 std::unique_ptr<ImageMatchResultT>>>
359 camera_delay_queue_;
360
361 void set_enable_cameras(bool enable) { enable_cameras_ = enable; }
James Kuszmaul688a2b02020-02-19 18:26:33 -0800362 void set_camera_is_turreted(bool turreted) { is_turreted_ = turreted; }
363
364 void set_turret(double position, double velocity) {
365 turret_position_ = position;
366 turret_velocity_ = velocity;
367 }
368
369 void SendGoal(double left, double right) {
370 auto builder = drivetrain_goal_sender_.MakeBuilder();
371
372 Goal::Builder drivetrain_builder = builder.MakeBuilder<Goal>();
373 drivetrain_builder.add_controller_type(
374 frc971::control_loops::drivetrain::ControllerType::MOTION_PROFILE);
375 drivetrain_builder.add_left_goal(left);
376 drivetrain_builder.add_right_goal(right);
377
378 EXPECT_TRUE(builder.Send(drivetrain_builder.Finish()));
379 }
James Kuszmaul5398fae2020-02-17 16:44:03 -0800380
381 private:
James Kuszmaul688a2b02020-02-19 18:26:33 -0800382 void UpdateTurretPosition() {
383 turret_position_ +=
384 turret_velocity_ *
385 aos::time::DurationInSeconds(monotonic_now() - last_turret_update_);
386 last_turret_update_ = monotonic_now();
387 }
388
James Kuszmaul5398fae2020-02-17 16:44:03 -0800389 bool enable_cameras_ = false;
James Kuszmaul688a2b02020-02-19 18:26:33 -0800390 // Whether to make the camera be on the turret or not.
391 bool is_turreted_ = true;
392
393 // The time at which we last incremented turret_position_.
394 monotonic_clock::time_point last_turret_update_ = monotonic_clock::min_time;
395 // Current turret position and velocity. These are set directly by the user in
396 // the test, and if velocity is non-zero, then we will automatically increment
397 // turret_position_ with every timestep.
398 double turret_position_ = 0.0; // rad
399 double turret_velocity_ = 0.0; // rad / sec
James Kuszmaul5398fae2020-02-17 16:44:03 -0800400
401 std::unique_ptr<aos::EventLoop> logger_event_loop_;
402 std::unique_ptr<aos::logger::DetachedBufferWriter> log_buffer_writer_;
403 std::unique_ptr<aos::logger::Logger> logger_;
404};
405
406// Tests that no camera updates, combined with a perfect model, results in no
407// error.
408TEST_F(LocalizedDrivetrainTest, NoCameraUpdate) {
409 SetEnabled(true);
410 set_enable_cameras(false);
James Kuszmaul688a2b02020-02-19 18:26:33 -0800411 EXPECT_TRUE(VerifyEstimatorAccurate(1e-7));
James Kuszmaul5398fae2020-02-17 16:44:03 -0800412
James Kuszmaul688a2b02020-02-19 18:26:33 -0800413 SendGoal(-1.0, 1.0);
James Kuszmaul5398fae2020-02-17 16:44:03 -0800414
James Kuszmaul5398fae2020-02-17 16:44:03 -0800415 RunFor(chrono::seconds(3));
416 VerifyNearGoal();
James Kuszmaul688a2b02020-02-19 18:26:33 -0800417 EXPECT_TRUE(VerifyEstimatorAccurate(5e-4));
James Kuszmaul5398fae2020-02-17 16:44:03 -0800418}
419
James Kuszmaul688a2b02020-02-19 18:26:33 -0800420// Tests that camera updates with a perfect models results in no errors.
James Kuszmaul5398fae2020-02-17 16:44:03 -0800421TEST_F(LocalizedDrivetrainTest, PerfectCameraUpdate) {
422 SetEnabled(true);
423 set_enable_cameras(true);
James Kuszmaul688a2b02020-02-19 18:26:33 -0800424 set_camera_is_turreted(true);
James Kuszmaul5398fae2020-02-17 16:44:03 -0800425
James Kuszmaul688a2b02020-02-19 18:26:33 -0800426 SendGoal(-1.0, 1.0);
James Kuszmaul5398fae2020-02-17 16:44:03 -0800427
James Kuszmaul5398fae2020-02-17 16:44:03 -0800428 RunFor(chrono::seconds(3));
429 VerifyNearGoal();
James Kuszmaul688a2b02020-02-19 18:26:33 -0800430 EXPECT_TRUE(VerifyEstimatorAccurate(5e-4));
James Kuszmaul5398fae2020-02-17 16:44:03 -0800431}
432
James Kuszmaul688a2b02020-02-19 18:26:33 -0800433// Tests that camera updates with a constant initial error in the position
James Kuszmaul5398fae2020-02-17 16:44:03 -0800434// results in convergence.
435TEST_F(LocalizedDrivetrainTest, InitialPositionError) {
436 SetEnabled(true);
437 set_enable_cameras(true);
James Kuszmaul688a2b02020-02-19 18:26:33 -0800438 set_camera_is_turreted(true);
James Kuszmaul5398fae2020-02-17 16:44:03 -0800439 drivetrain_plant_.mutable_state()->topRows(3) +=
440 Eigen::Vector3d(0.1, 0.1, 0.01);
James Kuszmaul5398fae2020-02-17 16:44:03 -0800441
James Kuszmaul688a2b02020-02-19 18:26:33 -0800442 // Confirm that some translational movement does get handled correctly.
443 SendGoal(-0.9, 1.0);
James Kuszmaul5398fae2020-02-17 16:44:03 -0800444
James Kuszmaul5398fae2020-02-17 16:44:03 -0800445 // Give the filters enough time to converge.
446 RunFor(chrono::seconds(10));
447 VerifyNearGoal(5e-3);
James Kuszmaul688a2b02020-02-19 18:26:33 -0800448 EXPECT_TRUE(VerifyEstimatorAccurate(1e-2));
449}
450
451// Tests that camera updates using a non-turreted camera work.
452TEST_F(LocalizedDrivetrainTest, InitialPositionErrorNoTurret) {
453 SetEnabled(true);
454 set_enable_cameras(true);
455 set_camera_is_turreted(false);
456 drivetrain_plant_.mutable_state()->topRows(3) +=
457 Eigen::Vector3d(0.1, 0.1, 0.01);
458
459 SendGoal(-1.0, 1.0);
460
461 // Give the filters enough time to converge.
462 RunFor(chrono::seconds(10));
463 VerifyNearGoal(5e-3);
464 EXPECT_TRUE(VerifyEstimatorAccurate(1e-2));
465}
466
467// Tests that we are able to handle a constant, non-zero turret angle.
468TEST_F(LocalizedDrivetrainTest, NonZeroTurret) {
469 SetEnabled(true);
470 set_enable_cameras(true);
471 set_camera_is_turreted(true);
472 set_turret(1.0, 0.0);
473 drivetrain_plant_.mutable_state()->topRows(3) +=
474 Eigen::Vector3d(0.1, 0.1, 0.0);
475
476 SendGoal(-1.0, 1.0);
477
478 // Give the filters enough time to converge.
479 RunFor(chrono::seconds(10));
480 VerifyNearGoal(5e-3);
481 EXPECT_TRUE(VerifyEstimatorAccurate(1e-3));
482}
483
484// Tests that we are able to handle a constant velocity turret.
485TEST_F(LocalizedDrivetrainTest, MovingTurret) {
486 SetEnabled(true);
487 set_enable_cameras(true);
488 set_camera_is_turreted(true);
489 set_turret(0.0, 0.2);
490 drivetrain_plant_.mutable_state()->topRows(3) +=
491 Eigen::Vector3d(0.1, 0.1, 0.0);
492
493 SendGoal(-1.0, 1.0);
494
495 // Give the filters enough time to converge.
496 RunFor(chrono::seconds(10));
497 VerifyNearGoal(5e-3);
498 EXPECT_TRUE(VerifyEstimatorAccurate(1e-3));
499}
500
501// Tests that we reject camera measurements when the turret is spinning too
502// fast.
503TEST_F(LocalizedDrivetrainTest, TooFastTurret) {
504 SetEnabled(true);
505 set_enable_cameras(true);
506 set_camera_is_turreted(true);
507 set_turret(0.0, -10.0);
508 const Eigen::Vector3d disturbance(0.1, 0.1, 0.0);
509 drivetrain_plant_.mutable_state()->topRows(3) += disturbance;
510
511 SendGoal(-1.0, 1.0);
512
513 RunFor(chrono::seconds(10));
514 EXPECT_FALSE(VerifyEstimatorAccurate(1e-3));
515 // If we remove the disturbance, we should now be correct.
516 drivetrain_plant_.mutable_state()->topRows(3) -= disturbance;
517 VerifyNearGoal(5e-3);
518 EXPECT_TRUE(VerifyEstimatorAccurate(1e-3));
519}
520
521// Tests that we don't reject camera measurements when the turret is spinning
522// too fast but we aren't using a camera attached to the turret.
523TEST_F(LocalizedDrivetrainTest, TooFastTurretDoesntAffectFixedCamera) {
524 SetEnabled(true);
525 set_enable_cameras(true);
526 set_camera_is_turreted(false);
527 set_turret(0.0, -10.0);
528 const Eigen::Vector3d disturbance(0.1, 0.1, 0.0);
529 drivetrain_plant_.mutable_state()->topRows(3) += disturbance;
530
531 SendGoal(-1.0, 1.0);
532
533 RunFor(chrono::seconds(10));
534 VerifyNearGoal(5e-3);
535 EXPECT_TRUE(VerifyEstimatorAccurate(1e-3));
James Kuszmaul5398fae2020-02-17 16:44:03 -0800536}
537
538} // namespace testing
539} // namespace drivetrain
540} // namespace control_loops
541} // namespace y2020