blob: c438af512677892441dca043c8483e8afc99902b [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);
James Kuszmaul5398fae2020-02-17 16:44:03 -0800136 set_battery_voltage(12.0);
137
138 if (!FLAGS_output_file.empty()) {
139 log_buffer_writer_ = std::make_unique<aos::logger::DetachedBufferWriter>(
140 FLAGS_output_file);
Austin Schuh6aa77be2020-02-22 21:06:40 -0800141 logger_event_loop_ = MakeEventLoop("logger", roborio_);
James Kuszmaul5398fae2020-02-17 16:44:03 -0800142 logger_ = std::make_unique<aos::logger::Logger>(log_buffer_writer_.get(),
143 logger_event_loop_.get());
144 }
145
146 test_event_loop_->MakeWatcher(
147 "/drivetrain",
148 [this](const frc971::control_loops::drivetrain::Status &) {
149 // Needs to do camera updates right after we run the control loop.
150 if (enable_cameras_) {
151 SendDelayedFrames();
152 if (last_frame_ + std::chrono::milliseconds(100) <
153 monotonic_now()) {
154 CaptureFrames();
155 last_frame_ = monotonic_now();
156 }
157 }
James Kuszmaul688a2b02020-02-19 18:26:33 -0800158 });
159
160 test_event_loop_->AddPhasedLoop(
161 [this](int) {
James Kuszmaul958b21e2020-02-26 21:51:40 -0800162 auto builder = server_statistics_sender_.MakeBuilder();
163 auto name_offset = builder.fbb()->CreateString("pi1");
164 auto node_builder = builder.MakeBuilder<aos::Node>();
165 node_builder.add_name(name_offset);
166 auto node_offset = node_builder.Finish();
167 auto connection_builder =
168 builder.MakeBuilder<aos::message_bridge::ServerConnection>();
169 connection_builder.add_node(node_offset);
170 connection_builder.add_monotonic_offset(
171 chrono::duration_cast<chrono::nanoseconds>(-kPiTimeOffset)
172 .count());
173 auto connection_offset = connection_builder.Finish();
174 auto connections_offset =
175 builder.fbb()->CreateVector(&connection_offset, 1);
176 auto statistics_builder =
177 builder.MakeBuilder<aos::message_bridge::ServerStatistics>();
178 statistics_builder.add_connections(connections_offset);
179 builder.Send(statistics_builder.Finish());
180 },
181 chrono::milliseconds(500));
182
183 test_event_loop_->AddPhasedLoop(
184 [this](int) {
James Kuszmaul688a2b02020-02-19 18:26:33 -0800185 // Also use the opportunity to send out turret messages.
186 UpdateTurretPosition();
187 auto builder = superstructure_status_sender_.MakeBuilder();
188 auto turret_builder =
189 builder
190 .MakeBuilder<frc971::control_loops::
191 PotAndAbsoluteEncoderProfiledJointStatus>();
192 turret_builder.add_position(turret_position_);
193 turret_builder.add_velocity(turret_velocity_);
194 auto turret_offset = turret_builder.Finish();
195 auto status_builder = builder.MakeBuilder<superstructure::Status>();
196 status_builder.add_turret(turret_offset);
197 builder.Send(status_builder.Finish());
198 },
199 chrono::milliseconds(5));
James Kuszmaul5398fae2020-02-17 16:44:03 -0800200
James Kuszmaul286b4282020-02-26 20:29:32 -0800201 test_event_loop_->OnRun([this]() { SetStartingPosition({3.0, 2.0, 0.0}); });
202
James Kuszmaul5398fae2020-02-17 16:44:03 -0800203 // Run for enough time to allow the gyro/imu zeroing code to run.
204 RunFor(std::chrono::seconds(10));
205 }
206
207 virtual ~LocalizedDrivetrainTest() override {}
208
209 void SetStartingPosition(const Eigen::Matrix<double, 3, 1> &xytheta) {
210 *drivetrain_plant_.mutable_state() << xytheta.x(), xytheta.y(),
211 xytheta(2, 0), 0.0, 0.0;
212 Eigen::Matrix<double, Localizer::HybridEkf::kNStates, 1> localizer_state;
213 localizer_state.setZero();
214 localizer_state.block<3, 1>(0, 0) = xytheta;
215 localizer_.Reset(monotonic_now(), localizer_state);
216 }
217
218 void VerifyNearGoal(double eps = 1e-3) {
219 drivetrain_goal_fetcher_.Fetch();
220 EXPECT_NEAR(drivetrain_goal_fetcher_->left_goal(),
221 drivetrain_plant_.GetLeftPosition(), eps);
222 EXPECT_NEAR(drivetrain_goal_fetcher_->right_goal(),
223 drivetrain_plant_.GetRightPosition(), eps);
224 }
225
James Kuszmaul688a2b02020-02-19 18:26:33 -0800226 ::testing::AssertionResult IsNear(double expected, double actual,
227 double epsilon) {
228 if (std::abs(expected - actual) < epsilon) {
229 return ::testing::AssertionSuccess();
230 } else {
231 return ::testing::AssertionFailure()
232 << "Expected " << expected << " but got " << actual
233 << " with a max difference of " << epsilon
234 << " and an actual difference of " << std::abs(expected - actual);
235 }
236 }
237 ::testing::AssertionResult VerifyEstimatorAccurate(double eps) {
James Kuszmaul5398fae2020-02-17 16:44:03 -0800238 const Eigen::Matrix<double, 5, 1> true_state = drivetrain_plant_.state();
James Kuszmaul688a2b02020-02-19 18:26:33 -0800239 ::testing::AssertionResult result(true);
240 if (!(result = IsNear(localizer_.x(), true_state(0), eps))) {
241 return result;
242 }
243 if (!(result = IsNear(localizer_.y(), true_state(1), eps))) {
244 return result;
245 }
246 if (!(result = IsNear(localizer_.theta(), true_state(2), eps))) {
247 return result;
248 }
249 if (!(result = IsNear(localizer_.left_velocity(), true_state(3), eps))) {
250 return result;
251 }
252 if (!(result = IsNear(localizer_.right_velocity(), true_state(4), eps))) {
253 return result;
254 }
255 return result;
James Kuszmaul5398fae2020-02-17 16:44:03 -0800256 }
257
258 // Goes through and captures frames on the camera(s), queueing them up to be
259 // sent by SendDelayedFrames().
260 void CaptureFrames() {
261 const frc971::control_loops::Pose robot_pose(
262 {drivetrain_plant_.GetPosition().x(),
263 drivetrain_plant_.GetPosition().y(), 0.0},
264 drivetrain_plant_.state()(2, 0));
265 std::unique_ptr<ImageMatchResultT> frame(new ImageMatchResultT());
266
James Kuszmaulc51dbfe2020-02-23 15:39:00 -0800267 for (const auto &H_field_target : TargetLocations()) {
James Kuszmaul5398fae2020-02-17 16:44:03 -0800268 std::unique_ptr<CameraPoseT> camera_target(new CameraPoseT());
269
270 camera_target->field_to_target.reset(new TransformationMatrixT());
James Kuszmaulc51dbfe2020-02-23 15:39:00 -0800271 camera_target->field_to_target->data = MatrixToVector(H_field_target);
James Kuszmaul688a2b02020-02-19 18:26:33 -0800272
James Kuszmaulc51dbfe2020-02-23 15:39:00 -0800273 Eigen::Matrix<double, 4, 4> H_turret_camera =
James Kuszmaul688a2b02020-02-19 18:26:33 -0800274 Eigen::Matrix<double, 4, 4>::Identity();
275 if (is_turreted_) {
James Kuszmaulc51dbfe2020-02-23 15:39:00 -0800276 H_turret_camera = frc971::control_loops::TransformationMatrixForYaw(
James Kuszmaul688a2b02020-02-19 18:26:33 -0800277 turret_position_) *
278 CameraTurretTransformation();
279 }
James Kuszmaul5398fae2020-02-17 16:44:03 -0800280
281 // TODO(james): Use non-zero turret angles.
282 camera_target->camera_to_target.reset(new TransformationMatrixT());
James Kuszmaulc51dbfe2020-02-23 15:39:00 -0800283 camera_target->camera_to_target->data =
284 MatrixToVector((robot_pose.AsTransformationMatrix() *
285 TurretRobotTransformation() * H_turret_camera)
286 .inverse() *
287 H_field_target);
James Kuszmaul5398fae2020-02-17 16:44:03 -0800288
289 frame->camera_poses.emplace_back(std::move(camera_target));
290 }
291
292 frame->image_monotonic_timestamp_ns =
293 chrono::duration_cast<chrono::nanoseconds>(
James Kuszmaul958b21e2020-02-26 21:51:40 -0800294 event_loop_factory()
295 ->GetNodeEventLoopFactory(pi1_)
296 ->monotonic_now()
297 .time_since_epoch())
James Kuszmaul5398fae2020-02-17 16:44:03 -0800298 .count();
299 frame->camera_calibration.reset(new CameraCalibrationT());
300 {
301 frame->camera_calibration->fixed_extrinsics.reset(
302 new TransformationMatrixT());
James Kuszmaulc51dbfe2020-02-23 15:39:00 -0800303 TransformationMatrixT *H_robot_turret =
James Kuszmaul5398fae2020-02-17 16:44:03 -0800304 frame->camera_calibration->fixed_extrinsics.get();
James Kuszmaulc51dbfe2020-02-23 15:39:00 -0800305 H_robot_turret->data = MatrixToVector(TurretRobotTransformation());
James Kuszmaul5398fae2020-02-17 16:44:03 -0800306 }
James Kuszmaul688a2b02020-02-19 18:26:33 -0800307
308 if (is_turreted_) {
James Kuszmaul5398fae2020-02-17 16:44:03 -0800309 frame->camera_calibration->turret_extrinsics.reset(
310 new TransformationMatrixT());
James Kuszmaulc51dbfe2020-02-23 15:39:00 -0800311 TransformationMatrixT *H_turret_camera =
James Kuszmaul5398fae2020-02-17 16:44:03 -0800312 frame->camera_calibration->turret_extrinsics.get();
James Kuszmaulc51dbfe2020-02-23 15:39:00 -0800313 H_turret_camera->data = MatrixToVector(CameraTurretTransformation());
James Kuszmaul5398fae2020-02-17 16:44:03 -0800314 }
315
316 camera_delay_queue_.emplace(monotonic_now(), std::move(frame));
317 }
318
319 // Actually sends out all the camera frames.
320 void SendDelayedFrames() {
321 const std::chrono::milliseconds camera_latency(150);
322 while (!camera_delay_queue_.empty() &&
323 std::get<0>(camera_delay_queue_.front()) <
324 monotonic_now() - camera_latency) {
325 auto builder = camera_sender_.MakeBuilder();
326 ASSERT_TRUE(builder.Send(ImageMatchResult::Pack(
327 *builder.fbb(), std::get<1>(camera_delay_queue_.front()).get())));
328 camera_delay_queue_.pop();
329 }
330 }
331
Austin Schuh6aa77be2020-02-22 21:06:40 -0800332 const aos::Node *const roborio_;
333 const aos::Node *const pi1_;
334
James Kuszmaul5398fae2020-02-17 16:44:03 -0800335 std::unique_ptr<aos::EventLoop> test_event_loop_;
336 aos::Sender<Goal> drivetrain_goal_sender_;
337 aos::Fetcher<Goal> drivetrain_goal_fetcher_;
338 aos::Sender<LocalizerControl> localizer_control_sender_;
James Kuszmaul688a2b02020-02-19 18:26:33 -0800339 aos::Sender<superstructure::Status> superstructure_status_sender_;
James Kuszmaul958b21e2020-02-26 21:51:40 -0800340 aos::Sender<aos::message_bridge::ServerStatistics> server_statistics_sender_;
James Kuszmaul5398fae2020-02-17 16:44:03 -0800341
342 std::unique_ptr<aos::EventLoop> drivetrain_event_loop_;
343 const frc971::control_loops::drivetrain::DrivetrainConfig<double>
344 dt_config_;
345
Austin Schuh6aa77be2020-02-22 21:06:40 -0800346 std::unique_ptr<aos::EventLoop> pi1_event_loop_;
James Kuszmaul5398fae2020-02-17 16:44:03 -0800347 aos::Sender<ImageMatchResult> camera_sender_;
348
349 Localizer localizer_;
350 DrivetrainLoop drivetrain_;
351
352 std::unique_ptr<aos::EventLoop> drivetrain_plant_event_loop_;
353 DrivetrainSimulation drivetrain_plant_;
354 monotonic_clock::time_point last_frame_;
355
356 // A queue of camera frames so that we can add a time delay to the data
357 // coming from the cameras.
358 std::queue<std::tuple<aos::monotonic_clock::time_point,
359 std::unique_ptr<ImageMatchResultT>>>
360 camera_delay_queue_;
361
362 void set_enable_cameras(bool enable) { enable_cameras_ = enable; }
James Kuszmaul688a2b02020-02-19 18:26:33 -0800363 void set_camera_is_turreted(bool turreted) { is_turreted_ = turreted; }
364
365 void set_turret(double position, double velocity) {
366 turret_position_ = position;
367 turret_velocity_ = velocity;
368 }
369
370 void SendGoal(double left, double right) {
371 auto builder = drivetrain_goal_sender_.MakeBuilder();
372
373 Goal::Builder drivetrain_builder = builder.MakeBuilder<Goal>();
374 drivetrain_builder.add_controller_type(
375 frc971::control_loops::drivetrain::ControllerType::MOTION_PROFILE);
376 drivetrain_builder.add_left_goal(left);
377 drivetrain_builder.add_right_goal(right);
378
379 EXPECT_TRUE(builder.Send(drivetrain_builder.Finish()));
380 }
James Kuszmaul5398fae2020-02-17 16:44:03 -0800381
382 private:
James Kuszmaul688a2b02020-02-19 18:26:33 -0800383 void UpdateTurretPosition() {
384 turret_position_ +=
385 turret_velocity_ *
386 aos::time::DurationInSeconds(monotonic_now() - last_turret_update_);
387 last_turret_update_ = monotonic_now();
388 }
389
James Kuszmaul5398fae2020-02-17 16:44:03 -0800390 bool enable_cameras_ = false;
James Kuszmaul688a2b02020-02-19 18:26:33 -0800391 // Whether to make the camera be on the turret or not.
392 bool is_turreted_ = true;
393
394 // The time at which we last incremented turret_position_.
395 monotonic_clock::time_point last_turret_update_ = monotonic_clock::min_time;
396 // Current turret position and velocity. These are set directly by the user in
397 // the test, and if velocity is non-zero, then we will automatically increment
398 // turret_position_ with every timestep.
399 double turret_position_ = 0.0; // rad
400 double turret_velocity_ = 0.0; // rad / sec
James Kuszmaul5398fae2020-02-17 16:44:03 -0800401
402 std::unique_ptr<aos::EventLoop> logger_event_loop_;
403 std::unique_ptr<aos::logger::DetachedBufferWriter> log_buffer_writer_;
404 std::unique_ptr<aos::logger::Logger> logger_;
405};
406
407// Tests that no camera updates, combined with a perfect model, results in no
408// error.
409TEST_F(LocalizedDrivetrainTest, NoCameraUpdate) {
410 SetEnabled(true);
411 set_enable_cameras(false);
James Kuszmaul688a2b02020-02-19 18:26:33 -0800412 EXPECT_TRUE(VerifyEstimatorAccurate(1e-7));
James Kuszmaul5398fae2020-02-17 16:44:03 -0800413
James Kuszmaul688a2b02020-02-19 18:26:33 -0800414 SendGoal(-1.0, 1.0);
James Kuszmaul5398fae2020-02-17 16:44:03 -0800415
James Kuszmaul5398fae2020-02-17 16:44:03 -0800416 RunFor(chrono::seconds(3));
417 VerifyNearGoal();
James Kuszmaul688a2b02020-02-19 18:26:33 -0800418 EXPECT_TRUE(VerifyEstimatorAccurate(5e-4));
James Kuszmaul5398fae2020-02-17 16:44:03 -0800419}
420
James Kuszmaul688a2b02020-02-19 18:26:33 -0800421// Tests that camera updates with a perfect models results in no errors.
James Kuszmaul5398fae2020-02-17 16:44:03 -0800422TEST_F(LocalizedDrivetrainTest, PerfectCameraUpdate) {
423 SetEnabled(true);
424 set_enable_cameras(true);
James Kuszmaul688a2b02020-02-19 18:26:33 -0800425 set_camera_is_turreted(true);
James Kuszmaul5398fae2020-02-17 16:44:03 -0800426
James Kuszmaul688a2b02020-02-19 18:26:33 -0800427 SendGoal(-1.0, 1.0);
James Kuszmaul5398fae2020-02-17 16:44:03 -0800428
James Kuszmaul5398fae2020-02-17 16:44:03 -0800429 RunFor(chrono::seconds(3));
430 VerifyNearGoal();
James Kuszmaul688a2b02020-02-19 18:26:33 -0800431 EXPECT_TRUE(VerifyEstimatorAccurate(5e-4));
James Kuszmaul5398fae2020-02-17 16:44:03 -0800432}
433
James Kuszmaul688a2b02020-02-19 18:26:33 -0800434// Tests that camera updates with a constant initial error in the position
James Kuszmaul5398fae2020-02-17 16:44:03 -0800435// results in convergence.
436TEST_F(LocalizedDrivetrainTest, InitialPositionError) {
437 SetEnabled(true);
438 set_enable_cameras(true);
James Kuszmaul688a2b02020-02-19 18:26:33 -0800439 set_camera_is_turreted(true);
James Kuszmaul5398fae2020-02-17 16:44:03 -0800440 drivetrain_plant_.mutable_state()->topRows(3) +=
441 Eigen::Vector3d(0.1, 0.1, 0.01);
James Kuszmaul5398fae2020-02-17 16:44:03 -0800442
James Kuszmaul688a2b02020-02-19 18:26:33 -0800443 // Confirm that some translational movement does get handled correctly.
444 SendGoal(-0.9, 1.0);
James Kuszmaul5398fae2020-02-17 16:44:03 -0800445
James Kuszmaul5398fae2020-02-17 16:44:03 -0800446 // Give the filters enough time to converge.
447 RunFor(chrono::seconds(10));
448 VerifyNearGoal(5e-3);
James Kuszmaul688a2b02020-02-19 18:26:33 -0800449 EXPECT_TRUE(VerifyEstimatorAccurate(1e-2));
450}
451
452// Tests that camera updates using a non-turreted camera work.
453TEST_F(LocalizedDrivetrainTest, InitialPositionErrorNoTurret) {
454 SetEnabled(true);
455 set_enable_cameras(true);
456 set_camera_is_turreted(false);
457 drivetrain_plant_.mutable_state()->topRows(3) +=
458 Eigen::Vector3d(0.1, 0.1, 0.01);
459
460 SendGoal(-1.0, 1.0);
461
462 // Give the filters enough time to converge.
463 RunFor(chrono::seconds(10));
464 VerifyNearGoal(5e-3);
465 EXPECT_TRUE(VerifyEstimatorAccurate(1e-2));
466}
467
468// Tests that we are able to handle a constant, non-zero turret angle.
469TEST_F(LocalizedDrivetrainTest, NonZeroTurret) {
470 SetEnabled(true);
471 set_enable_cameras(true);
472 set_camera_is_turreted(true);
473 set_turret(1.0, 0.0);
474 drivetrain_plant_.mutable_state()->topRows(3) +=
475 Eigen::Vector3d(0.1, 0.1, 0.0);
476
477 SendGoal(-1.0, 1.0);
478
479 // Give the filters enough time to converge.
480 RunFor(chrono::seconds(10));
481 VerifyNearGoal(5e-3);
482 EXPECT_TRUE(VerifyEstimatorAccurate(1e-3));
483}
484
485// Tests that we are able to handle a constant velocity turret.
486TEST_F(LocalizedDrivetrainTest, MovingTurret) {
487 SetEnabled(true);
488 set_enable_cameras(true);
489 set_camera_is_turreted(true);
490 set_turret(0.0, 0.2);
491 drivetrain_plant_.mutable_state()->topRows(3) +=
492 Eigen::Vector3d(0.1, 0.1, 0.0);
493
494 SendGoal(-1.0, 1.0);
495
496 // Give the filters enough time to converge.
497 RunFor(chrono::seconds(10));
498 VerifyNearGoal(5e-3);
499 EXPECT_TRUE(VerifyEstimatorAccurate(1e-3));
500}
501
502// Tests that we reject camera measurements when the turret is spinning too
503// fast.
504TEST_F(LocalizedDrivetrainTest, TooFastTurret) {
505 SetEnabled(true);
506 set_enable_cameras(true);
507 set_camera_is_turreted(true);
508 set_turret(0.0, -10.0);
509 const Eigen::Vector3d disturbance(0.1, 0.1, 0.0);
510 drivetrain_plant_.mutable_state()->topRows(3) += disturbance;
511
512 SendGoal(-1.0, 1.0);
513
514 RunFor(chrono::seconds(10));
515 EXPECT_FALSE(VerifyEstimatorAccurate(1e-3));
516 // If we remove the disturbance, we should now be correct.
517 drivetrain_plant_.mutable_state()->topRows(3) -= disturbance;
518 VerifyNearGoal(5e-3);
519 EXPECT_TRUE(VerifyEstimatorAccurate(1e-3));
520}
521
522// Tests that we don't reject camera measurements when the turret is spinning
523// too fast but we aren't using a camera attached to the turret.
524TEST_F(LocalizedDrivetrainTest, TooFastTurretDoesntAffectFixedCamera) {
525 SetEnabled(true);
526 set_enable_cameras(true);
527 set_camera_is_turreted(false);
528 set_turret(0.0, -10.0);
529 const Eigen::Vector3d disturbance(0.1, 0.1, 0.0);
530 drivetrain_plant_.mutable_state()->topRows(3) += disturbance;
531
532 SendGoal(-1.0, 1.0);
533
534 RunFor(chrono::seconds(10));
535 VerifyNearGoal(5e-3);
536 EXPECT_TRUE(VerifyEstimatorAccurate(1e-3));
James Kuszmaul5398fae2020-02-17 16:44:03 -0800537}
538
539} // namespace testing
540} // namespace drivetrain
541} // namespace control_loops
542} // namespace y2020