DSPRelated.com
Forums

Quadrature encoders and sampling

Started by Unknown June 6, 2016
You can buy these with up to 1024 pulses per rotation. They require two interrupts if you are doing direction. How does the number of pulses effect the bandwidth of the loop in a closed-loop system? I think it introduces a time-delay if there are not enough - does that sound right? hence bandwidth is compromised. However, with too many pulses is the processor not going to spend a lot of time servicing these? Surely this is a sampling process in the sense of Nyquist.


<gyansorova@gmail.com> wrote:

>You can buy these with up to 1024 pulses per rotation. They require two >interrupts if you are doing direction. How does the number of pulses >effect the bandwidth of the loop in a closed-loop system? I think it >introduces a time-delay if there are not enough - does that sound right? >hence bandwidth is compromised. However, with too many pulses is the >processor not going to spend a lot of time servicing these? Surely this >is a sampling process in the sense of Nyquist.
Interesting. It seems these pulse signals could be uniformly sampled at a high enough sample rate to support both the desired loop bandwidth and the max rotational speed, but this might be overkill, especially if as you allude to, there might be one interrupt per sample or per pulse. Perhaps you could convert the pulse signal to a pulse-rate signal (representing rotational speed) with some sort of analog circuit before processing it digitally. Steve
gyansorova@gmail.com writes:

> You can buy these with up to 1024 pulses per rotation. They require > two interrupts if you are doing direction.
Not true. You can simply monitor the state of the other pulse (via GPIO, e.g.) in the one interrupt handler.
> [...] > Surely this is a sampling process in the sense of Nyquist.
Yes, it is. It is spatially sampling the rotational position, assuming you have a home position. -- Randy Yates, DSP/Embedded Firmware Developer Digital Signal Labs http://www.digitalsignallabs.com
On Mon, 06 Jun 2016 17:41:20 -0400, Randy Yates wrote:

> gyansorova@gmail.com writes: > >> You can buy these with up to 1024 pulses per rotation. They require two >> interrupts if you are doing direction. > > Not true. You can simply monitor the state of the other pulse (via GPIO, > e.g.) in the one interrupt handler.
In my experience with multiple systems, it's best to watch for both. My preferred method is to servo the last two bits of a 2s compliment number to the state of the encoder, taking gray code into account. If the gray-code translated version of the last two bits of my accumulator is off by one I increment or decrement as necessary. In the unlikely event that it's off by two, I pop a fault or ignore, as appropriate for the system at hand. This has served me well for two decades, so I'm sticking with it. -- Tim Wescott Wescott Design Services http://www.wescottdesign.com I'm looking for work -- see my website!
On Tuesday, June 7, 2016 at 9:41:23 AM UTC+12, Randy Yates wrote:
> gyansorova@gmail.com writes: > > > You can buy these with up to 1024 pulses per rotation. They require > > two interrupts if you are doing direction. > > Not true. You can simply monitor the state of the other pulse (via > GPIO, e.g.) in the one interrupt handler. > > > [...] > > Surely this is a sampling process in the sense of Nyquist. > > Yes, it is. It is spatially sampling the rotational position, assuming > you have a home position. > -- > Randy Yates, DSP/Embedded Firmware Developer > Digital Signal Labs > http://www.digitalsignallabs.com
So what rate must you sample? ie how many pulses per second. Also, this adds to the sampling interval of your main while loop in a real-time system. the comment about using one interrupt is correct but I was led to believe that two is better than one - but I do not pretend to be an expert.
On Mon, 06 Jun 2016 13:51:09 -0700, gyansorova wrote:

> You can buy these with up to 1024 pulses per rotation. They require two > interrupts if you are doing direction. How does the number of pulses > effect the bandwidth of the loop in a closed-loop system? I think it > introduces a time-delay if there are not enough - does that sound right? > hence bandwidth is compromised. However, with too many pulses is the > processor not going to spend a lot of time servicing these? Surely this > is a sampling process in the sense of Nyquist.
You can view it that way. I prefer to treat the decoded position as any other ADC reading, with a given amount of quantization noise. I.e., with a 1024-count* encoder you always have +/-360/2048 degrees of uncertainty in your real position. If I must do it in software, or if I have a friendly FPGA guy standing by to do it in one of them things, my preferred method of sampling an encoder is to sample at a rate high enough that I'm going to unambiguously catch every transition (which puts a top speed on the shaft being encoded), then to servo an accumulator value to the encoder bits. The reason I do this is that it doesn't present the processor with a variable load depending on how fast the shaft is spinning, and it's not subject to floods of interrupts in the event of switch bounce. * You can get encoders with much more precision than that, BTW. Most of them only have 256 or 512 physical lines (a normal quadrature encoder has four counts/line), but do interpolation internally. The manufacturer then has clever internal circuitry that turns this into pulses coming out. -- Tim Wescott Wescott Design Services http://www.wescottdesign.com I'm looking for work -- see my website!
gyansorova@gmail.com wrote:
> You can buy these with up to 1024 pulses per rotation. They require > two interrupts if you are doing direction.
I'd sample them. So "two PIO". If you use interrupts and it goes too fast it's worse than gently shrugging "it's too fast". Plus, you need some state between the two ISRs to interpret this. So you either have to manage the state in the ISRs or have middleware. If you know the maximum rotation makes interrupts alright then you're okay. Let's say you can afford 10k interrupts/second. Then the maximum speed you can handle is 10k/1024 which isn't much. If you can do neither then...
> How does the number of > pulses effect the bandwidth of the loop in a closed-loop system?
It doesn't. It's a Nyquist Fs thing.
> I > think it introduces a time-delay if there are not enough - does that > sound right?
You're concerned with angular uncertainty which is then expressed as time uncertainty. Plus, the duty cycle may or may not be perfectly 50%.
> hence bandwidth is compromised. However, with too many > pulses is the processor not going to spend a lot of time servicing > these? Surely this is a sampling process in the sense of Nyquist. > >
Yup. Fs is proportional to max RPM * pulses per rotation. -- Les Cargill
Tim Wescott <seemywebsite@myfooter.really> writes:

