Uncategorized

rotary encoder reading error discussion notes

Asked 
Active today
Viewed 10 times
-1

I am using LS s40-6-1000ZT(H) 1000 p/r rotary encoder for my project with raspberry pi 3,but raspberry not working properly as per encoder specification(i.e not counting event properly).What could be the issue? Any solution??

from RPi import GPIO
from time import sleep
import tkinter as tk
a=22
b=23
GPIO.setmode(GPIO.BCM)
GPIO.setup(a,GPIO.IN)
GPIO.setup(b,GPIO.IN)
root=tk.Tk()
clkLastState=GPIO.input(a)
def call():
   global clkLastState
   global counter
   clkState=GPIO.input(a)
   if clkState != clkLastState:
           dtState=GPIO.input(b)
           if dtState != clkState:
              counter +=1
           else:
              counter -=1
           text=str(counter/2)
           print (text[0:-2])
           var.set(text)
   clkLastState=clkState

def my_callback(channel):
   call()

counter=0
clkLastState=GPIO.input(a)
#GPIO.add_event_detect(23,GPIO.BOTH,callback=my_callback)
print (counter)
var=tk.IntVar()
L1=tk.Label(root,bg="orange",fg="red",textvariable=var)
L1.pack()
# var.set(counter)
# root.after(500,main)
root.geometry("100x100")
#GPIO.add_event_detect(22,GPIO.BOTH,callback=my_callback)
GPIO.add_event_detect(23,GPIO.BOTH,callback=my_callback,debouncetime=6)
root.mainloop()
#var=tk.IntVar()
#L1=tk.Label(root,bg="orange",fg="red",textvariable=var)
#L1.pack()  
#GPIO.add_event_detect(23,GPIO.BOTH,callback=my_callback)

GPIO.cleanup()
  • Ah, let me see. (1) Nuvoton is MCU (Micro Controller), like Arduino, so you can ask her to pay attention to do one thing and just one thing, counting pulses, so never missing any. On the other hand, Rpi is a “multi-tasking” computer, like a naught girl, doing many tasks at the same time, jumping up and down, here and there, therefore you cannot ask her to pay attention to just one thing. (2) If you like, Rpi is sort of ADHD (Attention-deficit/hyperactivity disorder), always absent mindedly miscount pulses. / to continue, … – tlfong01 1 hour ago    
  • (3) In real life, one workaround is big sister Rpi partnering with the little sister WiFi MCU, ESP8266/32, which won’t miscount pulses and at the same time doing WiFi work (OK, she is doing two tasks at the same time, but not multitasking. Me giving up explaining deeper, you google :)) – tlfong01 1 hour ago    
  • Actually there are a couple of other work around to reduce miscount. Say if you are using Rpi to do a Micky Mouse project using low speed geared cheapie, ugly looking, yellow toy motors, you can use the “Discard Odd Man Out” moving average counting method. Take an oversimplified sample. Suppose your motor speed is about 600 rpm (revolutions per minute). You can either (1) count pulses every minute, and get the results 600, 601, 599 rpm etc. – tlfong01 13 mins ago   
  • But this is not very accurate, because Rpi, during that minute, might pause a bit to do other side jobs. Now the trick is to count pulses not every minute, but every second. So now you would get almost always 10, 11, 9, 10 revolutions and so on. If is likely that once in a blue moons, you get abnormal results in some seconds, like 5, or 6 counts in a second. The reason for this odd man out result is because Rpi during that second pause counting and divert to do other house keeping chores, such as answer the door etc. – tlfong01 9 mins ago   
  • Now you have have already guessed that you simply discard the odd man out and use say only 10 normal second results and multiple by 10 to get the rpm. Actually this trick is not used in toy projects. Even CalTech JPL (Jet Propulsation Lab)’s rocket scientist, using the same, yes, same trick, but using analog hardware, such as operational amplifiers to find the odd man out. They called this trick digital filtering and the most famous version is Karman Filter. – tlfong01 2 mins ago   Edit   
  • You might heard of other Rpi and Arduino guys using this digital filtering trick their balancing their two wheel car algorithms. Ah, evening tea time. See you later. PS I am just thinking aloud, my apologies for all the typos. – tlfong01 26 secs ago   Edit   

1

pigpio has a more deterministic response to GPIO level changes.

Try my pigpio Python example.

It requires the pigpio daemon.

sudo pigpiod

If Python can’t keep up you will have to use C instead. Perhaps try this pdif2 C example. It also use the pigpio daemon.

Categories: Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: