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