Make monotonic_now a required parameter to PhasedLoop

This prepares us much better for mocking out time as part of the event
loop conversion.

Change-Id: I57560b97b265ddd41fe7a4e9f74d7b1324d15955
diff --git a/aos/actions/actor.h b/aos/actions/actor.h
index 57a2ecb..53b7040 100644
--- a/aos/actions/actor.h
+++ b/aos/actions/actor.h
@@ -53,6 +53,7 @@
   // succeeded.
   bool WaitOrCancel(monotonic_clock::duration duration) {
     ::aos::time::PhasedLoop phased_loop(::aos::controls::kLoopFrequency,
+                                        event_loop_->monotonic_now(),
                                         ::std::chrono::milliseconds(5) / 2);
     return !WaitUntil(
         [&phased_loop]() {
@@ -92,6 +93,8 @@
   // Set to true when we should stop ASAP.
   bool abort_ = false;
 
+  ::aos::EventLoop *event_loop() { return event_loop_; }
+
  private:
   // Checks if an action was initially running when the thread started.
   bool CheckInitialRunning();
diff --git a/aos/util/phased_loop.h b/aos/util/phased_loop.h
index 1c1aef1..7aa51d2 100644
--- a/aos/util/phased_loop.h
+++ b/aos/util/phased_loop.h
@@ -19,12 +19,13 @@
   // offset must be >= chrono::seconds(0) and < interval.
   PhasedLoop(
       const monotonic_clock::duration interval,
+      const monotonic_clock::time_point monotonic_now,
       const monotonic_clock::duration offset = monotonic_clock::duration(0))
       : interval_(interval), offset_(offset), last_time_(offset) {
     CHECK_GE(offset, monotonic_clock::duration(0));
     CHECK_GT(interval, monotonic_clock::duration(0));
     CHECK_LT(offset, interval);
-    Reset();
+    Reset(monotonic_now);
   }
 
   // Updates the offset and interval.
@@ -52,8 +53,7 @@
   // Resets the count of skipped iterations.
   // Iterate(monotonic_now) will return 1 and set sleep_time() to something
   // within interval of monotonic_now.
-  void Reset(const monotonic_clock::time_point monotonic_now =
-                 monotonic_clock::now()) {
+  void Reset(const monotonic_clock::time_point monotonic_now) {
     Iterate(monotonic_now - interval_);
   }
 
diff --git a/aos/util/phased_loop_test.cc b/aos/util/phased_loop_test.cc
index 2a52b90..4f07bcc 100644
--- a/aos/util/phased_loop_test.cc
+++ b/aos/util/phased_loop_test.cc
@@ -23,7 +23,8 @@
 
 TEST_F(PhasedLoopTest, Reset) {
   {
-    PhasedLoop loop(milliseconds(100), milliseconds(0));
+    PhasedLoop loop(milliseconds(100), monotonic_clock::epoch(),
+                    milliseconds(0));
 
     loop.Reset(monotonic_clock::epoch());
     EXPECT_EQ(InMs(0), loop.sleep_time());
@@ -46,14 +47,16 @@
     EXPECT_EQ(InMs(200), loop.sleep_time());
   }
   {
-    PhasedLoop loop(milliseconds(100), milliseconds(1));
+    PhasedLoop loop(milliseconds(100), monotonic_clock::epoch(),
+                    milliseconds(1));
     loop.Reset(monotonic_clock::epoch());
     EXPECT_EQ(InMs(-99), loop.sleep_time());
     EXPECT_EQ(1, loop.Iterate(monotonic_clock::epoch()));
     EXPECT_EQ(InMs(1), loop.sleep_time());
   }
   {
-    PhasedLoop loop(milliseconds(100), milliseconds(99));
+    PhasedLoop loop(milliseconds(100), monotonic_clock::epoch(),
+                    milliseconds(99));
 
     loop.Reset(monotonic_clock::epoch());
     EXPECT_EQ(InMs(-1), loop.sleep_time());
@@ -79,7 +82,8 @@
 
 TEST_F(PhasedLoopTest, Iterate) {
   {
-    PhasedLoop loop(milliseconds(100), milliseconds(99));
+    PhasedLoop loop(milliseconds(100), monotonic_clock::epoch(),
+                    milliseconds(99));
     loop.Reset(monotonic_clock::epoch());
     EXPECT_EQ(1, loop.Iterate(monotonic_clock::epoch()));
     EXPECT_EQ(InMs(99), loop.sleep_time());
@@ -99,7 +103,8 @@
     EXPECT_EQ(InMs(699), loop.sleep_time());
   }
   {
-    PhasedLoop loop(milliseconds(100), milliseconds(1));
+    PhasedLoop loop(milliseconds(100), monotonic_clock::epoch(),
+                    milliseconds(1));
     loop.Reset(monotonic_clock::epoch());
     EXPECT_EQ(1, loop.Iterate(monotonic_clock::epoch()));
     EXPECT_EQ(InMs(1), loop.sleep_time());
@@ -124,7 +129,7 @@
 // This seems like a rare case at first, but starting from zero needs to
 // work, which means negatives should too.
 TEST_F(PhasedLoopTest, CrossingZero) {
-  PhasedLoop loop(milliseconds(100), milliseconds(1));
+  PhasedLoop loop(milliseconds(100), monotonic_clock::epoch(), milliseconds(1));
   loop.Reset(InMs(-1000));
   EXPECT_EQ(InMs(-1099), loop.sleep_time());
   EXPECT_EQ(9, loop.Iterate(InMs(-250)));
@@ -152,7 +157,8 @@
 
 // Tests OffsetFromIntervalAndTime for various edge conditions.
 TEST_F(PhasedLoopTest, OffsetFromIntervalAndTimeTest) {
-  PhasedLoop loop(milliseconds(1000), milliseconds(300));
+  PhasedLoop loop(milliseconds(1000), monotonic_clock::epoch(),
+                  milliseconds(300));
 
   EXPECT_EQ(milliseconds(1),
             loop.OffsetFromIntervalAndTime(milliseconds(1000), InMs(1001)));
@@ -175,14 +181,18 @@
 
 // Tests that passing invalid values to the constructor dies correctly.
 TEST_F(PhasedLoopDeathTest, InvalidValues) {
-  EXPECT_DEATH(PhasedLoop(milliseconds(1), milliseconds(2)),
-               ".*offset<interval.*");
-  EXPECT_DEATH(PhasedLoop(milliseconds(1), milliseconds(1)),
-               ".*offset<interval.*");
-  EXPECT_DEATH(PhasedLoop(milliseconds(1), milliseconds(-1)),
-               ".*offset>=monotonic_clock::duration\\(0\\).*");
-  EXPECT_DEATH(PhasedLoop(milliseconds(0), milliseconds(0)),
-               ".*interval>monotonic_clock::duration\\(0\\).*");
+  EXPECT_DEATH(
+      PhasedLoop(milliseconds(1), monotonic_clock::epoch(), milliseconds(2)),
+      ".*offset<interval.*");
+  EXPECT_DEATH(
+      PhasedLoop(milliseconds(1), monotonic_clock::epoch(), milliseconds(1)),
+      ".*offset<interval.*");
+  EXPECT_DEATH(
+      PhasedLoop(milliseconds(1), monotonic_clock::epoch(), milliseconds(-1)),
+      ".*offset>=monotonic_clock::duration\\(0\\).*");
+  EXPECT_DEATH(
+      PhasedLoop(milliseconds(0), monotonic_clock::epoch(), milliseconds(0)),
+      ".*interval>monotonic_clock::duration\\(0\\).*");
 }
 
 }  // namespace testing
diff --git a/frc971/autonomous/base_autonomous_actor.cc b/frc971/autonomous/base_autonomous_actor.cc
index 40d65c3..4d3f3fc 100644
--- a/frc971/autonomous/base_autonomous_actor.cc
+++ b/frc971/autonomous/base_autonomous_actor.cc
@@ -87,6 +87,7 @@
   }
 
   ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
+                                      event_loop()->monotonic_now(),
                                       ::std::chrono::milliseconds(5) / 2);
   while (true) {
     // Poll the running bit and see if we should cancel.
@@ -99,6 +100,7 @@
 
 bool BaseAutonomousActor::WaitForDriveDone() {
   ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
+                                      event_loop()->monotonic_now(),
                                       ::std::chrono::milliseconds(5) / 2);
 
   while (true) {
@@ -140,6 +142,7 @@
 
 bool BaseAutonomousActor::WaitForAboveAngle(double angle) {
   ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
+                                      event_loop()->monotonic_now(),
                                       ::std::chrono::milliseconds(5) / 2);
   while (true) {
     if (ShouldCancel()) {
@@ -160,6 +163,7 @@
 
 bool BaseAutonomousActor::WaitForBelowAngle(double angle) {
   ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
+                                      event_loop()->monotonic_now(),
                                       ::std::chrono::milliseconds(5) / 2);
   while (true) {
     if (ShouldCancel()) {
@@ -180,6 +184,7 @@
 
 bool BaseAutonomousActor::WaitForMaxBy(double angle) {
   ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
+                                      event_loop()->monotonic_now(),
                                       ::std::chrono::milliseconds(5) / 2);
   double max_angle = -M_PI;
   while (true) {
@@ -204,6 +209,7 @@
 
 bool BaseAutonomousActor::WaitForDriveNear(double distance, double angle) {
   ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
+                                      event_loop()->monotonic_now(),
                                       ::std::chrono::milliseconds(5) / 2);
   constexpr double kPositionTolerance = 0.02;
   constexpr double kProfileTolerance = 0.001;
@@ -272,6 +278,7 @@
 
 bool BaseAutonomousActor::WaitForDriveProfileNear(double tolerance) {
   ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
+                                      event_loop()->monotonic_now(),
                                       ::std::chrono::milliseconds(5) / 2);
   while (true) {
     if (ShouldCancel()) {
@@ -307,6 +314,7 @@
 
 bool BaseAutonomousActor::WaitForTurnProfileNear(double tolerance) {
   ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
+                                      event_loop()->monotonic_now(),
                                       ::std::chrono::milliseconds(5) / 2);
   while (true) {
     if (ShouldCancel()) {
@@ -369,8 +377,10 @@
 }
 bool BaseAutonomousActor::SplineHandle::WaitForSplineDistanceRemaining(
     double distance) {
-  ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
-                                      ::std::chrono::milliseconds(5) / 2);
+  ::aos::time::PhasedLoop phased_loop(
+      ::std::chrono::milliseconds(5),
+      base_autonomous_actor_->event_loop()->monotonic_now(),
+      ::std::chrono::milliseconds(5) / 2);
   while (true) {
     if (base_autonomous_actor_->ShouldCancel()) {
       return false;
@@ -440,8 +450,10 @@
 }
 
 bool BaseAutonomousActor::SplineHandle::WaitForPlan() {
-  ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
-                                      ::std::chrono::milliseconds(5) / 2);
+  ::aos::time::PhasedLoop phased_loop(
+      ::std::chrono::milliseconds(5),
+      base_autonomous_actor_->event_loop()->monotonic_now(),
+      ::std::chrono::milliseconds(5) / 2);
   while (true) {
     if (base_autonomous_actor_->ShouldCancel()) {
       return false;
@@ -488,8 +500,10 @@
 }
 
 bool BaseAutonomousActor::SplineHandle::WaitForDone() {
-  ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
-                                      ::std::chrono::milliseconds(5) / 2);
+  ::aos::time::PhasedLoop phased_loop(
+      ::std::chrono::milliseconds(5),
+      base_autonomous_actor_->event_loop()->monotonic_now(),
+      ::std::chrono::milliseconds(5) / 2);
   while (true) {
     if (base_autonomous_actor_->ShouldCancel()) {
       return false;
diff --git a/frc971/wpilib/gyro_sender.cc b/frc971/wpilib/gyro_sender.cc
index 894e187..99dc735 100644
--- a/frc971/wpilib/gyro_sender.cc
+++ b/frc971/wpilib/gyro_sender.cc
@@ -64,6 +64,7 @@
   ::aos::SetCurrentThreadRealtimePriority(33);
 
   ::aos::time::PhasedLoop phased_loop(::aos::time::FromRate(kReadingRate),
+                                      event_loop_->monotonic_now(),
                                       chrono::milliseconds(4));
   // How many timesteps the next reading represents.
   int number_readings = 0;
diff --git a/frc971/wpilib/pdp_fetcher.cc b/frc971/wpilib/pdp_fetcher.cc
index eff53e7..01c9613 100644
--- a/frc971/wpilib/pdp_fetcher.cc
+++ b/frc971/wpilib/pdp_fetcher.cc
@@ -18,6 +18,7 @@
       new frc::PowerDistributionPanel());
 
   ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(20),
+                                      ::aos::monotonic_clock::now(),
                                       ::std::chrono::milliseconds(4));
 
   // TODO(austin): Event loop instead of while loop.
diff --git a/frc971/wpilib/sensor_reader.cc b/frc971/wpilib/sensor_reader.cc
index ec1e822..2e0cd0a 100644
--- a/frc971/wpilib/sensor_reader.cc
+++ b/frc971/wpilib/sensor_reader.cc
@@ -100,7 +100,7 @@
     LOG(INFO, "Defaulting to open loop pwm synchronization\n");
   }
   ::aos::time::PhasedLoop phased_loop(
-      period,
+      period, ::aos::monotonic_clock::now(),
       pwm_trigger_ ? ::std::chrono::milliseconds(3) : chrono::milliseconds(4));
 
   ::aos::SetCurrentThreadRealtimePriority(40);
diff --git a/y2012/wpilib_interface.cc b/y2012/wpilib_interface.cc
index 98d35c9..59ceac5 100644
--- a/y2012/wpilib_interface.cc
+++ b/y2012/wpilib_interface.cc
@@ -127,6 +127,7 @@
     ::aos::SetCurrentThreadRealtimePriority(27);
 
     ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(20),
+                                        ::aos::monotonic_clock::now(),
                                         ::std::chrono::milliseconds(1));
 
     while (run_) {
diff --git a/y2014/actors/autonomous_actor.cc b/y2014/actors/autonomous_actor.cc
index 0a242eb..1a874c2 100644
--- a/y2014/actors/autonomous_actor.cc
+++ b/y2014/actors/autonomous_actor.cc
@@ -112,6 +112,7 @@
 bool AutonomousActor::WaitUntilClawDone() {
   while (true) {
     ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(10),
+                                        event_loop()->monotonic_now(),
                                         ::std::chrono::milliseconds(10) / 2);
     // Poll the running bit and auto done bits.
     phased_loop.SleepUntilNext();
diff --git a/y2014/wpilib_interface.cc b/y2014/wpilib_interface.cc
index c9d15dd..09edf96 100644
--- a/y2014/wpilib_interface.cc
+++ b/y2014/wpilib_interface.cc
@@ -456,6 +456,7 @@
     ::aos::SetCurrentThreadRealtimePriority(27);
 
     ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(20),
+                                        ::aos::monotonic_clock::now(),
                                         ::std::chrono::milliseconds(1));
 
     while (run_) {
diff --git a/y2014_bot3/actors/autonomous_actor.cc b/y2014_bot3/actors/autonomous_actor.cc
index 99bfba4..fac6d2f 100644
--- a/y2014_bot3/actors/autonomous_actor.cc
+++ b/y2014_bot3/actors/autonomous_actor.cc
@@ -41,6 +41,7 @@
       ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
 
   ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
+                                      event_loop()->monotonic_now(),
                                       ::std::chrono::milliseconds(5) / 2);
   while (!ShouldCancel()) {
     phased_loop.SleepUntilNext();
diff --git a/y2014_bot3/wpilib_interface.cc b/y2014_bot3/wpilib_interface.cc
index bd5ae3d..f0d0ba2 100644
--- a/y2014_bot3/wpilib_interface.cc
+++ b/y2014_bot3/wpilib_interface.cc
@@ -149,6 +149,7 @@
     ::aos::SetCurrentThreadRealtimePriority(27);
 
     ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(20),
+                                        ::aos::monotonic_clock::now(),
                                         ::std::chrono::milliseconds(1));
 
     while (run_) {
diff --git a/y2016/actors/autonomous_actor.cc b/y2016/actors/autonomous_actor.cc
index 74a1282..f486d38 100644
--- a/y2016/actors/autonomous_actor.cc
+++ b/y2016/actors/autonomous_actor.cc
@@ -166,6 +166,7 @@
   }
 
   ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
+                                      event_loop()->monotonic_now(),
                                       ::std::chrono::milliseconds(5) / 2);
   while (true) {
     if (ShouldCancel()) return;
@@ -183,6 +184,7 @@
 
 void AutonomousActor::WaitForShooterSpeed() {
   ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
+                                      event_loop()->monotonic_now(),
                                       ::std::chrono::milliseconds(5) / 2);
   while (true) {
     if (ShouldCancel()) return;
@@ -211,6 +213,7 @@
   int ready_to_fire = 0;
 
   ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
+                                      event_loop()->monotonic_now(),
                                       ::std::chrono::milliseconds(5) / 2);
   monotonic_clock::time_point end_time =
       monotonic_clock::now() + align_duration;
@@ -552,6 +555,7 @@
 
 void AutonomousActor::WaitForBallOrDriveDone() {
   ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
+                                      event_loop()->monotonic_now(),
                                       ::std::chrono::milliseconds(5) / 2);
   while (true) {
     if (ShouldCancel()) {
@@ -927,6 +931,7 @@
       ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
 
   ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
+                                      event_loop()->monotonic_now(),
                                       ::std::chrono::milliseconds(5) / 2);
 
   while (!ShouldCancel()) {
diff --git a/y2016/actors/vision_align_actor.cc b/y2016/actors/vision_align_actor.cc
index fba545e..1110f92 100644
--- a/y2016/actors/vision_align_actor.cc
+++ b/y2016/actors/vision_align_actor.cc
@@ -34,6 +34,7 @@
   const double robot_radius =
       control_loops::drivetrain::GetDrivetrainConfig().robot_radius;
   ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
+                                      event_loop()->monotonic_now(),
                                       ::std::chrono::milliseconds(5) / 2);
   while (true) {
     const int iterations = phased_loop.SleepUntilNext();
diff --git a/y2016/dashboard/dashboard.cc b/y2016/dashboard/dashboard.cc
index 98fcd7b..300986e 100644
--- a/y2016/dashboard/dashboard.cc
+++ b/y2016/dashboard/dashboard.cc
@@ -49,7 +49,8 @@
 //#define DASHBOARD_READ_VISION_QUEUE
 
 DataCollector::DataCollector(::aos::EventLoop *event_loop)
-    : vision_status_fetcher_(
+    : event_loop_(event_loop),
+      vision_status_fetcher_(
           event_loop->MakeFetcher<::y2016::vision::VisionStatus>(
               ".y2016.vision.vision_status")),
       ball_detector_fetcher_(
@@ -235,6 +236,7 @@
   ::aos::SetCurrentThreadName("DashboardData");
 
   ::aos::time::PhasedLoop phased_loop(chrono::milliseconds(100),
+                                      event_loop_->monotonic_now(),
                                       chrono::seconds(0));
   while (run_) {
     phased_loop.SleepUntilNext();
diff --git a/y2016/dashboard/dashboard.h b/y2016/dashboard/dashboard.h
index 2e01f40..58c9890 100644
--- a/y2016/dashboard/dashboard.h
+++ b/y2016/dashboard/dashboard.h
@@ -65,6 +65,8 @@
     ::std::vector<ItemDatapoint> datapoints;
   };
 
+  ::aos::EventLoop *event_loop_;
+
   ::aos::Fetcher<::y2016::vision::VisionStatus> vision_status_fetcher_;
   ::aos::Fetcher<::y2016::sensors::BallDetector> ball_detector_fetcher_;
   ::aos::Fetcher<::frc971::autonomous::AutonomousMode> autonomous_mode_fetcher_;
diff --git a/y2016/wpilib_interface.cc b/y2016/wpilib_interface.cc
index 3b00248..86ac349 100644
--- a/y2016/wpilib_interface.cc
+++ b/y2016/wpilib_interface.cc
@@ -385,6 +385,7 @@
     ::aos::SetCurrentThreadRealtimePriority(27);
 
     ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(20),
+                                        ::aos::monotonic_clock::now(),
                                         ::std::chrono::milliseconds(1));
 
     while (run_) {
diff --git a/y2017/actors/autonomous_actor.cc b/y2017/actors/autonomous_actor.cc
index 841b18c..b263139 100644
--- a/y2017/actors/autonomous_actor.cc
+++ b/y2017/actors/autonomous_actor.cc
@@ -298,6 +298,7 @@
       ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
 
   ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
+                                      event_loop()->monotonic_now(),
                                       ::std::chrono::milliseconds(5) / 2);
 
   while (!ShouldCancel()) {
diff --git a/y2017/actors/autonomous_actor.h b/y2017/actors/autonomous_actor.h
index 7332344..ac79eb9 100644
--- a/y2017/actors/autonomous_actor.h
+++ b/y2017/actors/autonomous_actor.h
@@ -72,6 +72,7 @@
 
   void WaitForHoodZeroed() {
     ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
+                                        event_loop()->monotonic_now(),
                                         ::std::chrono::milliseconds(5) / 2);
     while (true) {
       if (ShouldCancel()) return;
diff --git a/y2017/wpilib_interface.cc b/y2017/wpilib_interface.cc
index 6fd02fc..b72d6e9 100644
--- a/y2017/wpilib_interface.cc
+++ b/y2017/wpilib_interface.cc
@@ -300,6 +300,7 @@
     ::aos::SetCurrentThreadRealtimePriority(27);
 
     ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(20),
+                                        ::aos::monotonic_clock::now(),
                                         ::std::chrono::milliseconds(1));
 
     while (run_) {
diff --git a/y2018/actors/autonomous_actor.cc b/y2018/actors/autonomous_actor.cc
index 425f6d3..c195fff 100644
--- a/y2018/actors/autonomous_actor.cc
+++ b/y2018/actors/autonomous_actor.cc
@@ -106,6 +106,7 @@
       ::aos::time::DurationInSeconds(monotonic_clock::now() - start_time));
 
   ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
+                                      event_loop()->monotonic_now(),
                                       ::std::chrono::milliseconds(5) / 2);
 
   while (!ShouldCancel()) {
diff --git a/y2018/actors/autonomous_actor.h b/y2018/actors/autonomous_actor.h
index 50218bb..a97f0d1 100644
--- a/y2018/actors/autonomous_actor.h
+++ b/y2018/actors/autonomous_actor.h
@@ -108,6 +108,7 @@
   bool WaitForArmTrajectoryOrDriveClose(double drive_threshold,
                                         double arm_threshold) {
     ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
+                                        event_loop()->monotonic_now(),
                                         ::std::chrono::milliseconds(5) / 2);
 
     constexpr double kPositionTolerance = 0.02;
@@ -168,6 +169,7 @@
 
   bool WaitForArmTrajectoryClose(double threshold) {
     ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
+                                        event_loop()->monotonic_now(),
                                         ::std::chrono::milliseconds(5) / 2);
     while (true) {
       if (ShouldCancel()) {
@@ -188,6 +190,7 @@
 
   bool WaitForBoxGrabed() {
     ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
+                                        event_loop()->monotonic_now(),
                                         ::std::chrono::milliseconds(5) / 2);
     while (true) {
       if (ShouldCancel()) {
diff --git a/y2018/wpilib_interface.cc b/y2018/wpilib_interface.cc
index 4e18114..dc327a7 100644
--- a/y2018/wpilib_interface.cc
+++ b/y2018/wpilib_interface.cc
@@ -417,6 +417,7 @@
     ::aos::SetCurrentThreadRealtimePriority(27);
 
     ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(20),
+                                        ::aos::monotonic_clock::now(),
                                         ::std::chrono::milliseconds(1));
 
     while (run_) {
diff --git a/y2019/actors/autonomous_actor.cc b/y2019/actors/autonomous_actor.cc
index ee7f492..c933181 100644
--- a/y2019/actors/autonomous_actor.cc
+++ b/y2019/actors/autonomous_actor.cc
@@ -30,6 +30,7 @@
 bool AutonomousActor::WaitForDriveXGreater(double x) {
   LOG(INFO, "Waiting until x > %f\n", x);
   ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
+                                      event_loop()->monotonic_now(),
                                       ::std::chrono::milliseconds(5) / 2);
 
   while (true) {
@@ -48,6 +49,7 @@
 bool AutonomousActor::WaitForDriveYCloseToZero(double y) {
   LOG(INFO, "Waiting until |y| < %f\n", y);
   ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
+                                      event_loop()->monotonic_now(),
                                       ::std::chrono::milliseconds(5) / 2);
 
   while (true) {
diff --git a/y2019/actors/autonomous_actor.h b/y2019/actors/autonomous_actor.h
index 864a328..13b336d 100644
--- a/y2019/actors/autonomous_actor.h
+++ b/y2019/actors/autonomous_actor.h
@@ -110,6 +110,7 @@
 
   bool WaitForGamePiece() {
     ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
+                                        event_loop()->monotonic_now(),
                                         ::std::chrono::milliseconds(5) / 2);
 
     while (true) {
@@ -161,6 +162,7 @@
 
   bool WaitForSuperstructureDone() {
     ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
+                                        event_loop()->monotonic_now(),
                                         ::std::chrono::milliseconds(5) / 2);
 
     while (true) {
diff --git a/y2019/wpilib_interface.cc b/y2019/wpilib_interface.cc
index e6dbb1f..16487b9 100644
--- a/y2019/wpilib_interface.cc
+++ b/y2019/wpilib_interface.cc
@@ -564,6 +564,7 @@
     ::aos::SetCurrentThreadRealtimePriority(27);
 
     ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(20),
+                                        ::aos::monotonic_clock::now(),
                                         ::std::chrono::milliseconds(1));
 
     while (run_) {