blob: 4e8a15870828b0cca3a8e8c47653dc931ac1728e [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;
milind-ua2e612b2023-02-24 18:00:59 -080031 poll_count_ = 0;
milind-u6b672f82023-02-24 17:36:27 -080032 } else if (have_prev_sample_ && !prev_sample_.Get(input_) &&
33 sample.Get(input_)) {
milind-u3a7f9212023-02-24 20:46:59 -080034 last_period_ = (sample.GetTime() - high_time_) * 1e-6;
35 high_time_ = 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();
milind-u3a7f9212023-02-24 20:46:59 -080049 last_period_ = ::std::numeric_limits<double>::quiet_NaN();
milind-ua2e612b2023-02-24 18:00:59 -080050 }
51 poll_count_++;
52}
53
Ravago Jones128a50e2021-09-18 15:19:39 -070054void DMAPulseSeparationReader::UpdateFromSample(const DMASample &sample) {
Austin Schuh6c053ef2021-09-26 14:32:16 -070055 // save the time of the falling edge of the input one
56 if (have_prev_sample_ && !sample.Get(input_one_) &&
57 prev_sample_.Get(input_one_)) {
Ravago Jones128a50e2021-09-18 15:19:39 -070058 input_one_time_ = sample.GetTimestamp();
59 }
60
Austin Schuh6c053ef2021-09-26 14:32:16 -070061 // take the difference in time between the falling edge of the input one and
62 // the falling edge of the input two
63 if (!sample.Get(input_two_) && input_one_time_.has_value()) {
Ravago Jones128a50e2021-09-18 15:19:39 -070064 last_width_ = sample.GetTimestamp() - input_one_time_.value();
65 pulses_detected_++;
66 input_one_time_.reset();
67 }
Austin Schuh6c053ef2021-09-26 14:32:16 -070068
69 have_prev_sample_ = true;
70 prev_sample_ = sample;
Ravago Jones128a50e2021-09-18 15:19:39 -070071}
72
Brian Silvermanff7b3472015-01-26 17:53:04 -050073void DMASynchronizer::CheckDMA() {
74 DMASample current_sample;
75
76 size_t remaining = 0;
77 while (true) {
78 switch (dma_->Read(&current_sample, 0, &remaining)) {
79 case DMA::STATUS_OK:
80 for (auto &c : handlers_) {
81 c->UpdateFromSample(current_sample);
82 }
83
84 if (remaining == 0) {
Austin Schuhf853bde2017-11-23 13:23:27 -080085 if (sample_time_ < static_cast<int64_t>(current_sample.GetTime())) {
Brian Silvermanff7b3472015-01-26 17:53:04 -050086 // If the latest DMA sample happened after we started polling, then
87 // just use the values from it because they're more recent.
88 for (auto &c : handlers_) {
89 c->PollFromSample(current_sample);
90 }
91 }
92 return;
93 }
Austin Schuh8e5950d2018-03-21 20:29:40 -070094 break;
Brian Silvermanff7b3472015-01-26 17:53:04 -050095 case DMA::STATUS_TIMEOUT:
96 return;
97 case DMA::STATUS_ERROR:
Austin Schuhf257f3c2019-10-27 21:00:43 -070098 AOS_LOG(WARNING, "DMA read failed\n");
Brian Silvermanff7b3472015-01-26 17:53:04 -050099 break;
100 }
101 }
102}
103
104} // namespace wpilib
105} // namespace frc971