blob: dbed9aedff491d9b2fa98a6f0621e43d75b78ed5 [file] [log] [blame]
Milind Upadhyayc6e42ee2022-12-27 00:02:11 -08001#ifndef FRC971_VISION_CALIBRATION_ACCUMULATOR_H_
2#define FRC971_VISION_CALIBRATION_ACCUMULATOR_H_
milind-u8c72d532021-12-11 15:02:42 -08003
4#include <vector>
5
6#include "Eigen/Dense"
Philipp Schrader790cb542023-07-05 21:06:52 -07007
milind-u8c72d532021-12-11 15:02:42 -08008#include "aos/events/simulated_event_loop.h"
9#include "aos/time/time.h"
10#include "frc971/control_loops/quaternion_utils.h"
Austin Schuhdcb6b362022-02-25 18:06:21 -080011#include "frc971/vision/charuco_lib.h"
James Kuszmaul77d536c2023-02-11 17:30:59 -080012#include "frc971/vision/foxglove_image_converter_lib.h"
milind-u8c72d532021-12-11 15:02:42 -080013#include "frc971/wpilib/imu_batch_generated.h"
milind-u8c72d532021-12-11 15:02:42 -080014
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -080015namespace frc971::vision {
milind-u8c72d532021-12-11 15:02:42 -080016
Austin Schuh5b379072021-12-26 16:01:04 -080017// This class provides an interface for an application to be notified of all
18// camera and IMU samples in order with the correct timestamps.
milind-u8c72d532021-12-11 15:02:42 -080019class CalibrationDataObserver {
20 public:
Austin Schuh5b379072021-12-26 16:01:04 -080021 // Observes a camera sample at the corresponding time t, and with the
22 // corresponding rotation and translation vectors rt.
milind-u8c72d532021-12-11 15:02:42 -080023 virtual void UpdateCamera(aos::distributed_clock::time_point t,
24 std::pair<Eigen::Vector3d, Eigen::Vector3d> rt) = 0;
25
Austin Schuh5b379072021-12-26 16:01:04 -080026 // Observes an IMU sample at the corresponding time t, and with the
27 // corresponding angular velocity and linear acceleration vectors wa.
milind-u8c72d532021-12-11 15:02:42 -080028 virtual void UpdateIMU(aos::distributed_clock::time_point t,
29 std::pair<Eigen::Vector3d, Eigen::Vector3d> wa) = 0;
Austin Schuh2895f4c2022-02-26 16:38:46 -080030
31 // Observes a turret sample at the corresponding time t, and with the
32 // corresponding state.
33 virtual void UpdateTurret(aos::distributed_clock::time_point t,
34 Eigen::Vector2d state) = 0;
milind-u8c72d532021-12-11 15:02:42 -080035};
36
37// Class to both accumulate and replay camera and IMU data in time order.
38class CalibrationData {
39 public:
40 // Adds a camera/charuco detection to the list at the provided time.
41 // This has only been tested with a charuco board.
42 void AddCameraPose(aos::distributed_clock::time_point distributed_now,
43 Eigen::Vector3d rvec, Eigen::Vector3d tvec);
44
45 // Adds an IMU point to the list at the provided time.
46 void AddImu(aos::distributed_clock::time_point distributed_now,
47 Eigen::Vector3d gyro, Eigen::Vector3d accel);
48
Austin Schuh2895f4c2022-02-26 16:38:46 -080049 // Adds a turret reading (position; velocity) to the list at the provided
50 // time.
51 void AddTurret(aos::distributed_clock::time_point distributed_now,
52 Eigen::Vector2d state);
53
milind-u8c72d532021-12-11 15:02:42 -080054 // Processes the data points by calling UpdateCamera and UpdateIMU in time
55 // order.
Austin Schuhdcb6b362022-02-25 18:06:21 -080056 void ReviewData(CalibrationDataObserver *observer) const;
milind-u8c72d532021-12-11 15:02:42 -080057
58 size_t camera_samples_size() const { return rot_trans_points_.size(); }
59
Jim Ostrowskiba2edd12022-12-03 15:44:37 -080060 size_t imu_samples_size() const { return imu_points_.size(); }
61
62 size_t turret_samples_size() const { return turret_points_.size(); }
Austin Schuh2895f4c2022-02-26 16:38:46 -080063
milind-u8c72d532021-12-11 15:02:42 -080064 private:
65 std::vector<std::pair<aos::distributed_clock::time_point,
66 std::pair<Eigen::Vector3d, Eigen::Vector3d>>>
67 imu_points_;
68
69 // Store pose samples as timestamp, along with
70 // pair of rotation, translation vectors
71 std::vector<std::pair<aos::distributed_clock::time_point,
72 std::pair<Eigen::Vector3d, Eigen::Vector3d>>>
73 rot_trans_points_;
Austin Schuh2895f4c2022-02-26 16:38:46 -080074
75 // Turret state as a timestamp and [x, v].
76 std::vector<std::pair<aos::distributed_clock::time_point, Eigen::Vector2d>>
77 turret_points_;
milind-u8c72d532021-12-11 15:02:42 -080078};
79
James Kuszmaul969e4ab2023-01-28 16:09:19 -080080class CalibrationFoxgloveVisualizer {
81 public:
Maxwell Hendersonecc8a7c2024-02-29 20:19:45 -080082 CalibrationFoxgloveVisualizer(aos::EventLoop *event_loop,
83 std::string_view camera_channel = "/camera");
James Kuszmaul969e4ab2023-01-28 16:09:19 -080084
85 static aos::FlatbufferDetachedBuffer<aos::Configuration>
86 AddVisualizationChannels(const aos::Configuration *config,
87 const aos::Node *node);
88
89 void HandleCharuco(const aos::monotonic_clock::time_point eof,
90 std::vector<std::vector<cv::Point2f>> charuco_corners) {
91 auto builder = annotations_sender_.MakeBuilder();
milind-u7aa29e22023-02-23 20:22:01 -080092 builder.CheckOk(builder.Send(
Jim Ostrowski5e2c5e62023-02-26 12:52:56 -080093 BuildAnnotations(builder.fbb(), eof, charuco_corners,
94 std::vector<double>{0.0, 1.0, 0.0, 1.0}, 2.0)));
James Kuszmaul969e4ab2023-01-28 16:09:19 -080095 }
96
97 private:
98 aos::EventLoop *event_loop_;
99 FoxgloveImageConverter image_converter_;
100
101 aos::Sender<foxglove::ImageAnnotations> annotations_sender_;
102};
103
milind-u8c72d532021-12-11 15:02:42 -0800104// Class to register image and IMU callbacks in AOS and route them to the
105// corresponding CalibrationData class.
106class Calibration {
107 public:
108 Calibration(aos::SimulatedEventLoopFactory *event_loop_factory,
109 aos::EventLoop *image_event_loop, aos::EventLoop *imu_event_loop,
Jim Ostrowski3dc21642024-01-22 16:08:40 -0800110 std::string_view hostname,
James Kuszmaul7e958812023-02-11 15:34:31 -0800111 const calibration::CameraCalibration *intrinsics_calibration,
112 TargetType target_type, std::string_view image_channel,
113 CalibrationData *data);
milind-u8c72d532021-12-11 15:02:42 -0800114
Jim Ostrowskib3cab972022-12-03 15:47:00 -0800115 // Processes a charuco detection that is returned from charuco_lib.
116 // For a valid detection(s), it stores camera observation
117 // Also optionally displays and saves annotated images based on visualize and
118 // save_path flags, respectively
milind-u8c72d532021-12-11 15:02:42 -0800119 void HandleCharuco(cv::Mat rgb_image,
120 const aos::monotonic_clock::time_point eof,
Jim Ostrowskib3cab972022-12-03 15:47:00 -0800121 std::vector<cv::Vec4i> /*charuco_ids*/,
122 std::vector<std::vector<cv::Point2f>> /*charuco_corners*/,
123 bool valid, std::vector<Eigen::Vector3d> rvecs_eigen,
124 std::vector<Eigen::Vector3d> tvecs_eigen);
milind-u8c72d532021-12-11 15:02:42 -0800125
Jim Ostrowskib3cab972022-12-03 15:47:00 -0800126 // Processes an IMU reading by storing for later processing
milind-u8c72d532021-12-11 15:02:42 -0800127 void HandleIMU(const frc971::IMUValues *imu);
128
129 private:
130 aos::EventLoop *image_event_loop_;
131 aos::NodeEventLoopFactory *image_factory_;
132 aos::EventLoop *imu_event_loop_;
133 aos::NodeEventLoopFactory *imu_factory_;
134
135 CharucoExtractor charuco_extractor_;
Jim Ostrowskib3cab972022-12-03 15:47:00 -0800136 ImageCallback image_callback_;
milind-u8c72d532021-12-11 15:02:42 -0800137
138 CalibrationData *data_;
139
James Kuszmaul969e4ab2023-01-28 16:09:19 -0800140 std::unique_ptr<aos::EventLoop> visualizer_event_loop_;
141 CalibrationFoxgloveVisualizer visualizer_;
142
milind-u8c72d532021-12-11 15:02:42 -0800143 frc971::IMUValuesT last_value_;
144};
145
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -0800146} // namespace frc971::vision
milind-u8c72d532021-12-11 15:02:42 -0800147
Milind Upadhyayc6e42ee2022-12-27 00:02:11 -0800148#endif // FRC971_VISION_CALIBRATION_ACCUMULATOR_H_