Make max image age a parameter in code

Remove the flag and put it in the constructor for ImageCallback, so that
intrinsics calibration can define a much shorter age. This shorter age
is needed so that the watcher doesn't get too far behind and we aren't
able to quit the application.

Signed-off-by: milind-u <milind.upadhyay@gmail.com>
Change-Id: I9400f1c42d93285cc30065513e766ec7c6f34186
diff --git a/frc971/vision/charuco_lib.cc b/frc971/vision/charuco_lib.cc
index b8d4086..622c871 100644
--- a/frc971/vision/charuco_lib.cc
+++ b/frc971/vision/charuco_lib.cc
@@ -27,7 +27,6 @@
     "The mininum number of aruco targets in charuco board required to match.");
 DEFINE_bool(visualize, false, "Whether to visualize the resulting data.");
 
-DEFINE_uint32(age, 100, "Age to start dropping frames at.");
 DEFINE_uint32(disable_delay, 100, "Time after an issue to disable tracing at.");
 
 DECLARE_bool(enable_ftrace);
@@ -94,8 +93,8 @@
 
 ImageCallback::ImageCallback(
     aos::EventLoop *event_loop, std::string_view channel,
-    std::function<void(cv::Mat, monotonic_clock::time_point)> &&handle_image_fn)
-
+    std::function<void(cv::Mat, monotonic_clock::time_point)> &&handle_image_fn,
+    monotonic_clock::duration max_age)
     : event_loop_(event_loop),
       server_fetcher_(
           event_loop_->MakeFetcher<aos::message_bridge::ServerStatistics>(
@@ -106,7 +105,8 @@
               ->source_node()
               ->string_view())),
       handle_image_(std::move(handle_image_fn)),
-      timer_fn_(event_loop->AddTimer([this]() { DisableTracing(); })) {
+      timer_fn_(event_loop->AddTimer([this]() { DisableTracing(); })),
+      max_age_(max_age) {
   event_loop_->MakeWatcher(channel, [this](const CameraImage &image) {
     const monotonic_clock::time_point eof_source_node =
         monotonic_clock::time_point(
@@ -145,7 +145,7 @@
     const monotonic_clock::duration age = event_loop_->monotonic_now() - eof;
     const double age_double =
         std::chrono::duration_cast<std::chrono::duration<double>>(age).count();
-    if (age > std::chrono::milliseconds(FLAGS_age)) {
+    if (age > max_age_) {
       if (FLAGS_enable_ftrace) {
         ftrace_.FormatMessage("Too late receiving image, age: %f\n",
                               age_double);
diff --git a/frc971/vision/charuco_lib.h b/frc971/vision/charuco_lib.h
index de6d4fb..7759482 100644
--- a/frc971/vision/charuco_lib.h
+++ b/frc971/vision/charuco_lib.h
@@ -54,9 +54,13 @@
     BGR = 1,
     GRAYSCALE = 2,
   };
-  ImageCallback(aos::EventLoop *event_loop, std::string_view channel,
-                std::function<void(cv::Mat, aos::monotonic_clock::time_point)>
-                    &&handle_image_fn);
+
+  // `max_age` is the age to start dropping frames at
+  ImageCallback(
+      aos::EventLoop *event_loop, std::string_view channel,
+      std::function<void(cv::Mat, aos::monotonic_clock::time_point)>
+          &&handle_image_fn,
+      aos::monotonic_clock::duration max_age = std::chrono::milliseconds(100));
 
   void set_format(Format format) { format_ = format; }
 
@@ -74,6 +78,8 @@
   aos::Ftrace ftrace_;
 
   Format format_ = Format::BGR;
+
+  aos::monotonic_clock::duration max_age_;
 };
 
 // Types of targets that a CharucoExtractor can detect in images