Austin Schuh | 2895f4c | 2022-02-26 16:38:46 -0800 | [diff] [blame] | 1 | #include "frc971/vision/extrinsics_calibration.h" |
| 2 | |
Austin Schuh | bb4aae7 | 2021-10-08 22:12:25 -0700 | [diff] [blame] | 3 | #include "Eigen/Dense" |
| 4 | #include "Eigen/Geometry" |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 5 | #include "absl/flags/flag.h" |
Austin Schuh | bb4aae7 | 2021-10-08 22:12:25 -0700 | [diff] [blame] | 6 | #include "absl/strings/str_format.h" |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 7 | |
Austin Schuh | bb4aae7 | 2021-10-08 22:12:25 -0700 | [diff] [blame] | 8 | #include "aos/events/logging/log_reader.h" |
Austin Schuh | bb4aae7 | 2021-10-08 22:12:25 -0700 | [diff] [blame] | 9 | #include "aos/init.h" |
| 10 | #include "aos/network/team_number.h" |
| 11 | #include "aos/time/time.h" |
| 12 | #include "aos/util/file.h" |
milind-u | 8c72d53 | 2021-12-11 15:02:42 -0800 | [diff] [blame] | 13 | #include "frc971/control_loops/quaternion_utils.h" |
Jim Ostrowski | b3cab97 | 2022-12-03 15:47:00 -0800 | [diff] [blame] | 14 | #include "frc971/vision/charuco_lib.h" |
Jim Ostrowski | 977850f | 2022-01-22 21:04:22 -0800 | [diff] [blame] | 15 | #include "frc971/vision/vision_generated.h" |
milind-u | 8c72d53 | 2021-12-11 15:02:42 -0800 | [diff] [blame] | 16 | #include "frc971/wpilib/imu_batch_generated.h" |
Austin Schuh | 2895f4c | 2022-02-26 16:38:46 -0800 | [diff] [blame] | 17 | #include "y2020/control_loops/superstructure/superstructure_status_generated.h" |
Austin Schuh | bb4aae7 | 2021-10-08 22:12:25 -0700 | [diff] [blame] | 18 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 19 | ABSL_FLAG(std::string, pi, "pi-7971-2", "Pi name to calibrate."); |
| 20 | ABSL_FLAG(bool, plot, false, "Whether to plot the resulting data."); |
| 21 | ABSL_FLAG(bool, turret, false, "If true, the camera is on the turret"); |
| 22 | ABSL_FLAG(std::string, base_intrinsics, "", |
| 23 | "Intrinsics to use for extrinsics calibration."); |
Austin Schuh | bb4aae7 | 2021-10-08 22:12:25 -0700 | [diff] [blame] | 24 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 25 | namespace frc971::vision { |
Austin Schuh | bb4aae7 | 2021-10-08 22:12:25 -0700 | [diff] [blame] | 26 | namespace chrono = std::chrono; |
| 27 | using aos::distributed_clock; |
| 28 | using aos::monotonic_clock; |
| 29 | |
Austin Schuh | bb4aae7 | 2021-10-08 22:12:25 -0700 | [diff] [blame] | 30 | void Main(int argc, char **argv) { |
| 31 | CalibrationData data; |
| 32 | |
| 33 | { |
| 34 | // Now, accumulate all the data into the data object. |
| 35 | aos::logger::LogReader reader( |
| 36 | aos::logger::SortParts(aos::logger::FindLogs(argc, argv))); |
| 37 | |
| 38 | aos::SimulatedEventLoopFactory factory(reader.configuration()); |
| 39 | reader.Register(&factory); |
| 40 | |
| 41 | CHECK(aos::configuration::MultiNode(reader.configuration())); |
| 42 | |
| 43 | // Find the nodes we care about. |
Jim Ostrowski | ba2edd1 | 2022-12-03 15:44:37 -0800 | [diff] [blame] | 44 | const aos::Node *const imu_node = |
| 45 | aos::configuration::GetNode(factory.configuration(), "imu"); |
Austin Schuh | bb4aae7 | 2021-10-08 22:12:25 -0700 | [diff] [blame] | 46 | const aos::Node *const roborio_node = |
| 47 | aos::configuration::GetNode(factory.configuration(), "roborio"); |
| 48 | |
Jim Ostrowski | cb8b408 | 2024-01-21 02:23:46 -0800 | [diff] [blame] | 49 | std::optional<uint16_t> pi_number = |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 50 | aos::network::ParsePiOrOrinNumber(absl::GetFlag(FLAGS_pi)); |
Austin Schuh | bb4aae7 | 2021-10-08 22:12:25 -0700 | [diff] [blame] | 51 | CHECK(pi_number); |
| 52 | LOG(INFO) << "Pi " << *pi_number; |
| 53 | const aos::Node *const pi_node = aos::configuration::GetNode( |
| 54 | factory.configuration(), absl::StrCat("pi", *pi_number)); |
| 55 | |
Jim Ostrowski | ba2edd1 | 2022-12-03 15:44:37 -0800 | [diff] [blame] | 56 | LOG(INFO) << "imu " << aos::FlatbufferToJson(imu_node); |
Austin Schuh | bb4aae7 | 2021-10-08 22:12:25 -0700 | [diff] [blame] | 57 | LOG(INFO) << "roboRIO " << aos::FlatbufferToJson(roborio_node); |
| 58 | LOG(INFO) << "Pi " << aos::FlatbufferToJson(pi_node); |
| 59 | |
Jim Ostrowski | ba2edd1 | 2022-12-03 15:44:37 -0800 | [diff] [blame] | 60 | std::unique_ptr<aos::EventLoop> imu_event_loop = |
| 61 | factory.MakeEventLoop("calibration", imu_node); |
Austin Schuh | bb4aae7 | 2021-10-08 22:12:25 -0700 | [diff] [blame] | 62 | std::unique_ptr<aos::EventLoop> roborio_event_loop = |
| 63 | factory.MakeEventLoop("calibration", roborio_node); |
| 64 | std::unique_ptr<aos::EventLoop> pi_event_loop = |
| 65 | factory.MakeEventLoop("calibration", pi_node); |
| 66 | |
James Kuszmaul | 7e95881 | 2023-02-11 15:34:31 -0800 | [diff] [blame] | 67 | aos::FlatbufferDetachedBuffer<calibration::CameraCalibration> intrinsics = |
| 68 | aos::JsonFileToFlatbuffer<calibration::CameraCalibration>( |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 69 | absl::GetFlag(FLAGS_base_intrinsics)); |
Austin Schuh | bb4aae7 | 2021-10-08 22:12:25 -0700 | [diff] [blame] | 70 | // Now, hook Calibration up to everything. |
Jim Ostrowski | ba2edd1 | 2022-12-03 15:44:37 -0800 | [diff] [blame] | 71 | Calibration extractor(&factory, pi_event_loop.get(), imu_event_loop.get(), |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 72 | absl::GetFlag(FLAGS_pi), &intrinsics.message(), |
| 73 | TargetType::kCharuco, "/camera", &data); |
Austin Schuh | bb4aae7 | 2021-10-08 22:12:25 -0700 | [diff] [blame] | 74 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 75 | if (absl::GetFlag(FLAGS_turret)) { |
Austin Schuh | 2895f4c | 2022-02-26 16:38:46 -0800 | [diff] [blame] | 76 | aos::NodeEventLoopFactory *roborio_factory = |
| 77 | factory.GetNodeEventLoopFactory(roborio_node->name()->string_view()); |
| 78 | roborio_event_loop->MakeWatcher( |
| 79 | "/superstructure", |
| 80 | [roborio_factory, roborio_event_loop = roborio_event_loop.get(), |
| 81 | &data](const y2020::control_loops::superstructure::Status &status) { |
| 82 | data.AddTurret( |
| 83 | roborio_factory->ToDistributedClock( |
| 84 | roborio_event_loop->context().monotonic_event_time), |
| 85 | Eigen::Vector2d(status.turret()->position(), |
| 86 | status.turret()->velocity())); |
| 87 | }); |
| 88 | } |
| 89 | |
Austin Schuh | bb4aae7 | 2021-10-08 22:12:25 -0700 | [diff] [blame] | 90 | factory.Run(); |
| 91 | |
| 92 | reader.Deregister(); |
| 93 | } |
| 94 | |
| 95 | LOG(INFO) << "Done with event_loop running"; |
| 96 | // And now we have it, we can start processing it. |
milind-u | 8c72d53 | 2021-12-11 15:02:42 -0800 | [diff] [blame] | 97 | |
Austin Schuh | 5b37907 | 2021-12-26 16:01:04 -0800 | [diff] [blame] | 98 | const Eigen::Quaternion<double> nominal_initial_orientation( |
milind-u | e53bf55 | 2021-12-11 14:42:00 -0800 | [diff] [blame] | 99 | frc971::controls::ToQuaternionFromRotationVector( |
| 100 | Eigen::Vector3d(0.0, 0.0, M_PI))); |
Austin Schuh | 2895f4c | 2022-02-26 16:38:46 -0800 | [diff] [blame] | 101 | const Eigen::Quaternion<double> nominal_pivot_to_camera( |
milind-u | e53bf55 | 2021-12-11 14:42:00 -0800 | [diff] [blame] | 102 | Eigen::AngleAxisd(-0.5 * M_PI, Eigen::Vector3d::UnitX())); |
Jim Ostrowski | ba2edd1 | 2022-12-03 15:44:37 -0800 | [diff] [blame] | 103 | const Eigen::Quaternion<double> nominal_pivot_to_imu( |
| 104 | Eigen::AngleAxisd(0.0, Eigen::Vector3d::UnitX())); |
Austin Schuh | 5b37907 | 2021-12-26 16:01:04 -0800 | [diff] [blame] | 105 | const Eigen::Quaternion<double> nominal_board_to_world( |
| 106 | Eigen::AngleAxisd(0.5 * M_PI, Eigen::Vector3d::UnitX())); |
Jim Ostrowski | ba2edd1 | 2022-12-03 15:44:37 -0800 | [diff] [blame] | 107 | Eigen::Matrix<double, 6, 1> nominal_initial_state = |
| 108 | Eigen::Matrix<double, 6, 1>::Zero(); |
| 109 | // Set y value to -1 m (approx distance from imu to board/world |
| 110 | nominal_initial_state(1, 0) = -1.0; |
milind-u | e53bf55 | 2021-12-11 14:42:00 -0800 | [diff] [blame] | 111 | |
Austin Schuh | dcb6b36 | 2022-02-25 18:06:21 -0800 | [diff] [blame] | 112 | CalibrationParameters calibration_parameters; |
| 113 | calibration_parameters.initial_orientation = nominal_initial_orientation; |
Austin Schuh | 2895f4c | 2022-02-26 16:38:46 -0800 | [diff] [blame] | 114 | calibration_parameters.pivot_to_camera = nominal_pivot_to_camera; |
Jim Ostrowski | ba2edd1 | 2022-12-03 15:44:37 -0800 | [diff] [blame] | 115 | calibration_parameters.pivot_to_imu = nominal_pivot_to_imu; |
Austin Schuh | dcb6b36 | 2022-02-25 18:06:21 -0800 | [diff] [blame] | 116 | calibration_parameters.board_to_world = nominal_board_to_world; |
Jim Ostrowski | ba2edd1 | 2022-12-03 15:44:37 -0800 | [diff] [blame] | 117 | calibration_parameters.initial_state = nominal_initial_state; |
| 118 | if (data.turret_samples_size() > 0) { |
| 119 | LOG(INFO) << "Have turret, so using pivot setup"; |
| 120 | calibration_parameters.has_pivot = true; |
| 121 | } |
Austin Schuh | 5b37907 | 2021-12-26 16:01:04 -0800 | [diff] [blame] | 122 | |
Austin Schuh | dcb6b36 | 2022-02-25 18:06:21 -0800 | [diff] [blame] | 123 | Solve(data, &calibration_parameters); |
| 124 | LOG(INFO) << "Nominal initial_orientation " |
| 125 | << nominal_initial_orientation.coeffs().transpose(); |
Austin Schuh | 2895f4c | 2022-02-26 16:38:46 -0800 | [diff] [blame] | 126 | LOG(INFO) << "Nominal pivot_to_camera " |
| 127 | << nominal_pivot_to_camera.coeffs().transpose(); |
Jim Ostrowski | ba2edd1 | 2022-12-03 15:44:37 -0800 | [diff] [blame] | 128 | LOG(INFO) << "Nominal pivot_to_camera (rot-xyz) " |
| 129 | << frc971::controls::ToRotationVectorFromQuaternion( |
| 130 | nominal_pivot_to_camera) |
| 131 | .transpose(); |
| 132 | LOG(INFO) << "pivot_to_camera change " |
Austin Schuh | dcb6b36 | 2022-02-25 18:06:21 -0800 | [diff] [blame] | 133 | << frc971::controls::ToRotationVectorFromQuaternion( |
Austin Schuh | 2895f4c | 2022-02-26 16:38:46 -0800 | [diff] [blame] | 134 | calibration_parameters.pivot_to_camera * |
| 135 | nominal_pivot_to_camera.inverse()) |
Austin Schuh | dcb6b36 | 2022-02-25 18:06:21 -0800 | [diff] [blame] | 136 | .transpose(); |
Jim Ostrowski | ba2edd1 | 2022-12-03 15:44:37 -0800 | [diff] [blame] | 137 | LOG(INFO) << "Nominal pivot_to_imu " |
| 138 | << nominal_pivot_to_imu.coeffs().transpose(); |
Austin Schuh | dcb6b36 | 2022-02-25 18:06:21 -0800 | [diff] [blame] | 139 | LOG(INFO) << "board_to_world delta " |
| 140 | << frc971::controls::ToRotationVectorFromQuaternion( |
| 141 | calibration_parameters.board_to_world * |
| 142 | nominal_board_to_world.inverse()) |
| 143 | .transpose(); |
milind-u | e53bf55 | 2021-12-11 14:42:00 -0800 | [diff] [blame] | 144 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 145 | if (absl::GetFlag(FLAGS_plot)) { |
Austin Schuh | dcb6b36 | 2022-02-25 18:06:21 -0800 | [diff] [blame] | 146 | Plot(data, calibration_parameters); |
milind-u | 8c72d53 | 2021-12-11 15:02:42 -0800 | [diff] [blame] | 147 | } |
Austin Schuh | bb4aae7 | 2021-10-08 22:12:25 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 150 | } // namespace frc971::vision |
Austin Schuh | bb4aae7 | 2021-10-08 22:12:25 -0700 | [diff] [blame] | 151 | |
| 152 | int main(int argc, char **argv) { |
| 153 | aos::InitGoogle(&argc, &argv); |
| 154 | |
| 155 | frc971::vision::Main(argc, argv); |
| 156 | } |