Shop OBEX P1 Docs P2 Docs Learn Events
optical encoder — Parallax Forums

optical encoder

alnajjar1alnajjar1 Posts: 110
edited 2010-11-17 11:10 in BASIC Stamp
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
588 x 309 - 22K

Comments

  • RobotWorkshopRobotWorkshop Posts: 2,307
    edited 2010-11-13 13:53
    You may want to pickup the July 2008 issue of SERVO magazine. (still available from their website)

    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
  • ercoerco Posts: 20,256
    edited 2010-11-15 17:32
    Is it a speed issue? Is your RPM so high that you are nearly missing counts?
  • alnajjar1alnajjar1 Posts: 110
    edited 2010-11-15 18:43
    I don't think it is the speed. When looking at the direction, I keep getting erroneous reading. Example: while turning encoder CW at normal speed, I get let's 100 0s but every 10 to 20, I get a 1. Since I am monitoring the direction to control the travel of a motor, this screws up my routines.

    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
  • stamptrolstamptrol Posts: 1,731
    edited 2010-11-16 06:01
    What are the specs on your encoder? How many pulses per rev?

    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.
  • ercoerco Posts: 20,256
    edited 2010-11-16 09:38
    Long & winding thread & videos on encoders, (BS2 at 394 PPS) at http://forums.parallax.com/showthread.php?t=121179


    You may simply need some hardware filtering, see quickie LM339 app at http://list.dprg.org/archive/1998-July/005620.html
  • alnajjar1alnajjar1 Posts: 110
    edited 2010-11-16 18:57
    This is a great thread and proved to me that my rotation of the encoder is WAY too fast for my BS2 circuit.

    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
  • ercoerco Posts: 20,256
    edited 2010-11-17 11:10
    What RPM? If you only need direction, you could use an encoder disk with one stripe (half white, half black) to lower your PPS count.
Sign In or Register to comment.