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

    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.

  2. #2


    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.

  3. #3
    Join Date
    May 2013
    Location
    australia
    Posts
    2,632


    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

  4. #4


    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.

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


    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

  6. #6
    Join Date
    May 2013
    Location
    australia
    Posts
    2,632


    Did you find this post helpful? Yes | No

    Default Re: HEDS5540 optical encoder

    using n-bit math

    result

    ready v3
    turns 23 f 489
    33609 3664.3145
    turns 23 f 486
    33606 3663.9875
    turns 23 f 484
    33604 3663.7694



    Code:
     Include "N-Bit_MATH.pbp" 
     
    enc_new VAR BYTE   bank0
    enc_old VAR BYTE   bank0
    enc_counter VAR WORD   bank0
    Flag var BYTE        bank0
    tmp         var  byte[4]
    res         var  byte[4]
    tmp1        var  byte[4]
    posn        var WORD
    turns       var word
    frac        var WORD
     
     
     
    ; Set variable value @ startup
    Flag = 1                   ;preset a value    for testing
    enc_new = 0
    enc_old= 0
    enc_counter = 33609      ;preset a value    for testing
    
     
    Main_Loop:     
    if Flag = 1 then
    turns=  enc_counter /1440
    frac=   enc_counter //1440
    serout2 PORTa.0,84, [13,10,"turns ",#turns,9,"f ",#frac ] 
    if frac then
    @  MOVE?CP 157000, _tmp  
    @  MOVE?WP _frac, _tmp1  
    @  MATH_MUL  _tmp1, _tmp, _res 
    @  MOVE?CP 1440000, _tmp
    @  MATH_DIV  _res, _tmp, _tmp1
    @  MOVE?PW _tmp1, _posn
    @  MOVE?CP 144, _tmp
    @  MATH_DIV  REG_Z, _tmp, _tmp1
    @  MOVE?PW _tmp1, _frac
    endif
    posn=posn+turns*157
    serout2 PORTa.0,84, [13,10,#enc_counter,9,dec4 posn,".",dec4 frac       ] 
     
     
     
    Flag = 0
    endif
    goto Main_Loop
    end
    Warning I'm not a teacher

  7. #7


    Did you find this post helpful? Yes | No

    Default Re: HEDS5540 optical encoder

    I try to compile your code. I downloaded the N-Bit_MATH.pbp include, but the compiler stops with this error message:
    "Bad data type"

    If I try to compile only the N-Bit_MATH.pbp (only for test) and its do not compile. Stops on first line:
    REG_X VAR BYTE[PRECISION] BANK0 SYSTEM
    with the same error message MCU is the same 16F628A, For test I try also 16F688 with the same result. Something I'am doing wrong, I think.
    Last edited by louislouis; - 11th October 2016 at 13:05.

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