blob: 80b19d2c21281eb3fd78562948ca8478708cf2a8 [file] [log] [blame]
Austin Schuh99f7c6a2024-06-25 22:07:44 -07001#include "absl/flags/flag.h"
2
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -08003#include "aos/events/shm_event_loop.h"
4#include "aos/init.h"
Jim Ostrowskiff7b3de2022-01-22 22:20:26 -08005#include "y2022/vision/camera_reader.h"
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -08006
7// config used to allow running camera_reader independently. E.g.,
Austin Schuhc5fa6d92022-02-25 14:36:28 -08008// bazel run //y2022/vision:camera_reader -- --config y2022/aos_config.json
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -08009// --override_hostname pi-7971-1 --ignore_timestamps true
Austin Schuh99f7c6a2024-06-25 22:07:44 -070010ABSL_DECLARE_FLAG(bool, use_outdoors);
11ABSL_FLAG(std::string, config, "aos_config.json",
12 "Path to the config file to use.");
13ABSL_FLAG(double, duty_cycle, 0.65, "Duty cycle of the LEDs");
14ABSL_FLAG(uint32_t, exposure, 3,
15 "Exposure time, in 100us increments; 0 implies auto exposure");
16ABSL_FLAG(uint32_t, outdoors_exposure, 2,
17 "Exposure time when using --use_outdoors, in 100us increments; 0 "
18 "implies auto exposure");
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080019
Stephan Pleinesf63bde82024-01-13 15:59:33 -080020namespace y2022::vision {
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080021namespace {
22
23using namespace frc971::vision;
24
25void CameraReaderMain() {
26 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
Austin Schuh99f7c6a2024-06-25 22:07:44 -070027 aos::configuration::ReadConfig(absl::GetFlag(FLAGS_config));
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080028
Jim Ostrowski007e2ea2022-01-30 13:13:26 -080029 const aos::FlatbufferSpan<calibration::CalibrationData> calibration_data(
30 CalibrationData());
31 CHECK(calibration_data.Verify());
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080032
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080033 aos::ShmEventLoop event_loop(&config.message());
34
35 // First, log the data for future reference.
36 {
Jim Ostrowski007e2ea2022-01-30 13:13:26 -080037 aos::Sender<calibration::CalibrationData> calibration_data_sender =
38 event_loop.MakeSender<calibration::CalibrationData>("/camera");
39 CHECK_EQ(calibration_data_sender.Send(calibration_data),
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080040 aos::RawSender::Error::kOk);
41 }
42
43 V4L2Reader v4l2_reader(&event_loop, "/dev/video0");
Austin Schuh99f7c6a2024-06-25 22:07:44 -070044 const uint32_t exposure = (absl::GetFlag(FLAGS_use_outdoors)
45 ? absl::GetFlag(FLAGS_outdoors_exposure)
46 : absl::GetFlag(FLAGS_exposure));
Milind Upadhyayf67f67d2022-03-27 14:29:47 -070047 if (exposure > 0) {
Jim Ostrowski1ac95742022-10-01 17:21:11 -070048 LOG(INFO) << "Setting camera to Manual Exposure mode with exposure = "
49 << exposure << " or " << static_cast<double>(exposure) / 10.0
50 << " ms";
Milind Upadhyayf67f67d2022-03-27 14:29:47 -070051 v4l2_reader.SetExposure(exposure);
Jim Ostrowski1ac95742022-10-01 17:21:11 -070052 } else {
53 LOG(INFO) << "Setting camera to use Auto Exposure";
54 v4l2_reader.UseAutoExposure();
Jim Ostrowskifec0c332022-02-06 23:28:26 -080055 }
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080056
Jim Ostrowski007e2ea2022-01-30 13:13:26 -080057 CameraReader camera_reader(&event_loop, &calibration_data.message(),
Jim Ostrowskiff7b3de2022-01-22 22:20:26 -080058 &v4l2_reader);
Austin Schuh99f7c6a2024-06-25 22:07:44 -070059 camera_reader.SetDutyCycle(absl::GetFlag(FLAGS_duty_cycle));
Jim Ostrowskiff7b3de2022-01-22 22:20:26 -080060
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080061 event_loop.Run();
62}
63
64} // namespace
Stephan Pleinesf63bde82024-01-13 15:59:33 -080065} // namespace y2022::vision
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080066
67int main(int argc, char **argv) {
68 aos::InitGoogle(&argc, &argv);
69 y2022::vision::CameraReaderMain();
70}