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" |
James Kuszmaul | 651fc3f | 2019-05-15 21:14:25 -0700 | [diff] [blame] | 9 | #include "aos/time/time.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/Counter.h" |
| 12 | #include "frc971/wpilib/ahal/DigitalSource.h" |
| 13 | #include "frc971/wpilib/ahal/Encoder.h" |
Brian Silverman | b5b46ca | 2016-03-13 01:14:17 -0500 | [diff] [blame] | 14 | #include "frc971/wpilib/dma.h" |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 15 | #include "frc971/wpilib/dma_edge_counting.h" |
Brian Silverman | 4da5807 | 2015-01-26 20:18:52 -0500 | [diff] [blame] | 16 | |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame^] | 17 | namespace frc971::wpilib { |
Brian Silverman | 4da5807 | 2015-01-26 20:18:52 -0500 | [diff] [blame] | 18 | |
Brian Silverman | 7cce2d3 | 2017-02-19 21:48:48 -0800 | [diff] [blame] | 19 | // Latches values from an encoder on positive edges from another input using |
| 20 | // DMA. |
| 21 | class DMAEncoder : public DMASampleHandlerInterface { |
Brian Silverman | 4da5807 | 2015-01-26 20:18:52 -0500 | [diff] [blame] | 22 | public: |
Brian Silverman | 7cce2d3 | 2017-02-19 21:48:48 -0800 | [diff] [blame] | 23 | DMAEncoder() {} |
Brian Silverman | 4da5807 | 2015-01-26 20:18:52 -0500 | [diff] [blame] | 24 | |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 25 | void set_encoder(::std::unique_ptr<frc::Encoder> encoder) { |
Brian Silverman | 4da5807 | 2015-01-26 20:18:52 -0500 | [diff] [blame] | 26 | encoder_ = ::std::move(encoder); |
| 27 | } |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 28 | frc::Encoder *encoder() const { return encoder_.get(); } |
Brian Silverman | 4da5807 | 2015-01-26 20:18:52 -0500 | [diff] [blame] | 29 | |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 30 | void set_index(::std::unique_ptr<frc::DigitalSource> index) { |
Brian Silverman | 4da5807 | 2015-01-26 20:18:52 -0500 | [diff] [blame] | 31 | index_ = ::std::move(index); |
| 32 | } |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 33 | frc::DigitalSource *index() const { return index_.get(); } |
Brian Silverman | 4da5807 | 2015-01-26 20:18:52 -0500 | [diff] [blame] | 34 | |
Brian Silverman | 4da5807 | 2015-01-26 20:18:52 -0500 | [diff] [blame] | 35 | // Returns the most recent polled value of the encoder. |
| 36 | uint32_t polled_encoder_value() const { return polled_encoder_value_; } |
Brian Silverman | 4da5807 | 2015-01-26 20:18:52 -0500 | [diff] [blame] | 37 | |
| 38 | // Returns the number of poseges that have happened on the index input. |
| 39 | uint32_t index_posedge_count() const { return index_posedge_count_; } |
| 40 | // Returns the value of the encoder at the last index posedge. |
| 41 | int32_t last_encoder_value() const { return last_encoder_value_; } |
Brian Silverman | 7cce2d3 | 2017-02-19 21:48:48 -0800 | [diff] [blame] | 42 | |
| 43 | void UpdateFromSample(const DMASample &sample) override { |
| 44 | DoUpdateFromSample(sample); |
Brian Silverman | 4da5807 | 2015-01-26 20:18:52 -0500 | [diff] [blame] | 45 | } |
| 46 | |
Brian Silverman | 7cce2d3 | 2017-02-19 21:48:48 -0800 | [diff] [blame] | 47 | void PollFromSample(const DMASample &sample) override { |
Brian Silverman | 4da5807 | 2015-01-26 20:18:52 -0500 | [diff] [blame] | 48 | polled_encoder_value_ = sample.GetRaw(encoder_.get()); |
Brian Silverman | 4da5807 | 2015-01-26 20:18:52 -0500 | [diff] [blame] | 49 | } |
| 50 | |
Brian Silverman | 7cce2d3 | 2017-02-19 21:48:48 -0800 | [diff] [blame] | 51 | void UpdatePolledValue() override { |
Brian Silverman | 4da5807 | 2015-01-26 20:18:52 -0500 | [diff] [blame] | 52 | polled_encoder_value_ = encoder_->GetRaw(); |
Brian Silverman | 4da5807 | 2015-01-26 20:18:52 -0500 | [diff] [blame] | 53 | } |
| 54 | |
Brian Silverman | 7cce2d3 | 2017-02-19 21:48:48 -0800 | [diff] [blame] | 55 | void AddToDMA(DMA *dma) override { |
Brian Silverman | 4da5807 | 2015-01-26 20:18:52 -0500 | [diff] [blame] | 56 | dma->Add(encoder_.get()); |
| 57 | dma->Add(index_.get()); |
Austin Schuh | c6cc410 | 2015-02-15 23:19:53 -0800 | [diff] [blame] | 58 | dma->SetExternalTrigger(index_.get(), true, true); |
Brian Silverman | 4da5807 | 2015-01-26 20:18:52 -0500 | [diff] [blame] | 59 | } |
| 60 | |
Brian Silverman | 7cce2d3 | 2017-02-19 21:48:48 -0800 | [diff] [blame] | 61 | protected: |
| 62 | // The same as UpdateFromSample except also returns true if this sample is a |
| 63 | // new edge on the index. |
| 64 | bool DoUpdateFromSample(const DMASample &sample); |
| 65 | |
Brian Silverman | 4da5807 | 2015-01-26 20:18:52 -0500 | [diff] [blame] | 66 | private: |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 67 | ::std::unique_ptr<frc::Encoder> encoder_; |
| 68 | ::std::unique_ptr<frc::DigitalSource> index_; |
Brian Silverman | 4da5807 | 2015-01-26 20:18:52 -0500 | [diff] [blame] | 69 | |
| 70 | int32_t polled_encoder_value_ = 0; |
Brian Silverman | 4da5807 | 2015-01-26 20:18:52 -0500 | [diff] [blame] | 71 | |
| 72 | int32_t last_encoder_value_ = 0; |
Brian Silverman | 4da5807 | 2015-01-26 20:18:52 -0500 | [diff] [blame] | 73 | |
| 74 | uint32_t index_posedge_count_ = 0; |
| 75 | |
| 76 | // Whether or not it was triggered in the last sample. |
| 77 | bool index_last_value_ = false; |
| 78 | |
Brian Silverman | 7cce2d3 | 2017-02-19 21:48:48 -0800 | [diff] [blame] | 79 | DISALLOW_COPY_AND_ASSIGN(DMAEncoder); |
| 80 | }; |
| 81 | |
| 82 | // Latches values from an encoder and potentiometer on positive edges from |
| 83 | // another input using DMA. |
| 84 | class DMAEncoderAndPotentiometer : public DMAEncoder { |
| 85 | public: |
| 86 | DMAEncoderAndPotentiometer() {} |
| 87 | |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 88 | void set_potentiometer(::std::unique_ptr<frc::AnalogInput> potentiometer) { |
Brian Silverman | 7cce2d3 | 2017-02-19 21:48:48 -0800 | [diff] [blame] | 89 | potentiometer_ = ::std::move(potentiometer); |
| 90 | } |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 91 | frc::AnalogInput *potentiometer() const { return potentiometer_.get(); } |
Brian Silverman | 7cce2d3 | 2017-02-19 21:48:48 -0800 | [diff] [blame] | 92 | |
| 93 | // Returns the most recent polled voltage of the potentiometer. |
| 94 | float polled_potentiometer_voltage() const { |
| 95 | return polled_potentiometer_voltage_; |
| 96 | } |
| 97 | |
| 98 | // Returns the voltage of the potentiometer at the last index posedge. |
| 99 | float last_potentiometer_voltage() const { |
| 100 | return last_potentiometer_voltage_; |
| 101 | } |
| 102 | |
| 103 | void UpdateFromSample(const DMASample &sample) override { |
| 104 | if (DMAEncoder::DoUpdateFromSample(sample)) { |
| 105 | last_potentiometer_voltage_ = sample.GetVoltage(potentiometer_.get()); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | void PollFromSample(const DMASample &sample) override { |
| 110 | polled_potentiometer_voltage_ = sample.GetVoltage(potentiometer_.get()); |
| 111 | DMAEncoder::PollFromSample(sample); |
| 112 | } |
| 113 | |
| 114 | void UpdatePolledValue() override { |
| 115 | polled_potentiometer_voltage_ = potentiometer_->GetVoltage(); |
| 116 | DMAEncoder::UpdatePolledValue(); |
| 117 | } |
| 118 | |
| 119 | void AddToDMA(DMA *dma) override { |
| 120 | dma->Add(potentiometer_.get()); |
| 121 | DMAEncoder::AddToDMA(dma); |
| 122 | } |
| 123 | |
| 124 | private: |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 125 | ::std::unique_ptr<frc::AnalogInput> potentiometer_; |
Brian Silverman | 7cce2d3 | 2017-02-19 21:48:48 -0800 | [diff] [blame] | 126 | |
| 127 | float polled_potentiometer_voltage_ = 0.0f; |
| 128 | |
| 129 | float last_potentiometer_voltage_ = 0.0f; |
| 130 | |
Brian Silverman | 4da5807 | 2015-01-26 20:18:52 -0500 | [diff] [blame] | 131 | DISALLOW_COPY_AND_ASSIGN(DMAEncoderAndPotentiometer); |
| 132 | }; |
| 133 | |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 134 | // Class to read duty cycle of an input. This is tuned for the CTRE encoder's |
| 135 | // absolute position output. |
| 136 | class DutyCycleReader { |
| 137 | public: |
| 138 | // Configure the reader to use the provided digital input. |
| 139 | void set_input(::std::unique_ptr<::frc::DigitalInput> input) { |
| 140 | high_counter_.reset(new ::frc::Counter(input.get())); |
| 141 | high_counter_->SetMaxPeriod(kMaxPeriod); |
| 142 | high_counter_->SetSemiPeriodMode(true); |
| 143 | |
| 144 | period_length_counter_.reset(new ::frc::Counter(input.get())); |
| 145 | period_length_counter_->SetMaxPeriod(kMaxPeriod); |
| 146 | period_length_counter_->SetUpSourceEdge(true, false); |
| 147 | |
| 148 | input_ = ::std::move(input); |
| 149 | } |
| 150 | |
| 151 | // Returns the last duty cycle or nan if the signal is stale. |
| 152 | double Read() const { |
| 153 | const double high_time = high_counter_->GetPeriod(); |
| 154 | const double period_length = period_length_counter_->GetPeriod(); |
| 155 | if (!::std::isfinite(high_time) || !::std::isfinite(period_length)) { |
| 156 | return ::std::numeric_limits<double>::quiet_NaN(); |
| 157 | } |
| 158 | return high_time / period_length; |
| 159 | } |
| 160 | |
| 161 | private: |
| 162 | static constexpr ::std::chrono::nanoseconds kNominalPeriod = |
| 163 | ::std::chrono::microseconds(4096); |
| 164 | static constexpr double kMaxPeriod = |
James Kuszmaul | 651fc3f | 2019-05-15 21:14:25 -0700 | [diff] [blame] | 165 | ::aos::time::DurationInSeconds(kNominalPeriod * 2); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 166 | |
| 167 | ::std::unique_ptr<::frc::Counter> high_counter_, period_length_counter_; |
| 168 | ::std::unique_ptr<::frc::DigitalInput> input_; |
| 169 | }; |
| 170 | |
| 171 | // Class to hold a CTRE encoder with absolute angle pwm and potentiometer pair. |
| 172 | class AbsoluteEncoderAndPotentiometer { |
| 173 | public: |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 174 | void set_absolute_pwm(::std::unique_ptr<frc::DigitalInput> input) { |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 175 | duty_cycle_.set_input(::std::move(input)); |
| 176 | } |
| 177 | |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 178 | void set_encoder(::std::unique_ptr<frc::Encoder> encoder) { |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 179 | encoder_ = ::std::move(encoder); |
| 180 | } |
| 181 | |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 182 | void set_potentiometer(::std::unique_ptr<frc::AnalogInput> potentiometer) { |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 183 | potentiometer_ = ::std::move(potentiometer); |
| 184 | } |
| 185 | |
| 186 | double ReadAbsoluteEncoder() const { return duty_cycle_.Read(); } |
| 187 | int32_t ReadRelativeEncoder() const { return encoder_->GetRaw(); } |
| 188 | double ReadPotentiometerVoltage() const { |
| 189 | return potentiometer_->GetVoltage(); |
| 190 | } |
| 191 | |
| 192 | private: |
| 193 | DutyCycleReader duty_cycle_; |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 194 | ::std::unique_ptr<frc::Encoder> encoder_; |
| 195 | ::std::unique_ptr<frc::AnalogInput> potentiometer_; |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 196 | }; |
| 197 | |
Ravago Jones | 937587c | 2020-12-26 17:21:09 -0800 | [diff] [blame] | 198 | // Class to hold two CTRE encoders, one with both index pulses and absolute |
| 199 | // angle pwm, and another that can only turn once and only reports absolute |
| 200 | // angle pwm. |
| 201 | class AbsoluteAndAbsoluteEncoder { |
| 202 | public: |
| 203 | void set_single_turn_absolute_pwm( |
| 204 | ::std::unique_ptr<frc::DigitalInput> input) { |
| 205 | single_turn_duty_cycle_.set_input(::std::move(input)); |
| 206 | } |
| 207 | |
| 208 | void set_absolute_pwm(::std::unique_ptr<frc::DigitalInput> input) { |
| 209 | duty_cycle_.set_input(::std::move(input)); |
| 210 | } |
| 211 | |
| 212 | void set_encoder(::std::unique_ptr<frc::Encoder> encoder) { |
| 213 | encoder_ = ::std::move(encoder); |
| 214 | } |
| 215 | |
| 216 | double ReadAbsoluteEncoder() const { return duty_cycle_.Read(); } |
| 217 | double ReadSingleTurnAbsoluteEncoder() const { |
| 218 | return single_turn_duty_cycle_.Read(); |
| 219 | } |
| 220 | int32_t ReadRelativeEncoder() const { return encoder_->GetRaw(); } |
| 221 | |
| 222 | private: |
| 223 | DutyCycleReader duty_cycle_; |
| 224 | DutyCycleReader single_turn_duty_cycle_; |
| 225 | ::std::unique_ptr<frc::Encoder> encoder_; |
| 226 | }; |
| 227 | |
Sabina Davis | 8d8ac0a | 2019-02-06 23:51:07 -0800 | [diff] [blame] | 228 | class AbsoluteEncoder { |
| 229 | public: |
| 230 | void set_absolute_pwm(::std::unique_ptr<frc::DigitalInput> input) { |
| 231 | duty_cycle_.set_input(::std::move(input)); |
| 232 | } |
| 233 | |
| 234 | void set_encoder(::std::unique_ptr<frc::Encoder> encoder) { |
| 235 | encoder_ = ::std::move(encoder); |
| 236 | } |
| 237 | |
| 238 | double ReadAbsoluteEncoder() const { return duty_cycle_.Read(); } |
| 239 | int32_t ReadRelativeEncoder() const { return encoder_->GetRaw(); } |
| 240 | |
| 241 | private: |
| 242 | DutyCycleReader duty_cycle_; |
| 243 | ::std::unique_ptr<frc::Encoder> encoder_; |
| 244 | }; |
| 245 | |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame^] | 246 | } // namespace frc971::wpilib |
Brian Silverman | 4da5807 | 2015-01-26 20:18:52 -0500 | [diff] [blame] | 247 | |
| 248 | #endif // FRC971_ENCODER_AND_POTENTIOMETER_H_ |