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 | 977850f | 2022-01-22 21:04:22 -0800 | [diff] [blame] | 12 | #include "frc971/vision/vision_generated.h" |
milind-u | 8c72d53 | 2021-12-11 15:02:42 -0800 | [diff] [blame] | 13 | #include "frc971/wpilib/imu_batch_generated.h" |
Austin Schuh | 2895f4c | 2022-02-26 16:38:46 -0800 | [diff] [blame^] | 14 | #include "y2020/control_loops/superstructure/superstructure_status_generated.h" |
Austin Schuh | bb4aae7 | 2021-10-08 22:12:25 -0700 | [diff] [blame] | 15 | #include "y2020/vision/sift/sift_generated.h" |
| 16 | #include "y2020/vision/sift/sift_training_generated.h" |
| 17 | #include "y2020/vision/tools/python_code/sift_training_data.h" |
Austin Schuh | bb4aae7 | 2021-10-08 22:12:25 -0700 | [diff] [blame] | 18 | |
Austin Schuh | c5fa6d9 | 2022-02-25 14:36:28 -0800 | [diff] [blame] | 19 | 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] | 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. |
| 44 | const aos::Node *const roborio_node = |
| 45 | aos::configuration::GetNode(factory.configuration(), "roborio"); |
| 46 | |
| 47 | std::optional<uint16_t> pi_number = aos::network::ParsePiNumber(FLAGS_pi); |
| 48 | CHECK(pi_number); |
| 49 | LOG(INFO) << "Pi " << *pi_number; |
| 50 | const aos::Node *const pi_node = aos::configuration::GetNode( |
| 51 | factory.configuration(), absl::StrCat("pi", *pi_number)); |
| 52 | |
| 53 | LOG(INFO) << "roboRIO " << aos::FlatbufferToJson(roborio_node); |
| 54 | LOG(INFO) << "Pi " << aos::FlatbufferToJson(pi_node); |
| 55 | |
| 56 | std::unique_ptr<aos::EventLoop> roborio_event_loop = |
| 57 | factory.MakeEventLoop("calibration", roborio_node); |
| 58 | std::unique_ptr<aos::EventLoop> pi_event_loop = |
| 59 | factory.MakeEventLoop("calibration", pi_node); |
| 60 | |
| 61 | // Now, hook Calibration up to everything. |
| 62 | Calibration extractor(&factory, pi_event_loop.get(), |
| 63 | roborio_event_loop.get(), FLAGS_pi, &data); |
| 64 | |
Austin Schuh | 2895f4c | 2022-02-26 16:38:46 -0800 | [diff] [blame^] | 65 | if (FLAGS_turret) { |
| 66 | aos::NodeEventLoopFactory *roborio_factory = |
| 67 | factory.GetNodeEventLoopFactory(roborio_node->name()->string_view()); |
| 68 | roborio_event_loop->MakeWatcher( |
| 69 | "/superstructure", |
| 70 | [roborio_factory, roborio_event_loop = roborio_event_loop.get(), |
| 71 | &data](const y2020::control_loops::superstructure::Status &status) { |
| 72 | data.AddTurret( |
| 73 | roborio_factory->ToDistributedClock( |
| 74 | roborio_event_loop->context().monotonic_event_time), |
| 75 | Eigen::Vector2d(status.turret()->position(), |
| 76 | status.turret()->velocity())); |
| 77 | }); |
| 78 | } |
| 79 | |
Austin Schuh | bb4aae7 | 2021-10-08 22:12:25 -0700 | [diff] [blame] | 80 | factory.Run(); |
| 81 | |
| 82 | reader.Deregister(); |
| 83 | } |
| 84 | |
| 85 | LOG(INFO) << "Done with event_loop running"; |
| 86 | // And now we have it, we can start processing it. |
milind-u | 8c72d53 | 2021-12-11 15:02:42 -0800 | [diff] [blame] | 87 | |
Austin Schuh | 5b37907 | 2021-12-26 16:01:04 -0800 | [diff] [blame] | 88 | const Eigen::Quaternion<double> nominal_initial_orientation( |
milind-u | e53bf55 | 2021-12-11 14:42:00 -0800 | [diff] [blame] | 89 | frc971::controls::ToQuaternionFromRotationVector( |
| 90 | Eigen::Vector3d(0.0, 0.0, M_PI))); |
Austin Schuh | 2895f4c | 2022-02-26 16:38:46 -0800 | [diff] [blame^] | 91 | const Eigen::Quaternion<double> nominal_pivot_to_camera( |
milind-u | e53bf55 | 2021-12-11 14:42:00 -0800 | [diff] [blame] | 92 | Eigen::AngleAxisd(-0.5 * M_PI, Eigen::Vector3d::UnitX())); |
Austin Schuh | 5b37907 | 2021-12-26 16:01:04 -0800 | [diff] [blame] | 93 | const Eigen::Quaternion<double> nominal_board_to_world( |
| 94 | Eigen::AngleAxisd(0.5 * M_PI, Eigen::Vector3d::UnitX())); |
milind-u | e53bf55 | 2021-12-11 14:42:00 -0800 | [diff] [blame] | 95 | |
Austin Schuh | dcb6b36 | 2022-02-25 18:06:21 -0800 | [diff] [blame] | 96 | CalibrationParameters calibration_parameters; |
| 97 | calibration_parameters.initial_orientation = nominal_initial_orientation; |
Austin Schuh | 2895f4c | 2022-02-26 16:38:46 -0800 | [diff] [blame^] | 98 | calibration_parameters.pivot_to_camera = nominal_pivot_to_camera; |
Austin Schuh | dcb6b36 | 2022-02-25 18:06:21 -0800 | [diff] [blame] | 99 | calibration_parameters.board_to_world = nominal_board_to_world; |
Austin Schuh | 5b37907 | 2021-12-26 16:01:04 -0800 | [diff] [blame] | 100 | |
Austin Schuh | dcb6b36 | 2022-02-25 18:06:21 -0800 | [diff] [blame] | 101 | Solve(data, &calibration_parameters); |
| 102 | LOG(INFO) << "Nominal initial_orientation " |
| 103 | << nominal_initial_orientation.coeffs().transpose(); |
Austin Schuh | 2895f4c | 2022-02-26 16:38:46 -0800 | [diff] [blame^] | 104 | LOG(INFO) << "Nominal pivot_to_camera " |
| 105 | << nominal_pivot_to_camera.coeffs().transpose(); |
milind-u | e53bf55 | 2021-12-11 14:42:00 -0800 | [diff] [blame] | 106 | |
Austin Schuh | 2895f4c | 2022-02-26 16:38:46 -0800 | [diff] [blame^] | 107 | LOG(INFO) << "pivot_to_camera delta " |
Austin Schuh | dcb6b36 | 2022-02-25 18:06:21 -0800 | [diff] [blame] | 108 | << frc971::controls::ToRotationVectorFromQuaternion( |
Austin Schuh | 2895f4c | 2022-02-26 16:38:46 -0800 | [diff] [blame^] | 109 | calibration_parameters.pivot_to_camera * |
| 110 | nominal_pivot_to_camera.inverse()) |
Austin Schuh | dcb6b36 | 2022-02-25 18:06:21 -0800 | [diff] [blame] | 111 | .transpose(); |
| 112 | LOG(INFO) << "board_to_world delta " |
| 113 | << frc971::controls::ToRotationVectorFromQuaternion( |
| 114 | calibration_parameters.board_to_world * |
| 115 | nominal_board_to_world.inverse()) |
| 116 | .transpose(); |
milind-u | e53bf55 | 2021-12-11 14:42:00 -0800 | [diff] [blame] | 117 | |
Austin Schuh | dcb6b36 | 2022-02-25 18:06:21 -0800 | [diff] [blame] | 118 | if (FLAGS_plot) { |
| 119 | Plot(data, calibration_parameters); |
milind-u | 8c72d53 | 2021-12-11 15:02:42 -0800 | [diff] [blame] | 120 | } |
Austin Schuh | bb4aae7 | 2021-10-08 22:12:25 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | } // namespace vision |
| 124 | } // namespace frc971 |
| 125 | |
| 126 | int main(int argc, char **argv) { |
| 127 | aos::InitGoogle(&argc, &argv); |
| 128 | |
| 129 | frc971::vision::Main(argc, argv); |
| 130 | } |