我正在尝试用我的树莓派 pi3 运行一个小型 28BYJ48 步进电机。我希望能够使用 tkinter GUI 启动和停止电机转动。如果我单独运行此代码,电机会转好:
GPIO.setmode(GPIO.BOARD)
control_pins = [7,11,13,15]
for pin in control_pins:
GPIO.setup(pin, GPIO.OUT)
GPIO.output(pin, 0)
halfstep_seq = [[1,0,0,1],
[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]]
StepCount = len(halfstep_seq)
StepCounter = 0
StepDir = 2
while True:
for halfstep in range(8):
for pin in range(4):
GPIO.output(control_pins[pin],halfstep_seq[halfstep][pin])
time.sleep(0.001)
StepCounter += StepDir
if (StepCounter>=StepCount):
StepCounter = 0
if (StepCounter<0):
StepCounter = StepCount+StepDir
GPIO.cleanup
但如果我运行这个:
import RPi.GPIO as GPIO
import time
import tkinter as tk
from tkinter import *
root = Tk()
root.title("Superscope")
root.geometry("400x400")
GPIO.setmode(GPIO.BOARD)
control_pins = [7,11,13,15]
control_pins_state = True
def start_motor():
global control_pins_state
for pin in control_pins:
GPIO.setup(pin, GPIO.OUT)
GPIO.output(pin, 0)
halfstep_seq = [[1,0,0,1],
[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]]
StepCount = len(halfstep_seq)
StepCounter = 0
StepDir = 2
#global StepCount, StepCounter, StepDir
if control_pins_state==True:
for halfstep in range(8):
for pin in range(4):
GPIO.output(control_pins[pin], halfstep_seq[halfstep][pin])
time.sleep(0.0001)
StepCounter += StepDir
if (StepCounter<=StepCount):
control_pins_state = False
#time.sleep(0.0001)
#StepCounter += StepDir
#if we reach the end of the sequence start again
#if (StepCounter>=StepCount):
#StepCounter = 0
#if (StepCounter<0):
#StepCounter = StepCount+StepDir
#GPIO.cleanup()
def stop_motor():
GPIO.cleanup()
if __name__=="__main__":
startbutton=tk.Button(root, width=10,height=1,text="Start",command=start_motor)
stopbutton=tk.Button(root, width=10,height=1,text="Stop",command=stop_motor)
startbutton.grid(row=1,column=0)
stopbutton.grid(row=2,column=0)
电机似乎无法正常启动或根本无法移动。驱动板上只有一个灯亮第二个代码,而不是两个灯亮第一个代码。我似乎无法弄清楚问题是什么。任何帮助将非常感激!