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