Skip to content
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

Cant understand code #75

Open
kotopuz opened this issue Oct 29, 2020 · 1 comment
Open

Cant understand code #75

kotopuz opened this issue Oct 29, 2020 · 1 comment

Comments

@kotopuz
Copy link

kotopuz commented Oct 29, 2020

I cant understand how you read encoder inputs.
There is some array then some weird calcs. There is A and B channels. Generally when A comes you count pulse. Then inside A interrupt you may compare to B state to determine direction. Can you direct me to explanation of this code please

const int QEM [16] = {0,-1,1,2,1,0,2,-1,-1,2,0,1,2,1,-1,0}; // Quadrature Encoder Matrix
static unsigned char New, Old;

void encoderInt() { // handle pin change interrupt for D2
Old = New;
//New = PIND & 3; //(PINB & 1 )+ ((PIND & 4) >> 1); // Mauro Manco
New = digitalRead(encoder0PinA)*2 + digitalRead(encoder0PinB);
encoder0Pos+= QEM [Old * 4 + New];
}

Thanks !

@misan
Copy link
Owner

misan commented Oct 30, 2020

The encoder part I borrowed from Let's make robots blog, but they revamped their site and I cannot find it anymore. But evidence is there: https://www.robotshop.com/community/forum/t/dagu-5rover-arduino-code/10875

It is a state machine, where +1 or -1 corresponds to valid encoder count updates and 2 represents invalid transitions in the finite automaton that should never happen.

Hold on, I have found a document that explains it all: https://cdn.sparkfun.com/datasheets/Robotics/How%20to%20use%20a%20quadrature%20encoder.pdf

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants