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