Convert aos over to flatbuffers

Everything builds, and all the tests pass.  I suspect that some entries
are missing from the config files, but those will be found pretty
quickly on startup.

There is no logging or live introspection of queue messages.

Change-Id: I496ee01ed68f202c7851bed7e8786cee30df29f5
diff --git a/y2017/actors/BUILD b/y2017/actors/BUILD
index 8c1b1cd..3b3e0a2 100644
--- a/y2017/actors/BUILD
+++ b/y2017/actors/BUILD
@@ -1,7 +1,5 @@
 package(default_visibility = ["//visibility:public"])
 
-load("//aos/build:queues.bzl", "queue_library")
-
 filegroup(
     name = "binaries",
     srcs = [
@@ -19,14 +17,15 @@
     ],
     deps = [
         "//aos/actions:action_lib",
-        "//aos/events:event-loop",
+        "//aos/events:event_loop",
         "//aos/logging",
         "//aos/util:phased_loop",
         "//frc971/autonomous:base_autonomous_actor",
+        "//frc971/control_loops:profiled_subsystem_fbs",
         "//frc971/control_loops/drivetrain:drivetrain_config",
-        "//frc971/control_loops/drivetrain:drivetrain_queue",
         "//y2017/control_loops/drivetrain:drivetrain_base",
-        "//y2017/control_loops/superstructure:superstructure_queue",
+        "//y2017/control_loops/superstructure:superstructure_goal_fbs",
+        "//y2017/control_loops/superstructure:superstructure_status_fbs",
     ],
 )
 
@@ -38,7 +37,6 @@
     deps = [
         ":autonomous_action_lib",
         "//aos:init",
-        "//aos/events:shm-event-loop",
-        "//frc971/autonomous:auto_queue",
+        "//aos/events:shm_event_loop",
     ],
 )
diff --git a/y2017/actors/autonomous_actor.cc b/y2017/actors/autonomous_actor.cc
index a678e79..1124780 100644
--- a/y2017/actors/autonomous_actor.cc
+++ b/y2017/actors/autonomous_actor.cc
@@ -7,30 +7,37 @@
 
 #include "aos/logging/logging.h"
 #include "aos/util/phased_loop.h"
