blob: a945909c91b9eacb840f1f3c55ce24297ac4fc16 [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;
53 void UpdatePolledValue() override {}
54
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;
Austin Schuh8e5950d2018-03-21 20:29:40 -070069
70 double last_width_ = ::std::numeric_limits<double>::quiet_NaN();
71
72 DISALLOW_COPY_AND_ASSIGN(DMAPulseWidthReader);
73};
74
Ravago Jones128a50e2021-09-18 15:19:39 -070075// Takes two digital inputs and times the difference between the first one going
Austin Schuh6c053ef2021-09-26 14:32:16 -070076// low and the second one going low.
Ravago Jones128a50e2021-09-18 15:19:39 -070077class DMAPulseSeparationReader : public DMASampleHandlerInterface {
78 public:
79 DMAPulseSeparationReader(frc::DigitalInput *input_one,
80 frc::DigitalInput *input_two)
81 : input_one_(input_one), input_two_(input_two) {}
82 DMAPulseSeparationReader() = default;
83
84 void set_input_one(frc::DigitalInput *input) { input_one_ = input; }
85 void set_input_two(frc::DigitalInput *input) { input_two_ = input; }
86
87 double last_width() const { return last_width_; }
88 double pulses_detected() const { return pulses_detected_; }
89
90 private:
91 void UpdateFromSample(const DMASample & /*sample*/) override;
92 void UpdatePolledValue() override {}
93
94 void PollFromSample(const DMASample & /*sample*/) override {}
95 void AddToDMA(DMA *dma) override {
96 dma->Add(input_one_);
97 dma->SetExternalTrigger(input_one_, true, true);
98 dma->Add(input_two_);
Austin Schuh6c053ef2021-09-26 14:32:16 -070099 dma->SetExternalTrigger(input_two_, false, true);
Ravago Jones128a50e2021-09-18 15:19:39 -0700100 }
101
102 static constexpr double kSampleTimeoutSeconds = 0.1;
103
104 frc::DigitalInput *input_one_ = nullptr;
105 frc::DigitalInput *input_two_ = nullptr;
106
107 // The last DMA reading we got.
108 DMASample prev_sample_;
109 // Whether or not we actually have anything in prev_sample_.
110 bool have_prev_sample_ = false;
111
Austin Schuh6c053ef2021-09-26 14:32:16 -0700112 // the time when the input one went low.
Ravago Jones128a50e2021-09-18 15:19:39 -0700113 std::optional<double> input_one_time_;
114
115 int pulses_detected_ = 0;
116
117 double last_width_ = ::std::numeric_limits<double>::quiet_NaN();
118
119 DISALLOW_COPY_AND_ASSIGN(DMAPulseSeparationReader);
120};
121
Brian Silvermanff7b3472015-01-26 17:53:04 -0500122// Counts edges on a sensor using DMA data and latches encoder values
123// corresponding to those edges.
124class DMAEdgeCounter : public DMASampleHandlerInterface {
125 public:
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800126 DMAEdgeCounter(frc::Encoder *encoder, frc::DigitalInput *input)
Brian Silverman03a00cf2015-08-29 19:26:54 -0700127 : encoder_(encoder), input_(input) {}
Brianef030df2017-03-05 15:06:04 -0800128 DMAEdgeCounter() = default;
129
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800130 void set_encoder(frc::Encoder *encoder) { encoder_ = encoder; }
131 void set_input(frc::DigitalInput *input) { input_ = input; }
Brian Silvermanff7b3472015-01-26 17:53:04 -0500132
Brian Silverman552350b2015-08-02 18:23:34 -0700133 int positive_count() const { return pos_edge_count_; }
134 int negative_count() const { return neg_edge_count_; }
135 int last_positive_encoder_value() const { return pos_last_encoder_; }
136 int last_negative_encoder_value() const { return neg_last_encoder_; }
Brian Silvermanff7b3472015-01-26 17:53:04 -0500137
138 // Returns the value of the sensor in the last-read DMA sample.
Brian Silverman552350b2015-08-02 18:23:34 -0700139 bool last_value() const { return ExtractValue(prev_sample_); }
Brian Silvermanff7b3472015-01-26 17:53:04 -0500140 // Returns the most recent polled value of the sensor.
141 bool polled_value() const { return polled_value_; }
142 // Returns the most recent polled reading from the encoder.
143 int polled_encoder() const { return polled_encoder_; }
144
145 private:
Brian Silverman552350b2015-08-02 18:23:34 -0700146 void UpdateFromSample(const DMASample &sample) override;
147 void UpdatePolledValue() override {
Austin Schuhf83da152017-03-05 22:28:45 -0800148 previous_polled_value_ = polled_value_;
Brian Silverman552350b2015-08-02 18:23:34 -0700149 polled_value_ = input_->Get();
150 polled_encoder_ = encoder_->GetRaw();
151 }
152 void PollFromSample(const DMASample &sample) override {
Austin Schuhf83da152017-03-05 22:28:45 -0800153 previous_polled_value_ = polled_value_;
Brian Silverman552350b2015-08-02 18:23:34 -0700154 polled_value_ = ExtractValue(sample);
155 polled_encoder_ = sample.GetRaw(encoder_);
156 }
157 void AddToDMA(DMA *dma) override {
158 dma->Add(encoder_);
159 dma->Add(input_);
Brian Silverman03a00cf2015-08-29 19:26:54 -0700160 dma->SetExternalTrigger(input_, true, true);
Brian Silverman552350b2015-08-02 18:23:34 -0700161 }
162
163 bool ExtractValue(const DMASample &sample) const;
Brian Silvermanff7b3472015-01-26 17:53:04 -0500164
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800165 frc::Encoder *encoder_ = nullptr;
166 frc::DigitalInput *input_ = nullptr;
Brian Silvermanff7b3472015-01-26 17:53:04 -0500167
168 // The last DMA reading we got.
169 DMASample prev_sample_;
170 // Whether or not we actually have anything in prev_sample_.
171 bool have_prev_sample_ = false;
172
173 // Values related to the positive edge.
174 int pos_edge_count_ = 0;
Brian Silvermanff7b3472015-01-26 17:53:04 -0500175 int pos_last_encoder_ = 0;
176
177 // Values related to the negative edge.
178 int neg_edge_count_ = 0;
Brian Silvermanff7b3472015-01-26 17:53:04 -0500179 int neg_last_encoder_ = 0;
180
181 bool polled_value_ = false;
Austin Schuhf83da152017-03-05 22:28:45 -0800182 bool previous_polled_value_ = false;
Brian Silvermanff7b3472015-01-26 17:53:04 -0500183 int polled_encoder_ = 0;
184
185 DISALLOW_COPY_AND_ASSIGN(DMAEdgeCounter);
186};
187
Brian Silverman552350b2015-08-02 18:23:34 -0700188// Reads a hall effect in sync with DMA samples.
189class DMADigitalReader : public DMASampleHandlerInterface {
190 public:
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800191 DMADigitalReader(frc::DigitalInput *input) : input_(input) {}
Brian Silverman552350b2015-08-02 18:23:34 -0700192
193 bool value() const { return value_; }
194
195 private:
196 void UpdateFromSample(const DMASample & /*sample*/) override {}
197 void UpdatePolledValue() override { value_ = input_->Get(); }
198 void PollFromSample(const DMASample &sample) override {
Brian Silverman03a00cf2015-08-29 19:26:54 -0700199 value_ = sample.Get(input_);
Brian Silverman552350b2015-08-02 18:23:34 -0700200 }
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800201 void AddToDMA(DMA *dma) override { dma->Add(input_); }
Brian Silverman552350b2015-08-02 18:23:34 -0700202
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800203 frc::DigitalInput *const input_;
Brian Silverman552350b2015-08-02 18:23:34 -0700204
205 bool value_;
206
207 DISALLOW_COPY_AND_ASSIGN(DMADigitalReader);
208};
209
Austin Schuh89f1e092015-11-22 22:30:39 -0800210// Reads an analog sensor in sync with DMA samples.
211class DMAAnalogReader : public DMASampleHandlerInterface {
212 public:
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800213 DMAAnalogReader(frc::AnalogInput *input) : input_(input) {}
Austin Schuh89f1e092015-11-22 22:30:39 -0800214
215 double value() const { return value_; }
216
217 private:
218 void UpdateFromSample(const DMASample & /*sample*/) override {}
219 void UpdatePolledValue() override { value_ = input_->GetVoltage(); }
220 void PollFromSample(const DMASample &sample) override {
221 value_ = sample.GetVoltage(input_);
222 }
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800223 void AddToDMA(DMA *dma) override { dma->Add(input_); }
Austin Schuh89f1e092015-11-22 22:30:39 -0800224
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800225 frc::AnalogInput *const input_;
Austin Schuh89f1e092015-11-22 22:30:39 -0800226
227 double value_;
228
229 DISALLOW_COPY_AND_ASSIGN(DMAAnalogReader);
230};
231
Brian Silvermanff7b3472015-01-26 17:53:04 -0500232// This class handles updating the sampled data on multiple
233// DMASampleHandlerInterfaces. The caller should create an instance and then
234// periodically call RunIteration, retrieving whatever data from the
235// DMASampleHandlerInterfaces after each iteration.
236class DMASynchronizer {
237 public:
238 DMASynchronizer(::std::unique_ptr<DMA> dma) : dma_(::std::move(dma)) {}
239
240 // Adds a new handler to this object. This method must not be called after
241 // Start().
242 void Add(DMASampleHandlerInterface *handler) {
243 handler->AddToDMA(dma_.get());
Austin Schuhb1aaefe2021-10-12 22:05:44 -0700244 handlers_.push_back(handler);
Brian Silvermanff7b3472015-01-26 17:53:04 -0500245 }
246
247 // Actually starts watching for DMA samples.
248 // Add may not be called any more after this.
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800249 void Start() { dma_->Start(1024); }
Brian Silvermanff7b3472015-01-26 17:53:04 -0500250
251 // Updates all sensor values.
252 void RunIteration() {
253 SampleSensors();
254 CheckDMA();
255 }
256
257 private:
258 // Reads the state of all the sensors and records it as the polled values of
259 // all the inputs.
260 void SampleSensors() {
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800261 sample_time_ = frc::GetFPGATime();
Brian Silvermanff7b3472015-01-26 17:53:04 -0500262 for (auto &c : handlers_) {
263 c->UpdatePolledValue();
264 }
265 }
266
267 // Gets called by the DMA handler to update edge counts.
268 void CheckDMA();
269
270 const ::std::unique_ptr<DMA> dma_;
Austin Schuhb1aaefe2021-10-12 22:05:44 -0700271 aos::SizedArray<DMASampleHandlerInterface *, 4> handlers_;
Brian Silvermanff7b3472015-01-26 17:53:04 -0500272
273 // The time at which we most recently read the sensor values.
Austin Schuhf853bde2017-11-23 13:23:27 -0800274 int64_t sample_time_ = 0;
Brian Silvermanff7b3472015-01-26 17:53:04 -0500275
276 DISALLOW_COPY_AND_ASSIGN(DMASynchronizer);
277};
278
279} // namespace wpilib
280} // namespace frc971
281
282#endif // FRC971_WPILIB_DMA_EDGE_COUNTING_H_