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" |
James Kuszmaul | 958b21e | 2020-02-26 21:51:40 -0800 | [diff] [blame] | 7 | #include "aos/network/message_bridge_server_generated.h" |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 8 | #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 Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 14 | #include "y2020/control_loops/superstructure/superstructure_status_generated.h" |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 15 | |
| 16 | DEFINE_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 | |
| 21 | namespace y2020 { |
| 22 | namespace control_loops { |
| 23 | namespace drivetrain { |
| 24 | namespace testing { |
| 25 | |
| 26 | using frc971::control_loops::drivetrain::DrivetrainConfig; |
| 27 | using frc971::control_loops::drivetrain::Goal; |
| 28 | using frc971::control_loops::drivetrain::LocalizerControl; |
| 29 | using frc971::vision::sift::ImageMatchResult; |
| 30 | using frc971::vision::sift::ImageMatchResultT; |
| 31 | using frc971::vision::sift::CameraPoseT; |
| 32 | using frc971::vision::sift::CameraCalibrationT; |
| 33 | using frc971::vision::sift::TransformationMatrixT; |
| 34 | |
| 35 | namespace { |
| 36 | DrivetrainConfig<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. |
| 42 | std::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. |
| 55 | Eigen::Matrix<double, 4, 4> TurretRobotTransformation() { |
| 56 | Eigen::Matrix<double, 4, 4> H; |
| 57 | H.setIdentity(); |
James Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 58 | H.block<3, 1>(0, 3) << 1, 1.1, 0.9; |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 59 | 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. |
| 64 | Eigen::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 Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 70 | Eigen::AngleAxis<double>(0.1, Eigen::Vector3d::UnitY()) * |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 71 | 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 Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 78 | std::vector<Eigen::Matrix<double, 4, 4>> TargetLocations() { |
| 79 | std::vector<Eigen::Matrix<double, 4, 4>> locations; |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 80 | Eigen::Matrix<double, 4, 4> H; |
| 81 | H.setIdentity(); |
| 82 | H.block<3, 1>(0, 3) << 10.0, 0, 0; |
James Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 83 | locations.push_back(H); |
| 84 | H.block<3, 1>(0, 3) << -10.0, 0, 0; |
| 85 | locations.push_back(H); |
| 86 | return locations; |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 87 | } |
James Kuszmaul | 958b21e | 2020-02-26 21:51:40 -0800 | [diff] [blame] | 88 | |
Austin Schuh | be69cf3 | 2020-08-27 11:38:33 -0700 | [diff] [blame^] | 89 | constexpr std::chrono::seconds kPiTimeOffset(-10); |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 90 | } // namespace |
| 91 | |
| 92 | namespace chrono = std::chrono; |
| 93 | using frc971::control_loops::drivetrain::testing::DrivetrainSimulation; |
| 94 | using frc971::control_loops::drivetrain::DrivetrainLoop; |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 95 | using aos::monotonic_clock; |
| 96 | |
James Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 97 | class LocalizedDrivetrainTest : public aos::testing::ControlLoopTest { |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 98 | 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 Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 106 | roborio_(aos::configuration::GetNode(configuration(), "roborio")), |
| 107 | pi1_(aos::configuration::GetNode(configuration(), "pi1")), |
| 108 | test_event_loop_(MakeEventLoop("test", roborio_)), |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 109 | 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 Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 115 | superstructure_status_sender_( |
| 116 | test_event_loop_->MakeSender<superstructure::Status>( |
| 117 | "/superstructure")), |
James Kuszmaul | 958b21e | 2020-02-26 21:51:40 -0800 | [diff] [blame] | 118 | server_statistics_sender_( |
| 119 | test_event_loop_->MakeSender<aos::message_bridge::ServerStatistics>( |
| 120 | "/aos")), |
Austin Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 121 | drivetrain_event_loop_(MakeEventLoop("drivetrain", roborio_)), |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 122 | dt_config_(GetTest2020DrivetrainConfig()), |
Austin Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 123 | pi1_event_loop_(MakeEventLoop("test", pi1_)), |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 124 | camera_sender_( |
Austin Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 125 | pi1_event_loop_->MakeSender<ImageMatchResult>("/pi1/camera")), |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 126 | localizer_(drivetrain_event_loop_.get(), dt_config_), |
| 127 | drivetrain_(dt_config_, drivetrain_event_loop_.get(), &localizer_), |
Austin Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 128 | drivetrain_plant_event_loop_(MakeEventLoop("plant", roborio_)), |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 129 | drivetrain_plant_(drivetrain_plant_event_loop_.get(), dt_config_), |
| 130 | last_frame_(monotonic_now()) { |
James Kuszmaul | 958b21e | 2020-02-26 21:51:40 -0800 | [diff] [blame] | 131 | event_loop_factory()->GetNodeEventLoopFactory(pi1_)->SetDistributedOffset( |
Austin Schuh | be69cf3 | 2020-08-27 11:38:33 -0700 | [diff] [blame^] | 132 | kPiTimeOffset, 1.0); |
James Kuszmaul | 958b21e | 2020-02-26 21:51:40 -0800 | [diff] [blame] | 133 | |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 134 | set_team_id(frc971::control_loops::testing::kTeamNumber); |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 135 | set_battery_voltage(12.0); |
| 136 | |
| 137 | if (!FLAGS_output_file.empty()) { |
| 138 | log_buffer_writer_ = std::make_unique<aos::logger::DetachedBufferWriter>( |
| 139 | FLAGS_output_file); |
Austin Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 140 | logger_event_loop_ = MakeEventLoop("logger", roborio_); |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 141 | logger_ = std::make_unique<aos::logger::Logger>(log_buffer_writer_.get(), |
| 142 | logger_event_loop_.get()); |
| 143 | } |
| 144 | |
| 145 | test_event_loop_->MakeWatcher( |
| 146 | "/drivetrain", |
| 147 | [this](const frc971::control_loops::drivetrain::Status &) { |
| 148 | // Needs to do camera updates right after we run the control loop. |
| 149 | if (enable_cameras_) { |
| 150 | SendDelayedFrames(); |
| 151 | if (last_frame_ + std::chrono::milliseconds(100) < |
| 152 | monotonic_now()) { |
| 153 | CaptureFrames(); |
| 154 | last_frame_ = monotonic_now(); |
| 155 | } |
| 156 | } |
James Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 157 | }); |
| 158 | |
| 159 | test_event_loop_->AddPhasedLoop( |
| 160 | [this](int) { |
James Kuszmaul | 958b21e | 2020-02-26 21:51:40 -0800 | [diff] [blame] | 161 | auto builder = server_statistics_sender_.MakeBuilder(); |
| 162 | auto name_offset = builder.fbb()->CreateString("pi1"); |
| 163 | auto node_builder = builder.MakeBuilder<aos::Node>(); |
| 164 | node_builder.add_name(name_offset); |
| 165 | auto node_offset = node_builder.Finish(); |
| 166 | auto connection_builder = |
| 167 | builder.MakeBuilder<aos::message_bridge::ServerConnection>(); |
| 168 | connection_builder.add_node(node_offset); |
| 169 | connection_builder.add_monotonic_offset( |
Austin Schuh | be69cf3 | 2020-08-27 11:38:33 -0700 | [diff] [blame^] | 170 | chrono::duration_cast<chrono::nanoseconds>(kPiTimeOffset) |
James Kuszmaul | 958b21e | 2020-02-26 21:51:40 -0800 | [diff] [blame] | 171 | .count()); |
| 172 | auto connection_offset = connection_builder.Finish(); |
| 173 | auto connections_offset = |
| 174 | builder.fbb()->CreateVector(&connection_offset, 1); |
| 175 | auto statistics_builder = |
| 176 | builder.MakeBuilder<aos::message_bridge::ServerStatistics>(); |
| 177 | statistics_builder.add_connections(connections_offset); |
| 178 | builder.Send(statistics_builder.Finish()); |
| 179 | }, |
| 180 | chrono::milliseconds(500)); |
| 181 | |
| 182 | test_event_loop_->AddPhasedLoop( |
| 183 | [this](int) { |
James Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 184 | // Also use the opportunity to send out turret messages. |
| 185 | UpdateTurretPosition(); |
| 186 | auto builder = superstructure_status_sender_.MakeBuilder(); |
| 187 | auto turret_builder = |
| 188 | builder |
| 189 | .MakeBuilder<frc971::control_loops:: |
| 190 | PotAndAbsoluteEncoderProfiledJointStatus>(); |
| 191 | turret_builder.add_position(turret_position_); |
| 192 | turret_builder.add_velocity(turret_velocity_); |
| 193 | auto turret_offset = turret_builder.Finish(); |
| 194 | auto status_builder = builder.MakeBuilder<superstructure::Status>(); |
| 195 | status_builder.add_turret(turret_offset); |
| 196 | builder.Send(status_builder.Finish()); |
| 197 | }, |
| 198 | chrono::milliseconds(5)); |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 199 | |
James Kuszmaul | 286b428 | 2020-02-26 20:29:32 -0800 | [diff] [blame] | 200 | test_event_loop_->OnRun([this]() { SetStartingPosition({3.0, 2.0, 0.0}); }); |
| 201 | |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 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; |
James Kuszmaul | d478f87 | 2020-03-16 20:54:27 -0700 | [diff] [blame] | 211 | Eigen::Matrix<float, Localizer::HybridEkf::kNStates, 1> localizer_state; |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 212 | localizer_state.setZero(); |
James Kuszmaul | d478f87 | 2020-03-16 20:54:27 -0700 | [diff] [blame] | 213 | localizer_state.block<3, 1>(0, 0) = xytheta.cast<float>(); |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 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 Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 225 | ::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 Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 237 | const Eigen::Matrix<double, 5, 1> true_state = drivetrain_plant_.state(); |
James Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 238 | ::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 Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 255 | } |
| 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 Kuszmaul | c51dbfe | 2020-02-23 15:39:00 -0800 | [diff] [blame] | 266 | for (const auto &H_field_target : TargetLocations()) { |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 267 | std::unique_ptr<CameraPoseT> camera_target(new CameraPoseT()); |
| 268 | |
| 269 | camera_target->field_to_target.reset(new TransformationMatrixT()); |
James Kuszmaul | c51dbfe | 2020-02-23 15:39:00 -0800 | [diff] [blame] | 270 | camera_target->field_to_target->data = MatrixToVector(H_field_target); |
James Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 271 | |
James Kuszmaul | c51dbfe | 2020-02-23 15:39:00 -0800 | [diff] [blame] | 272 | Eigen::Matrix<double, 4, 4> H_turret_camera = |
James Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 273 | Eigen::Matrix<double, 4, 4>::Identity(); |
| 274 | if (is_turreted_) { |
James Kuszmaul | c51dbfe | 2020-02-23 15:39:00 -0800 | [diff] [blame] | 275 | H_turret_camera = frc971::control_loops::TransformationMatrixForYaw( |
James Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 276 | turret_position_) * |
| 277 | CameraTurretTransformation(); |
| 278 | } |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 279 | |
| 280 | // TODO(james): Use non-zero turret angles. |
| 281 | camera_target->camera_to_target.reset(new TransformationMatrixT()); |
James Kuszmaul | c51dbfe | 2020-02-23 15:39:00 -0800 | [diff] [blame] | 282 | camera_target->camera_to_target->data = |
| 283 | MatrixToVector((robot_pose.AsTransformationMatrix() * |
| 284 | TurretRobotTransformation() * H_turret_camera) |
| 285 | .inverse() * |
| 286 | H_field_target); |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 287 | |
| 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 Kuszmaul | 958b21e | 2020-02-26 21:51:40 -0800 | [diff] [blame] | 293 | event_loop_factory() |
| 294 | ->GetNodeEventLoopFactory(pi1_) |
| 295 | ->monotonic_now() |
| 296 | .time_since_epoch()) |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 297 | .count(); |
| 298 | frame->camera_calibration.reset(new CameraCalibrationT()); |
| 299 | { |
| 300 | frame->camera_calibration->fixed_extrinsics.reset( |
| 301 | new TransformationMatrixT()); |
James Kuszmaul | c51dbfe | 2020-02-23 15:39:00 -0800 | [diff] [blame] | 302 | TransformationMatrixT *H_robot_turret = |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 303 | frame->camera_calibration->fixed_extrinsics.get(); |
James Kuszmaul | c51dbfe | 2020-02-23 15:39:00 -0800 | [diff] [blame] | 304 | H_robot_turret->data = MatrixToVector(TurretRobotTransformation()); |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 305 | } |
James Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 306 | |
| 307 | if (is_turreted_) { |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 308 | frame->camera_calibration->turret_extrinsics.reset( |
| 309 | new TransformationMatrixT()); |
James Kuszmaul | c51dbfe | 2020-02-23 15:39:00 -0800 | [diff] [blame] | 310 | TransformationMatrixT *H_turret_camera = |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 311 | frame->camera_calibration->turret_extrinsics.get(); |
James Kuszmaul | c51dbfe | 2020-02-23 15:39:00 -0800 | [diff] [blame] | 312 | H_turret_camera->data = MatrixToVector(CameraTurretTransformation()); |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 313 | } |
| 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 Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 331 | const aos::Node *const roborio_; |
| 332 | const aos::Node *const pi1_; |
| 333 | |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 334 | 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 Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 338 | aos::Sender<superstructure::Status> superstructure_status_sender_; |
James Kuszmaul | 958b21e | 2020-02-26 21:51:40 -0800 | [diff] [blame] | 339 | aos::Sender<aos::message_bridge::ServerStatistics> server_statistics_sender_; |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 340 | |
| 341 | std::unique_ptr<aos::EventLoop> drivetrain_event_loop_; |
| 342 | const frc971::control_loops::drivetrain::DrivetrainConfig<double> |
| 343 | dt_config_; |
| 344 | |
Austin Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 345 | std::unique_ptr<aos::EventLoop> pi1_event_loop_; |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 346 | 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 Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 362 | 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 Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 380 | |
| 381 | private: |
James Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 382 | 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 Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 389 | bool enable_cameras_ = false; |
James Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 390 | // 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 Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 400 | |
| 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. |
| 408 | TEST_F(LocalizedDrivetrainTest, NoCameraUpdate) { |
| 409 | SetEnabled(true); |
| 410 | set_enable_cameras(false); |
James Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 411 | EXPECT_TRUE(VerifyEstimatorAccurate(1e-7)); |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 412 | |
James Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 413 | SendGoal(-1.0, 1.0); |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 414 | |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 415 | RunFor(chrono::seconds(3)); |
| 416 | VerifyNearGoal(); |
James Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 417 | EXPECT_TRUE(VerifyEstimatorAccurate(5e-4)); |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 418 | } |
| 419 | |
James Kuszmaul | 7f55f07 | 2020-03-01 10:21:26 -0800 | [diff] [blame] | 420 | // Tests that we can drive in a straight line and have things estimate |
| 421 | // correctly. |
| 422 | TEST_F(LocalizedDrivetrainTest, NoCameraUpdateStraightLine) { |
| 423 | SetEnabled(true); |
| 424 | set_enable_cameras(false); |
| 425 | EXPECT_TRUE(VerifyEstimatorAccurate(1e-7)); |
| 426 | |
| 427 | SendGoal(1.0, 1.0); |
| 428 | |
| 429 | RunFor(chrono::seconds(1)); |
| 430 | VerifyNearGoal(); |
| 431 | // Due to accelerometer drift, the straight-line driving tends to be less |
| 432 | // accurate... |
| 433 | EXPECT_TRUE(VerifyEstimatorAccurate(0.1)); |
| 434 | } |
| 435 | |
James Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 436 | // Tests that camera updates with a perfect models results in no errors. |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 437 | TEST_F(LocalizedDrivetrainTest, PerfectCameraUpdate) { |
| 438 | SetEnabled(true); |
| 439 | set_enable_cameras(true); |
James Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 440 | set_camera_is_turreted(true); |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 441 | |
James Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 442 | SendGoal(-1.0, 1.0); |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 443 | |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 444 | RunFor(chrono::seconds(3)); |
| 445 | VerifyNearGoal(); |
James Kuszmaul | 66efe83 | 2020-03-16 19:38:33 -0700 | [diff] [blame] | 446 | EXPECT_TRUE(VerifyEstimatorAccurate(2e-3)); |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 447 | } |
| 448 | |
James Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 449 | // Tests that camera updates with a constant initial error in the position |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 450 | // results in convergence. |
| 451 | TEST_F(LocalizedDrivetrainTest, InitialPositionError) { |
| 452 | SetEnabled(true); |
| 453 | set_enable_cameras(true); |
James Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 454 | set_camera_is_turreted(true); |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 455 | drivetrain_plant_.mutable_state()->topRows(3) += |
| 456 | Eigen::Vector3d(0.1, 0.1, 0.01); |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 457 | |
James Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 458 | // Confirm that some translational movement does get handled correctly. |
| 459 | SendGoal(-0.9, 1.0); |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 460 | |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 461 | // Give the filters enough time to converge. |
| 462 | RunFor(chrono::seconds(10)); |
| 463 | VerifyNearGoal(5e-3); |
James Kuszmaul | 58cb1fe | 2020-03-07 16:18:59 -0800 | [diff] [blame] | 464 | EXPECT_TRUE(VerifyEstimatorAccurate(4e-2)); |
James Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | // Tests that camera updates using a non-turreted camera work. |
| 468 | TEST_F(LocalizedDrivetrainTest, InitialPositionErrorNoTurret) { |
| 469 | SetEnabled(true); |
| 470 | set_enable_cameras(true); |
| 471 | set_camera_is_turreted(false); |
| 472 | drivetrain_plant_.mutable_state()->topRows(3) += |
| 473 | Eigen::Vector3d(0.1, 0.1, 0.01); |
| 474 | |
| 475 | SendGoal(-1.0, 1.0); |
| 476 | |
| 477 | // Give the filters enough time to converge. |
| 478 | RunFor(chrono::seconds(10)); |
| 479 | VerifyNearGoal(5e-3); |
James Kuszmaul | 58cb1fe | 2020-03-07 16:18:59 -0800 | [diff] [blame] | 480 | EXPECT_TRUE(VerifyEstimatorAccurate(4e-2)); |
James Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | // Tests that we are able to handle a constant, non-zero turret angle. |
| 484 | TEST_F(LocalizedDrivetrainTest, NonZeroTurret) { |
| 485 | SetEnabled(true); |
| 486 | set_enable_cameras(true); |
| 487 | set_camera_is_turreted(true); |
| 488 | set_turret(1.0, 0.0); |
| 489 | drivetrain_plant_.mutable_state()->topRows(3) += |
| 490 | Eigen::Vector3d(0.1, 0.1, 0.0); |
| 491 | |
| 492 | SendGoal(-1.0, 1.0); |
| 493 | |
| 494 | // Give the filters enough time to converge. |
| 495 | RunFor(chrono::seconds(10)); |
| 496 | VerifyNearGoal(5e-3); |
James Kuszmaul | 58cb1fe | 2020-03-07 16:18:59 -0800 | [diff] [blame] | 497 | EXPECT_TRUE(VerifyEstimatorAccurate(1e-2)); |
James Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | // Tests that we are able to handle a constant velocity turret. |
| 501 | TEST_F(LocalizedDrivetrainTest, MovingTurret) { |
| 502 | SetEnabled(true); |
| 503 | set_enable_cameras(true); |
| 504 | set_camera_is_turreted(true); |
| 505 | set_turret(0.0, 0.2); |
| 506 | drivetrain_plant_.mutable_state()->topRows(3) += |
| 507 | Eigen::Vector3d(0.1, 0.1, 0.0); |
| 508 | |
| 509 | SendGoal(-1.0, 1.0); |
| 510 | |
| 511 | // Give the filters enough time to converge. |
| 512 | RunFor(chrono::seconds(10)); |
| 513 | VerifyNearGoal(5e-3); |
James Kuszmaul | 58cb1fe | 2020-03-07 16:18:59 -0800 | [diff] [blame] | 514 | EXPECT_TRUE(VerifyEstimatorAccurate(1e-2)); |
James Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | // Tests that we reject camera measurements when the turret is spinning too |
| 518 | // fast. |
| 519 | TEST_F(LocalizedDrivetrainTest, TooFastTurret) { |
| 520 | SetEnabled(true); |
| 521 | set_enable_cameras(true); |
| 522 | set_camera_is_turreted(true); |
| 523 | set_turret(0.0, -10.0); |
| 524 | const Eigen::Vector3d disturbance(0.1, 0.1, 0.0); |
| 525 | drivetrain_plant_.mutable_state()->topRows(3) += disturbance; |
| 526 | |
| 527 | SendGoal(-1.0, 1.0); |
| 528 | |
| 529 | RunFor(chrono::seconds(10)); |
| 530 | EXPECT_FALSE(VerifyEstimatorAccurate(1e-3)); |
| 531 | // If we remove the disturbance, we should now be correct. |
| 532 | drivetrain_plant_.mutable_state()->topRows(3) -= disturbance; |
| 533 | VerifyNearGoal(5e-3); |
| 534 | EXPECT_TRUE(VerifyEstimatorAccurate(1e-3)); |
| 535 | } |
| 536 | |
| 537 | // Tests that we don't reject camera measurements when the turret is spinning |
| 538 | // too fast but we aren't using a camera attached to the turret. |
| 539 | TEST_F(LocalizedDrivetrainTest, TooFastTurretDoesntAffectFixedCamera) { |
| 540 | SetEnabled(true); |
| 541 | set_enable_cameras(true); |
| 542 | set_camera_is_turreted(false); |
| 543 | set_turret(0.0, -10.0); |
| 544 | const Eigen::Vector3d disturbance(0.1, 0.1, 0.0); |
| 545 | drivetrain_plant_.mutable_state()->topRows(3) += disturbance; |
| 546 | |
| 547 | SendGoal(-1.0, 1.0); |
| 548 | |
| 549 | RunFor(chrono::seconds(10)); |
| 550 | VerifyNearGoal(5e-3); |
James Kuszmaul | 58cb1fe | 2020-03-07 16:18:59 -0800 | [diff] [blame] | 551 | EXPECT_TRUE(VerifyEstimatorAccurate(1e-2)); |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | } // namespace testing |
| 555 | } // namespace drivetrain |
| 556 | } // namespace control_loops |
| 557 | } // namespace y2020 |