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; |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 29 | using frc971::vision::sift::CameraCalibrationT; |
| 30 | using frc971::vision::sift::CameraPoseT; |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 31 | using frc971::vision::sift::ImageMatchResult; |
| 32 | using frc971::vision::sift::ImageMatchResultT; |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 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; |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 93 | using aos::monotonic_clock; |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 94 | using frc971::control_loops::drivetrain::DrivetrainLoop; |
| 95 | using frc971::control_loops::drivetrain::testing::DrivetrainSimulation; |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 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()) { |
Austin Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 138 | logger_event_loop_ = MakeEventLoop("logger", roborio_); |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 139 | logger_ = std::make_unique<aos::logger::Logger>(logger_event_loop_.get()); |
| 140 | logger_->StartLoggingLocalNamerOnRun(FLAGS_output_file); |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | test_event_loop_->MakeWatcher( |
| 144 | "/drivetrain", |
| 145 | [this](const frc971::control_loops::drivetrain::Status &) { |
| 146 | // Needs to do camera updates right after we run the control loop. |
| 147 | if (enable_cameras_) { |
| 148 | SendDelayedFrames(); |
| 149 | if (last_frame_ + std::chrono::milliseconds(100) < |
| 150 | monotonic_now()) { |
| 151 | CaptureFrames(); |
| 152 | last_frame_ = monotonic_now(); |
| 153 | } |
| 154 | } |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 155 | }); |
James Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 156 | |
| 157 | test_event_loop_->AddPhasedLoop( |
| 158 | [this](int) { |
James Kuszmaul | 958b21e | 2020-02-26 21:51:40 -0800 | [diff] [blame] | 159 | auto builder = server_statistics_sender_.MakeBuilder(); |
| 160 | auto name_offset = builder.fbb()->CreateString("pi1"); |
| 161 | auto node_builder = builder.MakeBuilder<aos::Node>(); |
| 162 | node_builder.add_name(name_offset); |
| 163 | auto node_offset = node_builder.Finish(); |
| 164 | auto connection_builder = |
| 165 | builder.MakeBuilder<aos::message_bridge::ServerConnection>(); |
| 166 | connection_builder.add_node(node_offset); |
| 167 | connection_builder.add_monotonic_offset( |
Austin Schuh | be69cf3 | 2020-08-27 11:38:33 -0700 | [diff] [blame] | 168 | chrono::duration_cast<chrono::nanoseconds>(kPiTimeOffset) |
James Kuszmaul | 958b21e | 2020-02-26 21:51:40 -0800 | [diff] [blame] | 169 | .count()); |
| 170 | auto connection_offset = connection_builder.Finish(); |
| 171 | auto connections_offset = |
| 172 | builder.fbb()->CreateVector(&connection_offset, 1); |
| 173 | auto statistics_builder = |
| 174 | builder.MakeBuilder<aos::message_bridge::ServerStatistics>(); |
| 175 | statistics_builder.add_connections(connections_offset); |
| 176 | builder.Send(statistics_builder.Finish()); |
| 177 | }, |
| 178 | chrono::milliseconds(500)); |
| 179 | |
| 180 | test_event_loop_->AddPhasedLoop( |
| 181 | [this](int) { |
James Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 182 | // Also use the opportunity to send out turret messages. |
| 183 | UpdateTurretPosition(); |
| 184 | auto builder = superstructure_status_sender_.MakeBuilder(); |
| 185 | auto turret_builder = |
| 186 | builder |
| 187 | .MakeBuilder<frc971::control_loops:: |
| 188 | PotAndAbsoluteEncoderProfiledJointStatus>(); |
| 189 | turret_builder.add_position(turret_position_); |
| 190 | turret_builder.add_velocity(turret_velocity_); |
| 191 | auto turret_offset = turret_builder.Finish(); |
| 192 | auto status_builder = builder.MakeBuilder<superstructure::Status>(); |
| 193 | status_builder.add_turret(turret_offset); |
| 194 | builder.Send(status_builder.Finish()); |
| 195 | }, |
| 196 | chrono::milliseconds(5)); |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 197 | |
James Kuszmaul | 286b428 | 2020-02-26 20:29:32 -0800 | [diff] [blame] | 198 | test_event_loop_->OnRun([this]() { SetStartingPosition({3.0, 2.0, 0.0}); }); |
| 199 | |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 200 | // Run for enough time to allow the gyro/imu zeroing code to run. |
| 201 | RunFor(std::chrono::seconds(10)); |
| 202 | } |
| 203 | |
| 204 | virtual ~LocalizedDrivetrainTest() override {} |
| 205 | |
| 206 | void SetStartingPosition(const Eigen::Matrix<double, 3, 1> &xytheta) { |
| 207 | *drivetrain_plant_.mutable_state() << xytheta.x(), xytheta.y(), |
| 208 | xytheta(2, 0), 0.0, 0.0; |
James Kuszmaul | bcd96fc | 2020-10-12 20:29:32 -0700 | [diff] [blame^] | 209 | Eigen::Matrix<double, Localizer::HybridEkf::kNStates, 1> localizer_state; |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 210 | localizer_state.setZero(); |
James Kuszmaul | bcd96fc | 2020-10-12 20:29:32 -0700 | [diff] [blame^] | 211 | localizer_state.block<3, 1>(0, 0) = xytheta; |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 212 | localizer_.Reset(monotonic_now(), localizer_state); |
| 213 | } |
| 214 | |
| 215 | void VerifyNearGoal(double eps = 1e-3) { |
| 216 | drivetrain_goal_fetcher_.Fetch(); |
| 217 | EXPECT_NEAR(drivetrain_goal_fetcher_->left_goal(), |
| 218 | drivetrain_plant_.GetLeftPosition(), eps); |
| 219 | EXPECT_NEAR(drivetrain_goal_fetcher_->right_goal(), |
| 220 | drivetrain_plant_.GetRightPosition(), eps); |
| 221 | } |
| 222 | |
James Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 223 | ::testing::AssertionResult IsNear(double expected, double actual, |
| 224 | double epsilon) { |
| 225 | if (std::abs(expected - actual) < epsilon) { |
| 226 | return ::testing::AssertionSuccess(); |
| 227 | } else { |
| 228 | return ::testing::AssertionFailure() |
| 229 | << "Expected " << expected << " but got " << actual |
| 230 | << " with a max difference of " << epsilon |
| 231 | << " and an actual difference of " << std::abs(expected - actual); |
| 232 | } |
| 233 | } |
| 234 | ::testing::AssertionResult VerifyEstimatorAccurate(double eps) { |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 235 | const Eigen::Matrix<double, 5, 1> true_state = drivetrain_plant_.state(); |
James Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 236 | ::testing::AssertionResult result(true); |
| 237 | if (!(result = IsNear(localizer_.x(), true_state(0), eps))) { |
| 238 | return result; |
| 239 | } |
| 240 | if (!(result = IsNear(localizer_.y(), true_state(1), eps))) { |
| 241 | return result; |
| 242 | } |
| 243 | if (!(result = IsNear(localizer_.theta(), true_state(2), eps))) { |
| 244 | return result; |
| 245 | } |
| 246 | if (!(result = IsNear(localizer_.left_velocity(), true_state(3), eps))) { |
| 247 | return result; |
| 248 | } |
| 249 | if (!(result = IsNear(localizer_.right_velocity(), true_state(4), eps))) { |
| 250 | return result; |
| 251 | } |
| 252 | return result; |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | // Goes through and captures frames on the camera(s), queueing them up to be |
| 256 | // sent by SendDelayedFrames(). |
| 257 | void CaptureFrames() { |
| 258 | const frc971::control_loops::Pose robot_pose( |
| 259 | {drivetrain_plant_.GetPosition().x(), |
| 260 | drivetrain_plant_.GetPosition().y(), 0.0}, |
| 261 | drivetrain_plant_.state()(2, 0)); |
| 262 | std::unique_ptr<ImageMatchResultT> frame(new ImageMatchResultT()); |
| 263 | |
James Kuszmaul | c51dbfe | 2020-02-23 15:39:00 -0800 | [diff] [blame] | 264 | for (const auto &H_field_target : TargetLocations()) { |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 265 | std::unique_ptr<CameraPoseT> camera_target(new CameraPoseT()); |
| 266 | |
| 267 | camera_target->field_to_target.reset(new TransformationMatrixT()); |
James Kuszmaul | c51dbfe | 2020-02-23 15:39:00 -0800 | [diff] [blame] | 268 | camera_target->field_to_target->data = MatrixToVector(H_field_target); |
James Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 269 | |
James Kuszmaul | c51dbfe | 2020-02-23 15:39:00 -0800 | [diff] [blame] | 270 | Eigen::Matrix<double, 4, 4> H_turret_camera = |
James Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 271 | Eigen::Matrix<double, 4, 4>::Identity(); |
| 272 | if (is_turreted_) { |
James Kuszmaul | c51dbfe | 2020-02-23 15:39:00 -0800 | [diff] [blame] | 273 | H_turret_camera = frc971::control_loops::TransformationMatrixForYaw( |
James Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 274 | turret_position_) * |
| 275 | CameraTurretTransformation(); |
| 276 | } |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 277 | |
| 278 | // TODO(james): Use non-zero turret angles. |
| 279 | camera_target->camera_to_target.reset(new TransformationMatrixT()); |
James Kuszmaul | c51dbfe | 2020-02-23 15:39:00 -0800 | [diff] [blame] | 280 | camera_target->camera_to_target->data = |
| 281 | MatrixToVector((robot_pose.AsTransformationMatrix() * |
| 282 | TurretRobotTransformation() * H_turret_camera) |
| 283 | .inverse() * |
| 284 | H_field_target); |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 285 | |
| 286 | frame->camera_poses.emplace_back(std::move(camera_target)); |
| 287 | } |
| 288 | |
| 289 | frame->image_monotonic_timestamp_ns = |
| 290 | chrono::duration_cast<chrono::nanoseconds>( |
James Kuszmaul | 958b21e | 2020-02-26 21:51:40 -0800 | [diff] [blame] | 291 | event_loop_factory() |
| 292 | ->GetNodeEventLoopFactory(pi1_) |
| 293 | ->monotonic_now() |
| 294 | .time_since_epoch()) |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 295 | .count(); |
| 296 | frame->camera_calibration.reset(new CameraCalibrationT()); |
| 297 | { |
| 298 | frame->camera_calibration->fixed_extrinsics.reset( |
| 299 | new TransformationMatrixT()); |
James Kuszmaul | c51dbfe | 2020-02-23 15:39:00 -0800 | [diff] [blame] | 300 | TransformationMatrixT *H_robot_turret = |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 301 | frame->camera_calibration->fixed_extrinsics.get(); |
James Kuszmaul | c51dbfe | 2020-02-23 15:39:00 -0800 | [diff] [blame] | 302 | H_robot_turret->data = MatrixToVector(TurretRobotTransformation()); |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 303 | } |
James Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 304 | |
| 305 | if (is_turreted_) { |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 306 | frame->camera_calibration->turret_extrinsics.reset( |
| 307 | new TransformationMatrixT()); |
James Kuszmaul | c51dbfe | 2020-02-23 15:39:00 -0800 | [diff] [blame] | 308 | TransformationMatrixT *H_turret_camera = |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 309 | frame->camera_calibration->turret_extrinsics.get(); |
James Kuszmaul | c51dbfe | 2020-02-23 15:39:00 -0800 | [diff] [blame] | 310 | H_turret_camera->data = MatrixToVector(CameraTurretTransformation()); |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | camera_delay_queue_.emplace(monotonic_now(), std::move(frame)); |
| 314 | } |
| 315 | |
| 316 | // Actually sends out all the camera frames. |
| 317 | void SendDelayedFrames() { |
| 318 | const std::chrono::milliseconds camera_latency(150); |
| 319 | while (!camera_delay_queue_.empty() && |
| 320 | std::get<0>(camera_delay_queue_.front()) < |
| 321 | monotonic_now() - camera_latency) { |
| 322 | auto builder = camera_sender_.MakeBuilder(); |
| 323 | ASSERT_TRUE(builder.Send(ImageMatchResult::Pack( |
| 324 | *builder.fbb(), std::get<1>(camera_delay_queue_.front()).get()))); |
| 325 | camera_delay_queue_.pop(); |
| 326 | } |
| 327 | } |
| 328 | |
Austin Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 329 | const aos::Node *const roborio_; |
| 330 | const aos::Node *const pi1_; |
| 331 | |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 332 | std::unique_ptr<aos::EventLoop> test_event_loop_; |
| 333 | aos::Sender<Goal> drivetrain_goal_sender_; |
| 334 | aos::Fetcher<Goal> drivetrain_goal_fetcher_; |
| 335 | aos::Sender<LocalizerControl> localizer_control_sender_; |
James Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 336 | aos::Sender<superstructure::Status> superstructure_status_sender_; |
James Kuszmaul | 958b21e | 2020-02-26 21:51:40 -0800 | [diff] [blame] | 337 | aos::Sender<aos::message_bridge::ServerStatistics> server_statistics_sender_; |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 338 | |
| 339 | std::unique_ptr<aos::EventLoop> drivetrain_event_loop_; |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 340 | const frc971::control_loops::drivetrain::DrivetrainConfig<double> dt_config_; |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 341 | |
Austin Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 342 | std::unique_ptr<aos::EventLoop> pi1_event_loop_; |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 343 | aos::Sender<ImageMatchResult> camera_sender_; |
| 344 | |
| 345 | Localizer localizer_; |
| 346 | DrivetrainLoop drivetrain_; |
| 347 | |
| 348 | std::unique_ptr<aos::EventLoop> drivetrain_plant_event_loop_; |
| 349 | DrivetrainSimulation drivetrain_plant_; |
| 350 | monotonic_clock::time_point last_frame_; |
| 351 | |
| 352 | // A queue of camera frames so that we can add a time delay to the data |
| 353 | // coming from the cameras. |
| 354 | std::queue<std::tuple<aos::monotonic_clock::time_point, |
| 355 | std::unique_ptr<ImageMatchResultT>>> |
| 356 | camera_delay_queue_; |
| 357 | |
| 358 | void set_enable_cameras(bool enable) { enable_cameras_ = enable; } |
James Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 359 | void set_camera_is_turreted(bool turreted) { is_turreted_ = turreted; } |
| 360 | |
| 361 | void set_turret(double position, double velocity) { |
| 362 | turret_position_ = position; |
| 363 | turret_velocity_ = velocity; |
| 364 | } |
| 365 | |
| 366 | void SendGoal(double left, double right) { |
| 367 | auto builder = drivetrain_goal_sender_.MakeBuilder(); |
| 368 | |
| 369 | Goal::Builder drivetrain_builder = builder.MakeBuilder<Goal>(); |
| 370 | drivetrain_builder.add_controller_type( |
| 371 | frc971::control_loops::drivetrain::ControllerType::MOTION_PROFILE); |
| 372 | drivetrain_builder.add_left_goal(left); |
| 373 | drivetrain_builder.add_right_goal(right); |
| 374 | |
| 375 | EXPECT_TRUE(builder.Send(drivetrain_builder.Finish())); |
| 376 | } |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 377 | |
| 378 | private: |
James Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 379 | void UpdateTurretPosition() { |
| 380 | turret_position_ += |
| 381 | turret_velocity_ * |
| 382 | aos::time::DurationInSeconds(monotonic_now() - last_turret_update_); |
| 383 | last_turret_update_ = monotonic_now(); |
| 384 | } |
| 385 | |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 386 | bool enable_cameras_ = false; |
James Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 387 | // Whether to make the camera be on the turret or not. |
| 388 | bool is_turreted_ = true; |
| 389 | |
| 390 | // The time at which we last incremented turret_position_. |
| 391 | monotonic_clock::time_point last_turret_update_ = monotonic_clock::min_time; |
| 392 | // Current turret position and velocity. These are set directly by the user in |
| 393 | // the test, and if velocity is non-zero, then we will automatically increment |
| 394 | // turret_position_ with every timestep. |
| 395 | double turret_position_ = 0.0; // rad |
| 396 | double turret_velocity_ = 0.0; // rad / sec |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 397 | |
| 398 | std::unique_ptr<aos::EventLoop> logger_event_loop_; |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 399 | std::unique_ptr<aos::logger::Logger> logger_; |
| 400 | }; |
| 401 | |
| 402 | // Tests that no camera updates, combined with a perfect model, results in no |
| 403 | // error. |
| 404 | TEST_F(LocalizedDrivetrainTest, NoCameraUpdate) { |
| 405 | SetEnabled(true); |
| 406 | set_enable_cameras(false); |
James Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 407 | EXPECT_TRUE(VerifyEstimatorAccurate(1e-7)); |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 408 | |
James Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 409 | SendGoal(-1.0, 1.0); |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 410 | |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 411 | RunFor(chrono::seconds(3)); |
| 412 | VerifyNearGoal(); |
James Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 413 | EXPECT_TRUE(VerifyEstimatorAccurate(5e-4)); |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 414 | } |
| 415 | |
James Kuszmaul | 7f55f07 | 2020-03-01 10:21:26 -0800 | [diff] [blame] | 416 | // Tests that we can drive in a straight line and have things estimate |
| 417 | // correctly. |
| 418 | TEST_F(LocalizedDrivetrainTest, NoCameraUpdateStraightLine) { |
| 419 | SetEnabled(true); |
| 420 | set_enable_cameras(false); |
| 421 | EXPECT_TRUE(VerifyEstimatorAccurate(1e-7)); |
| 422 | |
| 423 | SendGoal(1.0, 1.0); |
| 424 | |
| 425 | RunFor(chrono::seconds(1)); |
| 426 | VerifyNearGoal(); |
| 427 | // Due to accelerometer drift, the straight-line driving tends to be less |
| 428 | // accurate... |
| 429 | EXPECT_TRUE(VerifyEstimatorAccurate(0.1)); |
| 430 | } |
| 431 | |
James Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 432 | // Tests that camera updates with a perfect models results in no errors. |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 433 | TEST_F(LocalizedDrivetrainTest, PerfectCameraUpdate) { |
| 434 | SetEnabled(true); |
| 435 | set_enable_cameras(true); |
James Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 436 | set_camera_is_turreted(true); |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 437 | |
James Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 438 | SendGoal(-1.0, 1.0); |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 439 | |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 440 | RunFor(chrono::seconds(3)); |
| 441 | VerifyNearGoal(); |
James Kuszmaul | 66efe83 | 2020-03-16 19:38:33 -0700 | [diff] [blame] | 442 | EXPECT_TRUE(VerifyEstimatorAccurate(2e-3)); |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 443 | } |
| 444 | |
James Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 445 | // Tests that camera updates with a constant initial error in the position |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 446 | // results in convergence. |
| 447 | TEST_F(LocalizedDrivetrainTest, InitialPositionError) { |
| 448 | SetEnabled(true); |
| 449 | set_enable_cameras(true); |
James Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 450 | set_camera_is_turreted(true); |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 451 | drivetrain_plant_.mutable_state()->topRows(3) += |
| 452 | Eigen::Vector3d(0.1, 0.1, 0.01); |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 453 | |
James Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 454 | // Confirm that some translational movement does get handled correctly. |
| 455 | SendGoal(-0.9, 1.0); |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 456 | |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 457 | // Give the filters enough time to converge. |
| 458 | RunFor(chrono::seconds(10)); |
| 459 | VerifyNearGoal(5e-3); |
James Kuszmaul | 58cb1fe | 2020-03-07 16:18:59 -0800 | [diff] [blame] | 460 | EXPECT_TRUE(VerifyEstimatorAccurate(4e-2)); |
James Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | // Tests that camera updates using a non-turreted camera work. |
| 464 | TEST_F(LocalizedDrivetrainTest, InitialPositionErrorNoTurret) { |
| 465 | SetEnabled(true); |
| 466 | set_enable_cameras(true); |
| 467 | set_camera_is_turreted(false); |
| 468 | drivetrain_plant_.mutable_state()->topRows(3) += |
| 469 | Eigen::Vector3d(0.1, 0.1, 0.01); |
| 470 | |
| 471 | SendGoal(-1.0, 1.0); |
| 472 | |
| 473 | // Give the filters enough time to converge. |
| 474 | RunFor(chrono::seconds(10)); |
| 475 | VerifyNearGoal(5e-3); |
James Kuszmaul | 58cb1fe | 2020-03-07 16:18:59 -0800 | [diff] [blame] | 476 | EXPECT_TRUE(VerifyEstimatorAccurate(4e-2)); |
James Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | // Tests that we are able to handle a constant, non-zero turret angle. |
| 480 | TEST_F(LocalizedDrivetrainTest, NonZeroTurret) { |
| 481 | SetEnabled(true); |
| 482 | set_enable_cameras(true); |
| 483 | set_camera_is_turreted(true); |
| 484 | set_turret(1.0, 0.0); |
| 485 | drivetrain_plant_.mutable_state()->topRows(3) += |
| 486 | Eigen::Vector3d(0.1, 0.1, 0.0); |
| 487 | |
| 488 | SendGoal(-1.0, 1.0); |
| 489 | |
| 490 | // Give the filters enough time to converge. |
| 491 | RunFor(chrono::seconds(10)); |
| 492 | VerifyNearGoal(5e-3); |
James Kuszmaul | 58cb1fe | 2020-03-07 16:18:59 -0800 | [diff] [blame] | 493 | EXPECT_TRUE(VerifyEstimatorAccurate(1e-2)); |
James Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | // Tests that we are able to handle a constant velocity turret. |
| 497 | TEST_F(LocalizedDrivetrainTest, MovingTurret) { |
| 498 | SetEnabled(true); |
| 499 | set_enable_cameras(true); |
| 500 | set_camera_is_turreted(true); |
| 501 | set_turret(0.0, 0.2); |
| 502 | drivetrain_plant_.mutable_state()->topRows(3) += |
| 503 | Eigen::Vector3d(0.1, 0.1, 0.0); |
| 504 | |
| 505 | SendGoal(-1.0, 1.0); |
| 506 | |
| 507 | // Give the filters enough time to converge. |
| 508 | RunFor(chrono::seconds(10)); |
| 509 | VerifyNearGoal(5e-3); |
James Kuszmaul | 58cb1fe | 2020-03-07 16:18:59 -0800 | [diff] [blame] | 510 | EXPECT_TRUE(VerifyEstimatorAccurate(1e-2)); |
James Kuszmaul | 688a2b0 | 2020-02-19 18:26:33 -0800 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | // Tests that we reject camera measurements when the turret is spinning too |
| 514 | // fast. |
| 515 | TEST_F(LocalizedDrivetrainTest, TooFastTurret) { |
| 516 | SetEnabled(true); |
| 517 | set_enable_cameras(true); |
| 518 | set_camera_is_turreted(true); |
| 519 | set_turret(0.0, -10.0); |
| 520 | const Eigen::Vector3d disturbance(0.1, 0.1, 0.0); |
| 521 | drivetrain_plant_.mutable_state()->topRows(3) += disturbance; |
| 522 | |
| 523 | SendGoal(-1.0, 1.0); |
| 524 | |
| 525 | RunFor(chrono::seconds(10)); |
| 526 | EXPECT_FALSE(VerifyEstimatorAccurate(1e-3)); |
| 527 | // If we remove the disturbance, we should now be correct. |
| 528 | drivetrain_plant_.mutable_state()->topRows(3) -= disturbance; |
| 529 | VerifyNearGoal(5e-3); |
| 530 | EXPECT_TRUE(VerifyEstimatorAccurate(1e-3)); |
| 531 | } |
| 532 | |
| 533 | // Tests that we don't reject camera measurements when the turret is spinning |
| 534 | // too fast but we aren't using a camera attached to the turret. |
| 535 | TEST_F(LocalizedDrivetrainTest, TooFastTurretDoesntAffectFixedCamera) { |
| 536 | SetEnabled(true); |
| 537 | set_enable_cameras(true); |
| 538 | set_camera_is_turreted(false); |
| 539 | set_turret(0.0, -10.0); |
| 540 | const Eigen::Vector3d disturbance(0.1, 0.1, 0.0); |
| 541 | drivetrain_plant_.mutable_state()->topRows(3) += disturbance; |
| 542 | |
| 543 | SendGoal(-1.0, 1.0); |
| 544 | |
| 545 | RunFor(chrono::seconds(10)); |
| 546 | VerifyNearGoal(5e-3); |
James Kuszmaul | 58cb1fe | 2020-03-07 16:18:59 -0800 | [diff] [blame] | 547 | EXPECT_TRUE(VerifyEstimatorAccurate(1e-2)); |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 548 | } |
| 549 | |
James Kuszmaul | bcd96fc | 2020-10-12 20:29:32 -0700 | [diff] [blame^] | 550 | // Tests that we don't blow up if we stop getting updates for an extended period |
| 551 | // of time and fall behind on fetching fron the cameras. |
| 552 | TEST_F(LocalizedDrivetrainTest, FetchersHandleTimeGap) { |
| 553 | set_enable_cameras(true); |
| 554 | set_send_delay(std::chrono::seconds(0)); |
| 555 | event_loop_factory()->set_network_delay(std::chrono::nanoseconds(1)); |
| 556 | test_event_loop_ |
| 557 | ->AddTimer([this]() { drivetrain_plant_.set_send_messages(false); }) |
| 558 | ->Setup(test_event_loop_->monotonic_now()); |
| 559 | test_event_loop_->AddPhasedLoop( |
| 560 | [this](int) { |
| 561 | auto builder = camera_sender_.MakeBuilder(); |
| 562 | ImageMatchResultT image; |
| 563 | ASSERT_TRUE( |
| 564 | builder.Send(ImageMatchResult::Pack(*builder.fbb(), &image))); |
| 565 | }, |
| 566 | std::chrono::milliseconds(20)); |
| 567 | test_event_loop_ |
| 568 | ->AddTimer([this]() { |
| 569 | drivetrain_plant_.set_send_messages(true); |
| 570 | SimulateSensorReset(); |
| 571 | }) |
| 572 | ->Setup(test_event_loop_->monotonic_now() + std::chrono::seconds(10)); |
| 573 | |
| 574 | RunFor(chrono::seconds(20)); |
| 575 | } |
| 576 | |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 577 | } // namespace testing |
| 578 | } // namespace drivetrain |
| 579 | } // namespace control_loops |
| 580 | } // namespace y2020 |