Brian Silverman | ff7b347 | 2015-01-26 17:53:04 -0500 | [diff] [blame] | 1 | #ifndef FRC971_WPILIB_DMA_EDGE_COUNTING_H_ |
| 2 | #define FRC971_WPILIB_DMA_EDGE_COUNTING_H_ |
| 3 | |
| 4 | #include <memory> |
Ravago Jones | 128a50e | 2021-09-18 15:19:39 -0700 | [diff] [blame] | 5 | #include <optional> |
Brian Silverman | ff7b347 | 2015-01-26 17:53:04 -0500 | [diff] [blame] | 6 | #include <vector> |
| 7 | |
Austin Schuh | b1aaefe | 2021-10-12 22:05:44 -0700 | [diff] [blame] | 8 | #include "aos/containers/sized_array.h" |
milind-u | 6b672f8 | 2023-02-24 17:36:27 -0800 | [diff] [blame^] | 9 | #include "aos/macros.h" |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 10 | #include "frc971/wpilib/ahal/AnalogInput.h" |
| 11 | #include "frc971/wpilib/ahal/DigitalInput.h" |
| 12 | #include "frc971/wpilib/ahal/Encoder.h" |
milind-u | 6b672f8 | 2023-02-24 17:36:27 -0800 | [diff] [blame^] | 13 | #include "frc971/wpilib/ahal/Utility.h" |
| 14 | #include "frc971/wpilib/dma.h" |
Austin Schuh | 5c25ab7 | 2015-11-01 13:17:11 -0800 | [diff] [blame] | 15 | #undef ERROR |
Brian Silverman | ff7b347 | 2015-01-26 17:53:04 -0500 | [diff] [blame] | 16 | |
| 17 | namespace frc971 { |
| 18 | namespace wpilib { |
| 19 | |
| 20 | // Generic interface for classes that do something with DMA samples and also |
| 21 | // poll current sensor values. |
| 22 | class 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 Schuh | 8e5950d | 2018-03-21 20:29:40 -0700 | [diff] [blame] | 40 | // TODO(brian): Timeout old data. |
| 41 | class DMAPulseWidthReader : public DMASampleHandlerInterface { |
| 42 | public: |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 43 | DMAPulseWidthReader(frc::DigitalInput *input) : input_(input) {} |
Austin Schuh | 8e5950d | 2018-03-21 20:29:40 -0700 | [diff] [blame] | 44 | DMAPulseWidthReader() = default; |
| 45 | |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 46 | void set_input(frc::DigitalInput *input) { input_ = input; } |
Austin Schuh | 8e5950d | 2018-03-21 20:29:40 -0700 | [diff] [blame] | 47 | |
milind-u | 6b672f8 | 2023-02-24 17:36:27 -0800 | [diff] [blame^] | 48 | // Last pulse width in seconds |
Austin Schuh | 8e5950d | 2018-03-21 20:29:40 -0700 | [diff] [blame] | 49 | 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 Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 61 | frc::DigitalInput *input_ = nullptr; |
Austin Schuh | 8e5950d | 2018-03-21 20:29:40 -0700 | [diff] [blame] | 62 | |
| 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-u | 6b672f8 | 2023-02-24 17:36:27 -0800 | [diff] [blame^] | 67 | // Last time the reading switched to high |
| 68 | uint64_t high_time_ = 0; |
Austin Schuh | 8e5950d | 2018-03-21 20:29:40 -0700 | [diff] [blame] | 69 | |
| 70 | double last_width_ = ::std::numeric_limits<double>::quiet_NaN(); |
| 71 | |
| 72 | DISALLOW_COPY_AND_ASSIGN(DMAPulseWidthReader); |
| 73 | }; |
| 74 | |
Ravago Jones | 128a50e | 2021-09-18 15:19:39 -0700 | [diff] [blame] | 75 | // Takes two digital inputs and times the difference between the first one going |
Austin Schuh | 6c053ef | 2021-09-26 14:32:16 -0700 | [diff] [blame] | 76 | // low and the second one going low. |
Ravago Jones | 128a50e | 2021-09-18 15:19:39 -0700 | [diff] [blame] | 77 | class 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 Schuh | 6c053ef | 2021-09-26 14:32:16 -0700 | [diff] [blame] | 99 | dma->SetExternalTrigger(input_two_, false, true); |
Ravago Jones | 128a50e | 2021-09-18 15:19:39 -0700 | [diff] [blame] | 100 | } |
| 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 Schuh | 6c053ef | 2021-09-26 14:32:16 -0700 | [diff] [blame] | 112 | // the time when the input one went low. |
Ravago Jones | 128a50e | 2021-09-18 15:19:39 -0700 | [diff] [blame] | 113 | 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 Silverman | ff7b347 | 2015-01-26 17:53:04 -0500 | [diff] [blame] | 122 | // Counts edges on a sensor using DMA data and latches encoder values |
| 123 | // corresponding to those edges. |
| 124 | class DMAEdgeCounter : public DMASampleHandlerInterface { |
| 125 | public: |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 126 | DMAEdgeCounter(frc::Encoder *encoder, frc::DigitalInput *input) |
Brian Silverman | 03a00cf | 2015-08-29 19:26:54 -0700 | [diff] [blame] | 127 | : encoder_(encoder), input_(input) {} |
Brian | ef030df | 2017-03-05 15:06:04 -0800 | [diff] [blame] | 128 | DMAEdgeCounter() = default; |
| 129 | |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 130 | void set_encoder(frc::Encoder *encoder) { encoder_ = encoder; } |
| 131 | void set_input(frc::DigitalInput *input) { input_ = input; } |
Brian Silverman | ff7b347 | 2015-01-26 17:53:04 -0500 | [diff] [blame] | 132 | |
Brian Silverman | 552350b | 2015-08-02 18:23:34 -0700 | [diff] [blame] | 133 | 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 Silverman | ff7b347 | 2015-01-26 17:53:04 -0500 | [diff] [blame] | 137 | |
| 138 | // Returns the value of the sensor in the last-read DMA sample. |
Brian Silverman | 552350b | 2015-08-02 18:23:34 -0700 | [diff] [blame] | 139 | bool last_value() const { return ExtractValue(prev_sample_); } |
Brian Silverman | ff7b347 | 2015-01-26 17:53:04 -0500 | [diff] [blame] | 140 | // 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 Silverman | 552350b | 2015-08-02 18:23:34 -0700 | [diff] [blame] | 146 | void UpdateFromSample(const DMASample &sample) override; |
| 147 | void UpdatePolledValue() override { |
Austin Schuh | f83da15 | 2017-03-05 22:28:45 -0800 | [diff] [blame] | 148 | previous_polled_value_ = polled_value_; |
Brian Silverman | 552350b | 2015-08-02 18:23:34 -0700 | [diff] [blame] | 149 | polled_value_ = input_->Get(); |
| 150 | polled_encoder_ = encoder_->GetRaw(); |
| 151 | } |
| 152 | void PollFromSample(const DMASample &sample) override { |
Austin Schuh | f83da15 | 2017-03-05 22:28:45 -0800 | [diff] [blame] | 153 | previous_polled_value_ = polled_value_; |
Brian Silverman | 552350b | 2015-08-02 18:23:34 -0700 | [diff] [blame] | 154 | 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 Silverman | 03a00cf | 2015-08-29 19:26:54 -0700 | [diff] [blame] | 160 | dma->SetExternalTrigger(input_, true, true); |
Brian Silverman | 552350b | 2015-08-02 18:23:34 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | bool ExtractValue(const DMASample &sample) const; |
Brian Silverman | ff7b347 | 2015-01-26 17:53:04 -0500 | [diff] [blame] | 164 | |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 165 | frc::Encoder *encoder_ = nullptr; |
| 166 | frc::DigitalInput *input_ = nullptr; |
Brian Silverman | ff7b347 | 2015-01-26 17:53:04 -0500 | [diff] [blame] | 167 | |
| 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 Silverman | ff7b347 | 2015-01-26 17:53:04 -0500 | [diff] [blame] | 175 | int pos_last_encoder_ = 0; |
| 176 | |
| 177 | // Values related to the negative edge. |
| 178 | int neg_edge_count_ = 0; |
Brian Silverman | ff7b347 | 2015-01-26 17:53:04 -0500 | [diff] [blame] | 179 | int neg_last_encoder_ = 0; |
| 180 | |
| 181 | bool polled_value_ = false; |
Austin Schuh | f83da15 | 2017-03-05 22:28:45 -0800 | [diff] [blame] | 182 | bool previous_polled_value_ = false; |
Brian Silverman | ff7b347 | 2015-01-26 17:53:04 -0500 | [diff] [blame] | 183 | int polled_encoder_ = 0; |
| 184 | |
| 185 | DISALLOW_COPY_AND_ASSIGN(DMAEdgeCounter); |
| 186 | }; |
| 187 | |
Brian Silverman | 552350b | 2015-08-02 18:23:34 -0700 | [diff] [blame] | 188 | // Reads a hall effect in sync with DMA samples. |
| 189 | class DMADigitalReader : public DMASampleHandlerInterface { |
| 190 | public: |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 191 | DMADigitalReader(frc::DigitalInput *input) : input_(input) {} |
Brian Silverman | 552350b | 2015-08-02 18:23:34 -0700 | [diff] [blame] | 192 | |
| 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 Silverman | 03a00cf | 2015-08-29 19:26:54 -0700 | [diff] [blame] | 199 | value_ = sample.Get(input_); |
Brian Silverman | 552350b | 2015-08-02 18:23:34 -0700 | [diff] [blame] | 200 | } |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 201 | void AddToDMA(DMA *dma) override { dma->Add(input_); } |
Brian Silverman | 552350b | 2015-08-02 18:23:34 -0700 | [diff] [blame] | 202 | |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 203 | frc::DigitalInput *const input_; |
Brian Silverman | 552350b | 2015-08-02 18:23:34 -0700 | [diff] [blame] | 204 | |
| 205 | bool value_; |
| 206 | |
| 207 | DISALLOW_COPY_AND_ASSIGN(DMADigitalReader); |
| 208 | }; |
| 209 | |
Austin Schuh | 89f1e09 | 2015-11-22 22:30:39 -0800 | [diff] [blame] | 210 | // Reads an analog sensor in sync with DMA samples. |
| 211 | class DMAAnalogReader : public DMASampleHandlerInterface { |
| 212 | public: |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 213 | DMAAnalogReader(frc::AnalogInput *input) : input_(input) {} |
Austin Schuh | 89f1e09 | 2015-11-22 22:30:39 -0800 | [diff] [blame] | 214 | |
| 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 Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 223 | void AddToDMA(DMA *dma) override { dma->Add(input_); } |
Austin Schuh | 89f1e09 | 2015-11-22 22:30:39 -0800 | [diff] [blame] | 224 | |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 225 | frc::AnalogInput *const input_; |
Austin Schuh | 89f1e09 | 2015-11-22 22:30:39 -0800 | [diff] [blame] | 226 | |
| 227 | double value_; |
| 228 | |
| 229 | DISALLOW_COPY_AND_ASSIGN(DMAAnalogReader); |
| 230 | }; |
| 231 | |
Brian Silverman | ff7b347 | 2015-01-26 17:53:04 -0500 | [diff] [blame] | 232 | // 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. |
| 236 | class 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 Schuh | b1aaefe | 2021-10-12 22:05:44 -0700 | [diff] [blame] | 244 | handlers_.push_back(handler); |
Brian Silverman | ff7b347 | 2015-01-26 17:53:04 -0500 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | // Actually starts watching for DMA samples. |
| 248 | // Add may not be called any more after this. |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 249 | void Start() { dma_->Start(1024); } |
Brian Silverman | ff7b347 | 2015-01-26 17:53:04 -0500 | [diff] [blame] | 250 | |
| 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 Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 261 | sample_time_ = frc::GetFPGATime(); |
Brian Silverman | ff7b347 | 2015-01-26 17:53:04 -0500 | [diff] [blame] | 262 | 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 Schuh | b1aaefe | 2021-10-12 22:05:44 -0700 | [diff] [blame] | 271 | aos::SizedArray<DMASampleHandlerInterface *, 4> handlers_; |
Brian Silverman | ff7b347 | 2015-01-26 17:53:04 -0500 | [diff] [blame] | 272 | |
| 273 | // The time at which we most recently read the sensor values. |
Austin Schuh | f853bde | 2017-11-23 13:23:27 -0800 | [diff] [blame] | 274 | int64_t sample_time_ = 0; |
Brian Silverman | ff7b347 | 2015-01-26 17:53:04 -0500 | [diff] [blame] | 275 | |
| 276 | DISALLOW_COPY_AND_ASSIGN(DMASynchronizer); |
| 277 | }; |
| 278 | |
| 279 | } // namespace wpilib |
| 280 | } // namespace frc971 |
| 281 | |
| 282 | #endif // FRC971_WPILIB_DMA_EDGE_COUNTING_H_ |