blob: 7bff9f0a5d7b7fceaa0aa7fc1a033e10d4c66267 [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"
10#include "frc971/wpilib/imu_batch_generated.h"
11#include "y2020/vision/charuco_lib.h"
12
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;
29};
30
31// Class to both accumulate and replay camera and IMU data in time order.
32class CalibrationData {
33 public:
34 // Adds a camera/charuco detection to the list at the provided time.
35 // This has only been tested with a charuco board.
36 void AddCameraPose(aos::distributed_clock::time_point distributed_now,
37 Eigen::Vector3d rvec, Eigen::Vector3d tvec);
38
39 // Adds an IMU point to the list at the provided time.
40 void AddImu(aos::distributed_clock::time_point distributed_now,
41 Eigen::Vector3d gyro, Eigen::Vector3d accel);
42
43 // Processes the data points by calling UpdateCamera and UpdateIMU in time
44 // order.
45 void ReviewData(CalibrationDataObserver *observer);
46
47 size_t camera_samples_size() const { return rot_trans_points_.size(); }
48
49 private:
50 std::vector<std::pair<aos::distributed_clock::time_point,
51 std::pair<Eigen::Vector3d, Eigen::Vector3d>>>
52 imu_points_;
53
54 // Store pose samples as timestamp, along with
55 // pair of rotation, translation vectors
56 std::vector<std::pair<aos::distributed_clock::time_point,
57 std::pair<Eigen::Vector3d, Eigen::Vector3d>>>
58 rot_trans_points_;
59};
60
61// Class to register image and IMU callbacks in AOS and route them to the
62// corresponding CalibrationData class.
63class Calibration {
64 public:
65 Calibration(aos::SimulatedEventLoopFactory *event_loop_factory,
66 aos::EventLoop *image_event_loop, aos::EventLoop *imu_event_loop,
67 std::string_view pi, CalibrationData *data);
68
69 // Processes a charuco detection.
70 void HandleCharuco(cv::Mat rgb_image,
71 const aos::monotonic_clock::time_point eof,
72 std::vector<int> /*charuco_ids*/,
73 std::vector<cv::Point2f> /*charuco_corners*/, bool valid,
74 Eigen::Vector3d rvec_eigen, Eigen::Vector3d tvec_eigen);
75
76 // Processes an IMU reading.
77 void HandleIMU(const frc971::IMUValues *imu);
78
79 private:
80 aos::EventLoop *image_event_loop_;
81 aos::NodeEventLoopFactory *image_factory_;
82 aos::EventLoop *imu_event_loop_;
83 aos::NodeEventLoopFactory *imu_factory_;
84
85 CharucoExtractor charuco_extractor_;
86
87 CalibrationData *data_;
88
89 frc971::IMUValuesT last_value_;
90};
91
92} // namespace vision
93} // namespace frc971
94
95#endif // Y2020_VISION_CALIBRATION_ACCUMULATOR_H_