Brian Silverman | 4da5807 | 2015-01-26 20:18:52 -0500 | [diff] [blame] | 1 | #ifndef FRC971_ENCODER_AND_POTENTIOMETER_H_ |
| 2 | #define FRC971_ENCODER_AND_POTENTIOMETER_H_ |
| 3 | |
| 4 | #include <atomic> |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 5 | #include <cmath> |
Brian Silverman | 4da5807 | 2015-01-26 20:18:52 -0500 | [diff] [blame] | 6 | #include <thread> |
| 7 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 8 | #include "aos/macros.h" |
| 9 | #include "aos/mutex/mutex.h" |
James Kuszmaul | 651fc3f | 2019-05-15 21:14:25 -0700 | [diff] [blame^] | 10 | #include "aos/time/time.h" |
Brian Silverman | 4da5807 | 2015-01-26 20:18:52 -0500 | [diff] [blame] | 11 | |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 12 | #include "frc971/wpilib/ahal/AnalogInput.h" |
| 13 | #include "frc971/wpilib/ahal/Counter.h" |
| 14 | #include "frc971/wpilib/ahal/DigitalSource.h" |
| 15 | #include "frc971/wpilib/ahal/Encoder.h" |
Brian Silverman | 4da5807 | 2015-01-26 20:18:52 -0500 | [diff] [blame] | 16 | |
Brian Silverman | b5b46ca | 2016-03-13 01:14:17 -0500 | [diff] [blame] | 17 | #include "frc971/wpilib/dma.h" |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 18 | #include "frc971/wpilib/dma_edge_counting.h" |
Brian Silverman | 4da5807 | 2015-01-26 20:18:52 -0500 | [diff] [blame] | 19 | |
| 20 | namespace frc971 { |
| 21 | namespace wpilib { |
| 22 | |
| 23 | // Latches values from an encoder and potentiometer on positive edges from |
| 24 | // another input using an interrupt. |
| 25 | class InterruptEncoderAndPotentiometer { |
| 26 | public: |
| 27 | // priority is the priority the thread will run at. |
| 28 | InterruptEncoderAndPotentiometer(int priority) : priority_(priority) {} |
| 29 | |
| 30 | // Starts the thread running so it can receive interrupts. |
| 31 | void Start(); |
| 32 | |
| 33 | // Tells the thread to stop running and then waits for it to finish. |
| 34 | void Stop() { |
| 35 | run_ = false; |
| 36 | thread_.join(); |
| 37 | } |
| 38 | |
| 39 | // Loops until Stop() is called, reading interrupts. |
| 40 | // Designed to be called by ::std::thread internally. |
| 41 | void operator()(); |
| 42 | |
| 43 | // Returns the mutex which must be held while calling index_posedge_count(), |
| 44 | // last_encoder_value(), and last_potentiometer_voltage(). |
| 45 | // Holding this mutex will increase the handling latency. |
| 46 | ::aos::Mutex *mutex() { return &mutex_; } |
| 47 | |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 48 | void set_encoder(::std::unique_ptr<frc::Encoder> encoder) { |
Brian Silverman | 4da5807 | 2015-01-26 20:18:52 -0500 | [diff] [blame] | 49 | encoder_ = ::std::move(encoder); |
| 50 | } |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 51 | frc::Encoder *encoder() const { return encoder_.get(); } |
Brian Silverman | 4da5807 | 2015-01-26 20:18:52 -0500 | [diff] [blame] | 52 | |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 53 | void set_index(::std::unique_ptr<frc::DigitalSource> index) { |
Brian Silverman | 4da5807 | 2015-01-26 20:18:52 -0500 | [diff] [blame] | 54 | index_ = ::std::move(index); |
| 55 | } |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 56 | frc::DigitalSource *index() const { return index_.get(); } |
Brian Silverman | 4da5807 | 2015-01-26 20:18:52 -0500 | [diff] [blame] | 57 | |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 58 | void set_potentiometer(::std::unique_ptr<frc::AnalogInput> potentiometer) { |
Brian Silverman | 4da5807 | 2015-01-26 20:18:52 -0500 | [diff] [blame] | 59 | potentiometer_ = ::std::move(potentiometer); |
| 60 | } |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 61 | frc::AnalogInput *potentiometer() const { return potentiometer_.get(); } |
Brian Silverman | 4da5807 | 2015-01-26 20:18:52 -0500 | [diff] [blame] | 62 | |
| 63 | // Returns the number of poseges that have happened on the index input. |
| 64 | // mutex() must be held while calling this. |
| 65 | uint32_t index_posedge_count() const { return index_posedge_count_; } |
| 66 | // Returns the value of the encoder at the last index posedge. |
| 67 | // mutex() must be held while calling this. |
| 68 | int32_t last_encoder_value() const { return last_encoder_value_; } |
| 69 | // Returns the voltage of the potentiometer at the last index posedge. |
| 70 | // mutex() must be held while calling this. |
| 71 | float last_potentiometer_voltage() const { |
| 72 | return last_potentiometer_voltage_; |
| 73 | } |
| 74 | |
| 75 | private: |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 76 | ::std::unique_ptr<frc::Encoder> encoder_; |
| 77 | ::std::unique_ptr<frc::DigitalSource> index_; |
| 78 | ::std::unique_ptr<frc::AnalogInput> potentiometer_; |
Brian Silverman | 4da5807 | 2015-01-26 20:18:52 -0500 | [diff] [blame] | 79 | |
| 80 | int32_t last_encoder_value_{0}; |
| 81 | float last_potentiometer_voltage_{0.0f}; |
| 82 | uint32_t index_posedge_count_{0}; |
| 83 | |
| 84 | ::aos::Mutex mutex_; |
| 85 | |
| 86 | const int priority_; |
| 87 | |
| 88 | ::std::atomic<bool> run_{true}; |
| 89 | ::std::thread thread_; |
| 90 | |
| 91 | DISALLOW_COPY_AND_ASSIGN(InterruptEncoderAndPotentiometer); |
| 92 | }; |
| 93 | |
Brian Silverman | 7cce2d3 | 2017-02-19 21:48:48 -0800 | [diff] [blame] | 94 | // Latches values from an encoder on positive edges from another input using |
| 95 | // DMA. |
| 96 | class DMAEncoder : public DMASampleHandlerInterface { |
Brian Silverman | 4da5807 | 2015-01-26 20:18:52 -0500 | [diff] [blame] | 97 | public: |
Brian Silverman | 7cce2d3 | 2017-02-19 21:48:48 -0800 | [diff] [blame] | 98 | DMAEncoder() {} |
Brian Silverman | 4da5807 | 2015-01-26 20:18:52 -0500 | [diff] [blame] | 99 | |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 100 | void set_encoder(::std::unique_ptr<frc::Encoder> encoder) { |
Brian Silverman | 4da5807 | 2015-01-26 20:18:52 -0500 | [diff] [blame] | 101 | encoder_ = ::std::move(encoder); |
| 102 | } |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 103 | frc::Encoder *encoder() const { return encoder_.get(); } |
Brian Silverman | 4da5807 | 2015-01-26 20:18:52 -0500 | [diff] [blame] | 104 | |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 105 | void set_index(::std::unique_ptr<frc::DigitalSource> index) { |
Brian Silverman | 4da5807 | 2015-01-26 20:18:52 -0500 | [diff] [blame] | 106 | index_ = ::std::move(index); |
| 107 | } |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 108 | frc::DigitalSource *index() const { return index_.get(); } |
Brian Silverman | 4da5807 | 2015-01-26 20:18:52 -0500 | [diff] [blame] | 109 | |
Brian Silverman | 4da5807 | 2015-01-26 20:18:52 -0500 | [diff] [blame] | 110 | // Returns the most recent polled value of the encoder. |
| 111 | uint32_t polled_encoder_value() const { return polled_encoder_value_; } |
Brian Silverman | 4da5807 | 2015-01-26 20:18:52 -0500 | [diff] [blame] | 112 | |
| 113 | // Returns the number of poseges that have happened on the index input. |
| 114 | uint32_t index_posedge_count() const { return index_posedge_count_; } |
| 115 | // Returns the value of the encoder at the last index posedge. |
| 116 | int32_t last_encoder_value() const { return last_encoder_value_; } |
Brian Silverman | 7cce2d3 | 2017-02-19 21:48:48 -0800 | [diff] [blame] | 117 | |
| 118 | void UpdateFromSample(const DMASample &sample) override { |
| 119 | DoUpdateFromSample(sample); |
Brian Silverman | 4da5807 | 2015-01-26 20:18:52 -0500 | [diff] [blame] | 120 | } |
| 121 | |
Brian Silverman | 7cce2d3 | 2017-02-19 21:48:48 -0800 | [diff] [blame] | 122 | void PollFromSample(const DMASample &sample) override { |
Brian Silverman | 4da5807 | 2015-01-26 20:18:52 -0500 | [diff] [blame] | 123 | polled_encoder_value_ = sample.GetRaw(encoder_.get()); |
Brian Silverman | 4da5807 | 2015-01-26 20:18:52 -0500 | [diff] [blame] | 124 | } |
| 125 | |
Brian Silverman | 7cce2d3 | 2017-02-19 21:48:48 -0800 | [diff] [blame] | 126 | void UpdatePolledValue() override { |
Brian Silverman | 4da5807 | 2015-01-26 20:18:52 -0500 | [diff] [blame] | 127 | polled_encoder_value_ = encoder_->GetRaw(); |
Brian Silverman | 4da5807 | 2015-01-26 20:18:52 -0500 | [diff] [blame] | 128 | } |
| 129 | |
Brian Silverman | 7cce2d3 | 2017-02-19 21:48:48 -0800 | [diff] [blame] | 130 | void AddToDMA(DMA *dma) override { |
Brian Silverman | 4da5807 | 2015-01-26 20:18:52 -0500 | [diff] [blame] | 131 | dma->Add(encoder_.get()); |
| 132 | dma->Add(index_.get()); |
Austin Schuh | c6cc410 | 2015-02-15 23:19:53 -0800 | [diff] [blame] | 133 | dma->SetExternalTrigger(index_.get(), true, true); |
Brian Silverman | 4da5807 | 2015-01-26 20:18:52 -0500 | [diff] [blame] | 134 | } |
| 135 | |
Brian Silverman | 7cce2d3 | 2017-02-19 21:48:48 -0800 | [diff] [blame] | 136 | protected: |
| 137 | // The same as UpdateFromSample except also returns true if this sample is a |
| 138 | // new edge on the index. |
| 139 | bool DoUpdateFromSample(const DMASample &sample); |
| 140 | |
Brian Silverman | 4da5807 | 2015-01-26 20:18:52 -0500 | [diff] [blame] | 141 | private: |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 142 | ::std::unique_ptr<frc::Encoder> encoder_; |
| 143 | ::std::unique_ptr<frc::DigitalSource> index_; |
Brian Silverman | 4da5807 | 2015-01-26 20:18:52 -0500 | [diff] [blame] | 144 | |
| 145 | int32_t polled_encoder_value_ = 0; |
Brian Silverman | 4da5807 | 2015-01-26 20:18:52 -0500 | [diff] [blame] | 146 | |
| 147 | int32_t last_encoder_value_ = 0; |
Brian Silverman | 4da5807 | 2015-01-26 20:18:52 -0500 | [diff] [blame] | 148 | |
| 149 | uint32_t index_posedge_count_ = 0; |
| 150 | |
| 151 | // Whether or not it was triggered in the last sample. |
| 152 | bool index_last_value_ = false; |
| 153 | |
Brian Silverman | 7cce2d3 | 2017-02-19 21:48:48 -0800 | [diff] [blame] | 154 | DISALLOW_COPY_AND_ASSIGN(DMAEncoder); |
| 155 | }; |
| 156 | |
| 157 | // Latches values from an encoder and potentiometer on positive edges from |
| 158 | // another input using DMA. |
| 159 | class DMAEncoderAndPotentiometer : public DMAEncoder { |
| 160 | public: |
| 161 | DMAEncoderAndPotentiometer() {} |
| 162 | |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 163 | void set_potentiometer(::std::unique_ptr<frc::AnalogInput> potentiometer) { |
Brian Silverman | 7cce2d3 | 2017-02-19 21:48:48 -0800 | [diff] [blame] | 164 | potentiometer_ = ::std::move(potentiometer); |
| 165 | } |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 166 | frc::AnalogInput *potentiometer() const { return potentiometer_.get(); } |
Brian Silverman | 7cce2d3 | 2017-02-19 21:48:48 -0800 | [diff] [blame] | 167 | |
| 168 | // Returns the most recent polled voltage of the potentiometer. |
| 169 | float polled_potentiometer_voltage() const { |
| 170 | return polled_potentiometer_voltage_; |
| 171 | } |
| 172 | |
| 173 | // Returns the voltage of the potentiometer at the last index posedge. |
| 174 | float last_potentiometer_voltage() const { |
| 175 | return last_potentiometer_voltage_; |
| 176 | } |
| 177 | |
| 178 | void UpdateFromSample(const DMASample &sample) override { |
| 179 | if (DMAEncoder::DoUpdateFromSample(sample)) { |
| 180 | last_potentiometer_voltage_ = sample.GetVoltage(potentiometer_.get()); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | void PollFromSample(const DMASample &sample) override { |
| 185 | polled_potentiometer_voltage_ = sample.GetVoltage(potentiometer_.get()); |
| 186 | DMAEncoder::PollFromSample(sample); |
| 187 | } |
| 188 | |
| 189 | void UpdatePolledValue() override { |
| 190 | polled_potentiometer_voltage_ = potentiometer_->GetVoltage(); |
| 191 | DMAEncoder::UpdatePolledValue(); |
| 192 | } |
| 193 | |
| 194 | void AddToDMA(DMA *dma) override { |
| 195 | dma->Add(potentiometer_.get()); |
| 196 | DMAEncoder::AddToDMA(dma); |
| 197 | } |
| 198 | |
| 199 | private: |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 200 | ::std::unique_ptr<frc::AnalogInput> potentiometer_; |
Brian Silverman | 7cce2d3 | 2017-02-19 21:48:48 -0800 | [diff] [blame] | 201 | |
| 202 | float polled_potentiometer_voltage_ = 0.0f; |
| 203 | |
| 204 | float last_potentiometer_voltage_ = 0.0f; |
| 205 | |
Brian Silverman | 4da5807 | 2015-01-26 20:18:52 -0500 | [diff] [blame] | 206 | DISALLOW_COPY_AND_ASSIGN(DMAEncoderAndPotentiometer); |
| 207 | }; |
| 208 | |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 209 | // Class to read duty cycle of an input. This is tuned for the CTRE encoder's |
| 210 | // absolute position output. |
| 211 | class DutyCycleReader { |
| 212 | public: |
| 213 | // Configure the reader to use the provided digital input. |
| 214 | void set_input(::std::unique_ptr<::frc::DigitalInput> input) { |
| 215 | high_counter_.reset(new ::frc::Counter(input.get())); |
| 216 | high_counter_->SetMaxPeriod(kMaxPeriod); |
| 217 | high_counter_->SetSemiPeriodMode(true); |
| 218 | |
| 219 | period_length_counter_.reset(new ::frc::Counter(input.get())); |
| 220 | period_length_counter_->SetMaxPeriod(kMaxPeriod); |
| 221 | period_length_counter_->SetUpSourceEdge(true, false); |
| 222 | |
| 223 | input_ = ::std::move(input); |
| 224 | } |
| 225 | |
| 226 | // Returns the last duty cycle or nan if the signal is stale. |
| 227 | double Read() const { |
| 228 | const double high_time = high_counter_->GetPeriod(); |
| 229 | const double period_length = period_length_counter_->GetPeriod(); |
| 230 | if (!::std::isfinite(high_time) || !::std::isfinite(period_length)) { |
| 231 | return ::std::numeric_limits<double>::quiet_NaN(); |
| 232 | } |
| 233 | return high_time / period_length; |
| 234 | } |
| 235 | |
| 236 | private: |
| 237 | static constexpr ::std::chrono::nanoseconds kNominalPeriod = |
| 238 | ::std::chrono::microseconds(4096); |
| 239 | static constexpr double kMaxPeriod = |
James Kuszmaul | 651fc3f | 2019-05-15 21:14:25 -0700 | [diff] [blame^] | 240 | ::aos::time::DurationInSeconds(kNominalPeriod * 2); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 241 | |
| 242 | ::std::unique_ptr<::frc::Counter> high_counter_, period_length_counter_; |
| 243 | ::std::unique_ptr<::frc::DigitalInput> input_; |
| 244 | }; |
| 245 | |
| 246 | // Class to hold a CTRE encoder with absolute angle pwm and potentiometer pair. |
| 247 | class AbsoluteEncoderAndPotentiometer { |
| 248 | public: |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 249 | void set_absolute_pwm(::std::unique_ptr<frc::DigitalInput> input) { |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 250 | duty_cycle_.set_input(::std::move(input)); |
| 251 | } |
| 252 | |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 253 | void set_encoder(::std::unique_ptr<frc::Encoder> encoder) { |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 254 | encoder_ = ::std::move(encoder); |
| 255 | } |
| 256 | |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 257 | void set_potentiometer(::std::unique_ptr<frc::AnalogInput> potentiometer) { |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 258 | potentiometer_ = ::std::move(potentiometer); |
| 259 | } |
| 260 | |
| 261 | double ReadAbsoluteEncoder() const { return duty_cycle_.Read(); } |
| 262 | int32_t ReadRelativeEncoder() const { return encoder_->GetRaw(); } |
| 263 | double ReadPotentiometerVoltage() const { |
| 264 | return potentiometer_->GetVoltage(); |
| 265 | } |
| 266 | |
| 267 | private: |
| 268 | DutyCycleReader duty_cycle_; |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 269 | ::std::unique_ptr<frc::Encoder> encoder_; |
| 270 | ::std::unique_ptr<frc::AnalogInput> potentiometer_; |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 271 | }; |
| 272 | |
Sabina Davis | 8d8ac0a | 2019-02-06 23:51:07 -0800 | [diff] [blame] | 273 | class AbsoluteEncoder { |
| 274 | public: |
| 275 | void set_absolute_pwm(::std::unique_ptr<frc::DigitalInput> input) { |
| 276 | duty_cycle_.set_input(::std::move(input)); |
| 277 | } |
| 278 | |
| 279 | void set_encoder(::std::unique_ptr<frc::Encoder> encoder) { |
| 280 | encoder_ = ::std::move(encoder); |
| 281 | } |
| 282 | |
| 283 | double ReadAbsoluteEncoder() const { return duty_cycle_.Read(); } |
| 284 | int32_t ReadRelativeEncoder() const { return encoder_->GetRaw(); } |
| 285 | |
| 286 | private: |
| 287 | DutyCycleReader duty_cycle_; |
| 288 | ::std::unique_ptr<frc::Encoder> encoder_; |
| 289 | }; |
| 290 | |
Brian Silverman | 4da5807 | 2015-01-26 20:18:52 -0500 | [diff] [blame] | 291 | } // namespace wpilib |
| 292 | } // namespace frc971 |
| 293 | |
| 294 | #endif // FRC971_ENCODER_AND_POTENTIOMETER_H_ |