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