decoding quadrature encoders


Closed Thread
Results 1 to 40 of 94

Hybrid View

  1. #1
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    Did you find this post helpful? Yes | No

    Default Re: decoding quadrature encoders

    Hi,
    If I'd venture a guess it would be that with a 360CPR encoder and 4x decoding the counter variable will "tick" 4 counts per degree but your math assumes the resolution is 0.1 degrees.

    As a simple workaround try multiplying your count value by 2.5 before you enter your display routine.
    Code:
    ' Here is were I struggle.....
    enc_counter = enc_counter */ 640   ' Multiply by 2.5
    '*********************************************************************************
    lcdout $fe,$C0, SDEC2 enc_counter/10, ".", SDEC2 enc_counter//10     	'display enc_counter value
    '*********************************************************************************
    Obviously, the encoder only provides 0.25 degree resolution and no math can change that.

    If it doesn't work properly then strip out the formating and display the raw value (pre or post multiplying with 2.5) and tell us between what values it ranges.

    /Henrik.

  2. #2
    cbrun17's Avatar
    cbrun17 Guest


    Did you find this post helpful? Yes | No

    Default Re: decoding quadrature encoders

    Hi Henrik,

    Thank you so much for the quick response. I will give your suggestion a try this evening and report back with the results.

    Kind regards,

    Chris

  3. #3
    cbrun17's Avatar
    cbrun17 Guest


    Did you find this post helpful? Yes | No

    Default Re: decoding quadrature encoders

    I changed the multiplier as suggested but I received very erroneous results. Using just "lcdout $fe,$C0, SDEC enc_counter"

    The count results in 2,5,7,10,12,15,,,455. So 2 and 3 counts alternately.
    Then if I multiply enc_counter by 250, I get exactly + 180 degrees which I think confirms 2 "ticks" Also note that by altering the value of enc_counter, I loose my negative count.

    Counting enc_counter without multiplier results in ~ +/- 184.

    To clarify, the shaft has a 12:00 o'clock "zero" position. Turning in either direction from center results in ~ +/- 184. Counting the number of pulses on my Fluke counter indicates the same count.

    So, the way I understand this quadrature coding, it compares the A/B channels to determine the direction. So changing the post result would only be wiped away by any subsequent updates from the interrupt routine.

    I would be satisfied with .25 degree increments if I could get the LCD output correctly. Showing +/- 90 in .25 increments in the form of xx.xx.

    Thank you again for sharing your knowledge.

    Chris

  4. #4
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    Did you find this post helpful? Yes | No

    Default Re: decoding quadrature encoders

    So you're getting ~360 counts for 180 degrees movement. Then the interrupt code must be doing 2x decoding instead of 4x - provided the encoder is actually 360cpr.
    You loose the negative value because PBP doesn't handle the multiplication properly and you're of course better off copying the enc_counter variable to a second one for scaling and display purposes, sorry about all that.

    Try this (untested):

    Code:
    Degrees VAR WORD
    Sign VAR BIT
    
    Degrees = enc_counter   ' Degrees is now -180 to 180
    Sign = Degrees.15   ' Save sign
    Degrees = (ABS Degrees) * 5   ' Degrees is now 0 to 900
    If Sign THEN Degrees = -Degrees   ' Restore sign, value is now -900 to 900
    /Henrik.

  5. #5
    cbrun17's Avatar
    cbrun17 Guest


    Did you find this post helpful? Yes | No

    Default Re: decoding quadrature encoders

    Hi Henrik,

    No worries... I learn from mistakes as well. I appreciate all your help.

    I believe we're getting very close. I have the following code:


    if enc_counter <> enc_counter_old then 'see if value has changed
    enc_counter_old = enc_counter 'move new value to old
    Counter = enc_counter
    Sign = Counter.15 ' Save sign
    Counter = ((ABS Counter) * 5) ' Degrees is now 0 to 900

    If Sign THEN Counter = -Counter ' Restore sign, value is now -900 to 900

    lcdout $FE,1, sdec Counter / 10, ".", sdec Counter //10
    lcdout $FE,$C0, sdec Counter


    First, the counter increments by 5.
    Also, in the first LCD statement, the count increments to +90 as expected. However, the negative value shows as 6553.1, 6552.6, etc...
    The second LCD out statement displays +/- 900 as you explained.

    Seems the absolute value gets lost when trying to display negative after formatting.

    Is there maybe a way I can parse the decimal values prior to display statement to maintain the value?

    Chris
    Last edited by cbrun17; - 7th February 2017 at 16:15.

  6. #6
    cbrun17's Avatar
    cbrun17 Guest


    Did you find this post helpful? Yes | No

    Default Re: decoding quadrature encoders

    I made some changes to the formatting and I successfully display the +/- values. Displays +/- xx.x. I updated the code to:

    if enc_counter <> enc_counter_old then 'see if value has changed
    enc_counter_old = enc_counter 'move new value to old
    Counter = enc_counter
    Sign = Counter.15 ' Save sign
    Counter = ((ABS Counter) * 5) ' Degrees is now 0 to 900
    If Sign THEN Counter = -Counter ' Restore sign, value is now -900 to 900

    If NOT sign then
    lcdout $FE,1, sdec Counter /10 , ".", sdec Counter // 10
    else
    lcdout $FE,1, "-", sdec -Counter /10 , ".", sdec -Counter // 10
    endif

    This takes care of displaying properly. Now is there a way I can bring the count down to .25 increments instead of .5?

    Thanks again.

    Chris

  7. #7
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    Did you find this post helpful? Yes | No

    Default Re: decoding quadrature encoders

    Hi,
    Great job! The problem with the display routine is, again, that PBP doesn't handle math with negative numbers properly so the result of the Counter / 10 operation gets messed up when Counter is negative.

    You can probably make that snippet a bit more efficient, somelike like this perhaps:
    Code:
    if enc_counter <> enc_counter_old then 'see if value has changed
    enc_counter_old = enc_counter 'move new value to old
    Counter = enc_counter 
    
    Sign = Counter.15 ' Save sign
    Counter = ((ABS Counter) * 5) ' Degrees is now 0 to 900
    
    LCDOUT $FE, 1  ' Clear screen
    IF Sign THEN
      LCDOUT "-"    ' Print a minus sign if the value is negative
    ENDIF
    LCDOUT SDEC Counter / 10, ".", SDEC Counter // 10   ' now print the value
    I must admit that I can't really follow the ASM code for the interrupt but since it's using Interrupt on change I think it "should" do x4 decoding so you "should" get 1440 counts per revoultion on a 360CPR encoder. I don't know why that's not happening. Hopefully someone else is able to help with that part.

    /Henrik.

  8. #8
    cbrun17's Avatar
    cbrun17 Guest


    Did you find this post helpful? Yes | No

    Default Re: decoding quadrature encoders

    Thank you Henrik.

    I couldn't have made it this far without you're help. The displayed number is perfect. As you said, the counts aren't making much sense. I'll continue to experiment with this to see if I can figure out how to get the correct resolution.

    Thanks again and kind regards....

    Chris

Similar Threads

  1. Quick thoughts: DT Ints with encoders
    By kevlar129bp in forum mel PIC BASIC Pro
    Replies: 19
    Last Post: - 7th January 2010, 01:01
  2. PMDC SERVO MOTOR WITH quadrature encoder DRIVE ?
    By phoenix_1 in forum Schematics
    Replies: 37
    Last Post: - 22nd November 2009, 19:45
  3. 32-bit Quadrature Counter With Serial Interface
    By precision in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 10th June 2008, 02:49
  4. quad encoders
    By cpayne in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 13th March 2007, 17:49
  5. "momentary" IR decoding
    By sporker in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 20th June 2005, 01:53

Members who have read this thread : 4

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts