blob: 4b5940685ba6785e973e8dbb88db048fdd6f00e1 [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"
7#include "aos/events/simulated_event_loop.h"
8#include "aos/time/time.h"
9#include "frc971/control_loops/quaternion_utils.h"
Austin Schuhdcb6b362022-02-25 18:06:21 -080010#include "frc971/vision/charuco_lib.h"
James Kuszmaul77d536c2023-02-11 17:30:59 -080011#include "frc971/vision/foxglove_image_converter_lib.h"
milind-u8c72d532021-12-11 15:02:42 -080012#include "frc971/wpilib/imu_batch_generated.h"
milind-u8c72d532021-12-11 15:02:42 -080013
14namespace frc971 {
15namespace vision {
16
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:
82 CalibrationFoxgloveVisualizer(aos::EventLoop *event_loop);
83
84 static aos::FlatbufferDetachedBuffer<aos::Configuration>
85 AddVisualizationChannels(const aos::Configuration *config,
86 const aos::Node *node);
87
88 void HandleCharuco(const aos::monotonic_clock::time_point eof,
89 std::vector<std::vector<cv::Point2f>> charuco_corners) {
90 auto builder = annotations_sender_.MakeBuilder();
milind-u7aa29e22023-02-23 20:22:01 -080091 builder.CheckOk(builder.Send(
92 BuildAnnotations(eof, charuco_corners, 2.0, builder.fbb())));
James Kuszmaul969e4ab2023-01-28 16:09:19 -080093 }
94
95 private:
96 aos::EventLoop *event_loop_;
97 FoxgloveImageConverter image_converter_;
98
99 aos::Sender<foxglove::ImageAnnotations> annotations_sender_;
100};
101
milind-u8c72d532021-12-11 15:02:42 -0800102// Class to register image and IMU callbacks in AOS and route them to the
103// corresponding CalibrationData class.
104class Calibration {
105 public:
106 Calibration(aos::SimulatedEventLoopFactory *event_loop_factory,
107 aos::EventLoop *image_event_loop, aos::EventLoop *imu_event_loop,
James Kuszmaul7e958812023-02-11 15:34:31 -0800108 std::string_view pi,
109 const calibration::CameraCalibration *intrinsics_calibration,
110 TargetType target_type, std::string_view image_channel,
111 CalibrationData *data);
milind-u8c72d532021-12-11 15:02:42 -0800112
Jim Ostrowskib3cab972022-12-03 15:47:00 -0800113 // Processes a charuco detection that is returned from charuco_lib.
114 // For a valid detection(s), it stores camera observation
115 // Also optionally displays and saves annotated images based on visualize and
116 // save_path flags, respectively
milind-u8c72d532021-12-11 15:02:42 -0800117 void HandleCharuco(cv::Mat rgb_image,
118 const aos::monotonic_clock::time_point eof,
Jim Ostrowskib3cab972022-12-03 15:47:00 -0800119 std::vector<cv::Vec4i> /*charuco_ids*/,
120 std::vector<std::vector<cv::Point2f>> /*charuco_corners*/,
121 bool valid, std::vector<Eigen::Vector3d> rvecs_eigen,
122 std::vector<Eigen::Vector3d> tvecs_eigen);
milind-u8c72d532021-12-11 15:02:42 -0800123
Jim Ostrowskib3cab972022-12-03 15:47:00 -0800124 // Processes an IMU reading by storing for later processing
milind-u8c72d532021-12-11 15:02:42 -0800125 void HandleIMU(const frc971::IMUValues *imu);
126
127 private:
128 aos::EventLoop *image_event_loop_;
129 aos::NodeEventLoopFactory *image_factory_;
130 aos::EventLoop *imu_event_loop_;
131 aos::NodeEventLoopFactory *imu_factory_;
132
133 CharucoExtractor charuco_extractor_;
Jim Ostrowskib3cab972022-12-03 15:47:00 -0800134 ImageCallback image_callback_;
milind-u8c72d532021-12-11 15:02:42 -0800135
136 CalibrationData *data_;
137
James Kuszmaul969e4ab2023-01-28 16:09:19 -0800138 std::unique_ptr<aos::EventLoop> visualizer_event_loop_;
139 CalibrationFoxgloveVisualizer visualizer_;
140
milind-u8c72d532021-12-11 15:02:42 -0800141 frc971::IMUValuesT last_value_;
142};
143
144} // namespace vision
145} // namespace frc971
146
Milind Upadhyayc6e42ee2022-12-27 00:02:11 -0800147#endif // FRC971_VISION_CALIBRATION_ACCUMULATOR_H_