Управление шаговым двигателем с помощью Tkinter GUI - двигатель не вращается? - PullRequest
0 голосов
/ 01 мая 2020

Я пытаюсь запустить небольшой шаговый двигатель 28BYJ48 от моего Raspberry Pi3. Я хочу иметь возможность запускать и останавливать вращение двигателя, используя 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)

Двигатель, кажется, не включается должным образом или не вращается все. Только второй индикатор на плате водителя включается со вторым кодом, а не два индикатора с первым кодом. Я не могу понять, в чем проблема. Любая помощь будет высоко ценится!

...