-
-#include "frc971/control_loops/drivetrain/drivetrain.q.h"
 #include "y2017/control_loops/drivetrain/drivetrain_base.h"
 
 namespace y2017 {
 namespace actors {
 using ::aos::monotonic_clock;
+using ::frc971::ProfileParametersT;
 namespace chrono = ::std::chrono;
 namespace this_thread = ::std::this_thread;
 
 namespace {
 
-const ProfileParameters kGearBallBackDrive = {3.0, 3.5};
-const ProfileParameters kGearDrive = {1.5, 2.0};
-const ProfileParameters kGearFastDrive = {2.0, 2.5};
-const ProfileParameters kGearSlowDrive = {1.0, 2.0};
-const ProfileParameters kGearPlaceDrive = {0.40, 2.0};
-const ProfileParameters kSlowDrive = {3.0, 2.0};
-const ProfileParameters kSlowTurn = {3.0, 3.0};
-const ProfileParameters kFirstTurn = {1.0, 1.5};
-const ProfileParameters kFirstGearStartTurn = {2.0, 3.0};
-const ProfileParameters kFirstGearTurn = {2.0, 5.0};
-const ProfileParameters kSecondGearTurn = {3.0, 5.0};
-const ProfileParameters kSmashTurn = {1.5, 5.0};
+ProfileParametersT MakeProfileParameters(float max_velocity,
+                                         float max_acceleration) {
+  ProfileParametersT result;
+  result.max_velocity = max_velocity;
+  result.max_acceleration = max_acceleration;
+  return result;
+}
+
+const ProfileParametersT kGearBallBackDrive = MakeProfileParameters(3.0, 3.5);
+const ProfileParametersT kGearDrive = MakeProfileParameters(1.5, 2.0);
+const ProfileParametersT kGearFastDrive = MakeProfileParameters(2.0, 2.5);
+const ProfileParametersT kGearSlowDrive = MakeProfileParameters(1.0, 2.0);
+const ProfileParametersT kGearPlaceDrive = MakeProfileParameters(0.40, 2.0);
+const ProfileParametersT kSlowDrive = MakeProfileParameters(3.0, 2.0);
+const ProfileParametersT kSlowTurn = MakeProfileParameters(3.0, 3.0);
+const ProfileParametersT kFirstTurn = MakeProfileParameters(1.0, 1.5);
+const ProfileParametersT kFirstGearStartTurn = MakeProfileParameters(2.0, 3.0);
+const ProfileParametersT kFirstGearTurn = MakeProfileParameters(2.0, 5.0);
+const ProfileParametersT kSecondGearTurn = MakeProfileParameters(3.0, 5.0);
+const ProfileParametersT kSmashTurn = MakeProfileParameters(1.5, 5.0);
 
 }  // namespace
 
@@ -38,22 +45,21 @@
     : frc971::autonomous::BaseAutonomousActor(
           event_loop, control_loops::drivetrain::GetDrivetrainConfig()),
       superstructure_status_fetcher_(
-          event_loop->MakeFetcher<
-              ::y2017::control_loops::SuperstructureQueue::Status>(
-              ".y2017.control_loops.superstructure_queue.status")),
-      superstructure_goal_sender_(
           event_loop
-              ->MakeSender<::y2017::control_loops::SuperstructureQueue::Goal>(
-                  ".y2017.control_loops.superstructure_queue.goal")) {}
+              ->MakeFetcher<::y2017::control_loops::superstructure::Status>(
+                  "/superstructure")),
+      superstructure_goal_sender_(
+          event_loop->MakeSender<::y2017::control_loops::superstructure::Goal>(
+              "/superstructure")) {}
 
 bool AutonomousActor::RunAction(
-    const ::frc971::autonomous::AutonomousActionParams &params) {
+    const ::frc971::autonomous::AutonomousActionParams *params) {
   const monotonic_clock::time_point start_time = monotonic_now();
   AOS_LOG(INFO, "Starting autonomous action with mode %" PRId32 "\n",
-          params.mode);
+          params->mode());
   Reset();
 
-  switch (params.mode) {
+  switch (params->mode()) {
     case 503: {
       // Middle gear auto.
       // Red is positive.
diff --git a/y2017/actors/autonomous_actor.h b/y2017/actors/autonomous_actor.h
index f5d4c3f..f0bcfc8 100644
--- a/y2017/actors/autonomous_actor.h
+++ b/y2017/actors/autonomous_actor.h
@@ -7,9 +7,9 @@
 #include "aos/actions/actions.h"
 #include "aos/actions/actor.h"
 #include "frc971/autonomous/base_autonomous_actor.h"
-#include "frc971/control_loops/drivetrain/drivetrain.q.h"
 #include "frc971/control_loops/drivetrain/drivetrain_config.h"
-#include "y2017/control_loops/superstructure/superstructure.q.h"
+#include "y2017/control_loops/superstructure/superstructure_goal_generated.h"
+#include "y2017/control_loops/superstructure/superstructure_status_generated.h"
 
 namespace y2017 {
 namespace actors {
@@ -20,7 +20,7 @@
   explicit AutonomousActor(::aos::EventLoop *event_loop);
 
   bool RunAction(
-      const ::frc971::autonomous::AutonomousActionParams &params) override;
+      const ::frc971::autonomous::AutonomousActionParams *params) override;
 
  private:
   void Reset() {
@@ -38,9 +38,9 @@
     SendSuperstructureGoal();
   }
 
-  ::aos::Fetcher<::y2017::control_loops::SuperstructureQueue::Status>
+  ::aos::Fetcher<::y2017::control_loops::superstructure::Status>
       superstructure_status_fetcher_;
-  ::aos::Sender<::y2017::control_loops::SuperstructureQueue::Goal>
+  ::aos::Sender<::y2017::control_loops::superstructure::Goal>
       superstructure_goal_sender_;
 
   double intake_goal_ = 0.0;
@@ -82,7 +82,7 @@
 
       superstructure_status_fetcher_.Fetch();
       if (superstructure_status_fetcher_.get()) {
-        if (superstructure_status_fetcher_->hood.zeroed) {
+        if (superstructure_status_fetcher_->hood()->zeroed()) {
           return;
         }
       }
@@ -91,40 +91,81 @@
   }
 
   void SendSuperstructureGoal() {
-    auto new_superstructure_goal = superstructure_goal_sender_.MakeMessage();
-    new_superstructure_goal->intake.distance = intake_goal_;
-    new_superstructure_goal->intake.disable_intake = false;
-    new_superstructure_goal->turret.angle = turret_goal_;
-    new_superstructure_goal->turret.track = vision_track_;
-    new_superstructure_goal->hood.angle = hood_goal_;
-    new_superstructure_goal->shooter.angular_velocity = shooter_velocity_;
+    auto builder = superstructure_goal_sender_.MakeBuilder();
 
-    new_superstructure_goal->intake.profile_params.max_velocity =
-        intake_max_velocity_;
-    new_superstructure_goal->turret.profile_params.max_velocity = 6.0;
-    new_superstructure_goal->hood.profile_params.max_velocity = 5.0;
+    flatbuffers::Offset<frc971::ProfileParameters>
+        intake_profile_parameters_offset = frc971::CreateProfileParameters(
+            *builder.fbb(), intake_max_velocity_, 5.0);
 
-    new_superstructure_goal->intake.profile_params.max_acceleration = 5.0;
-    new_superstructure_goal->turret.profile_params.max_acceleration = 15.0;
-    new_superstructure_goal->hood.profile_params.max_acceleration = 25.0;
+    control_loops::superstructure::IntakeGoal::Builder intake_builder =
+        builder.MakeBuilder<control_loops::superstructure::IntakeGoal>();
+    intake_builder.add_distance(intake_goal_);
+    intake_builder.add_disable_intake(false);
+    intake_builder.add_profile_params(intake_profile_parameters_offset);
+    intake_builder.add_voltage_rollers(0.0);
+    intake_builder.add_disable_intake(false);
+    intake_builder.add_gear_servo(gear_servo_);
+    flatbuffers::Offset<control_loops::superstructure::IntakeGoal>
+        intake_offset = intake_builder.Finish();
 
-    new_superstructure_goal->intake.voltage_rollers = 0.0;
-    new_superstructure_goal->lights_on = true;
-    new_superstructure_goal->intake.disable_intake = false;
-    new_superstructure_goal->intake.gear_servo = gear_servo_;
-    new_superstructure_goal->use_vision_for_shots = use_vision_for_shots_;
+    control_loops::superstructure::IndexerGoal::Builder indexer_builder =
+        builder.MakeBuilder<control_loops::superstructure::IndexerGoal>();
+    indexer_builder.add_angular_velocity(indexer_angular_velocity_);
+    indexer_builder.add_voltage_rollers(0.0);
+    flatbuffers::Offset<control_loops::superstructure::IndexerGoal>
+        indexer_offset = indexer_builder.Finish();
 
-    new_superstructure_goal->indexer.angular_velocity =
-        indexer_angular_velocity_;
+    flatbuffers::Offset<frc971::ProfileParameters>
+        turret_profile_parameters_offset = frc971::CreateProfileParameters(
+            *builder.fbb(), 6.0, 15.0);
+    control_loops::superstructure::TurretGoal::Builder turret_builder =
+        builder.MakeBuilder<control_loops::superstructure::TurretGoal>();
+    turret_builder.add_angle(turret_goal_);
+    turret_builder.add_track(vision_track_);
+    turret_builder.add_profile_params(turret_profile_parameters_offset);
+    flatbuffers::Offset<control_loops::superstructure::TurretGoal> turret_offset =
+      turret_builder.Finish();
+
+    flatbuffers::Offset<frc971::ProfileParameters>
+        hood_profile_parameters_offset = frc971::CreateProfileParameters(
+            *builder.fbb(), 5.0, 25.0);
+    control_loops::superstructure::HoodGoal::Builder hood_builder =
+        builder.MakeBuilder<control_loops::superstructure::HoodGoal>();
+    hood_builder.add_angle(hood_goal_);
+    hood_builder.add_profile_params(hood_profile_parameters_offset);
+    flatbuffers::Offset<control_loops::superstructure::HoodGoal> hood_offset =
+        hood_builder.Finish();
+
+    control_loops::superstructure::ShooterGoal::Builder shooter_builder =
+        builder.MakeBuilder<control_loops::superstructure::ShooterGoal>();
+    shooter_builder.add_angular_velocity(shooter_velocity_);
+    flatbuffers::Offset<control_loops::superstructure::ShooterGoal>
+        shooter_offset = shooter_builder.Finish();
+
+    control_loops::superstructure::Goal::Builder goal_builder =
+        builder.MakeBuilder<control_loops::superstructure::Goal>();
+    goal_builder.add_lights_on(true);
+    goal_builder.add_use_vision_for_shots(use_vision_for_shots_);
+    goal_builder.add_intake(intake_offset);
+    goal_builder.add_indexer(indexer_offset);
+    goal_builder.add_turret(turret_offset);
+    goal_builder.add_hood(hood_offset);
+    goal_builder.add_shooter(shooter_offset);
+
+    flatbuffers::Offset<control_loops::superstructure::Goal> goal_offset =
+        goal_builder.Finish();
+
+    control_loops::superstructure::Goal *goal =
+        GetMutableTemporaryPointer(*builder.fbb(), goal_offset);
 
     if (indexer_angular_velocity_ < -0.1) {
-      new_superstructure_goal->indexer.voltage_rollers = 12.0;
-      new_superstructure_goal->intake.voltage_rollers = 6.0;
+      goal->mutable_indexer()->mutate_voltage_rollers(12.0);
+      goal->mutable_intake()->mutate_voltage_rollers(6.0);
     } else {
-      new_superstructure_goal->indexer.voltage_rollers = 0.0;
+      goal->mutable_indexer()->mutate_voltage_rollers(0.0);
     }
 
-    if (!new_superstructure_goal.Send()) {
+    if (!builder.Send(goal_offset)) {
       AOS_LOG(ERROR, "Sending superstructure goal failed.\n");
     }
   }
diff --git a/y2017/actors/autonomous_actor_main.cc b/y2017/actors/autonomous_actor_main.cc
index 88779d8..d87ac84 100644
--- a/y2017/actors/autonomous_actor_main.cc
+++ b/y2017/actors/autonomous_actor_main.cc
@@ -1,14 +1,16 @@
 #include <stdio.h>
 
-#include "aos/events/shm-event-loop.h"
+#include "aos/events/shm_event_loop.h"
 #include "aos/init.h"
-#include "frc971/autonomous/auto.q.h"
 #include "y2017/actors/autonomous_actor.h"
 
 int main(int /*argc*/, char * /*argv*/ []) {
   ::aos::Init(-1);
 
-  ::aos::ShmEventLoop event_loop;
+  aos::FlatbufferDetachedBuffer<aos::Configuration> config =
+      aos::configuration::ReadConfig("config.json");
+
+  ::aos::ShmEventLoop event_loop(&config.message());
   ::y2017::actors::AutonomousActor autonomous(&event_loop);
   event_loop.Run();