blob: 7a5d7de88a50455fa0c4c013b78e819a87ca8c4c [file] [log] [blame]
Austin Schuh99f7c6a2024-06-25 22:07:44 -07001#include "absl/flags/flag.h"
2
James Kuszmaul3eb753d2022-03-12 15:21:12 -08003#include "aos/events/shm_event_loop.h"
James Kuszmaul3eb753d2022-03-12 15:21:12 -08004#include "aos/flatbuffers.h"
Philipp Schrader790cb542023-07-05 21:06:52 -07005#include "aos/init.h"
James Kuszmaul3eb753d2022-03-12 15:21:12 -08006#include "frc971/vision/vision_generated.h"
7
Austin Schuh99f7c6a2024-06-25 22:07:44 -07008ABSL_FLAG(std::string, config, "aos_config.json",
9 "Path to the config file to use.");
James Kuszmaul3eb753d2022-03-12 15:21:12 -080010
11namespace frc971::vision {
12// Reads images from /camera and resends them in /camera/decimated at a fixed
13// rate (1 Hz, in this case).
14class 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 Schrader790cb542023-07-05 21:06:52 -070020 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 Kuszmaul3eb753d2022-03-12 15:21:12 -080028 event_loop->OnRun([event_loop, timer]() {
Philipp Schradera6712522023-07-05 20:25:11 -070029 timer->Schedule(event_loop->monotonic_now(),
30 std::chrono::milliseconds(1000));
James Kuszmaul3eb753d2022-03-12 15:21:12 -080031 });
32 }
33
34 private:
35 aos::Sender<CameraImage> slow_image_sender_;
36 aos::Fetcher<CameraImage> image_fetcher_;
37};
Philipp Schrader790cb542023-07-05 21:06:52 -070038} // namespace frc971::vision
James Kuszmaul3eb753d2022-03-12 15:21:12 -080039
40int main(int argc, char *argv[]) {
41 aos::InitGoogle(&argc, &argv);
42
43 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
Austin Schuh99f7c6a2024-06-25 22:07:44 -070044 aos::configuration::ReadConfig(absl::GetFlag(FLAGS_config));
James Kuszmaul3eb753d2022-03-12 15:21:12 -080045
46 aos::ShmEventLoop event_loop(&config.message());
47 frc971::vision::ImageDecimator decimator(&event_loop);
48
49 event_loop.Run();
50
51 return 0;
52}