How to Use a Quadrature Encoder

Total Page:16

File Type:pdf, Size:1020Kb

How to Use a Quadrature Encoder

How to use a Quadrature encoder

As we know when we are thinking in simple easy and developing Localization system we can think about shaft encoder sensor that sense the number of revolution for a wheel so in this tutorial we will talking about the Quadrature encoder (" ("that Mounted in rover 5S platform

:Firstly if you want to create your own library here is some explanation :and steps to implement that

Quadrature encoder software is quite trivial. For low speed you can measure the time between pulses and for high speed you can count the pulses. Adafruit has example code for ::counting pulses. In general

Set up the associated I/O pins as inputs with Interrupt On-1 Change

Build a simple state machine to remember current state,-2 count, and direction

Create an ISR for the Interrupt On Change-3

Inside the ISR read, isolate, and align the I/O bits of-4 interest. Compare with previous state. You have 3 possible conditions: No Change, A follows B, B follows A. With this information you know direction. Increment or decrement the associated counter. Do NOT use a switch() statement .within the ISR as it is very slow

On a regular basis, ie every 10msec, read the counters,-5 convert to speed and distance, and zero the counter. Be careful to mask off interrupts when clearing the current .count

There are two channels typically called A & B. Each .generates a square wave that idealy are out of phase ----|___|----|___|---- A ____|----|___|-----|_ B In the above drawing channel B follows A. For this .condition we could assign the direction to forward ----|___|----|___ A ____|----|___|--- B

In this case A follows B and we would call this reverse. In all .cases, only one channel changes

It is necessary that the processor is fast enough to process the data before the next transition. Your encoder generates 8 transition on both A & B per motor revolution. If I recall correctly, the wheels are geared down by about 83:1. If you motor is spinning at 6000RPM that is 100 per second * 8 transitions / sec or 1.25 msec per transition / channel. For 4 encoders at 2 channels this means you have to process an !!!!interrupt every 157 usec. Please check my math Assuming 5usec ISR processing time per interrupt you should be able to keep ahead. Note that sometimes you might get transitions on multiple encoders at the same time. This will require some thought on your part. Lock out interrupts and service just one encoder or loop inside the .ISR to check for other active channels

Another "trick" is to only use quadrature encoding at low speeds. Once the motor is going fast you "know" it cannot .change direction in 0.1seconds

It is often necessary to disable interrupts when doing this type of work. Incrementing a 16-bit counter requires multiple CPU cycles and you do not want the main task and .ISR code to each overwrite the other

BTW, I only use software encoding for low speed systems. For anything that would begin to tax the CPU I would switch chips to one that has hardware support for .quadrature encoding such as the Microchip PIC24F series

I recall having some Arduino code to a very low speed test. .Will look and post later

: Secondly if it’s hard to implement your own system , there is a :library Compatible with this encoder type here is it

https://www.pjrc.com/teensy/td_libs_Encoder.html

Recommended publications