blob: e4f9c8a49c5764d8a793c1a859984e6e6beee25b [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
Austin Schuh2895f4c2022-02-26 16:38:46 -080059 size_t turret_samples() const { return turret_points_.size(); }
60
milind-u8c72d532021-12-11 15:02:42 -080061 private:
62 std::vector<std::pair<aos::distributed_clock::time_point,
63 std::pair<Eigen::Vector3d, Eigen::Vector3d>>>
64 imu_points_;
65
66 // Store pose samples as timestamp, along with
67 // pair of rotation, translation vectors
68 std::vector<std::pair<aos::distributed_clock::time_point,
69 std::pair<Eigen::Vector3d, Eigen::Vector3d>>>
70 rot_trans_points_;
Austin Schuh2895f4c2022-02-26 16:38:46 -080071
72 // Turret state as a timestamp and [x, v].
73 std::vector<std::pair<aos::distributed_clock::time_point, Eigen::Vector2d>>
74 turret_points_;
milind-u8c72d532021-12-11 15:02:42 -080075};
76
77// Class to register image and IMU callbacks in AOS and route them to the
78// corresponding CalibrationData class.
79class Calibration {
80 public:
81 Calibration(aos::SimulatedEventLoopFactory *event_loop_factory,
82 aos::EventLoop *image_event_loop, aos::EventLoop *imu_event_loop,
83 std::string_view pi, CalibrationData *data);
84
85 // Processes a charuco detection.
86 void HandleCharuco(cv::Mat rgb_image,
87 const aos::monotonic_clock::time_point eof,
88 std::vector<int> /*charuco_ids*/,
89 std::vector<cv::Point2f> /*charuco_corners*/, bool valid,
90 Eigen::Vector3d rvec_eigen, Eigen::Vector3d tvec_eigen);
91
92 // Processes an IMU reading.
93 void HandleIMU(const frc971::IMUValues *imu);
94
95 private:
96 aos::EventLoop *image_event_loop_;
97 aos::NodeEventLoopFactory *image_factory_;
98 aos::EventLoop *imu_event_loop_;
99 aos::NodeEventLoopFactory *imu_factory_;
100
101 CharucoExtractor charuco_extractor_;
102
103 CalibrationData *data_;
104
105 frc971::IMUValuesT last_value_;
106};
107
108} // namespace vision
109} // namespace frc971
110
111#endif // Y2020_VISION_CALIBRATION_ACCUMULATOR_H_