Skip to content

Add method to change encoder encoding type. #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/EncoderClass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 ****/
10 changes: 10 additions & 0 deletions src/EncoderClass.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 19 additions & 13 deletions src/utility/QEI/QEI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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 |
// +-------------+
Expand Down
7 changes: 7 additions & 0 deletions src/utility/QEI/QEI.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:

/**
Expand Down