blob: ba8fdf48bf71797c7235b41f8b22229448ea7822 [file] [log] [blame]
Brian Silvermanff7b3472015-01-26 17:53:04 -05001#include "frc971/wpilib/dma_edge_counting.h"
2
John Park33858a32018-09-28 23:05:48 -07003#include "aos/logging/logging.h"
Brian Silvermanff7b3472015-01-26 17:53:04 -05004
5namespace frc971 {
6namespace wpilib {
7
Brian Silverman552350b2015-08-02 18:23:34 -07008bool DMAEdgeCounter::ExtractValue(const DMASample &sample) const {
Brian Silverman03a00cf2015-08-29 19:26:54 -07009 return sample.Get(input_);
Brian Silvermanff7b3472015-01-26 17:53:04 -050010}
11
12void DMAEdgeCounter::UpdateFromSample(const DMASample &sample) {
Brian Silverman03a00cf2015-08-29 19:26:54 -070013 const bool previous_value =
Austin Schuhf83da152017-03-05 22:28:45 -080014 have_prev_sample_ ? ExtractValue(prev_sample_) : previous_polled_value_;
Brian Silverman03a00cf2015-08-29 19:26:54 -070015 have_prev_sample_ = true;
Brian Silvermanff7b3472015-01-26 17:53:04 -050016 prev_sample_ = sample;
Brian Silverman03a00cf2015-08-29 19:26:54 -070017
18 if (!previous_value && ExtractValue(sample)) {
19 pos_edge_count_++;
Brian Silverman03a00cf2015-08-29 19:26:54 -070020 pos_last_encoder_ = sample.GetRaw(encoder_);
21 } else if (previous_value && !ExtractValue(sample)) {
22 neg_edge_count_++;
Brian Silverman03a00cf2015-08-29 19:26:54 -070023 neg_last_encoder_ = sample.GetRaw(encoder_);
24 }
Brian Silvermanff7b3472015-01-26 17:53:04 -050025}
26
Austin Schuh8e5950d2018-03-21 20:29:40 -070027void DMAPulseWidthReader::UpdateFromSample(const DMASample &sample) {
milind-u6b672f82023-02-24 17:36:27 -080028 if (have_prev_sample_ && high_time_ != 0 && prev_sample_.Get(input_) &&
29 !sample.Get(input_)) {
milind-ua2e612b2023-02-24 18:00:59 -080030 last_width_ = (sample.GetTime() - high_time_) * 1e-6;
31 high_time_ = 0;
32 poll_count_ = 0;
milind-u6b672f82023-02-24 17:36:27 -080033 } else if (have_prev_sample_ && !prev_sample_.Get(input_) &&
34 sample.Get(input_)) {
35 high_time_ = prev_sample_.GetTime();
milind-ua2e612b2023-02-24 18:00:59 -080036 poll_count_ = 0;
Austin Schuh8e5950d2018-03-21 20:29:40 -070037 }
38 have_prev_sample_ = true;
39 prev_sample_ = sample;
40}
41
milind-ua2e612b2023-02-24 18:00:59 -080042void DMAPulseWidthReader::UpdatePolledValue() {
43 // If we are polled without an update for too long, reset
44 constexpr size_t kMaxPollCount = 4;
45 if (poll_count_ > kMaxPollCount) {
46 high_time_ = 0;
47 have_prev_sample_ = false;
48 last_width_ = ::std::numeric_limits<double>::quiet_NaN();
49 }
50 poll_count_++;
51}
52
Ravago Jones128a50e2021-09-18 15:19:39 -070053void DMAPulseSeparationReader::UpdateFromSample(const DMASample &sample) {
Austin Schuh6c053ef2021-09-26 14:32:16 -070054 // save the time of the falling edge of the input one
55 if (have_prev_sample_ && !sample.Get(input_one_) &&
56 prev_sample_.Get(input_one_)) {
Ravago Jones128a50e2021-09-18 15:19:39 -070057 input_one_time_ = sample.GetTimestamp();
58 }
59
Austin Schuh6c053ef2021-09-26 14:32:16 -070060 // take the difference in time between the falling edge of the input one and
61 // the falling edge of the input two
62 if (!sample.Get(input_two_) && input_one_time_.has_value()) {
Ravago Jones128a50e2021-09-18 15:19:39 -070063 last_width_ = sample.GetTimestamp() - input_one_time_.value();
64 pulses_detected_++;
65 input_one_time_.reset();
66 }
Austin Schuh6c053ef2021-09-26 14:32:16 -070067
68 have_prev_sample_ = true;
69 prev_sample_ = sample;
Ravago Jones128a50e2021-09-18 15:19:39 -070070}
71
Brian Silvermanff7b3472015-01-26 17:53:04 -050072void DMASynchronizer::CheckDMA() {
73 DMASample current_sample;
74
75 size_t remaining = 0;
76 while (true) {
77 switch (dma_->Read(&current_sample, 0, &remaining)) {
78 case DMA::STATUS_OK:
79 for (auto &c : handlers_) {
80 c->UpdateFromSample(current_sample);
81 }
82
83 if (remaining == 0) {
Austin Schuhf853bde2017-11-23 13:23:27 -080084 if (sample_time_ < static_cast<int64_t>(current_sample.GetTime())) {
Brian Silvermanff7b3472015-01-26 17:53:04 -050085 // If the latest DMA sample happened after we started polling, then
86 // just use the values from it because they're more recent.
87 for (auto &c : handlers_) {
88 c->PollFromSample(current_sample);
89 }
90 }
91 return;
92 }
Austin Schuh8e5950d2018-03-21 20:29:40 -070093 break;
Brian Silvermanff7b3472015-01-26 17:53:04 -050094 case DMA::STATUS_TIMEOUT:
95 return;
96 case DMA::STATUS_ERROR:
Austin Schuhf257f3c2019-10-27 21:00:43 -070097 AOS_LOG(WARNING, "DMA read failed\n");
Brian Silvermanff7b3472015-01-26 17:53:04 -050098 break;
99 }
100 }
101}
102
103} // namespace wpilib
104} // namespace frc971