From 21b2ff54a624a66fd716c656aa03772e3b7313c1 Mon Sep 17 00:00:00 2001 From: Jim Anderson Date: Mon, 21 Jul 2025 14:10:42 -0600 Subject: [PATCH 1/2] Add method to change encoder encoding type. --- src/EncoderClass.cpp | 9 +++++++++ src/EncoderClass.h | 10 ++++++++++ src/utility/QEI/QEI.cpp | 32 +++++++++++++++++++------------- src/utility/QEI/QEI.h | 9 ++++++++- 4 files changed, 46 insertions(+), 14 deletions(-) diff --git a/src/EncoderClass.cpp b/src/EncoderClass.cpp index 3fe546f..7308f07 100644 --- a/src/EncoderClass.cpp +++ b/src/EncoderClass.cpp @@ -63,5 +63,14 @@ int EncoderClass::getRevolutions(int channel) { } } +void EncoderClass::setEncoding(int channel, QEI::Encoding encoding) { + switch (channel) { + case 0: + return _enc0.setEncoding(encoding); + case 1: + return _enc1.setEncoding(encoding); + } +} + EncoderClass MachineControl_Encoders; /**** END OF FILE ****/ \ No newline at end of file diff --git a/src/EncoderClass.h b/src/EncoderClass.h index ecb18d1..3f76085 100644 --- a/src/EncoderClass.h +++ b/src/EncoderClass.h @@ -94,6 +94,16 @@ class EncoderClass { */ int getRevolutions(int channel); + /** + * @brief Set the encoding type for the specified encoder channel. + * + * This method changes the encoding type from the default X2_ENCODING. + * + * @param channel The encoder channel (0 or 1) to be changed. + * @param encoding The encoding type. + */ + void setEncoding(int channel, QEI::Encoding encoding); + private: QEI _enc0; // QEI object for encoder 0 QEI _enc1; // QEI object for encoder 1 diff --git a/src/utility/QEI/QEI.cpp b/src/utility/QEI/QEI.cpp index db767db..7ef3f57 100644 --- a/src/utility/QEI/QEI.cpp +++ b/src/utility/QEI/QEI.cpp @@ -148,19 +148,7 @@ QEI::QEI(PinName channelA, currState_ = (chanA << 1) | (chanB); prevState_ = currState_; - //X2 encoding uses interrupts on only channel A. - //X4 encoding uses interrupts on channel A, - //and on channel B. - channelA_.rise(mbed::callback(this, &QEI::encode)); - if(encoding != X1_ENCODING){ - channelA_.fall(mbed::callback(this, &QEI::encode)); - } - - //If we're using X4 encoding, then attach interrupts to channel B too. - if (encoding == X4_ENCODING) { - channelB_.rise(mbed::callback(this, &QEI::encode)); - channelB_.fall(mbed::callback(this, &QEI::encode)); - } + setEncoding(encoding); //Index is optional. if (index != NC) { index_.rise(mbed::callback(this, &QEI::index)); @@ -193,6 +181,24 @@ int QEI::getRevolutions(void) { } +void QEI::setEncoding(Encoding encoding) { + //X2 encoding uses interrupts on only channel A. + //X4 encoding uses interrupts on channel A, + //and on channel B. + channelA_.rise(mbed::callback(this, &QEI::encode)); + if(encoding != X1_ENCODING){ + channelA_.fall(mbed::callback(this, &QEI::encode)); + } else { + channelA_.fall(0); + } + + //If we're using X4 encoding, then attach interrupts to channel B too. + if (encoding == X4_ENCODING) { + channelB_.rise(mbed::callback(this, &QEI::encode)); + channelB_.fall(mbed::callback(this, &QEI::encode)); + } +} + // +-------------+ // | X1 Encoding | // +-------------+ diff --git a/src/utility/QEI/QEI.h b/src/utility/QEI/QEI.h index 8e6fba0..1d26730 100644 --- a/src/utility/QEI/QEI.h +++ b/src/utility/QEI/QEI.h @@ -176,7 +176,7 @@ class QEI { * of only channel A where as X4 uses them on both * channels. */ - QEI(PinName channelA, PinName channelB, PinName index, int pulsesPerRev, Encoding encoding = X2_ENCODING); + QEI(PinName channelA, PinName channelB, PinName index, int pulsesPerRev, Encoding encoding = X4_ENCODING); /** * Reset the encoder. @@ -208,6 +208,13 @@ class QEI { */ int getRevolutions(void); + /** + * Set the ecoding type of the encoder. + * + * Changes the type of encoding used by the encoder from the default X2_ENCODING. + */ + void setEncoding(Encoding encoding); + private: /** From ddb326d7e05589e70f589e835bb0dabae2fb163e Mon Sep 17 00:00:00 2001 From: Jim Anderson Date: Tue, 22 Jul 2025 06:14:11 -0600 Subject: [PATCH 2/2] Restore encoding default. --- src/utility/QEI/QEI.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utility/QEI/QEI.h b/src/utility/QEI/QEI.h index 1d26730..caa449d 100644 --- a/src/utility/QEI/QEI.h +++ b/src/utility/QEI/QEI.h @@ -176,7 +176,7 @@ class QEI { * of only channel A where as X4 uses them on both * channels. */ - QEI(PinName channelA, PinName channelB, PinName index, int pulsesPerRev, Encoding encoding = X4_ENCODING); + QEI(PinName channelA, PinName channelB, PinName index, int pulsesPerRev, Encoding encoding = X2_ENCODING); /** * Reset the encoder.