提问者:小点点

带Raspberry Pi的步进电机仅在GPIOZero的延迟时间内工作


我正在尝试使用Python和GPIOZero将带有ULN2003驱动板的步进电机28BYJ-48连接到Raspberry Pi型号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个答案

匿名用户

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