actions and control loops: frc971 shall depend on aos

And not the other way around.

Define the loop frequency in actor.h and use that in
control_loop.h.  Fix dependencies (and add missing dependency of
actor on event_loop).

Signed-off-by: Stephan Pleines <pleines.stephan@gmail.com>
Change-Id: Ieec0b3eb1c66737e1ed19e6bdee40655524f4016
diff --git a/aos/actions/BUILD b/aos/actions/BUILD
index 5667c12..2127547 100644
--- a/aos/actions/BUILD
+++ b/aos/actions/BUILD
@@ -16,10 +16,10 @@
     target_compatible_with = ["@platforms//os:linux"],
     deps = [
         ":actions_fbs",
+        "//aos/events:event_loop",
         "//aos/logging",
         "//aos/time",
         "//aos/util:phased_loop",
-        "//frc971/control_loops:control_loop",
     ],
 )
 
diff --git a/aos/actions/actor.h b/aos/actions/actor.h
index c27f7dd..a689af9 100644
--- a/aos/actions/actor.h
+++ b/aos/actions/actor.h
@@ -7,15 +7,19 @@
 #include <functional>
 
 #include "aos/actions/actions_generated.h"
+#include "aos/events/event_loop.h"
 #include "aos/logging/logging.h"
 #include "aos/time/time.h"
 #include "aos/util/phased_loop.h"
