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