C++ program for indicate the direction of incremental encoder

D

Thread Starter

danny

How to write a program for indicating the direction of incremental encoder.

There are channel A and channel B.
for ccw direction, the sequence are 1 0, 1 1, 0 1, 0 0 , and for cw direction the sequence are 0
0, 0 1, 1 1, 1 0.

How to indicate that the encoder is running in clockwise or counter clockwise direction?

I have a data acquisition card which can read digital input signal.

from,
danny
 
This was posted a few days ago. Hope it helps.

[email protected]ntrol.com


Re: Agilent's Optical Encoders
Mar 8, 2002 10:53 am, by Freq

"http://www.control.com/sqposting_html?pid=1026145031":http://www.control.com/sqposting_html?pid=1026145031

If your motor never runs in reverse, stalls, or drifts backwards, this will work. Quadrature lets your counter know what direction the motor
is turning. In a 1400 ppr incremental encoder A channel will pulse 1400 times per rev if your go backwards it will still pulse. B channel
will also pulse 1400 times per rev, but it is out of phase with A channel. Thus in forward you get A pulse prior to B. In reverse you get B
pulse prior to A. The Z or marker pulse gives you one pulse per rev and is relvent to shaft position. This can be used just as a prox,
posibly for a reset. Your only other option is an absolute encoder that will output a gray code value that relates to shaft postion. This
option is more expensive and requires more inputs. Hope this helps, good luck.
 
C
First of all, your software must loop fast enough that it is guaranteed to check the inputs again before two edges have come by. This is usually hard to do, and so this decode is usually done in a hardware circuit. But presuming you have the speed and determinism in your software, you just need to look at this cycle's inputs (Anew and Bnew) and compare them to last cycle's inputs
(Aold and Bold). Fundamentally, when you see a transition on one signal, you look at its direction (up or down) and the level of the other signal (high or low) to determine the direction of the count. The logic could look something
like this:

Anew=A
Bnew=B
if (Anew=0)
if (Aold=0) ; A stays low
if (Bnew=0)
if (Bold=1) ; B rises
Count=Count+1 ; CCW count
endif
else ; Bnew=1
if (Bold=0) ; B falls
Count=Count-1 ; CW count
endif
endif
else ; Aold=1, A falls
if (Bnew=0)
if (Bold=0) ; B stays low
Count=Count-1 ; CW count
else
CountError=1 ; Illegal transition
endif
else ; Bnew=1
if (Bold=0)
CountError=1 ; Illegal transition
else ; B stays high
Count=Count+1 ; CCW count
endif
endif
endif
else ; Anew=1
if (Aold=1) ; A stays high
if (Bnew=0)
if (Bold=1) ; B rises
Count=Count-1 ; CW count
endif
else ; Bnew=1
if (Bold=0) ; B falls
Count=Count+1 ; CCW count
endif
endif
else ; Aold=0, A rises
if (Bnew=0)
if (Bold=0) ; B stays low
Count=Count+1 ; CCW count
else
CountError=1 ; Illegal transition
endif
else ; Bnew=1
if (Bold=0)
CountError=1 ; Illegal transition
else ; B stays high
Count=Count-1 ; CW count
endif
endif
endif
endif
Aold=Anew
Bold=Bnew

You certainly could implement this more efficiently, but this shows the concept.

Curt Wilson
Delta Tau Data Systems
 
Top