blob: 5f19d64af6f038b80ad0fdf1255876d68ad0b0df [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
17namespace y2022 {
18namespace vision {
19namespace {
20
21using namespace frc971::vision;
22
23void CameraReaderMain() {
24 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
25 aos::configuration::ReadConfig(FLAGS_config);
26
Jim Ostrowski007e2ea2022-01-30 13:13:26 -080027 const aos::FlatbufferSpan<calibration::CalibrationData> calibration_data(
28 CalibrationData());
29 CHECK(calibration_data.Verify());
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080030
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080031 aos::ShmEventLoop event_loop(&config.message());
32
33 // First, log the data for future reference.
34 {
Jim Ostrowski007e2ea2022-01-30 13:13:26 -080035 aos::Sender<calibration::CalibrationData> calibration_data_sender =
36 event_loop.MakeSender<calibration::CalibrationData>("/camera");
37 CHECK_EQ(calibration_data_sender.Send(calibration_data),
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080038 aos::RawSender::Error::kOk);
39 }
40
41 V4L2Reader v4l2_reader(&event_loop, "/dev/video0");
Milind Upadhyayf67f67d2022-03-27 14:29:47 -070042 const uint32_t exposure =
43 (FLAGS_use_outdoors ? FLAGS_outdoors_exposure : FLAGS_exposure);
44 if (exposure > 0) {
Jim Ostrowski1ac95742022-10-01 17:21:11 -070045 LOG(INFO) << "Setting camera to Manual Exposure mode with exposure = "
46 << exposure << " or " << static_cast<double>(exposure) / 10.0
47 << " ms";
Milind Upadhyayf67f67d2022-03-27 14:29:47 -070048 v4l2_reader.SetExposure(exposure);
Jim Ostrowski1ac95742022-10-01 17:21:11 -070049 } else {
50 LOG(INFO) << "Setting camera to use Auto Exposure";
51 v4l2_reader.UseAutoExposure();
Jim Ostrowskifec0c332022-02-06 23:28:26 -080052 }
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080053
Jim Ostrowski007e2ea2022-01-30 13:13:26 -080054 CameraReader camera_reader(&event_loop, &calibration_data.message(),
Jim Ostrowskiff7b3de2022-01-22 22:20:26 -080055 &v4l2_reader);
Jim Ostrowski2a483b32022-02-15 18:19:14 -080056 camera_reader.SetDutyCycle(FLAGS_duty_cycle);
Jim Ostrowskiff7b3de2022-01-22 22:20:26 -080057
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080058 event_loop.Run();
59}
60
61} // namespace
62} // namespace vision
63} // namespace y2022
64
65int main(int argc, char **argv) {
66 aos::InitGoogle(&argc, &argv);
67 y2022::vision::CameraReaderMain();
68}