0

I want to Count pulses based on some Conditions which are:

(1) Count all the rotations generated during motor Rotations.

(2) Count all the motor rotation pulses when an additional signal is enabled.

Code i am trying

import time
import pigpio
import RPi.GPIO as GPIO

pi = pigpio.pi()
good_count=0
rotationPin=4
GoodSignal=12

GPIO.setmode(GPIO.BOARD)
pi.set_mode(rotationPin, pigpio.INPUT)
GPIO.setup(GoodSignal, GPIO.IN, pull_up_down=GPIO.PUD_UP) 
pi.set_pull_up_down(rotationPin, pigpio.PUD_UP)

wind_cb = pi.callback(rotationPin, pigpio.FALLING_EDGE)
if(pi.read(rotationPin)==0 and GPIO.input(GoodSignal)==0):
    print ("Here is good Signal count")
    good_count+=1

old_count = 0

while True:
   time.sleep(5)
   count = wind_cb.tally() 
   print("counted {} pulses".format(count - old_count))
   old_count = count

pigpio.stop()

I am using pigpio library for counting pulses which gives correct count for all pulses but not able to achieve second condition.

Any help please

Edit: After Studying about pigpio library, i modify my code to this

import time
import pigpio
import RPi.GPIO as GPIO

pi = pigpio.pi()
good_count=0
rotationPin=4
GoodSignal=12

GPIO.setmode(GPIO.BOARD)
pi.set_mode(rotationPin, pigpio.INPUT)
GPIO.setup(GoodSignal, GPIO.IN, pull_up_down=GPIO.PUD_UP) 
pi.set_pull_up_down(rotationPin, pigpio.PUD_UP)
totalcount=0
def abc(gpio,level,tick):
    global totalcount,good_count
    totalcount +=1
    print("ABC")
    if(GPIO.input(GoodSignal)==GPIO.LOW):
       good_count +=1
wind_cb = pi.callback(rotationPin, pigpio.FALLING_EDGE, abc)


old_count = 0

while True:
   time.sleep(5)
   count = wind_cb.tally() 
   print("counted {} pulses".format(count - old_count))
   print("The total Count is "+str(totalcount))
   print("The Good Count is "+str(good_count))
   old_count = count

pi.stop()

The above code gives no error but i guess it is not efficient, Any Suggestion would be appreciated.

  • Do you only want to count falling edges on rotationPin when goodSignal is high? – joan Sep 4 '19 at 13:27
  • yes, i want to count the total rotations + rotations when Good Signal is HIgh........ I found a way but it is not reliable i guess, let me edit the question. Please suggest any efficient way to do this – Lalit Verma Sep 4 '19 at 13:33
0

I'd use something like the following.

When you want to look at a GPIO in step with another GPIO use the same callback for both. That way you know the level of each GPIO at the same time. Trying to read() a GPIO in a callback is almost always the wrong thing to do. The thing which triggered the callback may have happened several milliseconds before.

#!/usr/bin/env python

import time
import pigpio

pi = pigpio.pi()
if not pi.connected:
   exit()


rotationPin=4
GoodSignal=12

GoodSignalLevel=pi.read(GoodSignal)

pi.set_pull_up_down(rotationPin, pigpio.PUD_UP)
pi.set_mode(rotationPin, pigpio.INPUT)

pi.set_pull_up_down(GoodSignal, pigpio.PUD_UP)
pi.set_mode(GoodSignal, pigpio.INPUT)

good_count=0
totalcount=0

def abc(gpio,level,tick):
    global totalcount,good_count,GoodSignalLevel
    if gpio == GoodSignal:
       GoodSignalLevel = level
    if gpio == rotationPin:
       totalcount +=1
       if GoodSignalLevel == pigpio.LOW:
          good_count += 1

wind_cb = pi.callback(rotationPin, pigpio.FALLING_EDGE, abc)
good_cb = pi.callback(GoodSignal, pigpio.EITHER_EDGE, abc)

old_count = 0

while True:
   time.sleep(5)
   count = totalcount
   print("counted {} pulses".format(count - old_count))
   print("The total Count is "+str(totalcount))
   print("The Good Count is "+str(good_count))
   old_count = count

wind_cb.cancel()
good_cb.cancel()

pi.stop()
| improve this answer | |
  • Thank you for this answer, can you please tell me flaws that my second code may cause as theoretically it seems to give correct answer – Lalit Verma Sep 5 '19 at 4:29
  • 1
    It will probably work if not many changes are happening, say a hundred a second or so. The faster things are switching the more likely errors will creep in. The callback events will be delayed a millisecond or so in real time. read() gets the state in real-time. It may not be the GoodSignal state at the time the events in the callback happened. I have also edited the code to check the GoodSIgnal level first in the callback. – joan Sep 5 '19 at 8:06
  • Thankyou for detailed explanation, am new to Pi and you are helping me alot. – Lalit Verma Sep 5 '19 at 10:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.