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 | bb4aae7 | 2021-10-08 22:12:25 -0700 | [diff] [blame] | 5 | #include "absl/strings/str_format.h" |
Austin Schuh | bb4aae7 | 2021-10-08 22:12:25 -0700 | [diff] [blame] | 6 | #include "aos/events/logging/log_reader.h" |
Austin Schuh | bb4aae7 | 2021-10-08 22:12:25 -0700 | [diff] [blame] | 7 | #include "aos/init.h" |
| 8 | #include "aos/network/team_number.h" |
| 9 | #include "aos/time/time.h" |
| 10 | #include "aos/util/file.h" |
milind-u | 8c72d53 | 2021-12-11 15:02:42 -0800 | [diff] [blame] | 11 | #include "frc971/control_loops/quaternion_utils.h" |
Jim Ostrowski | b3cab97 | 2022-12-03 15:47:00 -0800 | [diff] [blame^] | 12 | #include "frc971/vision/charuco_lib.h" |
Jim Ostrowski | 977850f | 2022-01-22 21:04:22 -0800 | [diff] [blame] | 13 | #include "frc971/vision/vision_generated.h" |
milind-u | 8c72d53 | 2021-12-11 15:02:42 -0800 | [diff] [blame] | 14 | #include "frc971/wpilib/imu_batch_generated.h" |
Austin Schuh | 2895f4c | 2022-02-26 16:38:46 -0800 | [diff] [blame] | 15 | #include "y2020/control_loops/superstructure/superstructure_status_generated.h" |
Austin Schuh | bb4aae7 | 2021-10-08 22:12:25 -0700 | [diff] [blame] | 16 | #include "y2020/vision/sift/sift_generated.h" |
| 17 | #include "y2020/vision/sift/sift_training_generated.h" |
| 18 | #include "y2020/vision/tools/python_code/sift_training_data.h" |
Austin Schuh | bb4aae7 | 2021-10-08 22:12:25 -0700 | [diff] [blame] | 19 | |
Austin Schuh | bb4aae7 | 2021-10-08 22:12:25 -0700 | [diff] [blame] | 20 | DEFINE_string(pi, "pi-7971-2", "Pi name to calibrate."); |
milind-u | 0084be6 | 2021-12-27 12:29:38 +0530 | [diff] [blame] | 21 | DEFINE_bool(plot, false, "Whether to plot the resulting data."); |
Austin Schuh | 2895f4c | 2022-02-26 16:38:46 -0800 | [diff] [blame] | 22 | DEFINE_bool(turret, false, "If true, the camera is on the turret"); |
Austin Schuh | bb4aae7 | 2021-10-08 22:12:25 -0700 | [diff] [blame] | 23 | |
| 24 | namespace frc971 { |
| 25 | namespace vision { |
| 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 | |
| 49 | std::optional<uint16_t> pi_number = aos::network::ParsePiNumber(FLAGS_pi); |
| 50 | CHECK(pi_number); |
| 51 | LOG(INFO) << "Pi " << *pi_number; |
| 52 | const aos::Node *const pi_node = aos::configuration::GetNode( |
| 53 | factory.configuration(), absl::StrCat("pi", *pi_number)); |
| 54 | |
Jim Ostrowski | ba2edd1 | 2022-12-03 15:44:37 -0800 | [diff] [blame] | 55 | LOG(INFO) << "imu " << aos::FlatbufferToJson(imu_node); |
Austin Schuh | bb4aae7 | 2021-10-08 22:12:25 -0700 | [diff] [blame] | 56 | LOG(INFO) << "roboRIO " << aos::FlatbufferToJson(roborio_node); |
| 57 | LOG(INFO) << "Pi " << aos::FlatbufferToJson(pi_node); |
| 58 | |
Jim Ostrowski | ba2edd1 | 2022-12-03 15:44:37 -0800 | [diff] [blame] | 59 | std::unique_ptr<aos::EventLoop> imu_event_loop = |
| 60 | factory.MakeEventLoop("calibration", imu_node); |
Austin Schuh | bb4aae7 | 2021-10-08 22:12:25 -0700 | [diff] [blame] | 61 | std::unique_ptr<aos::EventLoop> roborio_event_loop = |
| 62 | factory.MakeEventLoop("calibration", roborio_node); |
| 63 | std::unique_ptr<aos::EventLoop> pi_event_loop = |
| 64 | factory.MakeEventLoop("calibration", pi_node); |
| 65 | |
| 66 | // Now, hook Calibration up to everything. |
Jim Ostrowski | ba2edd1 | 2022-12-03 15:44:37 -0800 | [diff] [blame] | 67 | Calibration extractor(&factory, pi_event_loop.get(), imu_event_loop.get(), |
| 68 | FLAGS_pi, &data); |
Austin Schuh | bb4aae7 | 2021-10-08 22:12:25 -0700 | [diff] [blame] | 69 | |
Austin Schuh | 2895f4c | 2022-02-26 16:38:46 -0800 | [diff] [blame] | 70 | if (FLAGS_turret) { |
| 71 | aos::NodeEventLoopFactory *roborio_factory = |
| 72 | factory.GetNodeEventLoopFactory(roborio_node->name()->string_view()); |
| 73 | roborio_event_loop->MakeWatcher( |
| 74 | "/superstructure", |
| 75 | [roborio_factory, roborio_event_loop = roborio_event_loop.get(), |
| 76 | &data](const y2020::control_loops::superstructure::Status &status) { |
| 77 | data.AddTurret( |
| 78 | roborio_factory->ToDistributedClock( |
| 79 | roborio_event_loop->context().monotonic_event_time), |
| 80 | Eigen::Vector2d(status.turret()->position(), |
| 81 | status.turret()->velocity())); |
| 82 | }); |
| 83 | } |
| 84 | |
Austin Schuh | bb4aae7 | 2021-10-08 22:12:25 -0700 | [diff] [blame] | 85 | factory.Run(); |
| 86 | |
| 87 | reader.Deregister(); |
| 88 | } |
| 89 | |
| 90 | LOG(INFO) << "Done with event_loop running"; |
| 91 | // And now we have it, we can start processing it. |
milind-u | 8c72d53 | 2021-12-11 15:02:42 -0800 | [diff] [blame] | 92 | |
Austin Schuh | 5b37907 | 2021-12-26 16:01:04 -0800 | [diff] [blame] | 93 | const Eigen::Quaternion<double> nominal_initial_orientation( |
milind-u | e53bf55 | 2021-12-11 14:42:00 -0800 | [diff] [blame] | 94 | frc971::controls::ToQuaternionFromRotationVector( |
| 95 | Eigen::Vector3d(0.0, 0.0, M_PI))); |
Austin Schuh | 2895f4c | 2022-02-26 16:38:46 -0800 | [diff] [blame] | 96 | const Eigen::Quaternion<double> nominal_pivot_to_camera( |
milind-u | e53bf55 | 2021-12-11 14:42:00 -0800 | [diff] [blame] | 97 | Eigen::AngleAxisd(-0.5 * M_PI, Eigen::Vector3d::UnitX())); |
Jim Ostrowski | ba2edd1 | 2022-12-03 15:44:37 -0800 | [diff] [blame] | 98 | const Eigen::Quaternion<double> nominal_pivot_to_imu( |
| 99 | Eigen::AngleAxisd(0.0, Eigen::Vector3d::UnitX())); |
Austin Schuh | 5b37907 | 2021-12-26 16:01:04 -0800 | [diff] [blame] | 100 | const Eigen::Quaternion<double> nominal_board_to_world( |
| 101 | Eigen::AngleAxisd(0.5 * M_PI, Eigen::Vector3d::UnitX())); |
Jim Ostrowski | ba2edd1 | 2022-12-03 15:44:37 -0800 | [diff] [blame] | 102 | Eigen::Matrix<double, 6, 1> nominal_initial_state = |
| 103 | Eigen::Matrix<double, 6, 1>::Zero(); |
| 104 | // Set y value to -1 m (approx distance from imu to board/world |
| 105 | nominal_initial_state(1, 0) = -1.0; |
milind-u | e53bf55 | 2021-12-11 14:42:00 -0800 | [diff] [blame] | 106 | |
Austin Schuh | dcb6b36 | 2022-02-25 18:06:21 -0800 | [diff] [blame] | 107 | CalibrationParameters calibration_parameters; |
| 108 | calibration_parameters.initial_orientation = nominal_initial_orientation; |
Austin Schuh | 2895f4c | 2022-02-26 16:38:46 -0800 | [diff] [blame] | 109 | calibration_parameters.pivot_to_camera = nominal_pivot_to_camera; |
Jim Ostrowski | ba2edd1 | 2022-12-03 15:44:37 -0800 | [diff] [blame] | 110 | calibration_parameters.pivot_to_imu = nominal_pivot_to_imu; |
Austin Schuh | dcb6b36 | 2022-02-25 18:06:21 -0800 | [diff] [blame] | 111 | calibration_parameters.board_to_world = nominal_board_to_world; |
Jim Ostrowski | ba2edd1 | 2022-12-03 15:44:37 -0800 | [diff] [blame] | 112 | calibration_parameters.initial_state = nominal_initial_state; |
| 113 | if (data.turret_samples_size() > 0) { |
| 114 | LOG(INFO) << "Have turret, so using pivot setup"; |
| 115 | calibration_parameters.has_pivot = true; |
| 116 | } |
Austin Schuh | 5b37907 | 2021-12-26 16:01:04 -0800 | [diff] [blame] | 117 | |
Austin Schuh | dcb6b36 | 2022-02-25 18:06:21 -0800 | [diff] [blame] | 118 | Solve(data, &calibration_parameters); |
| 119 | LOG(INFO) << "Nominal initial_orientation " |
| 120 | << nominal_initial_orientation.coeffs().transpose(); |
Austin Schuh | 2895f4c | 2022-02-26 16:38:46 -0800 | [diff] [blame] | 121 | LOG(INFO) << "Nominal pivot_to_camera " |
| 122 | << nominal_pivot_to_camera.coeffs().transpose(); |
Jim Ostrowski | ba2edd1 | 2022-12-03 15:44:37 -0800 | [diff] [blame] | 123 | LOG(INFO) << "Nominal pivot_to_camera (rot-xyz) " |
| 124 | << frc971::controls::ToRotationVectorFromQuaternion( |
| 125 | nominal_pivot_to_camera) |
| 126 | .transpose(); |
| 127 | LOG(INFO) << "pivot_to_camera change " |
Austin Schuh | dcb6b36 | 2022-02-25 18:06:21 -0800 | [diff] [blame] | 128 | << frc971::controls::ToRotationVectorFromQuaternion( |
Austin Schuh | 2895f4c | 2022-02-26 16:38:46 -0800 | [diff] [blame] | 129 | calibration_parameters.pivot_to_camera * |
| 130 | nominal_pivot_to_camera.inverse()) |
Austin Schuh | dcb6b36 | 2022-02-25 18:06:21 -0800 | [diff] [blame] | 131 | .transpose(); |
Jim Ostrowski | ba2edd1 | 2022-12-03 15:44:37 -0800 | [diff] [blame] | 132 | LOG(INFO) << "Nominal pivot_to_imu " |
| 133 | << nominal_pivot_to_imu.coeffs().transpose(); |
Austin Schuh | dcb6b36 | 2022-02-25 18:06:21 -0800 | [diff] [blame] | 134 | LOG(INFO) << "board_to_world delta " |
| 135 | << frc971::controls::ToRotationVectorFromQuaternion( |
| 136 | calibration_parameters.board_to_world * |
| 137 | nominal_board_to_world.inverse()) |
| 138 | .transpose(); |
milind-u | e53bf55 | 2021-12-11 14:42:00 -0800 | [diff] [blame] | 139 | |
Austin Schuh | dcb6b36 | 2022-02-25 18:06:21 -0800 | [diff] [blame] | 140 | if (FLAGS_plot) { |
| 141 | Plot(data, calibration_parameters); |
milind-u | 8c72d53 | 2021-12-11 15:02:42 -0800 | [diff] [blame] | 142 | } |
Austin Schuh | bb4aae7 | 2021-10-08 22:12:25 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | } // namespace vision |
| 146 | } // namespace frc971 |
| 147 | |
| 148 | int main(int argc, char **argv) { |
| 149 | aos::InitGoogle(&argc, &argv); |
| 150 | |
| 151 | frc971::vision::Main(argc, argv); |
| 152 | } |