blob: eb5605bf586b256944aef3e093705559406662a7 [file] [log] [blame]
James Kuszmaul7e958812023-02-11 15:34:31 -08001#include <cmath>
James Kuszmaul7e958812023-02-11 15:34:31 -08002#include <regex>
3
Austin Schuh99f7c6a2024-06-25 22:07:44 -07004#include "absl/flags/flag.h"
James Kuszmaul7e958812023-02-11 15:34:31 -08005#include "absl/strings/str_format.h"
Philipp Schrader790cb542023-07-05 21:06:52 -07006#include <opencv2/calib3d.hpp>
7#include <opencv2/highgui/highgui.hpp>
8#include <opencv2/imgproc.hpp>
9
James Kuszmaul7e958812023-02-11 15:34:31 -080010#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 Ostrowskib974cca2024-01-28 15:07:50 -080017// TODO: Would be nice to remove this, but it depends on year-by-year Constants
Austin Schuh99f7c6a2024-06-25 22:07:44 -070018ABSL_FLAG(std::string, base_intrinsics, "",
19 "Intrinsics to use for estimating board pose prior to solving "
20 "for the new intrinsics.");
21ABSL_FLAG(std::string, calibration_folder, ".",
22 "Folder to place calibration files.");
23ABSL_FLAG(std::string, camera_id, "",
24 "Camera ID in format YY-NN-- year and number.");
25ABSL_FLAG(std::string, channel, "/camera", "Camera channel to use");
26ABSL_FLAG(std::string, config, "aos_config.json",
27 "Path to the config file to use.");
28ABSL_FLAG(std::string, cpu_name, "", "Pi/Orin name to calibrate.");
29ABSL_FLAG(bool, display_undistorted, false,
30 "If true, display the undistorted image.");
James Kuszmaul7e958812023-02-11 15:34:31 -080031
Stephan Pleinesf63bde82024-01-13 15:59:33 -080032namespace frc971::vision {
James Kuszmaul7e958812023-02-11 15:34:31 -080033namespace {
34
35void Main() {
36 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
Austin Schuh99f7c6a2024-06-25 22:07:44 -070037 aos::configuration::ReadConfig(absl::GetFlag(FLAGS_config));
James Kuszmaul7e958812023-02-11 15:34:31 -080038
39 aos::ShmEventLoop event_loop(&config.message());
40
Austin Schuh99f7c6a2024-06-25 22:07:44 -070041 std::string hostname = absl::GetFlag(FLAGS_cpu_name);
James Kuszmaul7e958812023-02-11 15:34:31 -080042 if (hostname == "") {
43 hostname = aos::network::GetHostname();
Jim Ostrowskicb8b4082024-01-21 02:23:46 -080044 LOG(INFO) << "Using pi/orin name from hostname as " << hostname;
James Kuszmaul7e958812023-02-11 15:34:31 -080045 }
Austin Schuh99f7c6a2024-06-25 22:07:44 -070046 CHECK(!absl::GetFlag(FLAGS_base_intrinsics).empty())
James Kuszmaul7e958812023-02-11 15:34:31 -080047 << "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 Ostrowski144ab392024-03-17 18:41:49 -070050
Jim Ostrowski66044a02024-03-23 15:51:32 -070051 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 Ostrowski144ab392024-03-17 18:41:49 -070058 std::string camera_name = absl::StrCat(
59 "/", aos::network::ParsePiOrOrin(hostname).value(),
60 std::to_string(aos::network::ParsePiOrOrinNumber(hostname).value()),
Austin Schuh99f7c6a2024-06-25 22:07:44 -070061 absl::GetFlag(FLAGS_channel));
Jim Ostrowski144ab392024-03-17 18:41:49 -070062 // 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 Schuh99f7c6a2024-06-25 22:07:44 -070066 camera_name = absl::StrCat("/imu", absl::GetFlag(FLAGS_channel));
Jim Ostrowski144ab392024-03-17 18:41:49 -070067 }
68
Austin Schuh99f7c6a2024-06-25 22:07:44 -070069 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 Kuszmaul7e958812023-02-11 15:34:31 -080074
75 event_loop.Run();
76
77 extractor.MaybeCalibrate();
78}
79
80} // namespace
Stephan Pleinesf63bde82024-01-13 15:59:33 -080081} // namespace frc971::vision
James Kuszmaul7e958812023-02-11 15:34:31 -080082
83int main(int argc, char **argv) {
84 aos::InitGoogle(&argc, &argv);
85 frc971::vision::Main();
86}