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; |
Austin Schuh | 2895f4c | 2022-02-26 16:38:46 -0800 | [diff] [blame] | 29 | |
| 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-u | 8c72d53 | 2021-12-11 15:02:42 -0800 | [diff] [blame] | 34 | }; |
| 35 | |
| 36 | // Class to both accumulate and replay camera and IMU data in time order. |
| 37 | class 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 Schuh | 2895f4c | 2022-02-26 16:38:46 -0800 | [diff] [blame] | 48 | // 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-u | 8c72d53 | 2021-12-11 15:02:42 -0800 | [diff] [blame] | 53 | // Processes the data points by calling UpdateCamera and UpdateIMU in time |
| 54 | // order. |
Austin Schuh | dcb6b36 | 2022-02-25 18:06:21 -0800 | [diff] [blame] | 55 | void ReviewData(CalibrationDataObserver *observer) const; |
milind-u | 8c72d53 | 2021-12-11 15:02:42 -0800 | [diff] [blame] | 56 | |
| 57 | size_t camera_samples_size() const { return rot_trans_points_.size(); } |
| 58 | |
Jim Ostrowski | ba2edd1 | 2022-12-03 15:44:37 -0800 | [diff] [blame] | 59 | size_t imu_samples_size() const { return imu_points_.size(); } |
| 60 | |
| 61 | size_t turret_samples_size() const { return turret_points_.size(); } |
Austin Schuh | 2895f4c | 2022-02-26 16:38:46 -0800 | [diff] [blame] | 62 | |
milind-u | 8c72d53 | 2021-12-11 15:02:42 -0800 | [diff] [blame] | 63 | 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 Schuh | 2895f4c | 2022-02-26 16:38:46 -0800 | [diff] [blame] | 73 | |
| 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-u | 8c72d53 | 2021-12-11 15:02:42 -0800 | [diff] [blame] | 77 | }; |
| 78 | |
| 79 | // Class to register image and IMU callbacks in AOS and route them to the |
| 80 | // corresponding CalibrationData class. |
| 81 | class 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 Ostrowski | b3cab97 | 2022-12-03 15:47:00 -0800 | [diff] [blame^] | 87 | // 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-u | 8c72d53 | 2021-12-11 15:02:42 -0800 | [diff] [blame] | 91 | void HandleCharuco(cv::Mat rgb_image, |
| 92 | const aos::monotonic_clock::time_point eof, |
Jim Ostrowski | b3cab97 | 2022-12-03 15:47:00 -0800 | [diff] [blame^] | 93 | 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-u | 8c72d53 | 2021-12-11 15:02:42 -0800 | [diff] [blame] | 97 | |
Jim Ostrowski | b3cab97 | 2022-12-03 15:47:00 -0800 | [diff] [blame^] | 98 | // Processes an IMU reading by storing for later processing |
milind-u | 8c72d53 | 2021-12-11 15:02:42 -0800 | [diff] [blame] | 99 | 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 Ostrowski | b3cab97 | 2022-12-03 15:47:00 -0800 | [diff] [blame^] | 108 | ImageCallback image_callback_; |
milind-u | 8c72d53 | 2021-12-11 15:02:42 -0800 | [diff] [blame] | 109 | |
| 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_ |