blob: 4d1246c0baacbde282265e71411e823975073e81 [file] [log] [blame]
Brian Silvermanff7b3472015-01-26 17:53:04 -05001#ifndef FRC971_WPILIB_DMA_EDGE_COUNTING_H_
2#define FRC971_WPILIB_DMA_EDGE_COUNTING_H_
3
4#include <memory>
Ravago Jones128a50e2021-09-18 15:19:39 -07005#include <optional>
Brian Silvermanff7b3472015-01-26 17:53:04 -05006#include <vector>
7
Austin Schuhb1aaefe2021-10-12 22:05:44 -07008#include "aos/containers/sized_array.h"
milind-u6b672f82023-02-24 17:36:27 -08009#include "aos/macros.h"
Parker Schuhd3b7a8872018-02-19 16:42:27 -080010#include "frc971/wpilib/ahal/AnalogInput.h"
11#include "frc971/wpilib/ahal/DigitalInput.h"
12#include "frc971/wpilib/ahal/Encoder.h"
milind-u6b672f82023-02-24 17:36:27 -080013#include "frc971/wpilib/ahal/Utility.h"
14#include "frc971/wpilib/dma.h"
Austin Schuh5c25ab72015-11-01 13:17:11 -080015#undef ERROR
Brian Silvermanff7b3472015-01-26 17:53:04 -050016
17namespace frc971 {
18namespace wpilib {
19
20// Generic interface for classes that do something with DMA samples and also
21// poll current sensor values.
22class DMASampleHandlerInterface {
23 public:
24 virtual ~DMASampleHandlerInterface() {}
25
26 // Updates values based on a new DMA sample.
27 virtual void UpdateFromSample(const DMASample &sample) = 0;
28
29 // Polls the current values and saves them for later reference.
30 virtual void UpdatePolledValue() = 0;
31
32 // Fills in the "polled" values from sample.
33 // This is only called when a DMA event happens right as we're polling values.
34 virtual void PollFromSample(const DMASample &sample) = 0;
35
36 // Adds readings and triggers appropriate to this object to dma.
37 virtual void AddToDMA(DMA *dma) = 0;
38};
39
Austin Schuh8e5950d2018-03-21 20:29:40 -070040// TODO(brian): Timeout old data.
41class DMAPulseWidthReader : public DMASampleHandlerInterface {
42 public:
Parker Schuhd3b7a8872018-02-19 16:42:27 -080043 DMAPulseWidthReader(frc::DigitalInput *input) : input_(input) {}
Austin Schuh8e5950d2018-03-21 20:29:40 -070044 DMAPulseWidthReader() = default;
45
Parker Schuhd3b7a8872018-02-19 16:42:27 -080046 void set_input(frc::DigitalInput *input) { input_ = input; }
Austin Schuh8e5950d2018-03-21 20:29:40 -070047
milind-u6b672f82023-02-24 17:36:27 -080048 // Last pulse width in seconds
Austin Schuh8e5950d2018-03-21 20:29:40 -070049 double last_width() const { return last_width_; }
50
51 private:
52 void UpdateFromSample(const DMASample & /*sample*/) override;
milind-ua2e612b2023-02-24 18:00:59 -080053 void UpdatePolledValue() override;
Austin Schuh8e5950d2018-03-21 20:29:40 -070054
55 void PollFromSample(const DMASample & /*sample*/) override {}
56 void AddToDMA(DMA *dma) override {
57 dma->Add(input_);
58 dma->SetExternalTrigger(input_, true, true);
59 }
60
Parker Schuhd3b7a8872018-02-19 16:42:27 -080061 frc::DigitalInput *input_ = nullptr;
Austin Schuh8e5950d2018-03-21 20:29:40 -070062
63 // The last DMA reading we got.
64 DMASample prev_sample_;
65 // Whether or not we actually have anything in prev_sample_.
66 bool have_prev_sample_ = false;
milind-u6b672f82023-02-24 17:36:27 -080067 // Last time the reading switched to high
68 uint64_t high_time_ = 0;
milind-ua2e612b2023-02-24 18:00:59 -080069 // Number of times we've been polled without an update
70 size_t poll_count_ = 0;
Austin Schuh8e5950d2018-03-21 20:29:40 -070071
72 double last_width_ = ::std::numeric_limits<double>::quiet_NaN();
73
74 DISALLOW_COPY_AND_ASSIGN(DMAPulseWidthReader);
75};
76
Ravago Jones128a50e2021-09-18 15:19:39 -070077// Takes two digital inputs and times the difference between the first one going
Austin Schuh6c053ef2021-09-26 14:32:16 -070078// low and the second one going low.
Ravago Jones128a50e2021-09-18 15:19:39 -070079class DMAPulseSeparationReader : public DMASampleHandlerInterface {
80 public:
81 DMAPulseSeparationReader(frc::DigitalInput *input_one,
82 frc::DigitalInput *input_two)
83 : input_one_(input_one), input_two_(input_two) {}
84 DMAPulseSeparationReader() = default;
85
86 void set_input_one(frc::DigitalInput *input) { input_one_ = input; }
87 void set_input_two(frc::DigitalInput *input) { input_two_ = input; }
88
89 double last_width() const { return last_width_; }
90 double pulses_detected() const { return pulses_detected_; }
91
92 private:
93 void UpdateFromSample(const DMASample & /*sample*/) override;
94 void UpdatePolledValue() override {}
95
96 void PollFromSample(const DMASample & /*sample*/) override {}
97 void AddToDMA(DMA *dma) override {
98 dma->Add(input_one_);
99 dma->SetExternalTrigger(input_one_, true, true);
100 dma->Add(input_two_);
Austin Schuh6c053ef2021-09-26 14:32:16 -0700101 dma->SetExternalTrigger(input_two_, false, true);
Ravago Jones128a50e2021-09-18 15:19:39 -0700102 }
103
104 static constexpr double kSampleTimeoutSeconds = 0.1;
105
106 frc::DigitalInput *input_one_ = nullptr;
107 frc::DigitalInput *input_two_ = nullptr;
108
109 // The last DMA reading we got.
110 DMASample prev_sample_;
111 // Whether or not we actually have anything in prev_sample_.
112 bool have_prev_sample_ = false;
113
Austin Schuh6c053ef2021-09-26 14:32:16 -0700114 // the time when the input one went low.
Ravago Jones128a50e2021-09-18 15:19:39 -0700115 std::optional<double> input_one_time_;
116
117 int pulses_detected_ = 0;
118
119 double last_width_ = ::std::numeric_limits<double>::quiet_NaN();
120
121 DISALLOW_COPY_AND_ASSIGN(DMAPulseSeparationReader);
122};
123
Brian Silvermanff7b3472015-01-26 17:53:04 -0500124// Counts edges on a sensor using DMA data and latches encoder values
125// corresponding to those edges.
126class DMAEdgeCounter : public DMASampleHandlerInterface {
127 public:
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800128 DMAEdgeCounter(frc::Encoder *encoder, frc::DigitalInput *input)
Brian Silverman03a00cf2015-08-29 19:26:54 -0700129 : encoder_(encoder), input_(input) {}
Brianef030df2017-03-05 15:06:04 -0800130 DMAEdgeCounter() = default;
131
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800132 void set_encoder(frc::Encoder *encoder) { encoder_ = encoder; }
133 void set_input(frc::DigitalInput *input) { input_ = input; }
Brian Silvermanff7b3472015-01-26 17:53:04 -0500134
Brian Silverman552350b2015-08-02 18:23:34 -0700135 int positive_count() const { return pos_edge_count_; }
136 int negative_count() const { return neg_edge_count_; }
137 int last_positive_encoder_value() const { return pos_last_encoder_; }
138 int last_negative_encoder_value() const { return neg_last_encoder_; }
Brian Silvermanff7b3472015-01-26 17:53:04 -0500139
140 // Returns the value of the sensor in the last-read DMA sample.
Brian Silverman552350b2015-08-02 18:23:34 -0700141 bool last_value() const { return ExtractValue(prev_sample_); }
Brian Silvermanff7b3472015-01-26 17:53:04 -0500142 // Returns the most recent polled value of the sensor.
143 bool polled_value() const { return polled_value_; }
144 // Returns the most recent polled reading from the encoder.
145 int polled_encoder() const { return polled_encoder_; }
146
147 private:
Brian Silverman552350b2015-08-02 18:23:34 -0700148 void UpdateFromSample(const DMASample &sample) override;
149 void UpdatePolledValue() override {
Austin Schuhf83da152017-03-05 22:28:45 -0800150 previous_polled_value_ = polled_value_;
Brian Silverman552350b2015-08-02 18:23:34 -0700151 polled_value_ = input_->Get();
152 polled_encoder_ = encoder_->GetRaw();
153 }
154 void PollFromSample(const DMASample &sample) override {
Austin Schuhf83da152017-03-05 22:28:45 -0800155 previous_polled_value_ = polled_value_;
Brian Silverman552350b2015-08-02 18:23:34 -0700156 polled_value_ = ExtractValue(sample);
157 polled_encoder_ = sample.GetRaw(encoder_);
158 }
159 void AddToDMA(DMA *dma) override {
160 dma->Add(encoder_);
161 dma->Add(input_);
Brian Silverman03a00cf2015-08-29 19:26:54 -0700162 dma->SetExternalTrigger(input_, true, true);
Brian Silverman552350b2015-08-02 18:23:34 -0700163 }
164
165 bool ExtractValue(const DMASample &sample) const;
Brian Silvermanff7b3472015-01-26 17:53:04 -0500166
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800167 frc::Encoder *encoder_ = nullptr;
168 frc::DigitalInput *input_ = nullptr;
Brian Silvermanff7b3472015-01-26 17:53:04 -0500169
170 // The last DMA reading we got.
171 DMASample prev_sample_;
172 // Whether or not we actually have anything in prev_sample_.
173 bool have_prev_sample_ = false;
174
175 // Values related to the positive edge.
176 int pos_edge_count_ = 0;
Brian Silvermanff7b3472015-01-26 17:53:04 -0500177 int pos_last_encoder_ = 0;
178
179 // Values related to the negative edge.
180 int neg_edge_count_ = 0;
Brian Silvermanff7b3472015-01-26 17:53:04 -0500181 int neg_last_encoder_ = 0;
182
183 bool polled_value_ = false;
Austin Schuhf83da152017-03-05 22:28:45 -0800184 bool previous_polled_value_ = false;
Brian Silvermanff7b3472015-01-26 17:53:04 -0500185 int polled_encoder_ = 0;
186
187 DISALLOW_COPY_AND_ASSIGN(DMAEdgeCounter);
188};
189
Brian Silverman552350b2015-08-02 18:23:34 -0700190// Reads a hall effect in sync with DMA samples.
191class DMADigitalReader : public DMASampleHandlerInterface {
192 public:
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800193 DMADigitalReader(frc::DigitalInput *input) : input_(input) {}
Brian Silverman552350b2015-08-02 18:23:34 -0700194
195 bool value() const { return value_; }
196
197 private:
198 void UpdateFromSample(const DMASample & /*sample*/) override {}
199 void UpdatePolledValue() override { value_ = input_->Get(); }
200 void PollFromSample(const DMASample &sample) override {
Brian Silverman03a00cf2015-08-29 19:26:54 -0700201 value_ = sample.Get(input_);
Brian Silverman552350b2015-08-02 18:23:34 -0700202 }
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800203 void AddToDMA(DMA *dma) override { dma->Add(input_); }
Brian Silverman552350b2015-08-02 18:23:34 -0700204
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800205 frc::DigitalInput *const input_;
Brian Silverman552350b2015-08-02 18:23:34 -0700206
207 bool value_;
208
209 DISALLOW_COPY_AND_ASSIGN(DMADigitalReader);
210};
211
Austin Schuh89f1e092015-11-22 22:30:39 -0800212// Reads an analog sensor in sync with DMA samples.
213class DMAAnalogReader : public DMASampleHandlerInterface {
214 public:
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800215 DMAAnalogReader(frc::AnalogInput *input) : input_(input) {}
Austin Schuh89f1e092015-11-22 22:30:39 -0800216
217 double value() const { return value_; }
218
219 private:
220 void UpdateFromSample(const DMASample & /*sample*/) override {}
221 void UpdatePolledValue() override { value_ = input_->GetVoltage(); }
222 void PollFromSample(const DMASample &sample) override {
223 value_ = sample.GetVoltage(input_);
224 }
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800225 void AddToDMA(DMA *dma) override { dma->Add(input_); }
Austin Schuh89f1e092015-11-22 22:30:39 -0800226
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800227 frc::AnalogInput *const input_;
Austin Schuh89f1e092015-11-22 22:30:39 -0800228
229 double value_;
230
231 DISALLOW_COPY_AND_ASSIGN(DMAAnalogReader);
232};
233
Brian Silvermanff7b3472015-01-26 17:53:04 -0500234// This class handles updating the sampled data on multiple
235// DMASampleHandlerInterfaces. The caller should create an instance and then
236// periodically call RunIteration, retrieving whatever data from the
237// DMASampleHandlerInterfaces after each iteration.
238class DMASynchronizer {
239 public:
240 DMASynchronizer(::std::unique_ptr<DMA> dma) : dma_(::std::move(dma)) {}
241
242 // Adds a new handler to this object. This method must not be called after
243 // Start().
244 void Add(DMASampleHandlerInterface *handler) {
245 handler->AddToDMA(dma_.get());
Austin Schuhb1aaefe2021-10-12 22:05:44 -0700246 handlers_.push_back(handler);
Brian Silvermanff7b3472015-01-26 17:53:04 -0500247 }
248
249 // Actually starts watching for DMA samples.
250 // Add may not be called any more after this.
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800251 void Start() { dma_->Start(1024); }
Brian Silvermanff7b3472015-01-26 17:53:04 -0500252
253 // Updates all sensor values.
254 void RunIteration() {
255 SampleSensors();
256 CheckDMA();
257 }
258
259 private:
260 // Reads the state of all the sensors and records it as the polled values of
261 // all the inputs.
262 void SampleSensors() {
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800263 sample_time_ = frc::GetFPGATime();
Brian Silvermanff7b3472015-01-26 17:53:04 -0500264 for (auto &c : handlers_) {
265 c->UpdatePolledValue();
266 }
267 }
268
269 // Gets called by the DMA handler to update edge counts.
270 void CheckDMA();
271
272 const ::std::unique_ptr<DMA> dma_;
Austin Schuhb1aaefe2021-10-12 22:05:44 -0700273 aos::SizedArray<DMASampleHandlerInterface *, 4> handlers_;
Brian Silvermanff7b3472015-01-26 17:53:04 -0500274
275 // The time at which we most recently read the sensor values.
Austin Schuhf853bde2017-11-23 13:23:27 -0800276 int64_t sample_time_ = 0;
Brian Silvermanff7b3472015-01-26 17:53:04 -0500277
278 DISALLOW_COPY_AND_ASSIGN(DMASynchronizer);
279};
280
281} // namespace wpilib
282} // namespace frc971
283
284#endif // FRC971_WPILIB_DMA_EDGE_COUNTING_H_