blob: e0ec38c9fe414f01cca2de6ccfb0db101558b681 [file] [log] [blame]
James Kuszmaul7e958812023-02-11 15:34:31 -08001#include <cmath>
James Kuszmaul7e958812023-02-11 15:34:31 -08002#include <regex>
3
4#include "absl/strings/str_format.h"
Philipp Schrader790cb542023-07-05 21:06:52 -07005#include <opencv2/calib3d.hpp>
6#include <opencv2/highgui/highgui.hpp>
7#include <opencv2/imgproc.hpp>
8
James Kuszmaul7e958812023-02-11 15:34:31 -08009#include "aos/events/shm_event_loop.h"
10#include "aos/init.h"
11#include "aos/network/team_number.h"
12#include "aos/time/time.h"
13#include "aos/util/file.h"
14#include "frc971/vision/intrinsics_calibration_lib.h"
15
Jim Ostrowskib974cca2024-01-28 15:07:50 -080016// TODO: Would be nice to remove this, but it depends on year-by-year Constants
James Kuszmaul7e958812023-02-11 15:34:31 -080017DEFINE_string(base_intrinsics, "",
18 "Intrinsics to use for estimating board pose prior to solving "
19 "for the new intrinsics.");
Jim Ostrowskib974cca2024-01-28 15:07:50 -080020DEFINE_string(calibration_folder, ".", "Folder to place calibration files.");
21DEFINE_string(camera_id, "", "Camera ID in format YY-NN-- year and number.");
22DEFINE_string(channel, "/camera", "Camera channel to use");
23DEFINE_string(config, "aos_config.json", "Path to the config file to use.");
24DEFINE_string(cpu_name, "", "Pi/Orin name to calibrate.");
25DEFINE_bool(display_undistorted, false,
26 "If true, display the undistorted image.");
James Kuszmaul7e958812023-02-11 15:34:31 -080027
Stephan Pleinesf63bde82024-01-13 15:59:33 -080028namespace frc971::vision {
James Kuszmaul7e958812023-02-11 15:34:31 -080029namespace {
30
31void Main() {
32 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
33 aos::configuration::ReadConfig(FLAGS_config);
34
35 aos::ShmEventLoop event_loop(&config.message());
36
Jim Ostrowskib974cca2024-01-28 15:07:50 -080037 std::string hostname = FLAGS_cpu_name;
James Kuszmaul7e958812023-02-11 15:34:31 -080038 if (hostname == "") {
39 hostname = aos::network::GetHostname();
Jim Ostrowskicb8b4082024-01-21 02:23:46 -080040 LOG(INFO) << "Using pi/orin name from hostname as " << hostname;
James Kuszmaul7e958812023-02-11 15:34:31 -080041 }
42 CHECK(!FLAGS_base_intrinsics.empty())
43 << "Need a base intrinsics json to use to auto-capture images when the "
44 "camera moves.";
45 std::unique_ptr<aos::ExitHandle> exit_handle = event_loop.MakeExitHandle();
Jim Ostrowski144ab392024-03-17 18:41:49 -070046
Jim Ostrowski66044a02024-03-23 15:51:32 -070047 CHECK(aos::network::ParsePiOrOrin(hostname))
48 << "Failed to parse node type from " << hostname
49 << ". Should be of form orin1-971-1";
50 CHECK(aos::network::ParsePiOrOrinNumber(hostname))
51 << "Failed to parse node number from " << hostname
52 << ". Should be of form orin2-7971-1";
53
Jim Ostrowski144ab392024-03-17 18:41:49 -070054 std::string camera_name = absl::StrCat(
55 "/", aos::network::ParsePiOrOrin(hostname).value(),
56 std::to_string(aos::network::ParsePiOrOrinNumber(hostname).value()),
57 FLAGS_channel);
58 // THIS IS A HACK FOR 2024, since we call Orin2 "Imu"
59 if (aos::network::ParsePiOrOrin(hostname).value() == "orin" &&
60 aos::network::ParsePiOrOrinNumber(hostname).value() == 2) {
61 LOG(INFO) << "\nHACK for 2024: Renaming orin2 to imu\n";
62 camera_name = absl::StrCat("/imu", FLAGS_channel);
63 }
64
65 IntrinsicsCalibration extractor(&event_loop, hostname, camera_name,
Jim Ostrowskib974cca2024-01-28 15:07:50 -080066 FLAGS_camera_id, FLAGS_base_intrinsics,
67 FLAGS_display_undistorted,
68 FLAGS_calibration_folder, exit_handle.get());
James Kuszmaul7e958812023-02-11 15:34:31 -080069
70 event_loop.Run();
71
72 extractor.MaybeCalibrate();
73}
74
75} // namespace
Stephan Pleinesf63bde82024-01-13 15:59:33 -080076} // namespace frc971::vision
James Kuszmaul7e958812023-02-11 15:34:31 -080077
78int main(int argc, char **argv) {
79 aos::InitGoogle(&argc, &argv);
80 frc971::vision::Main();
81}