blob: 278f14b88c08bc7f8c0bd356e38fd7707c14dfaf [file] [log] [blame]
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -08001#include "aos/events/shm_event_loop.h"
2#include "aos/init.h"
Jim Ostrowskiff7b3de2022-01-22 22:20:26 -08003#include "y2022/vision/camera_reader.h"
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -08004
5// config used to allow running camera_reader independently. E.g.,
Austin Schuhc5fa6d92022-02-25 14:36:28 -08006// bazel run //y2022/vision:camera_reader -- --config y2022/aos_config.json
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -08007// --override_hostname pi-7971-1 --ignore_timestamps true
Milind Upadhyayf67f67d2022-03-27 14:29:47 -07008DECLARE_bool(use_outdoors);
Austin Schuhc5fa6d92022-02-25 14:36:28 -08009DEFINE_string(config, "aos_config.json", "Path to the config file to use.");
Austin Schuh7bae0c32022-04-13 19:46:05 -070010DEFINE_double(duty_cycle, 0.65, "Duty cycle of the LEDs");
Jim Ostrowski1ac95742022-10-01 17:21:11 -070011DEFINE_uint32(exposure, 3,
Jim Ostrowskifec0c332022-02-06 23:28:26 -080012 "Exposure time, in 100us increments; 0 implies auto exposure");
Jim Ostrowski1ac95742022-10-01 17:21:11 -070013DEFINE_uint32(outdoors_exposure, 2,
Milind Upadhyayf67f67d2022-03-27 14:29:47 -070014 "Exposure time when using --use_outdoors, in 100us increments; 0 "
15 "implies auto exposure");
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080016
Stephan Pleinesf63bde82024-01-13 15:59:33 -080017namespace y2022::vision {
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080018namespace {
19
20using namespace frc971::vision;
21
22void CameraReaderMain() {
23 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
24 aos::configuration::ReadConfig(FLAGS_config);
25
Jim Ostrowski007e2ea2022-01-30 13:13:26 -080026 const aos::FlatbufferSpan<calibration::CalibrationData> calibration_data(
27 CalibrationData());
28 CHECK(calibration_data.Verify());
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080029
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080030 aos::ShmEventLoop event_loop(&config.message());
31
32 // First, log the data for future reference.
33 {
Jim Ostrowski007e2ea2022-01-30 13:13:26 -080034 aos::Sender<calibration::CalibrationData> calibration_data_sender =
35 event_loop.MakeSender<calibration::CalibrationData>("/camera");
36 CHECK_EQ(calibration_data_sender.Send(calibration_data),
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080037 aos::RawSender::Error::kOk);
38 }
39
40 V4L2Reader v4l2_reader(&event_loop, "/dev/video0");
Milind Upadhyayf67f67d2022-03-27 14:29:47 -070041 const uint32_t exposure =
42 (FLAGS_use_outdoors ? FLAGS_outdoors_exposure : FLAGS_exposure);
43 if (exposure > 0) {
Jim Ostrowski1ac95742022-10-01 17:21:11 -070044 LOG(INFO) << "Setting camera to Manual Exposure mode with exposure = "
45 << exposure << " or " << static_cast<double>(exposure) / 10.0
46 << " ms";
Milind Upadhyayf67f67d2022-03-27 14:29:47 -070047 v4l2_reader.SetExposure(exposure);
Jim Ostrowski1ac95742022-10-01 17:21:11 -070048 } else {
49 LOG(INFO) << "Setting camera to use Auto Exposure";
50 v4l2_reader.UseAutoExposure();
Jim Ostrowskifec0c332022-02-06 23:28:26 -080051 }
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080052
Jim Ostrowski007e2ea2022-01-30 13:13:26 -080053 CameraReader camera_reader(&event_loop, &calibration_data.message(),
Jim Ostrowskiff7b3de2022-01-22 22:20:26 -080054 &v4l2_reader);
Jim Ostrowski2a483b32022-02-15 18:19:14 -080055 camera_reader.SetDutyCycle(FLAGS_duty_cycle);
Jim Ostrowskiff7b3de2022-01-22 22:20:26 -080056
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080057 event_loop.Run();
58}
59
60} // namespace
Stephan Pleinesf63bde82024-01-13 15:59:33 -080061} // namespace y2022::vision
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080062
63int main(int argc, char **argv) {
64 aos::InitGoogle(&argc, &argv);
65 y2022::vision::CameraReaderMain();
66}