Can PBP read optical encoder damn fast? :)


Closed Thread
Results 1 to 40 of 47

Hybrid View

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


    Did you find this post helpful? Yes | No

    Default Re: Can PBP read optical encoder damn fast? :)

    If not pissing away 3/4 of the encoder resolution is something worth doing here's a replacement routine that does 4x decoding. It might not be the most optimized or elegant way but is works. Surprisingly (to me) it seems to work fine up to around 8kHz which means it does 32000 counts per second when being fed "perfect" quadrature signals my function gen.

    Code:
    ' Added variables:
    Encoder VAR BYTE
    Old_Enc VAR BYTE
    Test VAR BYTE
    Dir VAR WORD          ' Must be same size as Position variable
    
    
    '-------------------------------------------------------------
    '--------------Poll encoder and keep count -------------------
    '-------------Alternative routine with 4x decoding------------
    '-------------------------------------------------------------
    
        Encoder.0 = Ch_A
        Encoder.1 = Ch_B
    
        Test = Encoder ^ Old_Enc
    
        Dir = 0
    
        If Test = 1 THEN
            If Old_Enc = 0 THEN Dir = 1
            IF Old_Enc = 1 THEN DIR = -1
            IF Old_Enc = 2 THEN Dir = -1
            If Old_Enc = 3 Then Dir = 1
        ENDIF
    
        IF Test = 2 THEN
            IF Old_Enc = 0 THEN DIR = -1
            IF Old_Enc = 1 THEN Dir = 1
            IF Old_Enc = 2 THEN Dir = 1
            IF Old_Enc = 3 THEN Dir = -1
        ENDIF
    
        Position = Position + Dir
        
        Old_Enc = Encoder

  2. #2
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,133


    Did you find this post helpful? Yes | No

    Default Re: Can PBP read optical encoder damn fast? :)

    To me looks elegant enough! Wow, 32000 counts/s? I suppose on a fast 18F device, right?

    Ioannis

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


    Did you find this post helpful? Yes | No

    Default Re: Can PBP read optical encoder damn fast? :)

    Same setup as before, 18F4520 @20MHz so not really that fast.
    I don't know why it seems faster than the 1x version from earlier, perhaps I made a mistake when measuring either one of them.

  4. #4
    Join Date
    May 2013
    Location
    australia
    Posts
    2,645


    Did you find this post helpful? Yes | No

    Default Re: Can PBP read optical encoder damn fast? :)

    Henrik assuming i have implemented your ideascorrectly it works not at all well for a ky040 re in real life, i get about 400 counts per indent and rarely does it get direction correct.
    my old code previously posted works near perfectly in the same rig either as isr or polled even a couple of .1uf across the pins does not help.


    Code:
    #CONFIG
                 __config        _CONFIG1,    _FOSC_INTOSC & _CP_OFF & _WDTE_ON  &  _PWRTE_ON  &  _MCLRE_ON  & _CLKOUTEN_OFF
                  __config      _CONFIG2, _PLLEN_ON & _LVP_OFF
    #ENDCONFIG
    
    
    DEFINE OSC 32
        OSCCON=$70
        ANSELA=0
        OPTION_REG.6=0
        ENCODER    VAR BYTE   ;THE STATE OF THE ENCODER
        DIR   VAR BYTE    ;POSN
        Old_Enc  VAR BYTE    ;LAST MOVE 
        TEST   VAR BYTE 
        Position   VAR WORD 
        TRISA = %11111110
        lata.0=1 ;DEBUG
        Ch_A    VAR PORTA.4
        Ch_B    VAR PORTA.5
        
        
        DEFINE DEBUG_REG PORTA
        DEFINE DEBUG_BIT 0        
        DEFINE DEBUG_BAUD 38400
        DEFINE DEBUG_MODE 0     
        pause 2000
        Debug "Start",13 ,10
        Position =0 
        Old_Enc=0
        
    mainloop:
    '     Encoder.0= Ch_A    ;WON'T WORK AT ALL MOST TIMES
    '     Encoder.1= Ch_B 
        Encoder = (PORTA&48)>>4
        Test = Encoder ^ Old_Enc
        Dir = 0
        If Test = 1 THEN
            If Old_Enc = 0 THEN Dir = 1
            IF Old_Enc = 1 THEN DIR = -1
            IF Old_Enc = 2 THEN Dir = -1
            If Old_Enc = 3 Then Dir = 1
        ENDIF
        IF Test = 2 THEN
            IF Old_Enc = 0 THEN DIR = -1
            IF Old_Enc = 1 THEN Dir = 1
            IF Old_Enc = 2 THEN Dir = 1
            IF Old_Enc = 3 THEN Dir = -1
        ENDIF
        Old_Enc = Encoder
        Position = Position + Dir
        IF DIR THEN 
            debug 13,10, "C",SDEC Position
        ENDIF
    goto mainloop


    my take [no pic18 worky or shitty old chips with no wreg access]

    Code:
    '****************************************************************'*  Name    : RE16.BAS                                          *
    '*  Author  : RICHARD                                           *
    '*  Notice  :                                                   *
    '*          : All Rights Reserved                               *
    '*  Date    : 5/06/2020                                         *
    '*  Version : 1.0                                               *
    '*  Notes   :    ONLY PIC16'S AND 12'S WITH WREG                *
    '*          :   IN THIS INCARNTATION NO SHITTY OLD CHIPS        *
    '****************************************************************
    
    
    #CONFIG
                 __config        _CONFIG1,    _FOSC_INTOSC & _CP_OFF & _WDTE_ON  &  _PWRTE_ON  &  _MCLRE_ON  & _CLKOUTEN_OFF
                  __config      _CONFIG2, _PLLEN_ON & _LVP_OFF
    #ENDCONFIG
    
    
    DEFINE OSC 32
        OSCCON=$70
        ANSELA=0
        OPTION_REG.6=0
        ev    VAR BYTE bank0   ;THE STATE OF THE ENCODER
        cnt   VAR WORD bank0   ;POSN
        TMP   VAR BYTE bank0   ;LAST MOVE IS 1, 0, -1
        TRISA = %11111110
        lata.0=1 ;DEBUG
        
        DEFINE DEBUG_REG PORTA
        DEFINE DEBUG_BIT 0        
        DEFINE DEBUG_BAUD 38400
        DEFINE DEBUG_MODE 0     
        pause 2000
        Debug "Start",13 ,10
        CNT=0 
    
    
    mainloop:
    ASM
        bcf STATUS,C      
        RLF _ev ,f        ;shift last reading into position
        bcf STATUS,C
        RLF _ev ,f
        MOVF PORTA,W     ;read enc pins 4,5
        ANDLW 48         ;mask off others
        SWAPF   WREG ,w  ;shift NEW reading into position
        IORWF _ev,F     ;combine this read with last read
        MOVF _ev,W     ;save ev
        ANDLW 15
        L?CALL enc_lut       ; decide to inc , dec or ignore 
        MOVWF _TMP         ;save decision
        BTFSC  _TMP,7
        GOTO myDEC16
        BTFSC  _TMP,0
        GOTO myINC16
    enc_exit                    ;  exit
        GOTO OUT
    myINC16
        INCF _cnt ,f
        BTFSC STATUS,Z
        INCF  _cnt +1 ,f 
        GOTO enc_exit
    myDEC16
        MOVF  _cnt ,w
        BTFSC STATUS,Z
        DECF   _cnt +1, f   
        DECF   _cnt , f       
        GOTO enc_exit
    OUT    
        ENDASM
        IF TMP THEN 
            debug 13,10,SDEC CNT
        ENDIF
    goto mainloop
    
    
    enc:
    ASM
    enc_lut                     ; the decision matrix
        addwf   PCL, F          
        retlw   0
        retlw   255
        retlw   1
        retlw   0
        retlw   1
        retlw   0
        retlw   0
        retlw   255
        retlw   255
        retlw   0
        retlw   0
        retlw   1
        retlw   0
        retlw   1
        retlw   255
        retlw   0  
    ENDASM
    Warning I'm not a teacher

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


    Did you find this post helpful? Yes | No

    Default Re: Can PBP read optical encoder damn fast? :)

    I have it running on the bench, works perfectly fine with an optical encoder (Scancon 2RMHF-500, 500 lines / 2000 edges per rev) - IRL :-)
    Like I said previously I do not expect it to work reliably with a mechanical encoder unless it is properly debounced in hardware. Why your ASM version works with the same encoder I do not know :-(

    You have a DEBUG statement within the loop - that will obviously mess up the timing but I know you know that.

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


    Did you find this post helpful? Yes | No

    Default Re: Can PBP read optical encoder damn fast? :)

    silly me i had the dir var as byte, it now works with this added change

    ' Encoder.0 = Ch_A
    ' Encoder.1 = Ch_B
    encoder =(porta&48)>>4

    reading the pins individually wont work for me @32mhz
    Warning I'm not a teacher

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


    Did you find this post helpful? Yes | No

    Default Re: Can PBP read optical encoder damn fast? :)

    I made that exact mistake initially, hence the comment where the DIR variable is declared - yet I missed it when looking at you code...

    I wonder why reading the pins individually doesn't work in your case. I only did it that way because CuriousOne wanted to be able to have the signals placed arbitrarily. I suspect though that reading them individually might be faster than masking and shifting.

    I've been playing around with the math to go from encoder counts to actual frequency and I'm down to 110 cycles for calculating and stuffing the bits into the array. Had to resort to a tiny bit of ASM, getting the high word of a 16*16bit multiplication back from PBP without resorting to actually using LONGs.

Similar Threads

  1. HEDS5540 optical encoder
    By louislouis in forum mel PIC BASIC Pro
    Replies: 23
    Last Post: - 11th October 2016, 23:23
  2. how to read ky40 mechanical encoder ?
    By iw2fvo in forum mel PIC BASIC Pro
    Replies: 20
    Last Post: - 25th October 2015, 16:22
  3. Probe to read quadrature encoder with DT_INT need help
    By phoenix_1 in forum mel PIC BASIC Pro
    Replies: 11
    Last Post: - 31st August 2009, 20:43
  4. USB Optical mouse as motor encoder
    By RodSTAR in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 24th August 2008, 15:09
  5. Damn these low power PIC and other Microcontrollers
    By keithdoxey in forum Off Topic
    Replies: 8
    Last Post: - 12th November 2006, 21:52

Members who have read this thread : 2

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