blob: d21f4c695eac35405b78be5d341e2279c7a107da [file] [log] [blame]
milind-u8c72d532021-12-11 15:02:42 -08001#ifndef Y2020_VISION_CALIBRATION_ACCUMULATOR_H_
2#define Y2020_VISION_CALIBRATION_ACCUMULATOR_H_
3
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"
milind-u8c72d532021-12-11 15:02:42 -080011#include "frc971/wpilib/imu_batch_generated.h"
milind-u8c72d532021-12-11 15:02:42 -080012
13namespace frc971 {
14namespace vision {
15
Austin Schuh5b379072021-12-26 16:01:04 -080016// This class provides an interface for an application to be notified of all
17// camera and IMU samples in order with the correct timestamps.
milind-u8c72d532021-12-11 15:02:42 -080018class CalibrationDataObserver {
19 public:
Austin Schuh5b379072021-12-26 16:01:04 -080020 // Observes a camera sample at the corresponding time t, and with the
21 // corresponding rotation and translation vectors rt.
milind-u8c72d532021-12-11 15:02:42 -080022 virtual void UpdateCamera(aos::distributed_clock::time_point t,
23 std::pair<Eigen::Vector3d, Eigen::Vector3d> rt) = 0;
24
Austin Schuh5b379072021-12-26 16:01:04 -080025 // Observes an IMU sample at the corresponding time t, and with the
26 // corresponding angular velocity and linear acceleration vectors wa.
milind-u8c72d532021-12-11 15:02:42 -080027 virtual void UpdateIMU(aos::distributed_clock::time_point t,
28 std::pair<Eigen::Vector3d, Eigen::Vector3d> wa) = 0;
Austin Schuh2895f4c2022-02-26 16:38:46 -080029
30 // Observes a turret sample at the corresponding time t, and with the
31 // corresponding state.
32 virtual void UpdateTurret(aos::distributed_clock::time_point t,
33 Eigen::Vector2d state) = 0;
milind-u8c72d532021-12-11 15:02:42 -080034};
35
36// Class to both accumulate and replay camera and IMU data in time order.
37class CalibrationData {
38 public:
39 // Adds a camera/charuco detection to the list at the provided time.
40 // This has only been tested with a charuco board.
41 void AddCameraPose(aos::distributed_clock::time_point distributed_now,
42 Eigen::Vector3d rvec, Eigen::Vector3d tvec);
43
44 // Adds an IMU point to the list at the provided time.
45 void AddImu(aos::distributed_clock::time_point distributed_now,
46 Eigen::Vector3d gyro, Eigen::Vector3d accel);
47
Austin Schuh2895f4c2022-02-26 16:38:46 -080048 // Adds a turret reading (position; velocity) to the list at the provided
49 // time.
50 void AddTurret(aos::distributed_clock::time_point distributed_now,
51 Eigen::Vector2d state);
52
milind-u8c72d532021-12-11 15:02:42 -080053 // Processes the data points by calling UpdateCamera and UpdateIMU in time
54 // order.
Austin Schuhdcb6b362022-02-25 18:06:21 -080055 void ReviewData(CalibrationDataObserver *observer) const;
milind-u8c72d532021-12-11 15:02:42 -080056
57 size_t camera_samples_size() const { return rot_trans_points_.size(); }
58
Jim Ostrowskiba2edd12022-12-03 15:44:37 -080059 size_t imu_samples_size() const { return imu_points_.size(); }
60
61 size_t turret_samples_size() const { return turret_points_.size(); }
Austin Schuh2895f4c2022-02-26 16:38:46 -080062
milind-u8c72d532021-12-11 15:02:42 -080063 private:
64 std::vector<std::pair<aos::distributed_clock::time_point,
65 std::pair<Eigen::Vector3d, Eigen::Vector3d>>>
66 imu_points_;
67
68 // Store pose samples as timestamp, along with
69 // pair of rotation, translation vectors
70 std::vector<std::pair<aos::distributed_clock::time_point,
71 std::pair<Eigen::Vector3d, Eigen::Vector3d>>>
72 rot_trans_points_;
Austin Schuh2895f4c2022-02-26 16:38:46 -080073
74 // Turret state as a timestamp and [x, v].
75 std::vector<std::pair<aos::distributed_clock::time_point, Eigen::Vector2d>>
76 turret_points_;
milind-u8c72d532021-12-11 15:02:42 -080077};
78
79// Class to register image and IMU callbacks in AOS and route them to the
80// corresponding CalibrationData class.
81class Calibration {
82 public:
83 Calibration(aos::SimulatedEventLoopFactory *event_loop_factory,
84 aos::EventLoop *image_event_loop, aos::EventLoop *imu_event_loop,
85 std::string_view pi, CalibrationData *data);
86
Jim Ostrowskib3cab972022-12-03 15:47:00 -080087 // Processes a charuco detection that is returned from charuco_lib.
88 // For a valid detection(s), it stores camera observation
89 // Also optionally displays and saves annotated images based on visualize and
90 // save_path flags, respectively
milind-u8c72d532021-12-11 15:02:42 -080091 void HandleCharuco(cv::Mat rgb_image,
92 const aos::monotonic_clock::time_point eof,
Jim Ostrowskib3cab972022-12-03 15:47:00 -080093 std::vector<cv::Vec4i> /*charuco_ids*/,
94 std::vector<std::vector<cv::Point2f>> /*charuco_corners*/,
95 bool valid, std::vector<Eigen::Vector3d> rvecs_eigen,
96 std::vector<Eigen::Vector3d> tvecs_eigen);
milind-u8c72d532021-12-11 15:02:42 -080097
Jim Ostrowskib3cab972022-12-03 15:47:00 -080098 // Processes an IMU reading by storing for later processing
milind-u8c72d532021-12-11 15:02:42 -080099 void HandleIMU(const frc971::IMUValues *imu);
100
101 private:
102 aos::EventLoop *image_event_loop_;
103 aos::NodeEventLoopFactory *image_factory_;
104 aos::EventLoop *imu_event_loop_;
105 aos::NodeEventLoopFactory *imu_factory_;
106
107 CharucoExtractor charuco_extractor_;
Jim Ostrowskib3cab972022-12-03 15:47:00 -0800108 ImageCallback image_callback_;
milind-u8c72d532021-12-11 15:02:42 -0800109
110 CalibrationData *data_;
111
112 frc971::IMUValuesT last_value_;
113};
114
115} // namespace vision
116} // namespace frc971
117
118#endif // Y2020_VISION_CALIBRATION_ACCUMULATOR_H_