Shop OBEX P1 Docs P2 Docs Learn Events
pin code for Quadrature Motor Encoder — Parallax Forums

pin code for Quadrature Motor Encoder

Kris CardoenKris Cardoen Posts: 46
edited 2013-01-27 12:02 in Propeller 1
Hi,

I have a robot with the following type of encoder



Quadrature Motor Encoder
Encoder = 100 cycles per revolution
Encoder = 400 quadrature counts per revolution
Frequency = up to 30khz

two questions:





1. I guess the specs mean that 100 cycles are equal to one complete rotation of the wheel. And that the pulses changes for one rotation are 400, is that correct?

2. I guess I will have to count the state changes on the encoders outputs. I was thinking on using

if pin.In(5) == 1

Is that the right way to do this? How do I have to set my frequency to check the state change?

Any help would be much appreciated






Comments

  • HarpritHarprit Posts: 539
    edited 2012-12-29 17:09
    The
    Quadrature Encoder
    Object in the object exchange will give you the encoder count when ever you ask for it.
    Hook the encoder up to lines 0 and 1
    Turn the motor
    Read the position in encoder counts
    It is painless.
    If you need more , ask.
  • MicksterMickster Posts: 2,694
    edited 2012-12-29 19:14
    Hi,

    I have a robot with the following type of encoder



    Quadrature Motor Encoder
    Encoder = 100 cycles per revolution
    Encoder = 400 quadrature counts per revolution
    Frequency = up to 30khz

    two questions:





    1. I guess the specs mean that 100 cycles are equal to one complete rotation of the wheel. And that the pulses changes for one rotation are 400, is that correct?

    2. I guess I will have to count the state changes on the encoders outputs. I was thinking on using

    if pin.In(5) == 1

    Is that the right way to do this? How do I have to set my frequency to check the state change?

    Any help would be much appreciated







    Yes, you have at least two pulse-generating "channels" which are typically 90 degrees out of phase. Between the two channels you have four change-of-states occurring which can be decoded to provide 4X multiplication and an indication of the direction of rotation.

    True, there are quad-decode objects available in the OBEX but their function is not very straight-forward to me at least.

    The logic is VERY simple and I decided to create my own quad-decoder using PropBASIC. Just working simply with the very transitions that we are concerned with.

    This is actual functioning code. However, the counter value WILL need to be written to HUB memory which slows things down a bit but on the whole, this code executes faster than any pure PASM that I have found in the OBEX...albeit limited to one encoder (easily expandable but I require > 1.5M quadrature counts/sec).
    DEVICE P8X32A, XTAL1, PLL16X
    FREQ 80_000_000
    
    
    Chan_A PIN 18 INPUT
    Chan_B PIN 17 INPUT
    Chan_Z PIN 16 INPUT
    
    Counter VAR LONG = 0
    
    PROGRAM Start
    
    Start:
    
    'Decide where to start
    If Chan_A = 1 And
        Chan_B = 1 Then
        Goto A1B1
    Endif
    
    If Chan_A = 0 And
        Chan_B = 0 Then
        Goto A0B0
    Endif
    
    If Chan_A = 1 And
        Chan_B = 0 Then
        Goto A1B0
    Endif
    
    If Chan_A = 0 And
        Chan_B = 1 Then
        Goto A0B1
    Endif
    
    'This is the main loop
    'The only possible conditions are clearly spelled-out...no masking, no shifting, no rotating
      
     A1B1:
     If Chan_A = 0 Then   
       Dec Counter
       Goto A0B1
     Elseif Chan_B = 0 Then 
       Inc Counter
       Goto A1B0
     Endif
     Goto A1B1
     
    A0B0:
    If Chan_A = 1 Then
       Dec Counter
       Goto A1B0
    Elseif Chan_B = 1 Then
       Inc Counter
       Goto A0B1
    Endif
    Goto A0B0
     
     A1B0:
     If Chan_A = 0 Then
       Inc Counter
       Goto A0B0
     Elseif Chan_B = 1 Then
       Dec Counter
       Goto A1B1
     Endif
     Goto A1B0
     
     A0B1:
     If Chan_A = 1 Then
       Inc Counter
       Goto A1B1
     Elseif Chan_B = 0 Then
       Dec Counter
       Goto A0B0
     Endif
     Goto A0B1
    
    End
    
    
    
    

    P.S. My application is not battery powered and so this approach is not at all power efficient. The code could easily be modified to include the waitpne function to reduce the power consumption when no encoder transitions happen to be occurring.

    Mickster
  • Kris CardoenKris Cardoen Posts: 46
    edited 2013-01-27 12:02
    Thanks for the help, my encoders are up and running.

    One more question :-)

    When I make a full rotation I get value 20.000 reverse -20.000.
    Where does this number come from?
Sign In or Register to comment.