optical encoder
alnajjar1
Posts: 110
I am using a greyhill quadrature encoder to sense the direction of wheel. I used a code that I found for this encoder (I think from Parallax- but not sure) and it works fine. However, the direction sense fluctuates causing some serious problems for me.
Let's say I turn the encoder CW (a 1 in the code), then the direction indicates CW but at least once or twice per revolution, I get a CCW (a 0). I am not sure if it is the encoder or the code.
Is there a way of turning the encoder's resolution down so it ins't so sensitive? I tried inserting a counter where it checks for consitenty direction readings for 5 times for error checking but that slows things down.
Any help?
I am attaching a jpg of the circuit, and here is the code I am using:
' {$STAMP BS2}
' {$PBASIC 2.5}
'=== Test Rotary Encoder
new VAR Nib '(use a byte here for PBP)
old VAR Nib '(use a byte here for PBP)
direction VAR Bit 'direction flag bit
n VAR Word 'variable for counting
new.BIT0 = IN0 'input A on pin 7
new.BIT1 = IN1 'input B on pin 5
start:
old = new
LOOP1:
new.BIT0 = IN0
new.BIT1 = IN1
IF new = old THEN LOOP1 'stay looping if not change
direction = new.BIT0 ^ old.BIT1 'XOR to determine direction
IF direction = 1 THEN left:
IF direction = 0 THEN right:
GOTO start:
left:
n=n+1 'increase counter
GOTO printa:
right:
n=n-1 'decrease conter
GOTO printa:
printa:
DEBUG HOME
DEBUG ? direction, dec counter
GOTO start
Let's say I turn the encoder CW (a 1 in the code), then the direction indicates CW but at least once or twice per revolution, I get a CCW (a 0). I am not sure if it is the encoder or the code.
Is there a way of turning the encoder's resolution down so it ins't so sensitive? I tried inserting a counter where it checks for consitenty direction readings for 5 times for error checking but that slows things down.
Any help?
I am attaching a jpg of the circuit, and here is the code I am using:
' {$STAMP BS2}
' {$PBASIC 2.5}
'=== Test Rotary Encoder
new VAR Nib '(use a byte here for PBP)
old VAR Nib '(use a byte here for PBP)
direction VAR Bit 'direction flag bit
n VAR Word 'variable for counting
new.BIT0 = IN0 'input A on pin 7
new.BIT1 = IN1 'input B on pin 5
start:
old = new
LOOP1:
new.BIT0 = IN0
new.BIT1 = IN1
IF new = old THEN LOOP1 'stay looping if not change
direction = new.BIT0 ^ old.BIT1 'XOR to determine direction
IF direction = 1 THEN left:
IF direction = 0 THEN right:
GOTO start:
left:
n=n+1 'increase counter
GOTO printa:
right:
n=n-1 'decrease conter
GOTO printa:
printa:
DEBUG HOME
DEBUG ? direction, dec counter
GOTO start
Comments
There is an article in there I wrote about encoder matching which talks about this issue. I ran into this when using encoders that were changing too fast for the controller that was trying to watch them. I came up with a solution that used a small micro to scale the encoder readings to an acceptable level while not losing any counts. I can still offer pre-programmed microcontrollers if you want to use one to interface between your encoder and the BASIC Stamp.
Robert
I suspect that the stamp is catching the signal of the encoder in mid stream as the square wave is transitioning causing a flip in the direction reading.
To remedy this, I introduced an error checking routine where I make sure the encoder gives me 3 consistent direction readings before I accept the result. That seems to lessen the errors but it unacceptably slows down the program.
Is there a code out there for reliably reading these encoders? I have the NV for Stamps article from 1995 using the BS1 but I am not sure I will get any better results.
Many thanks in advance for any help,
Al
How fast is the encoder shaft turning?
If you are in the 60 pps or higher range, I bet you're missing pulses. Counting pulses for calculation of speed is much different than using quadrature pulses to determine direction.
You may simply need some hardware filtering, see quickie LM339 app at http://list.dprg.org/archive/1998-July/005620.html
My solution is to use the Position Controller Kit from Parallax so I can get the absolute position (and other stuff if I need) and from which I can deduce the the direction as well. This should be much faster (although more expensive of course)
I only need to detect the direction of rotation, not the speed and I wish there is a simpler and cheaper way to do that with the BS2.
Al