milind-u | 8c72d53 | 2021-12-11 15:02:42 -0800 | [diff] [blame] | 1 | #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 Schuh | dcb6b36 | 2022-02-25 18:06:21 -0800 | [diff] [blame^] | 10 | #include "frc971/vision/charuco_lib.h" |
milind-u | 8c72d53 | 2021-12-11 15:02:42 -0800 | [diff] [blame] | 11 | #include "frc971/wpilib/imu_batch_generated.h" |
milind-u | 8c72d53 | 2021-12-11 15:02:42 -0800 | [diff] [blame] | 12 | |
| 13 | namespace frc971 { |
| 14 | namespace vision { |
| 15 | |
Austin Schuh | 5b37907 | 2021-12-26 16:01:04 -0800 | [diff] [blame] | 16 | // 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-u | 8c72d53 | 2021-12-11 15:02:42 -0800 | [diff] [blame] | 18 | class CalibrationDataObserver { |
| 19 | public: |
Austin Schuh | 5b37907 | 2021-12-26 16:01:04 -0800 | [diff] [blame] | 20 | // Observes a camera sample at the corresponding time t, and with the |
| 21 | // corresponding rotation and translation vectors rt. |
milind-u | 8c72d53 | 2021-12-11 15:02:42 -0800 | [diff] [blame] | 22 | virtual void UpdateCamera(aos::distributed_clock::time_point t, |
| 23 | std::pair<Eigen::Vector3d, Eigen::Vector3d> rt) = 0; |
| 24 | |
Austin Schuh | 5b37907 | 2021-12-26 16:01:04 -0800 | [diff] [blame] | 25 | // Observes an IMU sample at the corresponding time t, and with the |
| 26 | // corresponding angular velocity and linear acceleration vectors wa. |
milind-u | 8c72d53 | 2021-12-11 15:02:42 -0800 | [diff] [blame] | 27 | 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. |
| 32 | class 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. |
Austin Schuh | dcb6b36 | 2022-02-25 18:06:21 -0800 | [diff] [blame^] | 45 | void ReviewData(CalibrationDataObserver *observer) const; |
milind-u | 8c72d53 | 2021-12-11 15:02:42 -0800 | [diff] [blame] | 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. |
| 63 | class 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_ |