HEDS5540 optical encoder


Closed Thread
Results 1 to 24 of 24

Hybrid View

  1. #1


    Did you find this post helpful? Yes | No

    Default Re: HEDS5540 optical encoder

    For testing and learning the 16F628A is seems to be OK. But now I try to convert the pulse reading to distance. One full encoder turn generates 1440 pulses. The wheel diameter is 25mm, the circumference is 157mm. I want reading precision to 0,1mm. I try basic math like:
    Code:
    Main_Loop: 
    if Flag = 1 then 
    let counter = (enc_counter/36) * 157 / 4
    Lcdout $fe, 1                    
    lcdout  Dec counter     ; display counter value on LCD
    Flag = 0
    endif
    goto Main_Loop
    But the precision is terrible, can you explain me the right way?
    Last edited by louislouis; - 5th October 2016 at 00:49.

  2. #2
    Join Date
    May 2013
    Location
    australia
    Posts
    2,654


    Did you find this post helpful? Yes | No

    Default Re: HEDS5540 optical encoder

    But the precision is terrible, can you explain me the right way?

    157/1440 = .109 mm per encoder tick

    .109 * 256 * 10 = 279

    distance * 10 = (counts * 279)/256

    to use pbp's higest resolution integer functions

    Code:
    counter = enc_counter */ 279 
    
    lcdout  Dec counter/10 ,".", dec counter//10     ; display counter value on LCD
    problem here is that when enc_counter > 60000 [or thereabouts] the internal 32bit result will overflow and the result will be wrong
    you could look at nbit math


    ps you could also use div32

    Code:
    counter = enc_counter * 1000
    counter =  div32 917
    
    lcdout  Dec counter/10 ,".", dec counter//10     ; display counter value on LCD
    Last edited by richard; - 5th October 2016 at 01:27.
    Warning I'm not a teacher

  3. #3


    Did you find this post helpful? Yes | No

    Default Re: HEDS5540 optical encoder

    Thank You Richard for helping me. That's great, now the "core" of my distance meter works perfectly. Yes, the max. distance what I can measure is near six and a half meter which is enough for me. If I want to measure more than 6,5m simply zeroed the reading and start count from that point. Now I must implement negative reading when I go to opposite direction from zero the reading must be from -1..... to -655,3.
    Your explanation helping me a lot, once again Thank You.

  4. #4


    Did you find this post helpful? Yes | No

    Default Re: HEDS5540 optical encoder

    I finally assembled my distance meter based on rotary optical encoder and precision wheel. But still I have problem with math and precision.
    For example, wheel circumference is 157mm, and when I take a 30 revs the result must be 4710mm, but the MCU calculates only 4708,1mm which is 1,9mm difference. Is there a way to improve the precision?
    I use Richard's math:
    157/1440 = .109 mm per encoder tick
    .109 * 256 * 10 = 279
    distance * 10 = (counts * 279)/256
    Code:
    counter = enc_counter */ 279 
    lcdout  Dec counter/10 ,".", dec counter//10     ; display counter value on LCD
    The precision 0,1mm is enough for me, the max. measurable distance want be 6000mm. I try some change, if I improve the precision to 0.01mm then the max. measurable distance shortened to 600mm and this not enough.
    Last edited by louislouis; - 9th October 2016 at 23:35.

  5. #5
    Join Date
    May 2013
    Location
    australia
    Posts
    2,654


    Did you find this post helpful? Yes | No

    Default Re: HEDS5540 optical encoder

    you could look at nbit math or use a pic18 and long vars

    you can glean a bit more precision breaking the calc up a bit

    where 43200 counts would give 4709.2

    Code:
     
    frac = enc_counter.lowbyte * 1090         
    posn1  = div32 100          ;mm*100          ;mv 2790
    posn2 = (enc_counter.highbyte&15) * 2791  ; mm*100   mv 41865
    frac = (enc_counter.highbyte>>4) * 44657
    posn3 =  div32 100                       ;mm     mv6698
    
    posn3=  posn3 + (posn1+posn2)/100       ;mm
    frac=   ((posn1+posn2)//100 +5)/10        ;round it up
    serout2 PORTa.0,84, [13,10,#enc_counter,9,dec posn3,".",#frac       ]
    Warning I'm not a teacher

  6. #6


    Did you find this post helpful? Yes | No

    Default Re: HEDS5540 optical encoder

    Hello Richard, thank you again for help. Your code works and take a little improvement. I make some changes, first got a smaller precision wheel dia. 25mm (circ. 78,53975mm) and divide the encoder reading /2. Now the encoder reading is only 720 per one rev.
    The code is as follows:
    Code:
    Main_Loop:
    enc_counter = enc_counter /2
    if Flag = 1 then 
    frac = enc_counter.lowbyte * 1090         
    posn1  = div32 100          ;mm*100          
    posn2 = (enc_counter.highbyte&15) * 2794  ; mm*100   
    frac = (enc_counter.highbyte>>4) * 44680
    posn3 =  div32 100                       ;mm     
    posn3=  posn3 + (posn1+posn2)/100       ;mm
    frac=   ((posn1+posn2)//100 +10)/10        ;round it up
     
    Lcdout $fe, 1
    lcdout  "Dist: ",dec posn3,".",#frac
    Flag = 0
    endif 
    goto Main_Loop
    Now I can measure up to 7100mm, but still more or less precise.
    Next step will be order one of the PIC18x mcu and try long variables.
    Here are suitable to use the same encoder reading code (written in ASM) and simple change the word var to long var?
    I think its not that easy. When I have the 18x PIC I think, I will need more help with this project. Thanks.

  7. #7
    Join Date
    May 2013
    Location
    australia
    Posts
    2,654


    Did you find this post helpful? Yes | No

    Default Re: HEDS5540 optical encoder

    frac= ((posn1+posn2)//100 +10)/10 ;round it up
    is incorrect , it will round the number up always

    frac= ((posn1+posn2)//100 +5)/10 ;round it up
    is the way to round the number up if the last digit is 5 or more
    Warning I'm not a teacher

Similar Threads

  1. 2 Beam Optical Pulse Generator
    By WOZZY-2010 in forum Schematics
    Replies: 8
    Last Post: - 6th April 2010, 04:03
  2. USB Optical mouse as motor encoder
    By RodSTAR in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 24th August 2008, 15:09
  3. optical voice link
    By Macgman2000 in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 18th May 2008, 21:11
  4. Using a optical coder
    By debutpic in forum mel PIC BASIC Pro
    Replies: 22
    Last Post: - 16th October 2007, 04:57
  5. Using the optical sensor from a ball mouse
    By lsteele in forum General
    Replies: 5
    Last Post: - 5th September 2007, 16:45

Members who have read this thread : 0

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