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