Make DMA pulse width work

The sensors return 1 when there is no ball...

Also fix a move before get to a get before move to fix a segfault

Change-Id: If7ebcda52c18f011eb7583a494eb52a8d038cb1b
Signed-off-by: Austin Schuh <austin.linux@gmail.com>
diff --git a/frc971/wpilib/dma.cc b/frc971/wpilib/dma.cc
index 432b77b..6cc8bf0 100644
--- a/frc971/wpilib/dma.cc
+++ b/frc971/wpilib/dma.cc
@@ -7,6 +7,7 @@
 #include "frc971/wpilib/ahal/AnalogInput.h"
 #include "frc971/wpilib/ahal/DigitalSource.h"
 #include "frc971/wpilib/ahal/Encoder.h"
+#include "glog/logging.h"
 #include "hal/HAL.h"
 
 // Interface to the roboRIO FPGA's DMA features.
@@ -142,6 +143,7 @@
 
 void DMA::SetExternalTrigger(frc::DigitalSource *input, bool rising,
                              bool falling) {
+  CHECK(input);
   tRioStatusCode status = 0;
 
   if (manager_) {
diff --git a/frc971/wpilib/dma_edge_counting.cc b/frc971/wpilib/dma_edge_counting.cc
index ca7e095..ca2a10e 100644
--- a/frc971/wpilib/dma_edge_counting.cc
+++ b/frc971/wpilib/dma_edge_counting.cc
@@ -33,22 +33,22 @@
 }
 
 void DMAPulseSeparationReader::UpdateFromSample(const DMASample &sample) {
-  // save the time of the rising edge of the input one
-  if (have_prev_sample_ && sample.Get(input_one_) &&
-      !prev_sample_.Get(input_one_)) {
+  // save the time of the falling edge of the input one
+  if (have_prev_sample_ && !sample.Get(input_one_) &&
+      prev_sample_.Get(input_one_)) {
     input_one_time_ = sample.GetTimestamp();
   }
 
-  have_prev_sample_ = true;
-  prev_sample_ = sample;
-
-  // take the difference in time between the rising edge of the input one and
-  // the rising edge of the input two
-  if (sample.Get(input_two_) && input_one_time_.has_value()) {
+  // take the difference in time between the falling edge of the input one and
+  // the falling edge of the input two
+  if (!sample.Get(input_two_) && input_one_time_.has_value()) {
     last_width_ = sample.GetTimestamp() - input_one_time_.value();
     pulses_detected_++;
     input_one_time_.reset();
   }
+
+  have_prev_sample_ = true;
+  prev_sample_ = sample;
 }
 
 void DMASynchronizer::CheckDMA() {
diff --git a/frc971/wpilib/dma_edge_counting.h b/frc971/wpilib/dma_edge_counting.h
index 03ceeb0..d8fe476 100644
--- a/frc971/wpilib/dma_edge_counting.h
+++ b/frc971/wpilib/dma_edge_counting.h
@@ -71,7 +71,7 @@
 };
 
 // Takes two digital inputs and times the difference between the first one going
-// high and the second one going high.
+// low and the second one going low.
 class DMAPulseSeparationReader : public DMASampleHandlerInterface {
  public:
   DMAPulseSeparationReader(frc::DigitalInput *input_one,
@@ -94,7 +94,7 @@
     dma->Add(input_one_);
     dma->SetExternalTrigger(input_one_, true, true);
     dma->Add(input_two_);
-    dma->SetExternalTrigger(input_two_, true, false);
+    dma->SetExternalTrigger(input_two_, false, true);
   }
 
   static constexpr double kSampleTimeoutSeconds = 0.1;
@@ -107,7 +107,7 @@
   // Whether or not we actually have anything in prev_sample_.
   bool have_prev_sample_ = false;
 
-  // the time when the input one went high.
+  // the time when the input one went low.
   std::optional<double> input_one_time_;
 
   int pulses_detected_ = 0;
diff --git a/y2020/control_loops/superstructure/shooter/shooter_tuning_readings.fbs b/y2020/control_loops/superstructure/shooter/shooter_tuning_readings.fbs
index ca10302..ae0c379 100644
--- a/y2020/control_loops/superstructure/shooter/shooter_tuning_readings.fbs
+++ b/y2020/control_loops/superstructure/shooter/shooter_tuning_readings.fbs
@@ -1,14 +1,14 @@
-namespace y2020.control_loops.superstructure.shooter;

-

-// Contains the readings used to tune the accelerator

-// and finisher velocities to minimize variation in ball velocity.

-// We have two sensors, and find the time that the ball takes to

-// pass between the two to find its velocity.

-// This will be sent each time a ball is detected.

-table TuningReadings {

-  // The velocity (m/s) of the last ball passing between the two

-  // sensors

-  velocity_ball:double (id: 0);

-}

-

-root_type TuningReadings;
\ No newline at end of file
+namespace y2020.control_loops.superstructure.shooter;
+
+// Contains the readings used to tune the accelerator
+// and finisher velocities to minimize variation in ball velocity.
+// We have two sensors, and find the time that the ball takes to
+// pass between the two to find its velocity.
+// This will be sent each time a ball is detected.
+table TuningReadings {
+  // The velocity (m/s) of the last ball passing between the two
+  // sensors
+  velocity_ball:double (id: 0);
+}
+
+root_type TuningReadings;
diff --git a/y2020/wpilib_interface.cc b/y2020/wpilib_interface.cc
index c79ba88..3e91d1e 100644
--- a/y2020/wpilib_interface.cc
+++ b/y2020/wpilib_interface.cc
@@ -207,8 +207,8 @@
                                  ::std::unique_ptr<frc::DigitalInput> sensor2) {
     ball_beambreak_inputs_[0] = ::std::move(sensor1);
     ball_beambreak_inputs_[1] = ::std::move(sensor2);
-    ball_beambreak_reader_.set_input_one(sensor1.get());
-    ball_beambreak_reader_.set_input_two(sensor2.get());
+    ball_beambreak_reader_.set_input_one(ball_beambreak_inputs_[0].get());
+    ball_beambreak_reader_.set_input_two(ball_beambreak_inputs_[1].get());
   }
 
   void Start() override {
@@ -316,19 +316,20 @@
       builder.Send(auto_mode_builder.Finish());
     }
 
-    if (FLAGS_shooter_tuning &&
-        ball_beambreak_reader_.pulses_detected() > balls_detected_) {
-      balls_detected_ = ball_beambreak_reader_.pulses_detected();
-
+    if (FLAGS_shooter_tuning) {
       // Distance between beambreak sensors, in meters.
       constexpr double kDistanceBetweenBeambreaks = 0.4813;
 
-      auto builder = shooter_tuning_readings_sender_.MakeBuilder();
-      auto shooter_tuning_readings_builder =
-          builder.MakeBuilder<superstructure::shooter::TuningReadings>();
-      shooter_tuning_readings_builder.add_velocity_ball(
-          kDistanceBetweenBeambreaks / ball_beambreak_reader_.last_width());
-      builder.Send(shooter_tuning_readings_builder.Finish());
+      if (ball_beambreak_reader_.pulses_detected() > balls_detected_) {
+        balls_detected_ = ball_beambreak_reader_.pulses_detected();
+
+        auto builder = shooter_tuning_readings_sender_.MakeBuilder();
+        auto shooter_tuning_readings_builder =
+            builder.MakeBuilder<superstructure::shooter::TuningReadings>();
+        shooter_tuning_readings_builder.add_velocity_ball(
+            kDistanceBetweenBeambreaks / ball_beambreak_reader_.last_width());
+        builder.Send(shooter_tuning_readings_builder.Finish());
+      }
     }
   }