0

我正在尝试使用 Python 和 GPIOZero 将带有 ULN2003 驱动板的步进电机 28BYJ-48 连接到 Raspberry Pi Model 3 B。我用我找到的几个不同的例子编写了一些有效的代码,现在我可以让电机朝任一方向转动。但是,存在时间延迟问题。逆时针转动时,代码使用从 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
4

1 回答 1

0

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

于 2019-02-24T15:03:59.163 回答