James Kuszmaul | 7e95881 | 2023-02-11 15:34:31 -0800 | [diff] [blame] | 1 | #include <cmath> |
James Kuszmaul | 7e95881 | 2023-02-11 15:34:31 -0800 | [diff] [blame] | 2 | #include <regex> |
| 3 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 4 | #include "absl/flags/flag.h" |
James Kuszmaul | 7e95881 | 2023-02-11 15:34:31 -0800 | [diff] [blame] | 5 | #include "absl/strings/str_format.h" |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 6 | #include <opencv2/calib3d.hpp> |
| 7 | #include <opencv2/highgui/highgui.hpp> |
| 8 | #include <opencv2/imgproc.hpp> |
| 9 | |
James Kuszmaul | 7e95881 | 2023-02-11 15:34:31 -0800 | [diff] [blame] | 10 | #include "aos/events/shm_event_loop.h" |
| 11 | #include "aos/init.h" |
| 12 | #include "aos/network/team_number.h" |
| 13 | #include "aos/time/time.h" |
| 14 | #include "aos/util/file.h" |
| 15 | #include "frc971/vision/intrinsics_calibration_lib.h" |
| 16 | |
Jim Ostrowski | b974cca | 2024-01-28 15:07:50 -0800 | [diff] [blame] | 17 | // TODO: Would be nice to remove this, but it depends on year-by-year Constants |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 18 | ABSL_FLAG(std::string, base_intrinsics, "", |
| 19 | "Intrinsics to use for estimating board pose prior to solving " |
| 20 | "for the new intrinsics."); |
| 21 | ABSL_FLAG(std::string, calibration_folder, ".", |
| 22 | "Folder to place calibration files."); |
| 23 | ABSL_FLAG(std::string, camera_id, "", |
| 24 | "Camera ID in format YY-NN-- year and number."); |
| 25 | ABSL_FLAG(std::string, channel, "/camera", "Camera channel to use"); |
| 26 | ABSL_FLAG(std::string, config, "aos_config.json", |
| 27 | "Path to the config file to use."); |
| 28 | ABSL_FLAG(std::string, cpu_name, "", "Pi/Orin name to calibrate."); |
| 29 | ABSL_FLAG(bool, display_undistorted, false, |
| 30 | "If true, display the undistorted image."); |
James Kuszmaul | 7e95881 | 2023-02-11 15:34:31 -0800 | [diff] [blame] | 31 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 32 | namespace frc971::vision { |
James Kuszmaul | 7e95881 | 2023-02-11 15:34:31 -0800 | [diff] [blame] | 33 | namespace { |
| 34 | |
| 35 | void Main() { |
| 36 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 37 | aos::configuration::ReadConfig(absl::GetFlag(FLAGS_config)); |
James Kuszmaul | 7e95881 | 2023-02-11 15:34:31 -0800 | [diff] [blame] | 38 | |
| 39 | aos::ShmEventLoop event_loop(&config.message()); |
| 40 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 41 | std::string hostname = absl::GetFlag(FLAGS_cpu_name); |
James Kuszmaul | 7e95881 | 2023-02-11 15:34:31 -0800 | [diff] [blame] | 42 | if (hostname == "") { |
| 43 | hostname = aos::network::GetHostname(); |
Jim Ostrowski | cb8b408 | 2024-01-21 02:23:46 -0800 | [diff] [blame] | 44 | LOG(INFO) << "Using pi/orin name from hostname as " << hostname; |
James Kuszmaul | 7e95881 | 2023-02-11 15:34:31 -0800 | [diff] [blame] | 45 | } |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 46 | CHECK(!absl::GetFlag(FLAGS_base_intrinsics).empty()) |
James Kuszmaul | 7e95881 | 2023-02-11 15:34:31 -0800 | [diff] [blame] | 47 | << "Need a base intrinsics json to use to auto-capture images when the " |
| 48 | "camera moves."; |
| 49 | std::unique_ptr<aos::ExitHandle> exit_handle = event_loop.MakeExitHandle(); |
Jim Ostrowski | 144ab39 | 2024-03-17 18:41:49 -0700 | [diff] [blame] | 50 | |
Jim Ostrowski | 66044a0 | 2024-03-23 15:51:32 -0700 | [diff] [blame] | 51 | CHECK(aos::network::ParsePiOrOrin(hostname)) |
| 52 | << "Failed to parse node type from " << hostname |
| 53 | << ". Should be of form orin1-971-1"; |
| 54 | CHECK(aos::network::ParsePiOrOrinNumber(hostname)) |
| 55 | << "Failed to parse node number from " << hostname |
| 56 | << ". Should be of form orin2-7971-1"; |
| 57 | |
Jim Ostrowski | 144ab39 | 2024-03-17 18:41:49 -0700 | [diff] [blame] | 58 | std::string camera_name = absl::StrCat( |
| 59 | "/", aos::network::ParsePiOrOrin(hostname).value(), |
| 60 | std::to_string(aos::network::ParsePiOrOrinNumber(hostname).value()), |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 61 | absl::GetFlag(FLAGS_channel)); |
Jim Ostrowski | 144ab39 | 2024-03-17 18:41:49 -0700 | [diff] [blame] | 62 | // THIS IS A HACK FOR 2024, since we call Orin2 "Imu" |
| 63 | if (aos::network::ParsePiOrOrin(hostname).value() == "orin" && |
| 64 | aos::network::ParsePiOrOrinNumber(hostname).value() == 2) { |
| 65 | LOG(INFO) << "\nHACK for 2024: Renaming orin2 to imu\n"; |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 66 | camera_name = absl::StrCat("/imu", absl::GetFlag(FLAGS_channel)); |
Jim Ostrowski | 144ab39 | 2024-03-17 18:41:49 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 69 | IntrinsicsCalibration extractor( |
| 70 | &event_loop, hostname, camera_name, absl::GetFlag(FLAGS_camera_id), |
| 71 | absl::GetFlag(FLAGS_base_intrinsics), |
| 72 | absl::GetFlag(FLAGS_display_undistorted), |
| 73 | absl::GetFlag(FLAGS_calibration_folder), exit_handle.get()); |
James Kuszmaul | 7e95881 | 2023-02-11 15:34:31 -0800 | [diff] [blame] | 74 | |
| 75 | event_loop.Run(); |
| 76 | |
| 77 | extractor.MaybeCalibrate(); |
| 78 | } |
| 79 | |
| 80 | } // namespace |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 81 | } // namespace frc971::vision |
James Kuszmaul | 7e95881 | 2023-02-11 15:34:31 -0800 | [diff] [blame] | 82 | |
| 83 | int main(int argc, char **argv) { |
| 84 | aos::InitGoogle(&argc, &argv); |
| 85 | frc971::vision::Main(); |
| 86 | } |