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;