Jim Ostrowski | ff7b3de | 2022-01-22 22:20:26 -0800 | [diff] [blame] | 1 | #ifndef Y2022_VISION_CAMERA_READER_H_ |
| 2 | #define Y2022_VISION_CAMERA_READER_H_ |
| 3 | |
| 4 | #include <math.h> |
| 5 | |
| 6 | #include <opencv2/calib3d.hpp> |
| 7 | #include <opencv2/features2d.hpp> |
Henry Speiser | 1f34eea | 2022-01-30 14:35:21 -0800 | [diff] [blame] | 8 | #include <opencv2/imgcodecs.hpp> |
Jim Ostrowski | ff7b3de | 2022-01-22 22:20:26 -0800 | [diff] [blame] | 9 | #include <opencv2/imgproc.hpp> |
| 10 | |
Henry Speiser | 1f34eea | 2022-01-30 14:35:21 -0800 | [diff] [blame] | 11 | #include "aos/events/shm_event_loop.h" |
Jim Ostrowski | ff7b3de | 2022-01-22 22:20:26 -0800 | [diff] [blame] | 12 | #include "aos/flatbuffer_merge.h" |
| 13 | #include "aos/network/team_number.h" |
James Kuszmaul | e3df1ed | 2023-02-20 16:21:17 -0800 | [diff] [blame] | 14 | #include "frc971/control_loops/drivetrain/localization/localizer_output_generated.h" |
milind-u | 2f101fc | 2023-01-21 12:28:49 -0800 | [diff] [blame] | 15 | #include "frc971/vision/calibration_generated.h" |
Jim Ostrowski | ff7b3de | 2022-01-22 22:20:26 -0800 | [diff] [blame] | 16 | #include "frc971/vision/v4l2_reader.h" |
| 17 | #include "frc971/vision/vision_generated.h" |
Jim Ostrowski | 007e2ea | 2022-01-30 13:13:26 -0800 | [diff] [blame] | 18 | #include "y2022/vision/calibration_data.h" |
Jim Ostrowski | 2a483b3 | 2022-02-15 18:19:14 -0800 | [diff] [blame] | 19 | #include "y2022/vision/gpio.h" |
milind-u | 9219598 | 2022-01-22 20:29:31 -0800 | [diff] [blame] | 20 | #include "y2022/vision/target_estimate_generated.h" |
Milind Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame] | 21 | #include "y2022/vision/target_estimator.h" |
Jim Ostrowski | ff7b3de | 2022-01-22 22:20:26 -0800 | [diff] [blame] | 22 | |
| 23 | namespace y2022 { |
| 24 | namespace vision { |
| 25 | |
| 26 | using namespace frc971::vision; |
Milind Upadhyay | d67e9cf | 2022-03-13 13:56:57 -0700 | [diff] [blame] | 27 | using frc971::controls::LedOutput; |
Jim Ostrowski | ff7b3de | 2022-01-22 22:20:26 -0800 | [diff] [blame] | 28 | |
Jim Ostrowski | 2a483b3 | 2022-02-15 18:19:14 -0800 | [diff] [blame] | 29 | // TODO<jim>: Probably need to break out LED control to separate process |
Jim Ostrowski | ff7b3de | 2022-01-22 22:20:26 -0800 | [diff] [blame] | 30 | class CameraReader { |
| 31 | public: |
Milind Upadhyay | a31f027 | 2022-04-03 13:55:22 -0700 | [diff] [blame] | 32 | static const calibration::CameraCalibration *FindCameraCalibration( |
| 33 | const calibration::CalibrationData *calibration_data, |
| 34 | std::string_view node_name, int team_number); |
| 35 | |
| 36 | static cv::Mat CameraIntrinsics( |
| 37 | const calibration::CameraCalibration *camera_calibration) { |
| 38 | cv::Mat result(3, 3, CV_32F, |
| 39 | const_cast<void *>(static_cast<const void *>( |
| 40 | camera_calibration->intrinsics()->data()))); |
| 41 | result.convertTo(result, CV_64F); |
| 42 | CHECK_EQ(result.total(), camera_calibration->intrinsics()->size()); |
| 43 | return result; |
| 44 | } |
| 45 | |
| 46 | static cv::Mat CameraExtrinsics( |
| 47 | const calibration::CameraCalibration *camera_calibration) { |
| 48 | // TODO(james): What's the principled way to handle non-z-axis turrets? |
| 49 | const frc971::vision::calibration::TransformationMatrix *transform = |
| 50 | camera_calibration->has_turret_extrinsics() |
| 51 | ? camera_calibration->turret_extrinsics() |
| 52 | : camera_calibration->fixed_extrinsics(); |
| 53 | |
| 54 | cv::Mat result(4, 4, CV_32F, |
| 55 | const_cast<void *>( |
| 56 | static_cast<const void *>(transform->data()->data()))); |
| 57 | result.convertTo(result, CV_64F); |
| 58 | CHECK_EQ(result.total(), transform->data()->size()); |
| 59 | return result; |
| 60 | } |
| 61 | |
| 62 | static cv::Mat CameraDistCoeffs( |
| 63 | const calibration::CameraCalibration *camera_calibration) { |
| 64 | const cv::Mat result(5, 1, CV_32F, |
| 65 | const_cast<void *>(static_cast<const void *>( |
| 66 | camera_calibration->dist_coeffs()->data()))); |
| 67 | CHECK_EQ(result.total(), camera_calibration->dist_coeffs()->size()); |
| 68 | return result; |
| 69 | } |
| 70 | |
| 71 | static std::pair<cv::Mat, cv::Mat> ComputeUndistortMaps( |
| 72 | const cv::Mat intrinsics, const cv::Mat dist_coeffs) { |
| 73 | std::pair<cv::Mat, cv::Mat> undistort_maps; |
| 74 | static const cv::Size kImageSize = {640, 480}; |
| 75 | cv::initUndistortRectifyMap(intrinsics, dist_coeffs, cv::Mat(), intrinsics, |
| 76 | kImageSize, CV_16SC2, undistort_maps.first, |
| 77 | undistort_maps.second); |
| 78 | return undistort_maps; |
| 79 | } |
| 80 | |
| 81 | static cv::Mat UndistortImage(cv::Mat image_distorted, |
| 82 | std::pair<cv::Mat, cv::Mat> undistort_maps) { |
| 83 | cv::Mat image; |
| 84 | cv::remap(image_distorted, image, undistort_maps.first, |
| 85 | undistort_maps.second, cv::INTER_LINEAR); |
| 86 | return image; |
| 87 | } |
| 88 | |
Henry Speiser | 1f34eea | 2022-01-30 14:35:21 -0800 | [diff] [blame] | 89 | CameraReader(aos::ShmEventLoop *event_loop, |
Jim Ostrowski | 007e2ea | 2022-01-30 13:13:26 -0800 | [diff] [blame] | 90 | const calibration::CalibrationData *calibration_data, |
| 91 | V4L2Reader *reader) |
Jim Ostrowski | ff7b3de | 2022-01-22 22:20:26 -0800 | [diff] [blame] | 92 | : event_loop_(event_loop), |
Jim Ostrowski | 007e2ea | 2022-01-30 13:13:26 -0800 | [diff] [blame] | 93 | calibration_data_(calibration_data), |
Milind Upadhyay | a31f027 | 2022-04-03 13:55:22 -0700 | [diff] [blame] | 94 | camera_calibration_(FindCameraCalibration( |
| 95 | calibration_data_, event_loop_->node()->name()->string_view(), |
| 96 | aos::network::GetTeamNumber())), |
| 97 | undistort_maps_( |
| 98 | ComputeUndistortMaps(CameraIntrinsics(), CameraDistCoeffs())), |
Jim Ostrowski | ff7b3de | 2022-01-22 22:20:26 -0800 | [diff] [blame] | 99 | reader_(reader), |
| 100 | image_sender_(event_loop->MakeSender<CameraImage>("/camera")), |
Milind Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame] | 101 | target_estimator_(CameraIntrinsics(), CameraExtrinsics()), |
milind-u | 9219598 | 2022-01-22 20:29:31 -0800 | [diff] [blame] | 102 | target_estimate_sender_( |
| 103 | event_loop->MakeSender<TargetEstimate>("/camera")), |
Milind Upadhyay | d67e9cf | 2022-03-13 13:56:57 -0700 | [diff] [blame] | 104 | localizer_output_fetcher_( |
| 105 | event_loop->MakeFetcher<frc971::controls::LocalizerOutput>( |
| 106 | "/localizer")), |
Jim Ostrowski | 2a483b3 | 2022-02-15 18:19:14 -0800 | [diff] [blame] | 107 | read_image_timer_(event_loop->AddTimer([this]() { ReadImage(); })), |
Jim Ostrowski | a49d20e | 2022-02-26 18:34:05 -0800 | [diff] [blame] | 108 | gpio_imu_pin_(GPIOControl(GPIO_PIN_SCLK_IMU, kGPIOIn)), |
Jim Ostrowski | 2a483b3 | 2022-02-15 18:19:14 -0800 | [diff] [blame] | 109 | gpio_pwm_control_(GPIOPWMControl(GPIO_PIN_SCK_PWM, duty_cycle_)), |
| 110 | gpio_disable_control_( |
| 111 | GPIOControl(GPIO_PIN_MOSI_DISABLE, kGPIOOut, kGPIOLow)) { |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame^] | 112 | event_loop->OnRun([this]() { |
| 113 | read_image_timer_->Schedule(event_loop_->monotonic_now()); |
| 114 | }); |
Jim Ostrowski | ff7b3de | 2022-01-22 22:20:26 -0800 | [diff] [blame] | 115 | } |
| 116 | |
Jim Ostrowski | 2a483b3 | 2022-02-15 18:19:14 -0800 | [diff] [blame] | 117 | void SetDutyCycle(double duty_cycle) { |
| 118 | duty_cycle_ = duty_cycle; |
| 119 | gpio_pwm_control_.setPWMDutyCycle(duty_cycle_); |
| 120 | } |
| 121 | |
| 122 | double GetDutyCycle() { return duty_cycle_; } |
| 123 | |
Jim Ostrowski | ff7b3de | 2022-01-22 22:20:26 -0800 | [diff] [blame] | 124 | private: |
Jim Ostrowski | ff7b3de | 2022-01-22 22:20:26 -0800 | [diff] [blame] | 125 | // Processes an image (including sending the results). |
Milind Upadhyay | 3c1a5c0 | 2022-03-27 16:27:19 -0700 | [diff] [blame] | 126 | void ProcessImage(cv::Mat image_mat_distorted, |
| 127 | int64_t image_monotonic_timestamp_ns); |
Jim Ostrowski | ff7b3de | 2022-01-22 22:20:26 -0800 | [diff] [blame] | 128 | |
| 129 | // Reads an image, and then performs all of our processing on it. |
| 130 | void ReadImage(); |
| 131 | |
| 132 | cv::Mat CameraIntrinsics() const { |
Milind Upadhyay | a31f027 | 2022-04-03 13:55:22 -0700 | [diff] [blame] | 133 | return CameraIntrinsics(camera_calibration_); |
Jim Ostrowski | ff7b3de | 2022-01-22 22:20:26 -0800 | [diff] [blame] | 134 | } |
| 135 | |
milind-u | 9219598 | 2022-01-22 20:29:31 -0800 | [diff] [blame] | 136 | cv::Mat CameraExtrinsics() const { |
Milind Upadhyay | a31f027 | 2022-04-03 13:55:22 -0700 | [diff] [blame] | 137 | return CameraExtrinsics(camera_calibration_); |
milind-u | 9219598 | 2022-01-22 20:29:31 -0800 | [diff] [blame] | 138 | } |
| 139 | |
Jim Ostrowski | ff7b3de | 2022-01-22 22:20:26 -0800 | [diff] [blame] | 140 | cv::Mat CameraDistCoeffs() const { |
Milind Upadhyay | a31f027 | 2022-04-03 13:55:22 -0700 | [diff] [blame] | 141 | return CameraDistCoeffs(camera_calibration_); |
Jim Ostrowski | ff7b3de | 2022-01-22 22:20:26 -0800 | [diff] [blame] | 142 | } |
| 143 | |
Henry Speiser | 1f34eea | 2022-01-30 14:35:21 -0800 | [diff] [blame] | 144 | aos::ShmEventLoop *const event_loop_; |
Jim Ostrowski | 007e2ea | 2022-01-30 13:13:26 -0800 | [diff] [blame] | 145 | const calibration::CalibrationData *const calibration_data_; |
| 146 | const calibration::CameraCalibration *const camera_calibration_; |
Milind Upadhyay | a31f027 | 2022-04-03 13:55:22 -0700 | [diff] [blame] | 147 | std::pair<cv::Mat, cv::Mat> undistort_maps_; |
Jim Ostrowski | ff7b3de | 2022-01-22 22:20:26 -0800 | [diff] [blame] | 148 | V4L2Reader *const reader_; |
| 149 | aos::Sender<CameraImage> image_sender_; |
Milind Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame] | 150 | TargetEstimator target_estimator_; |
milind-u | 9219598 | 2022-01-22 20:29:31 -0800 | [diff] [blame] | 151 | aos::Sender<TargetEstimate> target_estimate_sender_; |
Jim Ostrowski | ff7b3de | 2022-01-22 22:20:26 -0800 | [diff] [blame] | 152 | |
Milind Upadhyay | d67e9cf | 2022-03-13 13:56:57 -0700 | [diff] [blame] | 153 | LedOutput prev_led_output_ = LedOutput::ON; |
| 154 | aos::Fetcher<frc971::controls::LocalizerOutput> localizer_output_fetcher_; |
| 155 | |
milind-u | 9219598 | 2022-01-22 20:29:31 -0800 | [diff] [blame] | 156 | // We schedule this immediately to read an image. Having it on a timer |
| 157 | // means other things can run on the event loop in between. |
Jim Ostrowski | ff7b3de | 2022-01-22 22:20:26 -0800 | [diff] [blame] | 158 | aos::TimerHandler *const read_image_timer_; |
Jim Ostrowski | 2a483b3 | 2022-02-15 18:19:14 -0800 | [diff] [blame] | 159 | |
| 160 | double duty_cycle_ = 0.0; |
Jim Ostrowski | a49d20e | 2022-02-26 18:34:05 -0800 | [diff] [blame] | 161 | GPIOControl gpio_imu_pin_; |
Jim Ostrowski | 2a483b3 | 2022-02-15 18:19:14 -0800 | [diff] [blame] | 162 | GPIOPWMControl gpio_pwm_control_; |
| 163 | GPIOControl gpio_disable_control_; |
Jim Ostrowski | ff7b3de | 2022-01-22 22:20:26 -0800 | [diff] [blame] | 164 | }; |
| 165 | |
| 166 | } // namespace vision |
| 167 | } // namespace y2022 |
| 168 | #endif // Y2022_VISION_CAMERA_READER_H_ |