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" |
| 14 | #include "frc971/vision/v4l2_reader.h" |
| 15 | #include "frc971/vision/vision_generated.h" |
Jim Ostrowski | 007e2ea | 2022-01-30 13:13:26 -0800 | [diff] [blame] | 16 | #include "y2022/vision/calibration_data.h" |
| 17 | #include "y2022/vision/calibration_generated.h" |
Jim Ostrowski | 2a483b3 | 2022-02-15 18:19:14 -0800 | [diff] [blame] | 18 | #include "y2022/vision/gpio.h" |
milind-u | 9219598 | 2022-01-22 20:29:31 -0800 | [diff] [blame] | 19 | #include "y2022/vision/target_estimate_generated.h" |
Jim Ostrowski | ff7b3de | 2022-01-22 22:20:26 -0800 | [diff] [blame] | 20 | |
| 21 | namespace y2022 { |
| 22 | namespace vision { |
| 23 | |
| 24 | using namespace frc971::vision; |
| 25 | |
Jim Ostrowski | 2a483b3 | 2022-02-15 18:19:14 -0800 | [diff] [blame] | 26 | // TODO<jim>: Probably need to break out LED control to separate process |
| 27 | // TODO<jim>: Need to add sync with camera to strobe lights |
Jim Ostrowski | ff7b3de | 2022-01-22 22:20:26 -0800 | [diff] [blame] | 28 | |
| 29 | class CameraReader { |
| 30 | public: |
Henry Speiser | 1f34eea | 2022-01-30 14:35:21 -0800 | [diff] [blame] | 31 | CameraReader(aos::ShmEventLoop *event_loop, |
Jim Ostrowski | 007e2ea | 2022-01-30 13:13:26 -0800 | [diff] [blame] | 32 | const calibration::CalibrationData *calibration_data, |
| 33 | V4L2Reader *reader) |
Jim Ostrowski | ff7b3de | 2022-01-22 22:20:26 -0800 | [diff] [blame] | 34 | : event_loop_(event_loop), |
Jim Ostrowski | 007e2ea | 2022-01-30 13:13:26 -0800 | [diff] [blame] | 35 | calibration_data_(calibration_data), |
Jim Ostrowski | ff7b3de | 2022-01-22 22:20:26 -0800 | [diff] [blame] | 36 | camera_calibration_(FindCameraCalibration()), |
| 37 | reader_(reader), |
| 38 | image_sender_(event_loop->MakeSender<CameraImage>("/camera")), |
milind-u | 9219598 | 2022-01-22 20:29:31 -0800 | [diff] [blame] | 39 | target_estimate_sender_( |
| 40 | event_loop->MakeSender<TargetEstimate>("/camera")), |
Jim Ostrowski | 2a483b3 | 2022-02-15 18:19:14 -0800 | [diff] [blame] | 41 | read_image_timer_(event_loop->AddTimer([this]() { ReadImage(); })), |
Jim Ostrowski | a49d20e | 2022-02-26 18:34:05 -0800 | [diff] [blame] | 42 | gpio_imu_pin_(GPIOControl(GPIO_PIN_SCLK_IMU, kGPIOIn)), |
Jim Ostrowski | 2a483b3 | 2022-02-15 18:19:14 -0800 | [diff] [blame] | 43 | gpio_pwm_control_(GPIOPWMControl(GPIO_PIN_SCK_PWM, duty_cycle_)), |
| 44 | gpio_disable_control_( |
| 45 | GPIOControl(GPIO_PIN_MOSI_DISABLE, kGPIOOut, kGPIOLow)) { |
Jim Ostrowski | ff7b3de | 2022-01-22 22:20:26 -0800 | [diff] [blame] | 46 | event_loop->OnRun( |
| 47 | [this]() { read_image_timer_->Setup(event_loop_->monotonic_now()); }); |
| 48 | } |
| 49 | |
Jim Ostrowski | 2a483b3 | 2022-02-15 18:19:14 -0800 | [diff] [blame] | 50 | void SetDutyCycle(double duty_cycle) { |
| 51 | duty_cycle_ = duty_cycle; |
| 52 | gpio_pwm_control_.setPWMDutyCycle(duty_cycle_); |
| 53 | } |
| 54 | |
| 55 | double GetDutyCycle() { return duty_cycle_; } |
| 56 | |
Jim Ostrowski | ff7b3de | 2022-01-22 22:20:26 -0800 | [diff] [blame] | 57 | private: |
Jim Ostrowski | 007e2ea | 2022-01-30 13:13:26 -0800 | [diff] [blame] | 58 | const calibration::CameraCalibration *FindCameraCalibration() const; |
Jim Ostrowski | ff7b3de | 2022-01-22 22:20:26 -0800 | [diff] [blame] | 59 | |
| 60 | // Processes an image (including sending the results). |
Jim Ostrowski | 210765a | 2022-02-27 12:52:14 -0800 | [diff] [blame] | 61 | void ProcessImage(cv::Mat image, int64_t image_monotonic_timestamp_ns); |
Jim Ostrowski | ff7b3de | 2022-01-22 22:20:26 -0800 | [diff] [blame] | 62 | |
| 63 | // Reads an image, and then performs all of our processing on it. |
| 64 | void ReadImage(); |
| 65 | |
| 66 | cv::Mat CameraIntrinsics() const { |
| 67 | const cv::Mat result(3, 3, CV_32F, |
| 68 | const_cast<void *>(static_cast<const void *>( |
| 69 | camera_calibration_->intrinsics()->data()))); |
| 70 | CHECK_EQ(result.total(), camera_calibration_->intrinsics()->size()); |
| 71 | return result; |
| 72 | } |
| 73 | |
milind-u | 9219598 | 2022-01-22 20:29:31 -0800 | [diff] [blame] | 74 | cv::Mat CameraExtrinsics() const { |
| 75 | const cv::Mat result( |
| 76 | 4, 4, CV_32F, |
| 77 | const_cast<void *>(static_cast<const void *>( |
| 78 | camera_calibration_->fixed_extrinsics()->data()->data()))); |
| 79 | CHECK_EQ(result.total(), |
| 80 | camera_calibration_->fixed_extrinsics()->data()->size()); |
| 81 | return result; |
| 82 | } |
| 83 | |
Jim Ostrowski | ff7b3de | 2022-01-22 22:20:26 -0800 | [diff] [blame] | 84 | cv::Mat CameraDistCoeffs() const { |
| 85 | const cv::Mat result(5, 1, CV_32F, |
| 86 | const_cast<void *>(static_cast<const void *>( |
| 87 | camera_calibration_->dist_coeffs()->data()))); |
| 88 | CHECK_EQ(result.total(), camera_calibration_->dist_coeffs()->size()); |
| 89 | return result; |
| 90 | } |
| 91 | |
Henry Speiser | 1f34eea | 2022-01-30 14:35:21 -0800 | [diff] [blame] | 92 | aos::ShmEventLoop *const event_loop_; |
Jim Ostrowski | 007e2ea | 2022-01-30 13:13:26 -0800 | [diff] [blame] | 93 | const calibration::CalibrationData *const calibration_data_; |
| 94 | const calibration::CameraCalibration *const camera_calibration_; |
Jim Ostrowski | ff7b3de | 2022-01-22 22:20:26 -0800 | [diff] [blame] | 95 | V4L2Reader *const reader_; |
| 96 | aos::Sender<CameraImage> image_sender_; |
milind-u | 9219598 | 2022-01-22 20:29:31 -0800 | [diff] [blame] | 97 | aos::Sender<TargetEstimate> target_estimate_sender_; |
Jim Ostrowski | ff7b3de | 2022-01-22 22:20:26 -0800 | [diff] [blame] | 98 | |
milind-u | 9219598 | 2022-01-22 20:29:31 -0800 | [diff] [blame] | 99 | // We schedule this immediately to read an image. Having it on a timer |
| 100 | // means other things can run on the event loop in between. |
Jim Ostrowski | ff7b3de | 2022-01-22 22:20:26 -0800 | [diff] [blame] | 101 | aos::TimerHandler *const read_image_timer_; |
Jim Ostrowski | 2a483b3 | 2022-02-15 18:19:14 -0800 | [diff] [blame] | 102 | |
| 103 | double duty_cycle_ = 0.0; |
Jim Ostrowski | a49d20e | 2022-02-26 18:34:05 -0800 | [diff] [blame] | 104 | GPIOControl gpio_imu_pin_; |
Jim Ostrowski | 2a483b3 | 2022-02-15 18:19:14 -0800 | [diff] [blame] | 105 | GPIOPWMControl gpio_pwm_control_; |
| 106 | GPIOControl gpio_disable_control_; |
Jim Ostrowski | ff7b3de | 2022-01-22 22:20:26 -0800 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | } // namespace vision |
| 110 | } // namespace y2022 |
| 111 | #endif // Y2022_VISION_CAMERA_READER_H_ |