optical encoders?

Status
Not open for further replies.
Mike said:
I guess it's a matter of opinion and preference (grin)...

Why throw expensive hardware at a problem that can be solved with a dozen lines of 'free' code (grin)?

Kind regards, Mike


I didn't state my preference if that's what you had in mind, but you are right, he might be more familiar with the hardware. I did offer alternative to someone who was stuck, and couldn't get his code to work. I guess ASM code writen by someone who is maybe just learning to program is not quite efficient or easy to follow. While 4 states are not much to decode, someone who struggles with a problem, might appreciate alternative solution even if it is more expensive ($0.50...?) and that was the point (no need for grin).

I'm way over my head here.
I can't seem to grasp the concept.

The algorithym is also challenging,
and I've spent many hours getting it right.
I know there is always a better way.

Kind regards,

Panic Mode
 

Uh, it's some sort of thumb pressure gizmo. I think he can get instaneous acceleration of a lot more than you are thinking.

With your example, the limit is 10 RPS for a 1khz (1 mS) sample rate, by the way.

Let's do the math. 25 ppr = 100 edges per rev in a 90 degree phase relationship. depending on his gearing/linkage from the lever device to the encoder disk he could see a pretty fast pulse rate. How fast can you move your thumb? Remember, its about max instantaneous rate, not steady state. If you guess too low, you get errors (invalid grey code transitions). I still think 1K may be a bit low but it's easy enough to determine. The good news is timer interrupt overhead for this is tiny so the penalty for sampling too often is pretty low.
 
Finally, I had time to play with this.

Please don't be to critical, as I'm relatively new at Pic programming.
Remember, I'm trying to increase DC motor speed via an optical encoder which is more or less fashioned like a gas pedal in a car. When you apply pressure to accelerator speed goes up, when you let off accel, speed decrease.

Here is my code.
Code:
;16F628 ENCODER READING
;
;DEVICE 16F628
      LIST P=16F628,
      #INCLUDE P16F628.inc
      



RP1                          EQU     0x06       
RP0                          EQU     0x05        
PORTA                        EQU     0x05        
PORTB                        EQU     0x06        
STATUS                       EQU     0x03        
TRISA                        EQU     0x85        
TRISB                        EQU     0x86        
SPEED                        EQU     0x30        ; variable used for context saving
COUNT1                       EQU     0x32        ; variable used for context saving 
COUNT2                       EQU     0x33        ; variable used for context saving
COUNT3                       EQU     0x34        ; variable used for context saving
COUNT4                       EQU     0x35        ; variable used for context saving
COUNT5                       EQU     0x36        ; variable used for context saving
COUNT6                       EQU     0x37        ; variable used for context saving 
_ENCA                        EQU     0x53        ; variable used for context saving
_ENCB                        EQU     0x54        ; variable used for context saving
PC                           EQU     0x02


   

     

;PORT SETUP

SETUP    
     BSF STATUS, RP0
     MOVLW 31H       ;PORTA RB0-RB4 INPUT
     MOVWF TRISA
     MOVLW 00H
     MOVWF TRISB     ;PORTB ALL OUTPUTS
     BCF STATUS, RP1
     movlw 0x60
     movwf 0X32 ;COUNT1
     movlw 0X60
     movwf 0X33 ;COUNT2
     movlw 0x99
     movwf 0X34 ;COUNT3
     movlw 0X99
     movwf 0X35 ;COUNT4
     movlw 0x10
     movwf 0X36 ;COUNT5
     movlw 0X10
     movwf 0X37 ;COUNT6
     CLRW
     CLRF PORTA
     CLRF PORTB
     
    
   
    
MAIN
    
    BTFSC PORTA, 0  ;BRAKES RA0
    GOTO  LED1
    NOP
    BTFSS PORTA, 1  ;SAFETY SWITCH UNDER SEAT RA1
    GOTO  LED2
    NOP
    BTFSC PORTA, 2  ;REVERSE RA2
    GOTO  REVERSE
    GOTO  START
    
    
    
START
    BTFSS PORTA,3; test file RA3 'A' pin of encoder. 
    XORWF _ENCA ; compare if same with _ENCA
    BTFSS STATUS,2 ;check Z flag see if set or not
    GOTO SPEED_FWD 
    GOTO NEXT 
    
NEXT 
    BTFSS PORTA,4 ; test file RA4'B' pin of encoder. 
    XORWF _ENCB ; again compare with _ENCB 
    BTFSS STATUS,2 ;again test Z flag
    GOTO SPEED_RVRS
    GOTO MAIN   
       
SPEED_FWD
    INCF SPEED,1 ;increment file SPEED and put result back in SPEED register
    MOVF SPEED,0 ;move to W register
    CALL TABLE
    MOVWF PORTB
    GOTO MAIN
      
SPEED_RVRS
    DECF SPEED,1 ; decrement file SPEED and place result back in SPEED register.
    MOVF SPEED,0  ;move to W register
    CALL TABLE
    MOVWF PORTB
    GOTO MAIN
    
; ALARM Subroutines

        
DELAY
     
LOOP1  DECFSZ COUNT1,1 
       GOTO LOOP1
       DECFSZ COUNT2,1
       GOTO LOOP1
       GOTO MAIN

DELAY1
     
LOOP2  DECFSZ  COUNT3,1
       GOTO LOOP2
       DECFSZ COUNT4,1
       GOTO LOOP2
       GOTO MAIN
       
DELAY2
     
LOOP3  DECFSZ COUNT5,1
       GOTO LOOP3
       DECFSZ COUNT6,1
       GOTO LOOP3
       RETURN
       
       
             

LED1
     MOVLW 0x01
     MOVWF PORTB ;RB0
     CALL DELAY
     MOVLW 0x00
     MOVWF PORTB ;RB0
     CALL DELAY
     BTFSC PORTA,0
     GOTO LED1
     RETURN
     
LED2
     MOVLW 0x02
     MOVWF PORTA ;RB1
     CALL DELAY1
     MOVLW 0x00
     MOVWF PORTB ;RB1
     CALL DELAY1
     BTFSC PORTA, 1
     GOTO LED2
     RETURN 
     
REVERSE
     
     BSF PORTA,3
     GOTO MAIN
     
     
     
     
TABLE
     ADDWF PC
     retlw 0x01
     retlw 0x02
     retlw 0x04
     retlw 0x08
     retlw 0x10
     retlw 0x20
     retlw 0x40
     retlw 0x80
     return
      

     END

Please, just verify, and critisize as needed. All constructive critisism will be welcome.
Code is not complete, just need to weed out the bad.

Thank You.
 
hai nice to meet u all..
i'm a newbie

i've try to measuring how long my robot have walking arround with mouse encoder too..i use an ATMEL microcontroller as a proccessor...

n this is my source:


this source is working..
but may i know where you get datasheet of that mouse encoder??
the mouse that i use have 48 step/revolution..
i need that datasheet to get sure that my source algoritm is already right..
thx for you all..
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…