James Kuszmaul | 3eb753d | 2022-03-12 15:21:12 -0800 | [diff] [blame^] | 1 | #include "aos/events/shm_event_loop.h" |
| 2 | #include "aos/init.h" |
| 3 | #include "aos/flatbuffers.h" |
| 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")) { |
| 17 | aos::TimerHandler *timer = |
| 18 | event_loop->AddTimer( |
| 19 | [this]() { |
| 20 | if (image_fetcher_.Fetch()) { |
| 21 | const aos::FlatbufferSpan<CameraImage> image( |
| 22 | {reinterpret_cast<const uint8_t *>( |
| 23 | image_fetcher_.context().data), |
| 24 | image_fetcher_.context().size}); |
| 25 | slow_image_sender_.CheckOk(slow_image_sender_.Send(image)); |
| 26 | } |
| 27 | }); |
| 28 | event_loop->OnRun([event_loop, timer]() { |
| 29 | timer->Setup(event_loop->monotonic_now(), |
| 30 | std::chrono::milliseconds(1000)); |
| 31 | }); |
| 32 | } |
| 33 | |
| 34 | private: |
| 35 | aos::Sender<CameraImage> slow_image_sender_; |
| 36 | aos::Fetcher<CameraImage> image_fetcher_; |
| 37 | }; |
| 38 | } |
| 39 | |
| 40 | int main(int argc, char *argv[]) { |
| 41 | aos::InitGoogle(&argc, &argv); |
| 42 | |
| 43 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 44 | aos::configuration::ReadConfig(FLAGS_config); |
| 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 | } |