Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame^] | 1 | #include "absl/flags/flag.h" |
| 2 | |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 3 | #include "aos/events/shm_event_loop.h" |
| 4 | #include "aos/init.h" |
Jim Ostrowski | ff7b3de | 2022-01-22 22:20:26 -0800 | [diff] [blame] | 5 | #include "y2022/vision/camera_reader.h" |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 6 | |
| 7 | // config used to allow running camera_reader independently. E.g., |
Austin Schuh | c5fa6d9 | 2022-02-25 14:36:28 -0800 | [diff] [blame] | 8 | // bazel run //y2022/vision:camera_reader -- --config y2022/aos_config.json |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 9 | // --override_hostname pi-7971-1 --ignore_timestamps true |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame^] | 10 | ABSL_DECLARE_FLAG(bool, use_outdoors); |
| 11 | ABSL_FLAG(std::string, config, "aos_config.json", |
| 12 | "Path to the config file to use."); |
| 13 | ABSL_FLAG(double, duty_cycle, 0.65, "Duty cycle of the LEDs"); |
| 14 | ABSL_FLAG(uint32_t, exposure, 3, |
| 15 | "Exposure time, in 100us increments; 0 implies auto exposure"); |
| 16 | ABSL_FLAG(uint32_t, outdoors_exposure, 2, |
| 17 | "Exposure time when using --use_outdoors, in 100us increments; 0 " |
| 18 | "implies auto exposure"); |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 19 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 20 | namespace y2022::vision { |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 21 | namespace { |
| 22 | |
| 23 | using namespace frc971::vision; |
| 24 | |
| 25 | void CameraReaderMain() { |
| 26 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame^] | 27 | aos::configuration::ReadConfig(absl::GetFlag(FLAGS_config)); |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 28 | |
Jim Ostrowski | 007e2ea | 2022-01-30 13:13:26 -0800 | [diff] [blame] | 29 | const aos::FlatbufferSpan<calibration::CalibrationData> calibration_data( |
| 30 | CalibrationData()); |
| 31 | CHECK(calibration_data.Verify()); |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 32 | |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 33 | aos::ShmEventLoop event_loop(&config.message()); |
| 34 | |
| 35 | // First, log the data for future reference. |
| 36 | { |
Jim Ostrowski | 007e2ea | 2022-01-30 13:13:26 -0800 | [diff] [blame] | 37 | aos::Sender<calibration::CalibrationData> calibration_data_sender = |
| 38 | event_loop.MakeSender<calibration::CalibrationData>("/camera"); |
| 39 | CHECK_EQ(calibration_data_sender.Send(calibration_data), |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 40 | aos::RawSender::Error::kOk); |
| 41 | } |
| 42 | |
| 43 | V4L2Reader v4l2_reader(&event_loop, "/dev/video0"); |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame^] | 44 | const uint32_t exposure = (absl::GetFlag(FLAGS_use_outdoors) |
| 45 | ? absl::GetFlag(FLAGS_outdoors_exposure) |
| 46 | : absl::GetFlag(FLAGS_exposure)); |
Milind Upadhyay | f67f67d | 2022-03-27 14:29:47 -0700 | [diff] [blame] | 47 | if (exposure > 0) { |
Jim Ostrowski | 1ac9574 | 2022-10-01 17:21:11 -0700 | [diff] [blame] | 48 | LOG(INFO) << "Setting camera to Manual Exposure mode with exposure = " |
| 49 | << exposure << " or " << static_cast<double>(exposure) / 10.0 |
| 50 | << " ms"; |
Milind Upadhyay | f67f67d | 2022-03-27 14:29:47 -0700 | [diff] [blame] | 51 | v4l2_reader.SetExposure(exposure); |
Jim Ostrowski | 1ac9574 | 2022-10-01 17:21:11 -0700 | [diff] [blame] | 52 | } else { |
| 53 | LOG(INFO) << "Setting camera to use Auto Exposure"; |
| 54 | v4l2_reader.UseAutoExposure(); |
Jim Ostrowski | fec0c33 | 2022-02-06 23:28:26 -0800 | [diff] [blame] | 55 | } |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 56 | |
Jim Ostrowski | 007e2ea | 2022-01-30 13:13:26 -0800 | [diff] [blame] | 57 | CameraReader camera_reader(&event_loop, &calibration_data.message(), |
Jim Ostrowski | ff7b3de | 2022-01-22 22:20:26 -0800 | [diff] [blame] | 58 | &v4l2_reader); |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame^] | 59 | camera_reader.SetDutyCycle(absl::GetFlag(FLAGS_duty_cycle)); |
Jim Ostrowski | ff7b3de | 2022-01-22 22:20:26 -0800 | [diff] [blame] | 60 | |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 61 | event_loop.Run(); |
| 62 | } |
| 63 | |
| 64 | } // namespace |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 65 | } // namespace y2022::vision |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 66 | |
| 67 | int main(int argc, char **argv) { |
| 68 | aos::InitGoogle(&argc, &argv); |
| 69 | y2022::vision::CameraReaderMain(); |
| 70 | } |