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(); })), |
| 42 | gpio_pwm_control_(GPIOPWMControl(GPIO_PIN_SCK_PWM, duty_cycle_)), |
| 43 | gpio_disable_control_( |
| 44 | GPIOControl(GPIO_PIN_MOSI_DISABLE, kGPIOOut, kGPIOLow)) { |
Jim Ostrowski | ff7b3de | 2022-01-22 22:20:26 -0800 | [diff] [blame] | 45 | event_loop->OnRun( |
| 46 | [this]() { read_image_timer_->Setup(event_loop_->monotonic_now()); }); |
| 47 | } |
| 48 | |
Jim Ostrowski | 2a483b3 | 2022-02-15 18:19:14 -0800 | [diff] [blame^] | 49 | void SetDutyCycle(double duty_cycle) { |
| 50 | duty_cycle_ = duty_cycle; |
| 51 | gpio_pwm_control_.setPWMDutyCycle(duty_cycle_); |
| 52 | } |
| 53 | |
| 54 | double GetDutyCycle() { return duty_cycle_; } |
| 55 | |
Jim Ostrowski | ff7b3de | 2022-01-22 22:20:26 -0800 | [diff] [blame] | 56 | private: |
Jim Ostrowski | 007e2ea | 2022-01-30 13:13:26 -0800 | [diff] [blame] | 57 | const calibration::CameraCalibration *FindCameraCalibration() const; |
Jim Ostrowski | ff7b3de | 2022-01-22 22:20:26 -0800 | [diff] [blame] | 58 | |
| 59 | // Processes an image (including sending the results). |
Milind Upadhyay | 2b4404c | 2022-02-04 21:20:57 -0800 | [diff] [blame] | 60 | void ProcessImage(cv::Mat image); |
Jim Ostrowski | ff7b3de | 2022-01-22 22:20:26 -0800 | [diff] [blame] | 61 | |
| 62 | // Reads an image, and then performs all of our processing on it. |
| 63 | void ReadImage(); |
| 64 | |
| 65 | cv::Mat CameraIntrinsics() const { |
| 66 | const cv::Mat result(3, 3, CV_32F, |
| 67 | const_cast<void *>(static_cast<const void *>( |
| 68 | camera_calibration_->intrinsics()->data()))); |
| 69 | CHECK_EQ(result.total(), camera_calibration_->intrinsics()->size()); |
| 70 | return result; |
| 71 | } |
| 72 | |
milind-u | 9219598 | 2022-01-22 20:29:31 -0800 | [diff] [blame] | 73 | cv::Mat CameraExtrinsics() const { |
| 74 | const cv::Mat result( |
| 75 | 4, 4, CV_32F, |
| 76 | const_cast<void *>(static_cast<const void *>( |
| 77 | camera_calibration_->fixed_extrinsics()->data()->data()))); |
| 78 | CHECK_EQ(result.total(), |
| 79 | camera_calibration_->fixed_extrinsics()->data()->size()); |
| 80 | return result; |
| 81 | } |
| 82 | |
Jim Ostrowski | ff7b3de | 2022-01-22 22:20:26 -0800 | [diff] [blame] | 83 | cv::Mat CameraDistCoeffs() const { |
| 84 | const cv::Mat result(5, 1, CV_32F, |
| 85 | const_cast<void *>(static_cast<const void *>( |
| 86 | camera_calibration_->dist_coeffs()->data()))); |
| 87 | CHECK_EQ(result.total(), camera_calibration_->dist_coeffs()->size()); |
| 88 | return result; |
| 89 | } |
| 90 | |
Henry Speiser | 1f34eea | 2022-01-30 14:35:21 -0800 | [diff] [blame] | 91 | aos::ShmEventLoop *const event_loop_; |
Jim Ostrowski | 007e2ea | 2022-01-30 13:13:26 -0800 | [diff] [blame] | 92 | const calibration::CalibrationData *const calibration_data_; |
| 93 | const calibration::CameraCalibration *const camera_calibration_; |
Jim Ostrowski | ff7b3de | 2022-01-22 22:20:26 -0800 | [diff] [blame] | 94 | V4L2Reader *const reader_; |
| 95 | aos::Sender<CameraImage> image_sender_; |
milind-u | 9219598 | 2022-01-22 20:29:31 -0800 | [diff] [blame] | 96 | aos::Sender<TargetEstimate> target_estimate_sender_; |
Jim Ostrowski | ff7b3de | 2022-01-22 22:20:26 -0800 | [diff] [blame] | 97 | |
milind-u | 9219598 | 2022-01-22 20:29:31 -0800 | [diff] [blame] | 98 | // We schedule this immediately to read an image. Having it on a timer |
| 99 | // means other things can run on the event loop in between. |
Jim Ostrowski | ff7b3de | 2022-01-22 22:20:26 -0800 | [diff] [blame] | 100 | aos::TimerHandler *const read_image_timer_; |
Jim Ostrowski | 2a483b3 | 2022-02-15 18:19:14 -0800 | [diff] [blame^] | 101 | |
| 102 | double duty_cycle_ = 0.0; |
| 103 | GPIOPWMControl gpio_pwm_control_; |
| 104 | GPIOControl gpio_disable_control_; |
Jim Ostrowski | ff7b3de | 2022-01-22 22:20:26 -0800 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | } // namespace vision |
| 108 | } // namespace y2022 |
| 109 | #endif // Y2022_VISION_CAMERA_READER_H_ |