thalangeajit
Posts: 8
Joined: Fri Feb 17, 2017 12:19 pm

Problem With Encoder and Python

Fri Feb 17, 2017 12:32 pm

Hello Friends,
As i am new to raspberry pi and learning python with pi. i am trying to get the encoder reading over interrupt. Below is my code, my Encoder is of 1000 PPR. I am getting around 1000 PPR at slow speed but at high speed pi is missing pulses. So my question is "is the below code is correct ?" if not "How to get 1000PPR at high speed ?" and "What is the default bouncetime in GPIO.add_event_detect ?"

Thanks in advance

Code: Select all


import RPi.GPIO as GPIO
import time

GPIO.setmode (GPIO.BOARD)
GPIO.setwarnings(False)

pin_A = 3	
pin_B = 5

Encoder_Count = 0		# Encoder Count variable

GPIO.setup (pin_A, GPIO.IN, pull_up_down=GPIO.PUD_UP)			# pin input pullup
GPIO.setup (pin_B, GPIO.IN, pull_up_down=GPIO.PUD_UP)			# pin input pullup
	
def do_Encoder(channel):
	global Encoder_Count
	if GPIO.input(pin_B) == 1:
		Encoder_Count += 1
	else:
		Encoder_Count -= 1

GPIO.add_event_detect (pin_A, GPIO.FALLING, callback=do_Encoder)	# Enable interrupt

while(1):
	global Encoder_Count
	print Encoder_Count


User avatar
Etienne
Posts: 182
Joined: Wed Jul 24, 2013 6:23 am

Re: Problem With Encoder and Python

Fri Feb 17, 2017 3:45 pm

May be you can try to add a time.sleep(1) command in your while(1) loop, I suppose having the value every second or so should be enough. Doing this you may free some CPU to better handle the callback function?

thalangeajit
Posts: 8
Joined: Fri Feb 17, 2017 12:19 pm

Re: Problem With Encoder and Python

Fri Feb 17, 2017 4:10 pm

One more point to be noted... Difference of count was more with UI as compared to without UI

ghp
Posts: 1146
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: Problem With Encoder and Python

Fri Feb 17, 2017 6:48 pm

Hello,
checked this encoder logic with a pulse generator. The generator is an arduino due, pulse frequency was 10Hz, 100, 500 and 1000, 3000 and 5000.
The counter results have been (over a time of 20sec to reduce measurement errors)

Code: Select all

freq       exp     measure
   10Hz    200         200
  100Hz   2000        2000
  500Hz  10000       10000  +-2
 1000Hz  20000       20000 +-25
 2000Hz  40000       40000 -40
 3000Hz  60000       60000 -660
 5000Hz 100000     -100000 +-20, but received negative values, so python too slow to get matching level
There is a small error on higher frequencies, possibly deviations in clock precision between rpi and arduino, but also possible due to python being too slow.
Frequencies of 5000 Hz caused the counter to run opposite direction, so the 'read input from other pin' was too slow and captured opposite level.

Regards,
Gerhard
Last edited by ghp on Sat Feb 18, 2017 5:54 pm, edited 1 time in total.

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

Re: Problem With Encoder and Python

Fri Feb 17, 2017 8:18 pm

Have a look at http://abyz.co.uk/rpi/pigpio/examples.h ... encoder_py

The Python version should be good to about 30k steps per second on a Pi3.

Which Pi model are you using?

thalangeajit
Posts: 8
Joined: Fri Feb 17, 2017 12:19 pm

Re: Problem With Encoder and Python

Sat Feb 18, 2017 5:29 am

i am using pi 2 Model B V1.1
Is it possible to get all pulse count on this board at high speed ?

ghp
Posts: 1146
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: Problem With Encoder and Python

Sat Feb 18, 2017 8:52 am

Hello,

please define
at high speed ?
Regards,
Gerhard

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

Re: Problem With Encoder and Python

Sat Feb 18, 2017 9:16 am

thalangeajit wrote:i am using pi 2 Model B V1.1
Is it possible to get all pulse count on this board at high speed ?
On a Pi2B 20k seems fine, 30k lose 5 or so pulses in 60000 pulses. At these sort of rates pigpio will take time to push the updates through Python so expect a lag.

You can do the same test I'm doing by using http://abyz.co.uk/rpi/pigpio/examples.html#pdif2_tx_RED

Code: Select all

$ ./tx_RED -a2 -b3 -r60000 -s20000
$ ./tx_RED -a2 -b3 -r60000 -s20000
$ ./tx_RED -a2 -b3 -r60000 -s30000
$ ./tx_RED -a2 -b3 -r60000 -s30000

Code: Select all

$ ./rotary_encoder.py  
pos=0
...
pos=0
pos=16
pos=8656
pos=17377
pos=26223
pos=35196
pos=44170
pos=52890
pos=60000
...
pos=60000
pos=65493
pos=74322
pos=83172
pos=92092
pos=100798
pos=109775
pos=118729
pos=120000
...
pos=120000
pos=125902
pos=134854
pos=143853
pos=152652
pos=161712
pos=170781
pos=179827
pos=180000
...
pos=180000
pos=180541
pos=189467
pos=198393
pos=207443
pos=216235
pos=225294
pos=234347
pos=239999
pos=239999
pos=239999
pos=239999
pos=239999
^CTraceback (most recent call last):
  File "./rotary_encoder.py", line 136, in <module>
    time.sleep(1)
KeyboardInterrupt
$ 

Return to “Python”

Who is online

Users browsing this forum: No registered users and 13 guests