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