asdfasfa 发表于 2022-3-7 14:59:35

树莓派可以为编码器正交解码并计数吗?

[ 本帖最后由 asdfasfa 于 2022-3-7 15:10 编辑 ]\n\n[ 本帖最后由 asdfasfa 于 2022-3-7 15:03 编辑 ]\n\n我用的是omron E6B2-cwz6c 2000,程序如下import RPi.GPIO as GPIO
RoAPin = 11
globalCounter = 0
flag = 0
Last_RoB_Status = 0
Current_RoB_Status = 0

def setup():
    GPIO.setmode(GPIO.BOARD)
    GPIO.setup(RoAPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
    # input mode
    GPIO.setup(RoBPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
    GPIO.setup(RoCPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)

def rotaryDeal(a):
      global globalCounter
      if GPIO.input(RoBPin):
                globalCounter -= 1
      else:
      globalCounter += 1

def btnISR(channel):
    global globalCounter
    globalCounter = 0

def loop():
    global globalCounter    tmp = 0
    GPIO.add_event_detect(RoCPin, GPIO.RISING, callback=btnISR)
    GPIO.add_event_detect(RoAPin, GPIO.RISING, callback=rotaryDeal)
      while True:
      if abs(tmp - globalCounter) > 10:
            print('globalCounter = %d' % globalCounter)
            tmp = globalCounter

def destroy():
    GPIO.cleanup()
if __name__ == '__main__':
   # Program start from here
    setup()
    try:
      loop()
    except KeyboardInterrupt:
# When 'Ctrl+C' is pressed, the child program destroy() will beexecuted.
      destroy()
编码器转一圈2000个计数,我只能显示100左右,还是转得很慢的情况下
页: [1]
查看完整版本: 树莓派可以为编码器正交解码并计数吗?