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/y2016/vision/BUILD b/y2016/vision/BUILD
index 3427973..c4d472b 100644
--- a/y2016/vision/BUILD
+++ b/y2016/vision/BUILD
@@ -1,12 +1,13 @@
 load("@com_google_protobuf//:protobuf.bzl", "cc_proto_library")
-load("//aos/build:queues.bzl", "queue_library")
+load("@com_github_google_flatbuffers//:build_defs.bzl", "flatbuffer_cc_library")
 load("//tools/build_rules:gtk_dependent.bzl", "gtk_dependent_cc_binary", "gtk_dependent_cc_library")
 
-queue_library(
-    name = "vision_queue",
+flatbuffer_cc_library(
+    name = "vision_fbs",
     srcs = [
-        "vision.q",
+        "vision.fbs",
     ],
+    gen_reflections = 1,
     visibility = ["//visibility:public"],
 )
 
@@ -130,15 +131,15 @@
     deps = [
         ":stereo_geometry",
         ":vision_data",
-        ":vision_queue",
+        ":vision_fbs",
         "//aos:init",
-        "//aos/events:shm-event-loop",
+        "//aos/events:shm_event_loop",
         "//aos/logging",
-        "//aos/logging:queue_logging",
         "//aos/mutex",
         "//aos/time",
         "//aos/vision/events:udp",
-        "//frc971/control_loops/drivetrain:drivetrain_queue",
+        "//frc971/control_loops:control_loops_fbs",
+        "//frc971/control_loops/drivetrain:drivetrain_status_fbs",
         "//y2016:constants",
     ],
 )
diff --git a/y2016/vision/target_receiver.cc b/y2016/vision/target_receiver.cc
index a66be59..b946f8b 100644
--- a/y2016/vision/target_receiver.cc
+++ b/y2016/vision/target_receiver.cc
@@ -10,19 +10,18 @@
 #include <thread>
 #include <vector>
 
-#include "aos/events/event-loop.h"
-#include "aos/events/shm-event-loop.h"
+#include "aos/events/event_loop.h"
+#include "aos/events/shm_event_loop.h"
 #include "aos/init.h"
 #include "aos/logging/logging.h"
-#include "aos/logging/queue_logging.h"
 #include "aos/mutex/mutex.h"
 #include "aos/time/time.h"
 #include "aos/vision/events/udp.h"
-#include "frc971/control_loops/drivetrain/drivetrain.q.h"
+#include "frc971/control_loops/drivetrain/drivetrain_status_generated.h"
 #include "y2016/constants.h"
 #include "y2016/vision/stereo_geometry.h"
-#include "y2016/vision/vision.q.h"
 #include "y2016/vision/vision_data.pb.h"