> On Mon, 06 Jun 2016 17:41:20 -0400, Randy Yates wrote: > >> gyansorova@gmail.com writes: >> >>> You can buy these with up to 1024 pulses per rotation. They require two >>> interrupts if you are doing direction. >> >> Not true. You can simply monitor the state of the other pulse (via GPIO, >> e.g.) in the one interrupt handler. > > In my experience with multiple systems, it's best to watch for both. > > My preferred method is to servo the last two bits of a 2s compliment > number to the state of the encoder, taking gray code into account. If > the gray-code translated version of the last two bits of my accumulator > is off by one I increment or decrement as necessary. In the unlikely > event that it's off by two, I pop a fault or ignore, as appropriate for > the system at hand. > > This has served me well for two decades, so I'm sticking with it.
Well you're talking about more than just determining direction. My statement stands: to determine direction, you only need to the other input on an edge of the first input. I did this in hardware with a simple flip-flop (74LS74) back in the 80s. -- Randy Yates, DSP/Embedded Firmware Developer Digital Signal Labs http://www.digitalsignallabs.com
gyansorova@gmail.com writes:

> On Tuesday, June 7, 2016 at 9:41:23 AM UTC+12, Randy Yates wrote: >> gyansorova@gmail.com writes: >> >> > You can buy these with up to 1024 pulses per rotation. They require >> > two interrupts if you are doing direction. >> >> Not true. You can simply monitor the state of the other pulse (via >> GPIO, e.g.) in the one interrupt handler. >> >> > [...] >> > Surely this is a sampling process in the sense of Nyquist. >> >> Yes, it is. It is spatially sampling the rotational position, assuming >> you have a home position. >> -- >> Randy Yates, DSP/Embedded Firmware Developer >> Digital Signal Labs >> http://www.digitalsignallabs.com > > So what rate must you sample? ie how many pulses per second. Also, > this adds to the sampling interval of your main while loop in a > real-time system. > > the comment about using one interrupt is correct but I was led to > believe that two is better than one - but I do not pretend to be an > expert.
Two may be better, but not necessary. My quadrature (shaft) encoder application was a long, long time ago, and was simply a front panel knob used by an operator to change a parameter, so there was no need for fancy sampling. Regarding sample rate, when you use the edge of one of the encoder signals as an interrupt, then there is no "sample rate" per se; interrupts will come at a rate depending on how fast or slow the encoder is rotated. Thus the "sample rate" is tied to how fast the shaft is being turned, Fs = rho*N samples per second, where N is the number of pulses per revolution of the shaft encoder and rho is the rotational rate of the encoder in revolutions per second, and can vary to 0 to some max value depending on the maximum rotational rate. If you're using this in a control loop, Tim's suggestion (sample both inputs and servo to a binary value) sounds like a better way. -- Randy Yates, DSP/Embedded Firmware Developer Digital Signal Labs http://www.digitalsignallabs.com
Den tirsdag den 7. juni 2016 kl. 16.22.30 UTC+2 skrev Randy Yates:
> Tim Wescott <seemywebsite@myfooter.really> writes: > > > On Mon, 06 Jun 2016 17:41:20 -0400, Randy Yates wrote: > > > >> gyansorova@gmail.com writes: > >> > >>> You can buy these with up to 1024 pulses per rotation. They require two > >>> interrupts if you are doing direction. > >> > >> Not true. You can simply monitor the state of the other pulse (via GPIO, > >> e.g.) in the one interrupt handler. > > > > In my experience with multiple systems, it's best to watch for both. > > > > My preferred method is to servo the last two bits of a 2s compliment > > number to the state of the encoder, taking gray code into account. If > > the gray-code translated version of the last two bits of my accumulator > > is off by one I increment or decrement as necessary. In the unlikely > > event that it's off by two, I pop a fault or ignore, as appropriate for > > the system at hand. > > > > This has served me well for two decades, so I'm sticking with it. > > Well you're talking about more than just determining direction. My > statement stands: to determine direction, you only need to the other > input on an edge of the first input. > > I did this in hardware with a simple flip-flop (74LS74) back in the 80s.
that is usually a terrible way to do it because every glitch and bounce makes it count, a state machine counting based on the sequence of states doesn't -Lasse