带有Raspberry Pi的步进电机仅在使用GPIOZero的情况下才具有一定的延迟时间

时间:2019-02-22 19:18:56

标签: python raspberry-pi gpio gpiozero

我正在尝试使用Python和GPIOZero将带有ULN2003驱动程序板的步进电机28BYJ-48连接到Raspberry Pi Model 3B。我使用一些发现的不同示例,整理了一些有效的代码,现在可以使电动机朝任一方向旋转。但是,存在时间延迟问题。逆时针旋转时,代码使用从2毫秒一直到50的任何延迟时间起作用。但是,在顺时针方向上,它仅以2毫秒和3毫秒的延迟起作用,而没有其他延迟。很奇怪。

代码看起来像这样(非常粗糙-只是试图证明这一概念):

import time
import sys
from gpiozero import OutputDevice as stepper

def moveBackward(stepCounter):

  while True:
      stepDir = -1
      for pin in range(0,4):
         xPin=stepPins[pin]          # Get GPIO
         if seq[stepCounter][pin]!=0:
            xPin.on()
         else:
            xPin.off()

      stepCounter += stepDir
      print(stepCounter) 
      if (stepCounter < 0):
          stepCounter = stepCount+stepDir 

      time.sleep(waitTime)     # Wait before moving onimport time 

def moveForward(stepCounter):
  while True:
      stepDir = 1
      for pin in range(0,4):
          xPin=stepPins[pin]          # Get GPIO
          if seq[stepCounter][pin]!=0:
             xPin.on()
          else:
             xPin.off()

      stepCounter += stepDir
      if (stepCounter >= stepCount):
          stepCounter = 0   

      time.sleep(waitTime) 


IN1 = stepper(17)
IN2 = stepper(22)
IN3 = stepper(23)
IN4 = stepper(24)

stepPins = [IN1,IN2,IN3,IN4] # Motor GPIO pins</p><p>
stepDir = 1        # Set to 1 for clockwise
                       # Set to -1 for anti-clockwise
mode = 0            # mode = 1: Low Speed ==> Higher Power
                       # mode = 0: High Speed ==> Lower Power

if mode:              # Low Speed ==> High Power
  seq = [[1,0,0,1], # Define step sequence as shown in manufacturers datasheet
         [1,0,0,0], 
         [1,1,0,0],
         [0,1,0,0],
         [0,1,1,0],
         [0,0,1,0],
         [0,0,1,1],
         [0,0,0,1]]
else:                    # High Speed ==> Low Power 
  seq = [[1,0,0,0], # Define step sequence as shown in manufacturers datasheet
         [0,1,0,0],
         [0,0,1,0],
         [0,0,0,1]]
stepCount = len(seq)


if len(sys.argv)>1: # Read wait time from command line
   waitTime = int(sys.argv[1])/float(1000)
   print(waitTime)
else:
   waitTime = 0.004    # 2 miliseconds was the maximun speed got on my tests
stepCounter = 0



while True:                          # Start main loop

  moveBackward(stepCounter)  # counterclockwise
  #moveForward(stepCounter)  # clockwise

1 个答案:

答案 0 :(得分:0)

所以我发现这段代码很好用-不知道上面的问题是由什么引起的。 https://github.com/custom-build-robots/Stepper-motor-28BYJ-48-Raspberry-Pi/commit/d8f6b5f3ee4d6b22d3fe40d81d0be09287c97c89