henryzxu89
Posts: 4
Joined: Fri Feb 22, 2019 12:33 am
Location: Mountain View, CA

Optical Encoder Too Fast for GPIO Input?

Fri Feb 22, 2019 12:43 am

Hi All,

I am using optical encoders that is specified to run 948 state changes when directly attached onto the motor shaft and 8 state changes when attached onto the output shaft. This is my code:

Code: Select all

LEFT_ENCODER = 12
GPIO.setup(LEFT_ENCODER, GPIO.IN, pull_up_down = GPIO.PUD_DOWN)

GPIO.add_event_detect(LEFT_ENCODER, GPIO.BOTH, callback = interrupt_service_routine)

def interrupt_sevice_routine(LEFT_ENCODER):
      global ticks
      ticks = ticks + 1
      return
However, the gpio input is reading nowhere 948 ticks per rotation when the encoders are attached onto the motor shaft. In fact, the faster the motor runs, the more state changes the code misses. Does this have to do with raspberry pi input speed limits? Because when I attached the encoders onto the output shaft, it correctly reads 8 ticks per rotation of the wheel.

Thanks so much in advance!


henryzxu89
Posts: 4
Joined: Fri Feb 22, 2019 12:33 am
Location: Mountain View, CA

Re: Optical Encoder Too Fast for GPIO Input?

Sat Feb 23, 2019 3:22 am

OutoftheBOTS: Thanks so much for your reply and great youtube video!


pootle
Posts: 323
Joined: Wed Sep 04, 2013 10:20 am
Location: Staffordshire
Contact: Website

Re: Optical Encoder Too Fast for GPIO Input?

Sat Feb 23, 2019 9:43 am

I've reliably tracked tiny dc motors at up to around 17,000 rpm with 3 pole encoders. (3 poles * 2 pickups * 2 edges *17,000 rpm)

The Rotary_Encoder.py example in Joan's link will track this quite easily

henryzxu89
Posts: 4
Joined: Fri Feb 22, 2019 12:33 am
Location: Mountain View, CA

Re: Optical Encoder Too Fast for GPIO Input?

Sat Feb 23, 2019 10:22 pm

Thanks joan and pootle!

henryzxu89
Posts: 4
Joined: Fri Feb 22, 2019 12:33 am
Location: Mountain View, CA

Re: Optical Encoder Too Fast for GPIO Input?

Thu Mar 07, 2019 9:17 am

Hey Joan, why does the rotary encoder in your python code have two contacts? Thanks in advance.

User avatar
joan
Posts: 14093
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Optical Encoder Too Fast for GPIO Input?

Thu Mar 07, 2019 1:00 pm

I only have experience of inexpensive mechanical incremental rotary encoders (the sort used on hifi when I had hifi).

They have two outputs A and B which are pulled to ground as they pass the detentes.

https://en.wikipedia.org/wiki/Rotary_encoder

Return to “Automation, sensing and robotics”