Fix DMA pulse width reader
Didn't work with multiple readers because we were using the last sample
time, not the last time the reading went high.
Signed-off-by: milind-u <milind.upadhyay@gmail.com>
Change-Id: Ieedef0e8500f61d2881569bc7dfb56fbe843a97f
diff --git a/frc971/wpilib/dma_edge_counting.cc b/frc971/wpilib/dma_edge_counting.cc
index ca2a10e..8864116 100644
--- a/frc971/wpilib/dma_edge_counting.cc
+++ b/frc971/wpilib/dma_edge_counting.cc
@@ -25,8 +25,12 @@
}
void DMAPulseWidthReader::UpdateFromSample(const DMASample &sample) {
- if (have_prev_sample_ && prev_sample_.Get(input_) && !sample.Get(input_)) {
- last_width_ = sample.GetTimestamp() - prev_sample_.GetTimestamp();
+ if (have_prev_sample_ && high_time_ != 0 && prev_sample_.Get(input_) &&
+ !sample.Get(input_)) {
+ last_width_ = (sample.GetTime() - high_time_) * 0.000001;
+ } else if (have_prev_sample_ && !prev_sample_.Get(input_) &&
+ sample.Get(input_)) {
+ high_time_ = prev_sample_.GetTime();
}
have_prev_sample_ = true;
prev_sample_ = sample;
diff --git a/frc971/wpilib/dma_edge_counting.h b/frc971/wpilib/dma_edge_counting.h
index 2d49ecb..a945909 100644
--- a/frc971/wpilib/dma_edge_counting.h
+++ b/frc971/wpilib/dma_edge_counting.h
@@ -5,15 +5,13 @@
#include <optional>
#include <vector>
-#include "aos/macros.h"
-
#include "aos/containers/sized_array.h"
-#include "frc971/wpilib/ahal/Utility.h"
-#include "frc971/wpilib/dma.h"
-
+#include "aos/macros.h"
#include "frc971/wpilib/ahal/AnalogInput.h"
#include "frc971/wpilib/ahal/DigitalInput.h"
#include "frc971/wpilib/ahal/Encoder.h"
+#include "frc971/wpilib/ahal/Utility.h"
+#include "frc971/wpilib/dma.h"
#undef ERROR
namespace frc971 {
@@ -47,6 +45,7 @@
void set_input(frc::DigitalInput *input) { input_ = input; }
+ // Last pulse width in seconds
double last_width() const { return last_width_; }
private:
@@ -65,6 +64,8 @@
DMASample prev_sample_;
// Whether or not we actually have anything in prev_sample_.
bool have_prev_sample_ = false;
+ // Last time the reading switched to high
+ uint64_t high_time_ = 0;
double last_width_ = ::std::numeric_limits<double>::quiet_NaN();