+#include "y2016/vision/vision_generated.h"
 
 namespace y2016 {
 namespace vision {
@@ -197,17 +196,20 @@
   DrivetrainOffsetCalculator(::aos::EventLoop *event_loop)
       : drivetrain_status_fetcher_(
             event_loop
-                ->MakeFetcher<::frc971::control_loops::DrivetrainQueue::Status>(
-                    ".frc971.control_loops.drivetrain_queue.status")) {}
+                ->MakeFetcher<::frc971::control_loops::drivetrain::Status>(
+                    "/drivetrain")) {}
 
   // Takes a vision status message with everything except
   // drivetrain_{left,right}_position set and sets those.
   // Returns false if it doesn't have enough data to fill them out.
-  bool CompleteVisionStatus(::y2016::vision::VisionStatus *status) {
+  bool CompleteVisionStatus(::y2016::vision::VisionStatusT *status) {
     while (drivetrain_status_fetcher_.FetchNext()) {
-      data_[data_index_].time = drivetrain_status_fetcher_->sent_time;
-      data_[data_index_].left = drivetrain_status_fetcher_->estimated_left_position;
-      data_[data_index_].right = drivetrain_status_fetcher_->estimated_right_position;
+      data_[data_index_].time =
+          drivetrain_status_fetcher_.context().monotonic_sent_time;
+      data_[data_index_].left =
+          drivetrain_status_fetcher_->estimated_left_position();
+      data_[data_index_].right =
+          drivetrain_status_fetcher_->estimated_right_position();
       ++data_index_;
       if (data_index_ == data_.size()) data_index_ = 0;
       if (valid_data_ < data_.size()) ++valid_data_;
@@ -286,7 +288,7 @@
     }
   }
 
-  ::aos::Fetcher<::frc971::control_loops::DrivetrainQueue::Status>
+  ::aos::Fetcher<::frc971::control_loops::drivetrain::Status>
       drivetrain_status_fetcher_;
 
   ::std::array<DrivetrainData, 200> data_;
@@ -297,11 +299,13 @@
 };
 
 void Main() {
-  ::aos::ShmEventLoop event_loop;
+  aos::FlatbufferDetachedBuffer<aos::Configuration> config =
+      aos::configuration::ReadConfig("config.json");
+
+  ::aos::ShmEventLoop event_loop(&config.message());
 
   ::aos::Sender<::y2016::vision::VisionStatus> vision_status_sender =
-      event_loop.MakeSender<::y2016::vision::VisionStatus>(
-          ".y2016.vision.vision_status");
+      event_loop.MakeSender<::y2016::vision::VisionStatus>("/superstructure");
 
   StereoGeometry stereo(constants::GetValues().vision_name);
   AOS_LOG(INFO, "calibration: %s\n",
@@ -339,9 +343,10 @@
       const bool left_image_valid = left.is_valid();
       const bool right_image_valid = right.is_valid();
 
-      auto new_vision_status = vision_status_sender.MakeMessage();
-      new_vision_status->left_image_valid = left_image_valid;
-      new_vision_status->right_image_valid = right_image_valid;
+      auto builder = vision_status_sender.MakeBuilder();
+      VisionStatusT new_vision_status;
+      new_vision_status.left_image_valid = left_image_valid;
+      new_vision_status.right_image_valid = right_image_valid;
       if (left_image_valid && right_image_valid) {
         ::aos::vision::Vector<2> center_left(0.0, 0.0);
         ::aos::vision::Vector<2> center_right(0.0, 0.0);
@@ -367,7 +372,7 @@
         if (left.capture_time() < right.capture_time()) {
           filtered_center_left = center_left;
           filtered_angle_left = angle_left;
-          new_vision_status->target_time =
+          new_vision_status.target_time =
               chrono::duration_cast<chrono::nanoseconds>(
                   left.capture_time().time_since_epoch())
                   .count();
@@ -377,7 +382,7 @@
         } else {
           filtered_center_right = center_right;
           filtered_angle_right = angle_right;
-          new_vision_status->target_time =
+          new_vision_status.target_time =
               chrono::duration_cast<chrono::nanoseconds>(
                   right.capture_time().time_since_epoch())
                   .count();
@@ -389,28 +394,27 @@
         double distance, horizontal_angle, vertical_angle;
         stereo.Process(filtered_center_left, filtered_center_right, &distance,
                        &horizontal_angle, &vertical_angle);
-        new_vision_status->left_image_timestamp =
+        new_vision_status.left_image_timestamp =
             left.target().image_timestamp();
-        new_vision_status->right_image_timestamp =
+        new_vision_status.right_image_timestamp =
             right.target().image_timestamp();
-        new_vision_status->left_send_timestamp = left.target().send_timestamp();
-        new_vision_status->right_send_timestamp =
+        new_vision_status.left_send_timestamp = left.target().send_timestamp();
+        new_vision_status.right_send_timestamp =
             right.target().send_timestamp();
-        new_vision_status->horizontal_angle = horizontal_angle;
-        new_vision_status->vertical_angle = vertical_angle;
-        new_vision_status->distance = distance;
-        new_vision_status->angle =
+        new_vision_status.horizontal_angle = horizontal_angle;
+        new_vision_status.vertical_angle = vertical_angle;
+        new_vision_status.distance = distance;
+        new_vision_status.angle =
             (filtered_angle_left + filtered_angle_right) / 2.0;
       }
 
-      if (drivetrain_offset.CompleteVisionStatus(new_vision_status.get())) {
-        AOS_LOG_STRUCT(DEBUG, "vision", *new_vision_status);
-        if (!new_vision_status.Send()) {
+      if (drivetrain_offset.CompleteVisionStatus(&new_vision_status)) {
+        if (!builder.Send(
+                VisionStatus::Pack(*builder.fbb(), &new_vision_status))) {
           AOS_LOG(ERROR, "Failed to send vision information\n");
         }
       } else {
-        AOS_LOG_STRUCT(WARNING, "vision without drivetrain",
-                       *new_vision_status);
+        AOS_LOG(WARNING, "vision without drivetrain");
       }
     }
 
diff --git a/y2016/vision/vision.q b/y2016/vision/vision.fbs
similarity index 65%
rename from y2016/vision/vision.q
rename to y2016/vision/vision.fbs
index 06deb1f..4394b10 100644
--- a/y2016/vision/vision.q
+++ b/y2016/vision/vision.fbs
@@ -1,36 +1,38 @@
-package y2016.vision;
+namespace y2016.vision;
 
 // Published on ".y2016.vision.vision_status"
-message VisionStatus {
-  bool left_image_valid;
-  bool right_image_valid;
+table VisionStatus {
+  left_image_valid:bool;
+  right_image_valid:bool;
   // Times when the images were taken as nanoseconds on CLOCK_MONOTONIC on the
   // TK1.
-  int64_t left_image_timestamp;
-  int64_t right_image_timestamp;
+  left_image_timestamp:long;
+  right_image_timestamp:long;
   // Times when the images were sent from the TK1 as nanoseconds on the TK1's
   // CLOCK_MONOTONIC.
-  int64_t left_send_timestamp;
-  int64_t right_send_timestamp;
+  left_send_timestamp:long;
+  right_send_timestamp:long;
 
   // Horizontal angle of the goal in radians.
   // TODO(Brian): Figure out which way is positive.
-  double horizontal_angle;
+  horizontal_angle:double;
   // Vertical angle of the goal in radians.
   // TODO(Brian): Figure out which way is positive.
-  double vertical_angle;
+  vertical_angle:double;
   // Distance to the target in meters.
-  double distance;
+  distance:double;
   // The angle in radians of the bottom of the target.
-  double angle;
+  angle:double;
 
   // Capture time of the angle using the clock behind monotonic_clock::now().
-  int64_t target_time;
+  target_time:long;
 
   // The estimated positions of both sides of the drivetrain when the frame
   // was captured.
   // These are the estimated_left_position and estimated_right_position members
   // of the drivetrain queue.
-  double drivetrain_left_position;
-  double drivetrain_right_position;
-};
+  drivetrain_left_position:double;
+  drivetrain_right_position:double;
+}
+
+root_type VisionStatus;