Author Topic: Quadrature Decoder - For Reference  (Read 5773 times)

0 Members and 1 Guest are viewing this topic.

Offline ehughesTopic starter

  • Frequent Contributor
  • **
  • Posts: 409
  • Country: us
Quadrature Decoder - For Reference
« on: February 08, 2017, 02:48:35 pm »
Hello:

I found this .pdf and posting it here for future archival and reference.    The approach is quite simple and easily implement in an MCU or FPGA with just a few lines of code.   I have tested it personally (using a simple shift register and LUTs) and it is quite robust.   I also have tested with a high speed optical encoder.

All credit goes to the author.

Enjoy

http://www.mkesc.co.uk/ise.pdf
« Last Edit: February 08, 2017, 04:38:36 pm by ehughes »
 
The following users thanked this post: cgroen, aksg81

Offline chris_leyson

  • Super Contributor
  • ***
  • Posts: 1541
  • Country: wales
Re: Quadrature Decoder - For Reference
« Reply #1 on: February 08, 2017, 04:17:48 pm »
Found the same method used here: https://www.circuitsathome.com/mcu/reading-rotary-encoder-on-arduino/. I used a PIC and interrupt on change and found the method to be very robust.
 

Offline FrankBuss

  • Supporter
  • ****
  • Posts: 2365
  • Country: de
    • Frank Buss
Re: Quadrature Decoder - For Reference
« Reply #2 on: February 08, 2017, 04:31:59 pm »
I didn't know the paper, but the concept is obvious, I implemented it some years ago exactly like described, with 4 bits and a table:

http://www.instructables.com/id/Using-a-Quadrature-Encoder-with-an-ATtiny-2313-and/

See step 3 for details of the microcontroller program.
So Long, and Thanks for All the Fish
Electronics, hiking, retro-computing, electronic music etc.: https://www.youtube.com/c/FrankBussProgrammer
 

Online MarkF

  • Super Contributor
  • ***
  • Posts: 2549
  • Country: us
Re: Quadrature Decoder - For Reference
« Reply #3 on: February 08, 2017, 06:10:03 pm »
This pdf shows the technique I use. No lookup table required.
 

Online Benta

  • Super Contributor
  • ***
  • Posts: 5872
  • Country: de
Re: Quadrature Decoder - For Reference
« Reply #4 on: February 08, 2017, 09:35:05 pm »
In my opinion, overengineered.

Using one of the quadrature input signals as interrupt/clock, simply sampling the other input at this point will give you direction information.
The first input will give you relative position and speed.

 

Offline hamster_nz

  • Super Contributor
  • ***
  • Posts: 2803
  • Country: nz
Re: Quadrature Decoder - For Reference
« Reply #5 on: February 09, 2017, 12:37:08 am »
In my opinion, overengineered.

Using one of the quadrature input signals as interrupt/clock, simply sampling the other input at this point will give you direction information.
The first input will give you relative position and speed.
In an ideal world, where switches never go noisy, and contacts never bounce there are lots of easy answers.

I still like the "sample on a timer" method. It might waste CPU time, but that is very deterministic. A spew of interrupts from a noisy switch sounds like a recipe for strange intermittent behavior in the field. I would far rather that the dodgy encoder doesn't respond to a turn properly (e.g. skips backwards once in a while) than it crashes the H/W.
Gaze not into the abyss, lest you become recognized as an abyss domain expert, and they expect you keep gazing into the damn thing.
 

Offline KE5FX

  • Super Contributor
  • ***
  • Posts: 1891
  • Country: us
    • KE5FX.COM
Re: Quadrature Decoder - For Reference
« Reply #6 on: February 09, 2017, 01:37:55 am »
Pro tip: whichever technique you use, read twice and reject any direction pairs that disagree.
 

Offline FrankBuss

  • Supporter
  • ****
  • Posts: 2365
  • Country: de
    • Frank Buss
Re: Quadrature Decoder - For Reference
« Reply #7 on: February 09, 2017, 03:11:21 am »
In my opinion, overengineered.

Using one of the quadrature input signals as interrupt/clock, simply sampling the other input at this point will give you direction information.
The first input will give you relative position and speed.

The table has the advantage to compensate imperfect encoders. E.g. in my example I didn't increment and decrement for each quadrature step, see the source code (sorry, German comments), which was perfect for the ALPS EC11 und EC12 encoders. And of course, not a good idea to use an interrupt for a probably noisy input signal.
So Long, and Thanks for All the Fish
Electronics, hiking, retro-computing, electronic music etc.: https://www.youtube.com/c/FrankBussProgrammer
 

Offline jesuscf

  • Frequent Contributor
  • **
  • Posts: 499
  • Country: ca
Re: Quadrature Decoder - For Reference
« Reply #8 on: February 09, 2017, 06:42:01 am »
In my opinion, overengineered.

Using one of the quadrature input signals as interrupt/clock, simply sampling the other input at this point will give you direction information.
The first input will give you relative position and speed.

I disagree.  Many years ago I tried using one of the input signals as interrupt and the results were poor at best.  The literature on the subject agrees.  This paragraph is from "Linear Position Sensors: Theory and Application", by David S. Nyce ISBN 0-471-23326-9, John Wiley & Sons, Inc. Section 10.7: Quadrature, Page 157:

"Modern circuits that read the A quad B signals from a position transducer do not actually wait for a transition to occur and then count that transition.  Instead, its more common to monitor the states of A and B continuously at a higher sampling rate than the transitions are expected to occur.  With this (state, rather than transition) information and, usually, a microcontroller, smoother operation can be obtained with less likelihood of error."

The problem can be then easily visualized and solved with a finite state machine diagram as shown in the attached pictured and coded using your favorite programming language.
Homer: Kids, there's three ways to do things; the right way, the wrong way and the Max Power way!
Bart: Isn't that the wrong way?
Homer: Yeah, but faster!
 

Offline mikerj

  • Super Contributor
  • ***
  • Posts: 3240
  • Country: gb
Re: Quadrature Decoder - For Reference
« Reply #9 on: February 09, 2017, 09:48:18 am »
In my opinion, overengineered.

Using one of the quadrature input signals as interrupt/clock, simply sampling the other input at this point will give you direction information.
The first input will give you relative position and speed.

The timer method is far better IMO.  Using an external interrupt is very prone to failures in the real world e.g. from noisy encoder signals.  This also gives unknown CPU overhead that increases with the speed of the encoder, and especially with noisy encoder signals. 
 

Offline ehughesTopic starter

  • Frequent Contributor
  • **
  • Posts: 409
  • Country: us
Re: Quadrature Decoder - For Reference
« Reply #10 on: February 09, 2017, 02:45:51 pm »
Check out this logic.    I have not run a k-map but I *THINK* this is the "table" in a minimal form.

I have not simulated or tested this approach but it looks correct on 1st inspection.




*From fpgas4fun.com

With the fully synchronous version:


 

Online Benta

  • Super Contributor
  • ***
  • Posts: 5872
  • Country: de
Re: Quadrature Decoder - For Reference
« Reply #11 on: February 09, 2017, 05:17:24 pm »
In my opinion, overengineered.

Using one of the quadrature input signals as interrupt/clock, simply sampling the other input at this point will give you direction information.
The first input will give you relative position and speed.
In an ideal world, where switches never go noisy, and contacts never bounce there are lots of easy answers.


My apologies for a too brief answer. I imagined it would be obvious to other engineers that only properly debounced signals can be used with this method.

Concerning the "interrupt", I would of course not bang the signal directly into the IRQ pin! But most MCUs have edge triggered I/O ports, which in turn request interrupt service.

 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf