Jim Ostrowski | ba2edd1 | 2022-12-03 15:44:37 -0800 | [diff] [blame] | 1 | #include "Eigen/Dense" |
| 2 | #include "Eigen/Geometry" |
| 3 | #include "absl/strings/str_format.h" |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 4 | |
Jim Ostrowski | ba2edd1 | 2022-12-03 15:44:37 -0800 | [diff] [blame] | 5 | #include "aos/events/logging/log_reader.h" |
James Kuszmaul | 969e4ab | 2023-01-28 16:09:19 -0800 | [diff] [blame] | 6 | #include "aos/events/logging/log_writer.h" |
Jim Ostrowski | ba2edd1 | 2022-12-03 15:44:37 -0800 | [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" |
| 11 | #include "frc971/control_loops/quaternion_utils.h" |
| 12 | #include "frc971/vision/extrinsics_calibration.h" |
| 13 | #include "frc971/vision/vision_generated.h" |
| 14 | #include "frc971/wpilib/imu_batch_generated.h" |
Jim Ostrowski | ba2edd1 | 2022-12-03 15:44:37 -0800 | [diff] [blame] | 15 | #include "y2022/control_loops/superstructure/superstructure_status_generated.h" |
| 16 | |
| 17 | DEFINE_string(pi, "pi-7971-2", "Pi name to calibrate."); |
| 18 | DEFINE_bool(plot, false, "Whether to plot the resulting data."); |
Jim Ostrowski | ba2edd1 | 2022-12-03 15:44:37 -0800 | [diff] [blame] | 19 | DEFINE_bool(turret, true, "If true, the camera is on the turret"); |
Milind Upadhyay | c6e42ee | 2022-12-27 00:02:11 -0800 | [diff] [blame] | 20 | DEFINE_string(target_type, "charuco", |
milind-u | 09fb125 | 2023-01-28 19:21:41 -0800 | [diff] [blame] | 21 | "Type of target: aruco|charuco|charuco_diamond"); |
Milind Upadhyay | c6e42ee | 2022-12-27 00:02:11 -0800 | [diff] [blame] | 22 | DEFINE_string(image_channel, "/camera", "Channel to listen for images on"); |
James Kuszmaul | 969e4ab | 2023-01-28 16:09:19 -0800 | [diff] [blame] | 23 | DEFINE_string(output_logs, "/tmp/calibration/", |
| 24 | "Output folder for visualization logs."); |
James Kuszmaul | 7e95881 | 2023-02-11 15:34:31 -0800 | [diff] [blame] | 25 | DEFINE_string(base_intrinsics, "", |
| 26 | "Intrinsics to use for extrinsics calibration."); |
Jim Ostrowski | ba2edd1 | 2022-12-03 15:44:37 -0800 | [diff] [blame] | 27 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 28 | namespace frc971::vision { |
Jim Ostrowski | ba2edd1 | 2022-12-03 15:44:37 -0800 | [diff] [blame] | 29 | namespace chrono = std::chrono; |
| 30 | using aos::distributed_clock; |
| 31 | using aos::monotonic_clock; |
| 32 | |
| 33 | // TODO(austin): Source of IMU data? Is it the same? |
| 34 | // TODO(austin): Intrinsics data? |
| 35 | |
| 36 | void Main(int argc, char **argv) { |
| 37 | CalibrationData data; |
Jim Ostrowski | cb8b408 | 2024-01-21 02:23:46 -0800 | [diff] [blame^] | 38 | std::optional<uint16_t> pi_number = |
| 39 | aos::network::ParsePiOrOrinNumber(FLAGS_pi); |
James Kuszmaul | 969e4ab | 2023-01-28 16:09:19 -0800 | [diff] [blame] | 40 | CHECK(pi_number); |
| 41 | const std::string pi_name = absl::StrCat("pi", *pi_number); |
| 42 | LOG(INFO) << "Pi " << *pi_number; |
| 43 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = [argc, argv, |
| 44 | pi_name]() { |
| 45 | aos::logger::LogReader reader( |
| 46 | aos::logger::SortParts(aos::logger::FindLogs(argc, argv))); |
| 47 | return CalibrationFoxgloveVisualizer::AddVisualizationChannels( |
| 48 | reader.logged_configuration(), |
| 49 | aos::configuration::GetNode(reader.logged_configuration(), pi_name)); |
| 50 | }(); |
Jim Ostrowski | ba2edd1 | 2022-12-03 15:44:37 -0800 | [diff] [blame] | 51 | |
| 52 | { |
| 53 | // Now, accumulate all the data into the data object. |
| 54 | aos::logger::LogReader reader( |
James Kuszmaul | 969e4ab | 2023-01-28 16:09:19 -0800 | [diff] [blame] | 55 | aos::logger::SortParts(aos::logger::FindLogs(argc, argv)), |
| 56 | &config.message()); |
Jim Ostrowski | ba2edd1 | 2022-12-03 15:44:37 -0800 | [diff] [blame] | 57 | |
| 58 | aos::SimulatedEventLoopFactory factory(reader.configuration()); |
| 59 | reader.Register(&factory); |
| 60 | |
| 61 | CHECK(aos::configuration::MultiNode(reader.configuration())); |
| 62 | |
| 63 | // Find the nodes we care about. |
| 64 | const aos::Node *const imu_node = |
| 65 | aos::configuration::GetNode(factory.configuration(), "imu"); |
| 66 | const aos::Node *const roborio_node = |
| 67 | aos::configuration::GetNode(factory.configuration(), "roborio"); |
| 68 | |
James Kuszmaul | 969e4ab | 2023-01-28 16:09:19 -0800 | [diff] [blame] | 69 | const aos::Node *const pi_node = |
| 70 | aos::configuration::GetNode(factory.configuration(), pi_name); |
Jim Ostrowski | ba2edd1 | 2022-12-03 15:44:37 -0800 | [diff] [blame] | 71 | |
| 72 | LOG(INFO) << "imu " << aos::FlatbufferToJson(imu_node); |
| 73 | LOG(INFO) << "roboRIO " << aos::FlatbufferToJson(roborio_node); |
| 74 | LOG(INFO) << "Pi " << aos::FlatbufferToJson(pi_node); |
| 75 | |
| 76 | std::unique_ptr<aos::EventLoop> imu_event_loop = |
| 77 | factory.MakeEventLoop("calibration", imu_node); |
| 78 | std::unique_ptr<aos::EventLoop> roborio_event_loop = |
| 79 | factory.MakeEventLoop("calibration", roborio_node); |
| 80 | std::unique_ptr<aos::EventLoop> pi_event_loop = |
| 81 | factory.MakeEventLoop("calibration", pi_node); |
| 82 | |
James Kuszmaul | 969e4ab | 2023-01-28 16:09:19 -0800 | [diff] [blame] | 83 | std::unique_ptr<aos::EventLoop> logger_loop = |
| 84 | factory.MakeEventLoop("logger", pi_node); |
| 85 | aos::logger::Logger logger(logger_loop.get()); |
| 86 | logger.StartLoggingOnRun(FLAGS_output_logs); |
| 87 | |
Milind Upadhyay | c6e42ee | 2022-12-27 00:02:11 -0800 | [diff] [blame] | 88 | TargetType target_type = TargetType::kCharuco; |
milind-u | 09fb125 | 2023-01-28 19:21:41 -0800 | [diff] [blame] | 89 | if (FLAGS_target_type == "aruco") { |
Milind Upadhyay | c6e42ee | 2022-12-27 00:02:11 -0800 | [diff] [blame] | 90 | target_type = TargetType::kAruco; |
| 91 | } else if (FLAGS_target_type == "charuco") { |
| 92 | target_type = TargetType::kCharuco; |
| 93 | } else if (FLAGS_target_type == "charuco_diamond") { |
| 94 | target_type = TargetType::kCharucoDiamond; |
| 95 | } else { |
| 96 | LOG(FATAL) << "Unknown target type: " << FLAGS_target_type |
milind-u | 09fb125 | 2023-01-28 19:21:41 -0800 | [diff] [blame] | 97 | << ", expected: aruco|charuco|charuco_diamond"; |
Milind Upadhyay | c6e42ee | 2022-12-27 00:02:11 -0800 | [diff] [blame] | 98 | } |
| 99 | |
James Kuszmaul | 7e95881 | 2023-02-11 15:34:31 -0800 | [diff] [blame] | 100 | aos::FlatbufferDetachedBuffer<calibration::CameraCalibration> intrinsics = |
| 101 | aos::JsonFileToFlatbuffer<calibration::CameraCalibration>( |
| 102 | FLAGS_base_intrinsics); |
Jim Ostrowski | ba2edd1 | 2022-12-03 15:44:37 -0800 | [diff] [blame] | 103 | // Now, hook Calibration up to everything. |
| 104 | Calibration extractor(&factory, pi_event_loop.get(), imu_event_loop.get(), |
James Kuszmaul | 7e95881 | 2023-02-11 15:34:31 -0800 | [diff] [blame] | 105 | FLAGS_pi, &intrinsics.message(), target_type, |
| 106 | FLAGS_image_channel, &data); |
Jim Ostrowski | ba2edd1 | 2022-12-03 15:44:37 -0800 | [diff] [blame] | 107 | |
| 108 | if (FLAGS_turret) { |
| 109 | aos::NodeEventLoopFactory *roborio_factory = |
| 110 | factory.GetNodeEventLoopFactory(roborio_node->name()->string_view()); |
| 111 | roborio_event_loop->MakeWatcher( |
| 112 | "/superstructure", |
| 113 | [roborio_factory, roborio_event_loop = roborio_event_loop.get(), |
| 114 | &data](const y2022::control_loops::superstructure::Status &status) { |
| 115 | data.AddTurret( |
| 116 | roborio_factory->ToDistributedClock( |
| 117 | roborio_event_loop->context().monotonic_event_time), |
| 118 | Eigen::Vector2d(status.turret()->position(), |
| 119 | status.turret()->velocity())); |
| 120 | }); |
| 121 | } |
| 122 | |
| 123 | factory.Run(); |
| 124 | |
| 125 | reader.Deregister(); |
| 126 | } |
| 127 | |
| 128 | LOG(INFO) << "Done with event_loop running"; |
| 129 | CHECK(data.imu_samples_size() > 0) << "Didn't get any IMU data"; |
| 130 | CHECK(data.camera_samples_size() > 0) << "Didn't get any camera observations"; |
| 131 | |
| 132 | // And now we have it, we can start processing it. |
| 133 | const Eigen::Quaternion<double> nominal_initial_orientation( |
| 134 | frc971::controls::ToQuaternionFromRotationVector( |
| 135 | Eigen::Vector3d(0.0, 0.0, M_PI))); |
| 136 | const Eigen::Quaternion<double> nominal_pivot_to_camera( |
| 137 | Eigen::AngleAxisd(-0.5 * M_PI, Eigen::Vector3d::UnitX())); |
| 138 | const Eigen::Quaternion<double> nominal_pivot_to_imu( |
| 139 | Eigen::AngleAxisd(0.0, Eigen::Vector3d::UnitX())); |
| 140 | const Eigen::Quaternion<double> nominal_board_to_world( |
| 141 | Eigen::AngleAxisd(0.5 * M_PI, Eigen::Vector3d::UnitX())); |
| 142 | Eigen::Matrix<double, 6, 1> nominal_initial_state = |
| 143 | Eigen::Matrix<double, 6, 1>::Zero(); |
| 144 | // Set x value to 0.5 m (center view on the board) |
| 145 | // nominal_initial_state(0, 0) = 0.5; |
| 146 | // Set y value to -1 m (approx distance from imu to board/world) |
| 147 | nominal_initial_state(1, 0) = -1.0; |
| 148 | |
| 149 | CalibrationParameters calibration_parameters; |
| 150 | calibration_parameters.initial_orientation = nominal_initial_orientation; |
| 151 | calibration_parameters.pivot_to_camera = nominal_pivot_to_camera; |
| 152 | calibration_parameters.pivot_to_imu = nominal_pivot_to_imu; |
| 153 | calibration_parameters.board_to_world = nominal_board_to_world; |
| 154 | calibration_parameters.initial_state = nominal_initial_state; |
| 155 | |
| 156 | // Show the inverse of pivot_to_camera, since camera_to_pivot tells where the |
| 157 | // camera is with respect to the pivot frame |
| 158 | const Eigen::Affine3d nominal_affine_pivot_to_camera = |
| 159 | Eigen::Translation3d(calibration_parameters.pivot_to_camera_translation) * |
| 160 | nominal_pivot_to_camera; |
| 161 | const Eigen::Quaterniond nominal_camera_to_pivot_rotation( |
| 162 | nominal_affine_pivot_to_camera.inverse().rotation()); |
| 163 | const Eigen::Vector3d nominal_camera_to_pivot_translation( |
| 164 | nominal_affine_pivot_to_camera.inverse().translation()); |
| 165 | |
| 166 | if (data.turret_samples_size() > 0) { |
| 167 | LOG(INFO) << "Have turret, so using pivot setup"; |
| 168 | calibration_parameters.has_pivot = true; |
| 169 | } |
| 170 | |
| 171 | LOG(INFO) << "Initial Conditions for solver. Assumes:\n" |
| 172 | << "1) board origin is same as world, but rotated pi/2 about " |
| 173 | "x-axis, so z points out\n" |
| 174 | << "2) pivot origin matches imu origin\n" |
| 175 | << "3) camera is offset from pivot (depends on which camera)"; |
| 176 | |
| 177 | LOG(INFO) |
| 178 | << "Nominal initial_orientation of imu w.r.t. world (angle-axis vector): " |
| 179 | << frc971::controls::ToRotationVectorFromQuaternion( |
| 180 | nominal_initial_orientation) |
| 181 | .transpose(); |
| 182 | LOG(INFO) << "Nominal initial_state: \n" |
| 183 | << "Position: " |
| 184 | << nominal_initial_state.block<3, 1>(0, 0).transpose() << "\n" |
| 185 | << "Velocity: " |
| 186 | << nominal_initial_state.block<3, 1>(3, 0).transpose(); |
| 187 | LOG(INFO) << "Nominal pivot_to_imu (angle-axis vector) " |
| 188 | << frc971::controls::ToRotationVectorFromQuaternion( |
| 189 | calibration_parameters.pivot_to_imu) |
| 190 | .transpose(); |
| 191 | LOG(INFO) << "Nominal pivot_to_imu translation: " |
| 192 | << calibration_parameters.pivot_to_imu_translation.transpose(); |
| 193 | // TODO<Jim>: Might be nice to take out the rotation component that maps into |
| 194 | // camera image coordinates (with x right, y down, z forward) |
| 195 | LOG(INFO) << "Nominal camera_to_pivot (angle-axis vector): " |
| 196 | << frc971::controls::ToRotationVectorFromQuaternion( |
| 197 | nominal_camera_to_pivot_rotation) |
| 198 | .transpose(); |
| 199 | LOG(INFO) << "Nominal camera_to_pivot translation: " |
| 200 | << nominal_camera_to_pivot_translation.transpose(); |
| 201 | |
| 202 | Solve(data, &calibration_parameters); |
| 203 | |
| 204 | LOG(INFO) << "RESULTS OF CALIBRATION SOLVER:"; |
| 205 | LOG(INFO) << "initial_orientation of imu w.r.t. world (angle-axis vector): " |
| 206 | << frc971::controls::ToRotationVectorFromQuaternion( |
| 207 | calibration_parameters.initial_orientation) |
| 208 | .transpose(); |
| 209 | LOG(INFO) |
| 210 | << "initial_state: \n" |
| 211 | << "Position: " |
| 212 | << calibration_parameters.initial_state.block<3, 1>(0, 0).transpose() |
| 213 | << "\n" |
| 214 | << "Velocity: " |
| 215 | << calibration_parameters.initial_state.block<3, 1>(3, 0).transpose(); |
| 216 | |
| 217 | LOG(INFO) << "pivot_to_imu rotation (angle-axis vec) " |
| 218 | << frc971::controls::ToRotationVectorFromQuaternion( |
| 219 | calibration_parameters.pivot_to_imu) |
| 220 | .transpose(); |
| 221 | LOG(INFO) << "pivot_to_imu_translation " |
| 222 | << calibration_parameters.pivot_to_imu_translation.transpose(); |
| 223 | const Eigen::Affine3d affine_pivot_to_camera = |
| 224 | Eigen::Translation3d(calibration_parameters.pivot_to_camera_translation) * |
| 225 | calibration_parameters.pivot_to_camera; |
| 226 | const Eigen::Quaterniond camera_to_pivot_rotation( |
| 227 | affine_pivot_to_camera.inverse().rotation()); |
| 228 | const Eigen::Vector3d camera_to_pivot_translation( |
| 229 | affine_pivot_to_camera.inverse().translation()); |
| 230 | LOG(INFO) << "camera to pivot (angle-axis vec): " |
| 231 | << frc971::controls::ToRotationVectorFromQuaternion( |
| 232 | camera_to_pivot_rotation) |
| 233 | .transpose(); |
| 234 | LOG(INFO) << "camera to pivot translation: " |
| 235 | << camera_to_pivot_translation.transpose(); |
| 236 | LOG(INFO) << "board_to_world (rotation) " |
| 237 | << frc971::controls::ToRotationVectorFromQuaternion( |
| 238 | calibration_parameters.board_to_world) |
| 239 | .transpose(); |
| 240 | LOG(INFO) << "accelerometer bias " |
| 241 | << calibration_parameters.accelerometer_bias.transpose(); |
| 242 | LOG(INFO) << "gyro_bias " << calibration_parameters.gyro_bias.transpose(); |
| 243 | LOG(INFO) << "gravity " << 9.81 * calibration_parameters.gravity_scalar; |
| 244 | |
| 245 | LOG(INFO) << "pivot_to_camera change " |
| 246 | << frc971::controls::ToRotationVectorFromQuaternion( |
| 247 | calibration_parameters.pivot_to_camera * |
| 248 | nominal_pivot_to_camera.inverse()) |
| 249 | .transpose(); |
| 250 | LOG(INFO) << "board_to_world delta " |
| 251 | << frc971::controls::ToRotationVectorFromQuaternion( |
| 252 | calibration_parameters.board_to_world * |
| 253 | nominal_board_to_world.inverse()) |
| 254 | .transpose(); |
| 255 | |
| 256 | if (FLAGS_visualize) { |
| 257 | LOG(INFO) << "Showing visualization"; |
| 258 | Visualize(data, calibration_parameters); |
| 259 | } |
| 260 | |
| 261 | if (FLAGS_plot) { |
| 262 | Plot(data, calibration_parameters); |
| 263 | } |
Milind Upadhyay | c6e42ee | 2022-12-27 00:02:11 -0800 | [diff] [blame] | 264 | } // namespace vision |
Jim Ostrowski | ba2edd1 | 2022-12-03 15:44:37 -0800 | [diff] [blame] | 265 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 266 | } // namespace frc971::vision |
Jim Ostrowski | ba2edd1 | 2022-12-03 15:44:37 -0800 | [diff] [blame] | 267 | |
| 268 | int main(int argc, char **argv) { |
| 269 | aos::InitGoogle(&argc, &argv); |
| 270 | |
| 271 | frc971::vision::Main(argc, argv); |
| 272 | } |