Milind Upadhyay | c6e42ee | 2022-12-27 00:02:11 -0800 | [diff] [blame] | 1 | #ifndef FRC971_VISION_CALIBRATION_ACCUMULATOR_H_ |
| 2 | #define FRC971_VISION_CALIBRATION_ACCUMULATOR_H_ |
milind-u | 8c72d53 | 2021-12-11 15:02:42 -0800 | [diff] [blame] | 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" |
James Kuszmaul | 77d536c | 2023-02-11 17:30:59 -0800 | [diff] [blame] | 11 | #include "frc971/vision/foxglove_image_converter_lib.h" |
milind-u | 8c72d53 | 2021-12-11 15:02:42 -0800 | [diff] [blame] | 12 | #include "frc971/wpilib/imu_batch_generated.h" |
milind-u | 8c72d53 | 2021-12-11 15:02:42 -0800 | [diff] [blame] | 13 | |
| 14 | namespace frc971 { |
| 15 | namespace vision { |
| 16 | |
Austin Schuh | 5b37907 | 2021-12-26 16:01:04 -0800 | [diff] [blame] | 17 | // This class provides an interface for an application to be notified of all |
| 18 | // camera and IMU samples in order with the correct timestamps. |
milind-u | 8c72d53 | 2021-12-11 15:02:42 -0800 | [diff] [blame] | 19 | class CalibrationDataObserver { |
| 20 | public: |
Austin Schuh | 5b37907 | 2021-12-26 16:01:04 -0800 | [diff] [blame] | 21 | // Observes a camera sample at the corresponding time t, and with the |
| 22 | // corresponding rotation and translation vectors rt. |
milind-u | 8c72d53 | 2021-12-11 15:02:42 -0800 | [diff] [blame] | 23 | virtual void UpdateCamera(aos::distributed_clock::time_point t, |
| 24 | std::pair<Eigen::Vector3d, Eigen::Vector3d> rt) = 0; |
| 25 | |
Austin Schuh | 5b37907 | 2021-12-26 16:01:04 -0800 | [diff] [blame] | 26 | // Observes an IMU sample at the corresponding time t, and with the |
| 27 | // corresponding angular velocity and linear acceleration vectors wa. |
milind-u | 8c72d53 | 2021-12-11 15:02:42 -0800 | [diff] [blame] | 28 | virtual void UpdateIMU(aos::distributed_clock::time_point t, |
| 29 | std::pair<Eigen::Vector3d, Eigen::Vector3d> wa) = 0; |
Austin Schuh | 2895f4c | 2022-02-26 16:38:46 -0800 | [diff] [blame] | 30 | |
| 31 | // Observes a turret sample at the corresponding time t, and with the |
| 32 | // corresponding state. |
| 33 | virtual void UpdateTurret(aos::distributed_clock::time_point t, |
| 34 | Eigen::Vector2d state) = 0; |
milind-u | 8c72d53 | 2021-12-11 15:02:42 -0800 | [diff] [blame] | 35 | }; |
| 36 | |
| 37 | // Class to both accumulate and replay camera and IMU data in time order. |
| 38 | class CalibrationData { |
| 39 | public: |
| 40 | // Adds a camera/charuco detection to the list at the provided time. |
| 41 | // This has only been tested with a charuco board. |
| 42 | void AddCameraPose(aos::distributed_clock::time_point distributed_now, |
| 43 | Eigen::Vector3d rvec, Eigen::Vector3d tvec); |
| 44 | |
| 45 | // Adds an IMU point to the list at the provided time. |
| 46 | void AddImu(aos::distributed_clock::time_point distributed_now, |
| 47 | Eigen::Vector3d gyro, Eigen::Vector3d accel); |
| 48 | |
Austin Schuh | 2895f4c | 2022-02-26 16:38:46 -0800 | [diff] [blame] | 49 | // Adds a turret reading (position; velocity) to the list at the provided |
| 50 | // time. |
| 51 | void AddTurret(aos::distributed_clock::time_point distributed_now, |
| 52 | Eigen::Vector2d state); |
| 53 | |
milind-u | 8c72d53 | 2021-12-11 15:02:42 -0800 | [diff] [blame] | 54 | // Processes the data points by calling UpdateCamera and UpdateIMU in time |
| 55 | // order. |
Austin Schuh | dcb6b36 | 2022-02-25 18:06:21 -0800 | [diff] [blame] | 56 | void ReviewData(CalibrationDataObserver *observer) const; |
milind-u | 8c72d53 | 2021-12-11 15:02:42 -0800 | [diff] [blame] | 57 | |
| 58 | size_t camera_samples_size() const { return rot_trans_points_.size(); } |
| 59 | |
Jim Ostrowski | ba2edd1 | 2022-12-03 15:44:37 -0800 | [diff] [blame] | 60 | size_t imu_samples_size() const { return imu_points_.size(); } |
| 61 | |
| 62 | size_t turret_samples_size() const { return turret_points_.size(); } |
Austin Schuh | 2895f4c | 2022-02-26 16:38:46 -0800 | [diff] [blame] | 63 | |
milind-u | 8c72d53 | 2021-12-11 15:02:42 -0800 | [diff] [blame] | 64 | private: |
| 65 | std::vector<std::pair<aos::distributed_clock::time_point, |
| 66 | std::pair<Eigen::Vector3d, Eigen::Vector3d>>> |
| 67 | imu_points_; |
| 68 | |
| 69 | // Store pose samples as timestamp, along with |
| 70 | // pair of rotation, translation vectors |
| 71 | std::vector<std::pair<aos::distributed_clock::time_point, |
| 72 | std::pair<Eigen::Vector3d, Eigen::Vector3d>>> |
| 73 | rot_trans_points_; |
Austin Schuh | 2895f4c | 2022-02-26 16:38:46 -0800 | [diff] [blame] | 74 | |
| 75 | // Turret state as a timestamp and [x, v]. |
| 76 | std::vector<std::pair<aos::distributed_clock::time_point, Eigen::Vector2d>> |
| 77 | turret_points_; |
milind-u | 8c72d53 | 2021-12-11 15:02:42 -0800 | [diff] [blame] | 78 | }; |
| 79 | |
James Kuszmaul | 969e4ab | 2023-01-28 16:09:19 -0800 | [diff] [blame] | 80 | class CalibrationFoxgloveVisualizer { |
| 81 | public: |
| 82 | CalibrationFoxgloveVisualizer(aos::EventLoop *event_loop); |
| 83 | |
| 84 | static aos::FlatbufferDetachedBuffer<aos::Configuration> |
| 85 | AddVisualizationChannels(const aos::Configuration *config, |
| 86 | const aos::Node *node); |
| 87 | |
| 88 | void HandleCharuco(const aos::monotonic_clock::time_point eof, |
| 89 | std::vector<std::vector<cv::Point2f>> charuco_corners) { |
| 90 | auto builder = annotations_sender_.MakeBuilder(); |
milind-u | 7aa29e2 | 2023-02-23 20:22:01 -0800 | [diff] [blame] | 91 | builder.CheckOk(builder.Send( |
| 92 | BuildAnnotations(eof, charuco_corners, 2.0, builder.fbb()))); |
James Kuszmaul | 969e4ab | 2023-01-28 16:09:19 -0800 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | private: |
| 96 | aos::EventLoop *event_loop_; |
| 97 | FoxgloveImageConverter image_converter_; |
| 98 | |
| 99 | aos::Sender<foxglove::ImageAnnotations> annotations_sender_; |
| 100 | }; |
| 101 | |
milind-u | 8c72d53 | 2021-12-11 15:02:42 -0800 | [diff] [blame] | 102 | // Class to register image and IMU callbacks in AOS and route them to the |
| 103 | // corresponding CalibrationData class. |
| 104 | class Calibration { |
| 105 | public: |
| 106 | Calibration(aos::SimulatedEventLoopFactory *event_loop_factory, |
| 107 | aos::EventLoop *image_event_loop, aos::EventLoop *imu_event_loop, |
James Kuszmaul | 7e95881 | 2023-02-11 15:34:31 -0800 | [diff] [blame] | 108 | std::string_view pi, |
| 109 | const calibration::CameraCalibration *intrinsics_calibration, |
| 110 | TargetType target_type, std::string_view image_channel, |
| 111 | CalibrationData *data); |
milind-u | 8c72d53 | 2021-12-11 15:02:42 -0800 | [diff] [blame] | 112 | |
Jim Ostrowski | b3cab97 | 2022-12-03 15:47:00 -0800 | [diff] [blame] | 113 | // Processes a charuco detection that is returned from charuco_lib. |
| 114 | // For a valid detection(s), it stores camera observation |
| 115 | // Also optionally displays and saves annotated images based on visualize and |
| 116 | // save_path flags, respectively |
milind-u | 8c72d53 | 2021-12-11 15:02:42 -0800 | [diff] [blame] | 117 | void HandleCharuco(cv::Mat rgb_image, |
| 118 | const aos::monotonic_clock::time_point eof, |
Jim Ostrowski | b3cab97 | 2022-12-03 15:47:00 -0800 | [diff] [blame] | 119 | std::vector<cv::Vec4i> /*charuco_ids*/, |
| 120 | std::vector<std::vector<cv::Point2f>> /*charuco_corners*/, |
| 121 | bool valid, std::vector<Eigen::Vector3d> rvecs_eigen, |
| 122 | std::vector<Eigen::Vector3d> tvecs_eigen); |
milind-u | 8c72d53 | 2021-12-11 15:02:42 -0800 | [diff] [blame] | 123 | |
Jim Ostrowski | b3cab97 | 2022-12-03 15:47:00 -0800 | [diff] [blame] | 124 | // Processes an IMU reading by storing for later processing |
milind-u | 8c72d53 | 2021-12-11 15:02:42 -0800 | [diff] [blame] | 125 | void HandleIMU(const frc971::IMUValues *imu); |
| 126 | |
| 127 | private: |
| 128 | aos::EventLoop *image_event_loop_; |
| 129 | aos::NodeEventLoopFactory *image_factory_; |
| 130 | aos::EventLoop *imu_event_loop_; |
| 131 | aos::NodeEventLoopFactory *imu_factory_; |
| 132 | |
| 133 | CharucoExtractor charuco_extractor_; |
Jim Ostrowski | b3cab97 | 2022-12-03 15:47:00 -0800 | [diff] [blame] | 134 | ImageCallback image_callback_; |
milind-u | 8c72d53 | 2021-12-11 15:02:42 -0800 | [diff] [blame] | 135 | |
| 136 | CalibrationData *data_; |
| 137 | |
James Kuszmaul | 969e4ab | 2023-01-28 16:09:19 -0800 | [diff] [blame] | 138 | std::unique_ptr<aos::EventLoop> visualizer_event_loop_; |
| 139 | CalibrationFoxgloveVisualizer visualizer_; |
| 140 | |
milind-u | 8c72d53 | 2021-12-11 15:02:42 -0800 | [diff] [blame] | 141 | frc971::IMUValuesT last_value_; |
| 142 | }; |
| 143 | |
| 144 | } // namespace vision |
| 145 | } // namespace frc971 |
| 146 | |
Milind Upadhyay | c6e42ee | 2022-12-27 00:02:11 -0800 | [diff] [blame] | 147 | #endif // FRC971_VISION_CALIBRATION_ACCUMULATOR_H_ |