Sundry tweaks to aos/vision libs

Change-Id: Ia5578dcf2d42ac53b81af239bf329eb084fcf1d9
diff --git a/aos/vision/image/image_stream.h b/aos/vision/image/image_stream.h
index ac25394..5a1ad21 100644
--- a/aos/vision/image/image_stream.h
+++ b/aos/vision/image/image_stream.h
@@ -18,8 +18,8 @@
       camera::CameraParams params) {
     using namespace std::placeholders;
     std::unique_ptr<::camera::Reader> camread(new ::camera::Reader(
-        fname,
-        std::bind(&ImageStreamEvent::ProcessHelper, obj, _1, _2), params));
+        fname, std::bind(&ImageStreamEvent::ProcessHelper, obj, _1, _2),
+        params));
     camread->StartAsync();
     return camread;
   }
@@ -33,12 +33,13 @@
 
   void ProcessHelper(DataRef data, aos::monotonic_clock::time_point timestamp) {
     if (data.size() < 300) {
-      LOG(INFO, "got bad img of size(%zu)\n", data.size());
+      LOG(INFO, "got bad img of size(%d)\n", static_cast<int>(data.size()));
       return;
     }
     ProcessImage(data, timestamp);
   }
-  virtual void ProcessImage(DataRef data, aos::monotonic_clock::time_point timestamp) = 0;
+  virtual void ProcessImage(DataRef data,
+                            aos::monotonic_clock::time_point timestamp) = 0;
 
   void ReadEvent() override { reader_->HandleFrame(); }
 
diff --git a/aos/vision/image/image_types.h b/aos/vision/image/image_types.h
index 2c9ba56..68e6d67 100644
--- a/aos/vision/image/image_types.h
+++ b/aos/vision/image/image_types.h
@@ -19,7 +19,7 @@
 struct ImageFormat {
   ImageFormat() : w(0), h(0) {}
   ImageFormat(int nw, int nh) : w(nw), h(nh) {}
-  std::string ToString() {
+  std::string ToString() const {
     std::ostringstream s;
     s << "ImageFormat {" << w << ", " << h << "}";
     return s.str();
diff --git a/aos/vision/image/reader.cc b/aos/vision/image/reader.cc
index 95729da..3a4b349 100644
--- a/aos/vision/image/reader.cc
+++ b/aos/vision/image/reader.cc
@@ -29,6 +29,9 @@
   }
 
   Init();
+
+  InitMMap();
+  LOG(INFO, "Bat Vision Successfully Initialized.\n");
 }
 
 void Reader::QueueBuffer(v4l2_buffer *buf) {
@@ -57,6 +60,18 @@
   }
   --queued_;
 
+  if (tick_id_ % 10 == 0) {
+    if (!SetCameraControl(V4L2_CID_EXPOSURE_AUTO, "V4L2_CID_EXPOSURE_AUTO",
+                          V4L2_EXPOSURE_MANUAL)) {
+      LOG(FATAL, "Failed to set exposure\n");
+    }
+
+    if (!SetCameraControl(V4L2_CID_EXPOSURE_ABSOLUTE,
+                          "V4L2_CID_EXPOSURE_ABSOLUTE", params_.exposure)) {
+      LOG(FATAL, "Failed to set exposure\n");
+    }
+  }
+  ++tick_id_;
   // Get a timestamp now as proxy for when the image was taken
   // TODO(ben): the image should come with a timestamp, parker
   // will know how to get it.
@@ -66,6 +81,7 @@
                reinterpret_cast<const char *>(buffers_[buf.index].start),
                buf.bytesused),
            time);
+
   QueueBuffer(&buf);
 }
 
@@ -105,7 +121,7 @@
     }
   }
   queued_ = kNumBuffers;
-  if (req.count < kNumBuffers) {
+  if (req.count != kNumBuffers) {
     LOG(FATAL, "Insufficient buffer memory on %s\n", dev_name_.c_str());
   }
 }
@@ -236,9 +252,6 @@
       setfps->parm.capture.timeperframe.numerator,
       setfps->parm.capture.timeperframe.denominator);
   // #endif
-
-  InitMMap();
-  LOG(INFO, "Bat Vision Successfully Initialized.\n");
 }
 
 aos::vision::ImageFormat Reader::get_format() {
diff --git a/aos/vision/image/reader.h b/aos/vision/image/reader.h
index eabb80a..28735fb 100644
--- a/aos/vision/image/reader.h
+++ b/aos/vision/image/reader.h
@@ -41,8 +41,8 @@
 
   void HandleFrame();
   void StartAsync() {
-    Start();
     MMapBuffers();
+    Start();
   }
   int fd() { return fd_; }
 
@@ -60,6 +60,7 @@
 
   ProcessCb process_;
 
+  int tick_id_ = 0;
   // The number of buffers currently queued in v4l2.
   uint32_t queued_;
   struct Buffer;
@@ -67,7 +68,10 @@
   // because the buffers are not ummapped.
   Buffer *buffers_;
 
-  static const unsigned int kNumBuffers = 10;
+  // TODO(parker): The timestamps should be queue insertion timestamps
+  // which will remove the impact of kNumBuffers.
+  // TODO(parker): Flush the queue (or tweak the FPS) if we fall behind.
+  static const unsigned int kNumBuffers = 5;
 
   // set only at initialize
   CameraParams params_;