-#include "frc971/control_loops/control_loop.h"
 
 namespace aos {
 namespace common {
 namespace actions {
 
+constexpr monotonic_clock::duration kLoopFrequency =
+    std::chrono::milliseconds(5);
+constexpr monotonic_clock::duration kLoopOffset = kLoopFrequency / 2;
+
 template <class T>
 class ActorBase {
  public:
@@ -23,10 +27,6 @@
   typedef typename std::remove_pointer<typename std::invoke_result<
       decltype(&GoalType::params), const GoalType *>::type>::type ParamType;
 
-  // Commonly used offset for autonomous phased loops
-  static constexpr monotonic_clock::duration kLoopOffset =
-      frc971::controls::kLoopFrequency / 2;
-
   ActorBase(::aos::EventLoop *event_loop, const ::std::string &name)
       : event_loop_(event_loop),
         status_sender_(event_loop->MakeSender<Status>(name)),
@@ -80,7 +80,6 @@
   // Done condition are defined as functions that return true when done
   // end_time is when to stop and return true. Time(0, 0) (the default) means
   // never time out.
-  // This will be polled at ::frc971::controls::kLoopFrequency
   bool WaitUntil(::std::function<bool(void)> done_condition,
                  ::aos::monotonic_clock::time_point end_time =
                      ::aos::monotonic_clock::min_time);
@@ -191,9 +190,8 @@
 template <class T>
 bool ActorBase<T>::WaitUntil(::std::function<bool(void)> done_condition,
                              ::aos::monotonic_clock::time_point end_time) {
-  ::aos::time::PhasedLoop phased_loop(::frc971::controls::kLoopFrequency,
-                                      event_loop_->monotonic_now(),
-                                      kLoopOffset);
+  ::aos::time::PhasedLoop phased_loop(
+      kLoopFrequency, event_loop_->monotonic_now(), kLoopOffset);
 
   while (!done_condition()) {
     if (ShouldCancel() || abort_) {
diff --git a/frc971/autonomous/base_autonomous_actor.cc b/frc971/autonomous/base_autonomous_actor.cc
index 2156045..a8858ea 100644
--- a/frc971/autonomous/base_autonomous_actor.cc
+++ b/frc971/autonomous/base_autonomous_actor.cc
@@ -124,7 +124,7 @@
 
   ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency,
                                       event_loop()->monotonic_now(),
-                                      ActorBase::kLoopOffset);
+                                      aos::common::actions::kLoopOffset);
   while (true) {
     // Poll the running bit and see if we should cancel.
     phased_loop.SleepUntilNext();
@@ -137,7 +137,7 @@
 bool BaseAutonomousActor::WaitForDriveDone() {
   ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency,
                                       event_loop()->monotonic_now(),
-                                      ActorBase::kLoopOffset);
+                                      aos::common::actions::kLoopOffset);
 
   while (true) {
     if (ShouldCancel()) {
@@ -197,7 +197,7 @@
 bool BaseAutonomousActor::WaitForAboveAngle(double angle) {
   ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency,
                                       event_loop()->monotonic_now(),
-                                      ActorBase::kLoopOffset);
+                                      aos::common::actions::kLoopOffset);
   while (true) {
     if (ShouldCancel()) {
       return false;
@@ -218,7 +218,7 @@
 bool BaseAutonomousActor::WaitForBelowAngle(double angle) {
   ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency,
                                       event_loop()->monotonic_now(),
-                                      ActorBase::kLoopOffset);
+                                      aos::common::actions::kLoopOffset);
   while (true) {
     if (ShouldCancel()) {
       return false;
@@ -239,7 +239,7 @@
 bool BaseAutonomousActor::WaitForMaxBy(double angle) {
   ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency,
                                       event_loop()->monotonic_now(),
-                                      ActorBase::kLoopOffset);
+                                      aos::common::actions::kLoopOffset);
   double max_angle = -M_PI;
   while (true) {
     if (ShouldCancel()) {
@@ -264,7 +264,7 @@
 bool BaseAutonomousActor::WaitForDriveNear(double distance, double angle) {
   ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency,
                                       event_loop()->monotonic_now(),
-                                      ActorBase::kLoopOffset);
+                                      aos::common::actions::kLoopOffset);
   constexpr double kPositionTolerance = 0.02;
   constexpr double kProfileTolerance = 0.001;
 
@@ -333,7 +333,7 @@
 bool BaseAutonomousActor::WaitForDriveProfileNear(double tolerance) {
   ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency,
                                       event_loop()->monotonic_now(),
-                                      ActorBase::kLoopOffset);
+                                      aos::common::actions::kLoopOffset);
   while (true) {
     if (ShouldCancel()) {
       return false;
@@ -370,7 +370,7 @@
 bool BaseAutonomousActor::WaitForTurnProfileNear(double tolerance) {
   ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency,
                                       event_loop()->monotonic_now(),
-                                      ActorBase::kLoopOffset);
+                                      aos::common::actions::kLoopOffset);
   while (true) {
     if (ShouldCancel()) {
       return false;
@@ -483,7 +483,7 @@
   ::aos::time::PhasedLoop phased_loop(
       frc971::controls::kLoopFrequency,
       base_autonomous_actor_->event_loop()->monotonic_now(),
-      ActorBase::kLoopOffset);
+      aos::common::actions::kLoopOffset);
   while (true) {
     if (base_autonomous_actor_->ShouldCancel()) {
       return false;
@@ -500,7 +500,7 @@
   ::aos::time::PhasedLoop phased_loop(
       frc971::controls::kLoopFrequency,
       base_autonomous_actor_->event_loop()->monotonic_now(),
-      ActorBase::kLoopOffset);
+      aos::common::actions::kLoopOffset);
   while (true) {
     if (base_autonomous_actor_->ShouldCancel()) {
       return false;
@@ -615,7 +615,7 @@
   ::aos::time::PhasedLoop phased_loop(
       frc971::controls::kLoopFrequency,
       base_autonomous_actor_->event_loop()->monotonic_now(),
-      ActorBase::kLoopOffset);
+      aos::common::actions::kLoopOffset);
   while (true) {
     if (base_autonomous_actor_->ShouldCancel()) {
       return false;
@@ -665,7 +665,7 @@
   ::aos::time::PhasedLoop phased_loop(
       frc971::controls::kLoopFrequency,
       base_autonomous_actor_->event_loop()->monotonic_now(),
-      ActorBase::kLoopOffset);
+      aos::common::actions::kLoopOffset);
   while (true) {
     if (base_autonomous_actor_->ShouldCancel()) {
       return false;
diff --git a/frc971/control_loops/BUILD b/frc971/control_loops/BUILD
index 1c1b797..925dd43 100644
--- a/frc971/control_loops/BUILD
+++ b/frc971/control_loops/BUILD
@@ -84,6 +84,7 @@
     ],
     target_compatible_with = ["@platforms//os:linux"],
     deps = [
+        "//aos/actions:action_lib",
         "//aos/events:event_loop",
         "//aos/events:shm_event_loop",
         "//aos/logging",
diff --git a/frc971/control_loops/control_loop.h b/frc971/control_loops/control_loop.h
index c02d094..3430dbd 100644
--- a/frc971/control_loops/control_loop.h
+++ b/frc971/control_loops/control_loop.h
@@ -4,6 +4,7 @@
 #include <atomic>
 #include <cstring>
 
+#include "aos/actions/actor.h"
 #include "aos/events/event_loop.h"
 #include "aos/time/time.h"
 #include "aos/util/log_interval.h"
@@ -15,7 +16,7 @@
 
 // Control loops run this often, "starting" at time 0.
 constexpr ::std::chrono::nanoseconds kLoopFrequency =
-    ::std::chrono::milliseconds(5);
+    aos::common::actions::kLoopFrequency;
 
 // Provides helper methods to assist in writing control loops.
 // It will then call the RunIteration method every cycle that it has enough
diff --git a/y2014_bot3/actors/autonomous_actor.cc b/y2014_bot3/actors/autonomous_actor.cc
index 1166524..ae85092 100644
--- a/y2014_bot3/actors/autonomous_actor.cc
+++ b/y2014_bot3/actors/autonomous_actor.cc
@@ -48,7 +48,8 @@
           ::aos::time::DurationInSeconds(monotonic_now() - start_time));
 
   ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency,
-                                      monotonic_now(), ActorBase::kLoopOffset);
+                                      monotonic_now(),
+                                      aos::common::actions::kLoopOffset);
   while (!ShouldCancel()) {
     phased_loop.SleepUntilNext();
   }
diff --git a/y2016/actors/autonomous_actor.cc b/y2016/actors/autonomous_actor.cc
index d784d51..5a80642 100644
--- a/y2016/actors/autonomous_actor.cc
+++ b/y2016/actors/autonomous_actor.cc
@@ -205,7 +205,7 @@
 
   ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency,
                                       event_loop()->monotonic_now(),
-                                      ActorBase::kLoopOffset);
+                                      aos::common::actions::kLoopOffset);
   while (true) {
     if (ShouldCancel()) return;
 
@@ -223,7 +223,7 @@
 void AutonomousActor::WaitForShooterSpeed() {
   ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency,
                                       event_loop()->monotonic_now(),
-                                      ActorBase::kLoopOffset);
+                                      aos::common::actions::kLoopOffset);
   while (true) {
     if (ShouldCancel()) return;
 
@@ -252,7 +252,7 @@
 
   ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency,
                                       event_loop()->monotonic_now(),
-                                      ActorBase::kLoopOffset);
+                                      aos::common::actions::kLoopOffset);
   const monotonic_clock::time_point end_time = monotonic_now() + align_duration;
   while (end_time > monotonic_now()) {
     if (ShouldCancel()) break;
@@ -590,7 +590,7 @@
 void AutonomousActor::WaitForBallOrDriveDone() {
   ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency,
                                       event_loop()->monotonic_now(),
-                                      ActorBase::kLoopOffset);
+                                      aos::common::actions::kLoopOffset);
   while (true) {
     if (ShouldCancel()) {
       return;
@@ -974,7 +974,8 @@
           ::aos::time::DurationInSeconds(monotonic_now() - start_time));
 
   ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency,
-                                      monotonic_now(), ActorBase::kLoopOffset);
+                                      monotonic_now(),
+                                      aos::common::actions::kLoopOffset);
 
   while (!ShouldCancel()) {
     phased_loop.SleepUntilNext();
diff --git a/y2016/actors/vision_align_actor.cc b/y2016/actors/vision_align_actor.cc
index f636ddf..0672dd6 100644
--- a/y2016/actors/vision_align_actor.cc
+++ b/y2016/actors/vision_align_actor.cc
@@ -35,7 +35,7 @@
       control_loops::drivetrain::GetDrivetrainConfig().robot_radius;
   ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency,
                                       event_loop()->monotonic_now(),
-                                      ActorBase::kLoopOffset);
+                                      aos::common::actions::kLoopOffset);
   while (true) {
     const int iterations = phased_loop.SleepUntilNext();
     if (iterations != 1) {
diff --git a/y2017/actors/autonomous_actor.cc b/y2017/actors/autonomous_actor.cc
index f67f0d7..0f21cb6 100644
--- a/y2017/actors/autonomous_actor.cc
+++ b/y2017/actors/autonomous_actor.cc
@@ -310,7 +310,7 @@
 
   ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency,
                                       event_loop()->monotonic_now(),
-                                      ActorBase::kLoopOffset);
+                                      aos::common::actions::kLoopOffset);
 
   while (!ShouldCancel()) {
     phased_loop.SleepUntilNext();
diff --git a/y2017/actors/autonomous_actor.h b/y2017/actors/autonomous_actor.h
index ac60f23..f8f1734 100644
--- a/y2017/actors/autonomous_actor.h
+++ b/y2017/actors/autonomous_actor.h
@@ -74,7 +74,7 @@
   void WaitForHoodZeroed() {
     ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency,
                                         event_loop()->monotonic_now(),
-                                        ActorBase::kLoopOffset);
+                                        aos::common::actions::kLoopOffset);
     while (true) {
       if (ShouldCancel()) return;
 
diff --git a/y2018/actors/autonomous_actor.cc b/y2018/actors/autonomous_actor.cc
index ae017b5..0ba9665 100644
--- a/y2018/actors/autonomous_actor.cc
+++ b/y2018/actors/autonomous_actor.cc
@@ -121,7 +121,7 @@
 
   ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency,
                                       event_loop()->monotonic_now(),
-                                      ActorBase::kLoopOffset);
+                                      aos::common::actions::kLoopOffset);
 
   while (!ShouldCancel()) {
     phased_loop.SleepUntilNext();
diff --git a/y2018/actors/autonomous_actor.h b/y2018/actors/autonomous_actor.h
index 3e6be9f..88eb126 100644
--- a/y2018/actors/autonomous_actor.h
+++ b/y2018/actors/autonomous_actor.h
@@ -125,7 +125,7 @@
                                         double arm_threshold) {
     ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency,
                                         event_loop()->monotonic_now(),
-                                        ActorBase::kLoopOffset);
+                                        aos::common::actions::kLoopOffset);
 
     constexpr double kPositionTolerance = 0.02;
     constexpr double kProfileTolerance = 0.001;
@@ -187,7 +187,7 @@
   bool WaitForArmTrajectoryClose(double threshold) {
     ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency,
                                         event_loop()->monotonic_now(),
-                                        ActorBase::kLoopOffset);
+                                        aos::common::actions::kLoopOffset);
     while (true) {
       if (ShouldCancel()) {
         return false;
@@ -209,7 +209,7 @@
   bool WaitForBoxGrabed() {
     ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency,
                                         event_loop()->monotonic_now(),
-                                        ActorBase::kLoopOffset);
+                                        aos::common::actions::kLoopOffset);
     while (true) {
       if (ShouldCancel()) {
         return false;
diff --git a/y2019/actors/autonomous_actor.cc b/y2019/actors/autonomous_actor.cc
index 5200fed..3719038 100644
--- a/y2019/actors/autonomous_actor.cc
+++ b/y2019/actors/autonomous_actor.cc
@@ -36,7 +36,7 @@
   AOS_LOG(INFO, "Waiting until x > %f\n", x);
   ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency,
                                       event_loop()->monotonic_now(),
-                                      ActorBase::kLoopOffset);
+                                      aos::common::actions::kLoopOffset);
 
   while (true) {
     if (ShouldCancel()) {
@@ -55,7 +55,7 @@
   AOS_LOG(INFO, "Waiting until |y| < %f\n", y);
   ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency,
                                       event_loop()->monotonic_now(),
-                                      ActorBase::kLoopOffset);
+                                      aos::common::actions::kLoopOffset);
 
   while (true) {
     if (ShouldCancel()) {
diff --git a/y2019/actors/autonomous_actor.h b/y2019/actors/autonomous_actor.h
index 99b1364..5b21195 100644
--- a/y2019/actors/autonomous_actor.h
+++ b/y2019/actors/autonomous_actor.h
@@ -171,7 +171,7 @@
   bool WaitForGamePiece() {
     ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency,
                                         event_loop()->monotonic_now(),
-                                        ActorBase::kLoopOffset);
+                                        aos::common::actions::kLoopOffset);
 
     while (true) {
       if (ShouldCancel()) {
@@ -222,7 +222,7 @@
   bool WaitForSuperstructureDone() {
     ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency,
                                         event_loop()->monotonic_now(),
-                                        ActorBase::kLoopOffset);
+                                        aos::common::actions::kLoopOffset);
 
     while (true) {
       if (ShouldCancel()) {
diff --git a/y2020/actors/autonomous_actor.cc b/y2020/actors/autonomous_actor.cc
index 13b7d69..404372d 100644
--- a/y2020/actors/autonomous_actor.cc
+++ b/y2020/actors/autonomous_actor.cc
@@ -429,7 +429,7 @@
 bool AutonomousActor::WaitUntilAbsoluteBallsShot(int absolute_balls) {
   ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency,
                                       event_loop()->monotonic_now(),
-                                      ActorBase::kLoopOffset);
+                                      aos::common::actions::kLoopOffset);
   superstructure_status_fetcher_.Fetch();
   CHECK(superstructure_status_fetcher_.get() != nullptr);
   int last_balls = superstructure_status_fetcher_->shooter()->balls_shot();
diff --git a/y2022/actors/autonomous_actor.cc b/y2022/actors/autonomous_actor.cc
index 41f7e04..482a384 100644
--- a/y2022/actors/autonomous_actor.cc
+++ b/y2022/actors/autonomous_actor.cc
@@ -360,7 +360,7 @@
 
   ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency,
                                       event_loop()->monotonic_now(),
-                                      ActorBase::kLoopOffset);
+                                      aos::common::actions::kLoopOffset);
 
   bool loaded = false;
   while (!loaded) {
@@ -469,7 +469,7 @@
 
   ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency,
                                       event_loop()->monotonic_now(),
-                                      ActorBase::kLoopOffset);
+                                      aos::common::actions::kLoopOffset);
   superstructure_status_fetcher_.Fetch();
   CHECK(superstructure_status_fetcher_.get() != nullptr);
 
diff --git a/y2023/autonomous/autonomous_actor.cc b/y2023/autonomous/autonomous_actor.cc
index 652fe66..a23f693 100644
--- a/y2023/autonomous/autonomous_actor.cc
+++ b/y2023/autonomous/autonomous_actor.cc
@@ -521,7 +521,7 @@
 
   ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency,
                                       event_loop()->monotonic_now(),
-                                      ActorBase::kLoopOffset);
+                                      aos::common::actions::kLoopOffset);
 
   bool loaded = false;
   while (!loaded) {
@@ -604,7 +604,7 @@
 
   ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency,
                                       event_loop()->monotonic_now(),
-                                      ActorBase::kLoopOffset);
+                                      aos::common::actions::kLoopOffset);
 
   bool at_goal = false;
   while (!at_goal) {