blob: 6451d0c9b5bc7aaea9b225dfe4604cac1f1bed1c [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
15namespace frc971 {
16namespace vision {
17
Austin Schuh5b379072021-12-26 16:01:04 -080018// This class provides an interface for an application to be notified of all
19// camera and IMU samples in order with the correct timestamps.
milind-u8c72d532021-12-11 15:02:42 -080020class CalibrationDataObserver {
21 public:
Austin Schuh5b379072021-12-26 16:01:04 -080022 // Observes a camera sample at the corresponding time t, and with the
23 // corresponding rotation and translation vectors rt.
milind-u8c72d532021-12-11 15:02:42 -080024 virtual void UpdateCamera(aos::distributed_clock::time_point t,
25 std::pair<Eigen::Vector3d, Eigen::Vector3d> rt) = 0;
26
Austin Schuh5b379072021-12-26 16:01:04 -080027 // Observes an IMU sample at the corresponding time t, and with the
28 // corresponding angular velocity and linear acceleration vectors wa.
milind-u8c72d532021-12-11 15:02:42 -080029 virtual void UpdateIMU(aos::distributed_clock::time_point t,
30 std::pair<Eigen::Vector3d, Eigen::Vector3d> wa) = 0;
Austin Schuh2895f4c2022-02-26 16:38:46 -080031
32 // Observes a turret sample at the corresponding time t, and with the
33 // corresponding state.
34 virtual void UpdateTurret(aos::distributed_clock::time_point t,
35 Eigen::Vector2d state) = 0;
milind-u8c72d532021-12-11 15:02:42 -080036};
37
38// Class to both accumulate and replay camera and IMU data in time order.
39class CalibrationData {
40 public:
41 // Adds a camera/charuco detection to the list at the provided time.
42 // This has only been tested with a charuco board.
43 void AddCameraPose(aos::distributed_clock::time_point distributed_now,
44 Eigen::Vector3d rvec, Eigen::Vector3d tvec);
45
46 // Adds an IMU point to the list at the provided time.
47 void AddImu(aos::distributed_clock::time_point distributed_now,
48 Eigen::Vector3d gyro, Eigen::Vector3d accel);
49
Austin Schuh2895f4c2022-02-26 16:38:46 -080050 // Adds a turret reading (position; velocity) to the list at the provided
51 // time.
52 void AddTurret(aos::distributed_clock::time_point distributed_now,
53 Eigen::Vector2d state);
54
milind-u8c72d532021-12-11 15:02:42 -080055 // Processes the data points by calling UpdateCamera and UpdateIMU in time
56 // order.
Austin Schuhdcb6b362022-02-25 18:06:21 -080057 void ReviewData(CalibrationDataObserver *observer) const;
milind-u8c72d532021-12-11 15:02:42 -080058
59 size_t camera_samples_size() const { return rot_trans_points_.size(); }
60
Jim Ostrowskiba2edd12022-12-03 15:44:37 -080061 size_t imu_samples_size() const { return imu_points_.size(); }
62
63 size_t turret_samples_size() const { return turret_points_.size(); }
Austin Schuh2895f4c2022-02-26 16:38:46 -080064
milind-u8c72d532021-12-11 15:02:42 -080065 private:
66 std::vector<std::pair<aos::distributed_clock::time_point,
67 std::pair<Eigen::Vector3d, Eigen::Vector3d>>>
68 imu_points_;
69
70 // Store pose samples as timestamp, along with
71 // pair of rotation, translation vectors
72 std::vector<std::pair<aos::distributed_clock::time_point,
73 std::pair<Eigen::Vector3d, Eigen::Vector3d>>>
74 rot_trans_points_;
Austin Schuh2895f4c2022-02-26 16:38:46 -080075
76 // Turret state as a timestamp and [x, v].
77 std::vector<std::pair<aos::distributed_clock::time_point, Eigen::Vector2d>>
78 turret_points_;
milind-u8c72d532021-12-11 15:02:42 -080079};
80
James Kuszmaul969e4ab2023-01-28 16:09:19 -080081class CalibrationFoxgloveVisualizer {
82 public:
83 CalibrationFoxgloveVisualizer(aos::EventLoop *event_loop);
84
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,
James Kuszmaul7e958812023-02-11 15:34:31 -0800110 std::string_view pi,
111 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
146} // namespace vision
147} // namespace frc971
148
Milind Upadhyayc6e42ee2022-12-27 00:02:11 -0800149#endif // FRC971_VISION_CALIBRATION_ACCUMULATOR_H_