Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 1 | #include "absl/flags/flag.h" |
| 2 | |
James Kuszmaul | 3eb753d | 2022-03-12 15:21:12 -0800 | [diff] [blame] | 3 | #include "aos/events/shm_event_loop.h" |
James Kuszmaul | 3eb753d | 2022-03-12 15:21:12 -0800 | [diff] [blame] | 4 | #include "aos/flatbuffers.h" |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 5 | #include "aos/init.h" |
James Kuszmaul | 3eb753d | 2022-03-12 15:21:12 -0800 | [diff] [blame] | 6 | #include "frc971/vision/vision_generated.h" |
| 7 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 8 | ABSL_FLAG(std::string, config, "aos_config.json", |
| 9 | "Path to the config file to use."); |
James Kuszmaul | 3eb753d | 2022-03-12 15:21:12 -0800 | [diff] [blame] | 10 | |
| 11 | namespace frc971::vision { |
| 12 | // Reads images from /camera and resends them in /camera/decimated at a fixed |
| 13 | // rate (1 Hz, in this case). |
| 14 | class ImageDecimator { |
| 15 | public: |
| 16 | ImageDecimator(aos::EventLoop *event_loop) |
| 17 | : slow_image_sender_( |
| 18 | event_loop->MakeSender<CameraImage>("/camera/decimated")), |
| 19 | image_fetcher_(event_loop->MakeFetcher<CameraImage>("/camera")) { |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 20 | aos::TimerHandler *timer = event_loop->AddTimer([this]() { |
| 21 | if (image_fetcher_.Fetch()) { |
| 22 | const aos::FlatbufferSpan<CameraImage> image( |
| 23 | {reinterpret_cast<const uint8_t *>(image_fetcher_.context().data), |
| 24 | image_fetcher_.context().size}); |
| 25 | slow_image_sender_.CheckOk(slow_image_sender_.Send(image)); |
| 26 | } |
| 27 | }); |
James Kuszmaul | 3eb753d | 2022-03-12 15:21:12 -0800 | [diff] [blame] | 28 | event_loop->OnRun([event_loop, timer]() { |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 29 | timer->Schedule(event_loop->monotonic_now(), |
| 30 | std::chrono::milliseconds(1000)); |
James Kuszmaul | 3eb753d | 2022-03-12 15:21:12 -0800 | [diff] [blame] | 31 | }); |
| 32 | } |
| 33 | |
| 34 | private: |
| 35 | aos::Sender<CameraImage> slow_image_sender_; |
| 36 | aos::Fetcher<CameraImage> image_fetcher_; |
| 37 | }; |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 38 | } // namespace frc971::vision |
James Kuszmaul | 3eb753d | 2022-03-12 15:21:12 -0800 | [diff] [blame] | 39 | |
| 40 | int main(int argc, char *argv[]) { |
| 41 | aos::InitGoogle(&argc, &argv); |
| 42 | |
| 43 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 44 | aos::configuration::ReadConfig(absl::GetFlag(FLAGS_config)); |
James Kuszmaul | 3eb753d | 2022-03-12 15:21:12 -0800 | [diff] [blame] | 45 | |
| 46 | aos::ShmEventLoop event_loop(&config.message()); |
| 47 | frc971::vision::ImageDecimator decimator(&event_loop); |
| 48 | |
| 49 | event_loop.Run(); |
| 50 | |
| 51 | return 0; |
| 52 | } |