Austin Schuh | bb4aae7 | 2021-10-08 22:12:25 -0700 | [diff] [blame] | 1 | #include "Eigen/Dense" |
| 2 | #include "Eigen/Geometry" |
| 3 | |
Austin Schuh | bb4aae7 | 2021-10-08 22:12:25 -0700 | [diff] [blame] | 4 | #include "absl/strings/str_format.h" |
Austin Schuh | bb4aae7 | 2021-10-08 22:12:25 -0700 | [diff] [blame] | 5 | #include "aos/events/logging/log_reader.h" |
Austin Schuh | bb4aae7 | 2021-10-08 22:12:25 -0700 | [diff] [blame] | 6 | #include "aos/init.h" |
| 7 | #include "aos/network/team_number.h" |
| 8 | #include "aos/time/time.h" |
| 9 | #include "aos/util/file.h" |
milind-u | 8c72d53 | 2021-12-11 15:02:42 -0800 | [diff] [blame] | 10 | #include "frc971/control_loops/quaternion_utils.h" |
Jim Ostrowski | 977850f | 2022-01-22 21:04:22 -0800 | [diff] [blame] | 11 | #include "frc971/vision/vision_generated.h" |
Austin Schuh | dcb6b36 | 2022-02-25 18:06:21 -0800 | [diff] [blame^] | 12 | #include "frc971/vision/extrinsics_calibration.h" |
milind-u | 8c72d53 | 2021-12-11 15:02:42 -0800 | [diff] [blame] | 13 | #include "frc971/wpilib/imu_batch_generated.h" |
Austin Schuh | bb4aae7 | 2021-10-08 22:12:25 -0700 | [diff] [blame] | 14 | #include "y2020/vision/sift/sift_generated.h" |
| 15 | #include "y2020/vision/sift/sift_training_generated.h" |
| 16 | #include "y2020/vision/tools/python_code/sift_training_data.h" |
Austin Schuh | bb4aae7 | 2021-10-08 22:12:25 -0700 | [diff] [blame] | 17 | |
Austin Schuh | c5fa6d9 | 2022-02-25 14:36:28 -0800 | [diff] [blame] | 18 | DEFINE_string(config, "aos_config.json", "Path to the config file to use."); |
Austin Schuh | bb4aae7 | 2021-10-08 22:12:25 -0700 | [diff] [blame] | 19 | DEFINE_string(pi, "pi-7971-2", "Pi name to calibrate."); |
milind-u | 0084be6 | 2021-12-27 12:29:38 +0530 | [diff] [blame] | 20 | DEFINE_bool(plot, false, "Whether to plot the resulting data."); |
Austin Schuh | bb4aae7 | 2021-10-08 22:12:25 -0700 | [diff] [blame] | 21 | |
| 22 | namespace frc971 { |
| 23 | namespace vision { |
| 24 | namespace chrono = std::chrono; |
| 25 | using aos::distributed_clock; |
| 26 | using aos::monotonic_clock; |
| 27 | |
Austin Schuh | bb4aae7 | 2021-10-08 22:12:25 -0700 | [diff] [blame] | 28 | void Main(int argc, char **argv) { |
| 29 | CalibrationData data; |
| 30 | |
| 31 | { |
| 32 | // Now, accumulate all the data into the data object. |
| 33 | aos::logger::LogReader reader( |
| 34 | aos::logger::SortParts(aos::logger::FindLogs(argc, argv))); |
| 35 | |
| 36 | aos::SimulatedEventLoopFactory factory(reader.configuration()); |
| 37 | reader.Register(&factory); |
| 38 | |
| 39 | CHECK(aos::configuration::MultiNode(reader.configuration())); |
| 40 | |
| 41 | // Find the nodes we care about. |
| 42 | const aos::Node *const roborio_node = |
| 43 | aos::configuration::GetNode(factory.configuration(), "roborio"); |
| 44 | |
| 45 | std::optional<uint16_t> pi_number = aos::network::ParsePiNumber(FLAGS_pi); |
| 46 | CHECK(pi_number); |
| 47 | LOG(INFO) << "Pi " << *pi_number; |
| 48 | const aos::Node *const pi_node = aos::configuration::GetNode( |
| 49 | factory.configuration(), absl::StrCat("pi", *pi_number)); |
| 50 | |
| 51 | LOG(INFO) << "roboRIO " << aos::FlatbufferToJson(roborio_node); |
| 52 | LOG(INFO) << "Pi " << aos::FlatbufferToJson(pi_node); |
| 53 | |
| 54 | std::unique_ptr<aos::EventLoop> roborio_event_loop = |
| 55 | factory.MakeEventLoop("calibration", roborio_node); |
| 56 | std::unique_ptr<aos::EventLoop> pi_event_loop = |
| 57 | factory.MakeEventLoop("calibration", pi_node); |
| 58 | |
| 59 | // Now, hook Calibration up to everything. |
| 60 | Calibration extractor(&factory, pi_event_loop.get(), |
| 61 | roborio_event_loop.get(), FLAGS_pi, &data); |
| 62 | |
| 63 | factory.Run(); |
| 64 | |
| 65 | reader.Deregister(); |
| 66 | } |
| 67 | |
| 68 | LOG(INFO) << "Done with event_loop running"; |
| 69 | // And now we have it, we can start processing it. |
milind-u | 8c72d53 | 2021-12-11 15:02:42 -0800 | [diff] [blame] | 70 | |
Austin Schuh | 5b37907 | 2021-12-26 16:01:04 -0800 | [diff] [blame] | 71 | const Eigen::Quaternion<double> nominal_initial_orientation( |
milind-u | e53bf55 | 2021-12-11 14:42:00 -0800 | [diff] [blame] | 72 | frc971::controls::ToQuaternionFromRotationVector( |
| 73 | Eigen::Vector3d(0.0, 0.0, M_PI))); |
Austin Schuh | 5b37907 | 2021-12-26 16:01:04 -0800 | [diff] [blame] | 74 | const Eigen::Quaternion<double> nominal_imu_to_camera( |
milind-u | e53bf55 | 2021-12-11 14:42:00 -0800 | [diff] [blame] | 75 | Eigen::AngleAxisd(-0.5 * M_PI, Eigen::Vector3d::UnitX())); |
Austin Schuh | 5b37907 | 2021-12-26 16:01:04 -0800 | [diff] [blame] | 76 | const Eigen::Quaternion<double> nominal_board_to_world( |
| 77 | Eigen::AngleAxisd(0.5 * M_PI, Eigen::Vector3d::UnitX())); |
milind-u | e53bf55 | 2021-12-11 14:42:00 -0800 | [diff] [blame] | 78 | |
Austin Schuh | dcb6b36 | 2022-02-25 18:06:21 -0800 | [diff] [blame^] | 79 | CalibrationParameters calibration_parameters; |
| 80 | calibration_parameters.initial_orientation = nominal_initial_orientation; |
| 81 | calibration_parameters.imu_to_camera = nominal_imu_to_camera; |
| 82 | calibration_parameters.board_to_world = nominal_board_to_world; |
Austin Schuh | 5b37907 | 2021-12-26 16:01:04 -0800 | [diff] [blame] | 83 | |
Austin Schuh | dcb6b36 | 2022-02-25 18:06:21 -0800 | [diff] [blame^] | 84 | Solve(data, &calibration_parameters); |
| 85 | LOG(INFO) << "Nominal initial_orientation " |
| 86 | << nominal_initial_orientation.coeffs().transpose(); |
| 87 | LOG(INFO) << "Nominal imu_to_camera " |
| 88 | << nominal_imu_to_camera.coeffs().transpose(); |
milind-u | e53bf55 | 2021-12-11 14:42:00 -0800 | [diff] [blame] | 89 | |
Austin Schuh | dcb6b36 | 2022-02-25 18:06:21 -0800 | [diff] [blame^] | 90 | LOG(INFO) << "imu_to_camera delta " |
| 91 | << frc971::controls::ToRotationVectorFromQuaternion( |
| 92 | calibration_parameters.imu_to_camera * |
| 93 | nominal_imu_to_camera.inverse()) |
| 94 | .transpose(); |
| 95 | LOG(INFO) << "board_to_world delta " |
| 96 | << frc971::controls::ToRotationVectorFromQuaternion( |
| 97 | calibration_parameters.board_to_world * |
| 98 | nominal_board_to_world.inverse()) |
| 99 | .transpose(); |
milind-u | e53bf55 | 2021-12-11 14:42:00 -0800 | [diff] [blame] | 100 | |
Austin Schuh | dcb6b36 | 2022-02-25 18:06:21 -0800 | [diff] [blame^] | 101 | if (FLAGS_plot) { |
| 102 | Plot(data, calibration_parameters); |
milind-u | 8c72d53 | 2021-12-11 15:02:42 -0800 | [diff] [blame] | 103 | } |
Austin Schuh | bb4aae7 | 2021-10-08 22:12:25 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | } // namespace vision |
| 107 | } // namespace frc971 |
| 108 | |
| 109 | int main(int argc, char **argv) { |
| 110 | aos::InitGoogle(&argc, &argv); |
| 111 | |
| 112 | frc971::vision::Main(argc, argv); |
| 113 | } |