James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 1 | #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 Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 13 | #include "y2020/control_loops/superstructure/superstructure_status_generated.h" |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 14 | |
| 15 | DEFINE_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 | |
| 20 | namespace y2020 { |
| 21 | namespace control_loops { |
| 22 | namespace drivetrain { |
| 23 | namespace testing { |
| 24 | |
| 25 | using frc971::control_loops::drivetrain::DrivetrainConfig; |
| 26 | using frc971::control_loops::drivetrain::Goal; |
| 27 | using frc971::control_loops::drivetrain::LocalizerControl; |
| 28 | using frc971::vision::sift::ImageMatchResult; |
| 29 | using frc971::vision::sift::ImageMatchResultT; |
| 30 | using frc971::vision::sift::CameraPoseT; |
| 31 | using frc971::vision::sift::CameraCalibrationT; |
| 32 | using frc971::vision::sift::TransformationMatrixT; |
| 33 | |
| 34 | namespace { |
| 35 | DrivetrainConfig<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. |
| 41 | std::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. |
| 54 | Eigen::Matrix<double, 4, 4> TurretRobotTransformation() { |
| 55 | Eigen::Matrix<double, 4, 4> H; |
| 56 | H.setIdentity(); |
James Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 57 | H.block<3, 1>(0, 3) << 1, 1.1, 0.9; |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 58 | 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. |
| 63 | Eigen::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 Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 69 | Eigen::AngleAxis<double>(0.1, Eigen::Vector3d::UnitY()) * |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 70 | 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 Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 77 | std::vector<Eigen::Matrix<double, 4, 4>> TargetLocations() { |
| 78 | std::vector<Eigen::Matrix<double, 4, 4>> locations; |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 79 | Eigen::Matrix<double, 4, 4> H; |
| 80 | H.setIdentity(); |
| 81 | H.block<3, 1>(0, 3) << 10.0, 0, 0; |
James Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 82 | locations.push_back(H); |
| 83 | H.block<3, 1>(0, 3) << -10.0, 0, 0; |
| 84 | locations.push_back(H); |
| 85 | return locations; |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 86 | } |
| 87 | } // namespace |
| 88 | |
| 89 | namespace chrono = std::chrono; |
| 90 | using frc971::control_loops::drivetrain::testing::DrivetrainSimulation; |
| 91 | using frc971::control_loops::drivetrain::DrivetrainLoop; |
| 92 | using frc971::control_loops::drivetrain::testing::GetTestDrivetrainConfig; |
| 93 | using aos::monotonic_clock; |
| 94 | |
James Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 95 | class LocalizedDrivetrainTest : public aos::testing::ControlLoopTest { |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 96 | 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 Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 104 | roborio_(aos::configuration::GetNode(configuration(), "roborio")), |
| 105 | pi1_(aos::configuration::GetNode(configuration(), "pi1")), |
| 106 | test_event_loop_(MakeEventLoop("test", roborio_)), |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 107 | 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 Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 113 | superstructure_status_sender_( |
| 114 | test_event_loop_->MakeSender<superstructure::Status>( |
| 115 | "/superstructure")), |
Austin Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 116 | drivetrain_event_loop_(MakeEventLoop("drivetrain", roborio_)), |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 117 | dt_config_(GetTest2020DrivetrainConfig()), |
Austin Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 118 | pi1_event_loop_(MakeEventLoop("test", pi1_)), |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 119 | camera_sender_( |
Austin Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 120 | pi1_event_loop_->MakeSender<ImageMatchResult>("/pi1/camera")), |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 121 | localizer_(drivetrain_event_loop_.get(), dt_config_), |
| 122 | drivetrain_(dt_config_, drivetrain_event_loop_.get(), &localizer_), |
Austin Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 123 | drivetrain_plant_event_loop_(MakeEventLoop("plant", roborio_)), |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 124 | drivetrain_plant_(drivetrain_plant_event_loop_.get(), dt_config_), |
| 125 | last_frame_(monotonic_now()) { |
| 126 | set_team_id(frc971::control_loops::testing::kTeamNumber); |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 127 | 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 Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 132 | logger_event_loop_ = MakeEventLoop("logger", roborio_); |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 133 | 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 Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 149 | }); |
| 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 Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 168 | |
James Kuszmaul | 286b428 | 2020-02-26 20:29:32 -0800 | [diff] [blame^] | 169 | test_event_loop_->OnRun([this]() { SetStartingPosition({3.0, 2.0, 0.0}); }); |
| 170 | |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 171 | // 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 Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 194 | ::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 Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 206 | const Eigen::Matrix<double, 5, 1> true_state = drivetrain_plant_.state(); |
James Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 207 | ::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 Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 224 | } |
| 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 Kuszmaul | c51dbfe | 2020-02-23 15:39:00 -0800 | [diff] [blame] | 235 | for (const auto &H_field_target : TargetLocations()) { |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 236 | std::unique_ptr<CameraPoseT> camera_target(new CameraPoseT()); |
| 237 | |
| 238 | camera_target->field_to_target.reset(new TransformationMatrixT()); |
James Kuszmaul | c51dbfe | 2020-02-23 15:39:00 -0800 | [diff] [blame] | 239 | camera_target->field_to_target->data = MatrixToVector(H_field_target); |
James Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 240 | |
James Kuszmaul | c51dbfe | 2020-02-23 15:39:00 -0800 | [diff] [blame] | 241 | Eigen::Matrix<double, 4, 4> H_turret_camera = |
James Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 242 | Eigen::Matrix<double, 4, 4>::Identity(); |
| 243 | if (is_turreted_) { |
James Kuszmaul | c51dbfe | 2020-02-23 15:39:00 -0800 | [diff] [blame] | 244 | H_turret_camera = frc971::control_loops::TransformationMatrixForYaw( |
James Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 245 | turret_position_) * |
| 246 | CameraTurretTransformation(); |
| 247 | } |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 248 | |
| 249 | // TODO(james): Use non-zero turret angles. |
| 250 | camera_target->camera_to_target.reset(new TransformationMatrixT()); |
James Kuszmaul | c51dbfe | 2020-02-23 15:39:00 -0800 | [diff] [blame] | 251 | camera_target->camera_to_target->data = |
| 252 | MatrixToVector((robot_pose.AsTransformationMatrix() * |
| 253 | TurretRobotTransformation() * H_turret_camera) |
| 254 | .inverse() * |
| 255 | H_field_target); |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 256 | |
| 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 Kuszmaul | c51dbfe | 2020-02-23 15:39:00 -0800 | [diff] [blame] | 268 | TransformationMatrixT *H_robot_turret = |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 269 | frame->camera_calibration->fixed_extrinsics.get(); |
James Kuszmaul | c51dbfe | 2020-02-23 15:39:00 -0800 | [diff] [blame] | 270 | H_robot_turret->data = MatrixToVector(TurretRobotTransformation()); |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 271 | } |
James Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 272 | |
| 273 | if (is_turreted_) { |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 274 | frame->camera_calibration->turret_extrinsics.reset( |
| 275 | new TransformationMatrixT()); |
James Kuszmaul | c51dbfe | 2020-02-23 15:39:00 -0800 | [diff] [blame] | 276 | TransformationMatrixT *H_turret_camera = |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 277 | frame->camera_calibration->turret_extrinsics.get(); |
James Kuszmaul | c51dbfe | 2020-02-23 15:39:00 -0800 | [diff] [blame] | 278 | H_turret_camera->data = MatrixToVector(CameraTurretTransformation()); |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 279 | } |
| 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 Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 297 | const aos::Node *const roborio_; |
| 298 | const aos::Node *const pi1_; |
| 299 | |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 300 | 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 Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 304 | aos::Sender<superstructure::Status> superstructure_status_sender_; |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 305 | |
| 306 | std::unique_ptr<aos::EventLoop> drivetrain_event_loop_; |
| 307 | const frc971::control_loops::drivetrain::DrivetrainConfig<double> |
| 308 | dt_config_; |
| 309 | |
Austin Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 310 | std::unique_ptr<aos::EventLoop> pi1_event_loop_; |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 311 | 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 Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 327 | 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 Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 345 | |
| 346 | private: |
James Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 347 | 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 Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 354 | bool enable_cameras_ = false; |
James Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 355 | // 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 Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 365 | |
| 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. |
| 373 | TEST_F(LocalizedDrivetrainTest, NoCameraUpdate) { |
| 374 | SetEnabled(true); |
| 375 | set_enable_cameras(false); |
James Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 376 | EXPECT_TRUE(VerifyEstimatorAccurate(1e-7)); |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 377 | |
James Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 378 | SendGoal(-1.0, 1.0); |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 379 | |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 380 | RunFor(chrono::seconds(3)); |
| 381 | VerifyNearGoal(); |
James Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 382 | EXPECT_TRUE(VerifyEstimatorAccurate(5e-4)); |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 383 | } |
| 384 | |
James Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 385 | // Tests that camera updates with a perfect models results in no errors. |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 386 | TEST_F(LocalizedDrivetrainTest, PerfectCameraUpdate) { |
| 387 | SetEnabled(true); |
| 388 | set_enable_cameras(true); |
James Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 389 | set_camera_is_turreted(true); |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 390 | |
James Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 391 | SendGoal(-1.0, 1.0); |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 392 | |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 393 | RunFor(chrono::seconds(3)); |
| 394 | VerifyNearGoal(); |
James Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 395 | EXPECT_TRUE(VerifyEstimatorAccurate(5e-4)); |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 396 | } |
| 397 | |
James Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 398 | // Tests that camera updates with a constant initial error in the position |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 399 | // results in convergence. |
| 400 | TEST_F(LocalizedDrivetrainTest, InitialPositionError) { |
| 401 | SetEnabled(true); |
| 402 | set_enable_cameras(true); |
James Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 403 | set_camera_is_turreted(true); |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 404 | drivetrain_plant_.mutable_state()->topRows(3) += |
| 405 | Eigen::Vector3d(0.1, 0.1, 0.01); |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 406 | |
James Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 407 | // Confirm that some translational movement does get handled correctly. |
| 408 | SendGoal(-0.9, 1.0); |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 409 | |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 410 | // Give the filters enough time to converge. |
| 411 | RunFor(chrono::seconds(10)); |
| 412 | VerifyNearGoal(5e-3); |
James Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 413 | EXPECT_TRUE(VerifyEstimatorAccurate(1e-2)); |
| 414 | } |
| 415 | |
| 416 | // Tests that camera updates using a non-turreted camera work. |
| 417 | TEST_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. |
| 433 | TEST_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. |
| 450 | TEST_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. |
| 468 | TEST_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. |
| 488 | TEST_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 Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | } // namespace testing |
| 504 | } // namespace drivetrain |
| 505 | } // namespace control_loops |
| 506 | } // namespace y2020 |