Я работал над скриптом Python для управления двумя шаговыми двигателями через USB-контроллер StepperBee
.
Я планирую использовать raspi 3
с 7-дюймовым сенсорным экраном, используя Tkinter
для создания графического экрана.Это работает.
Мне нужно прочитать несколько прерываний из StepperBee
, скажем, для остановки двигателя.
В моей неграфической версии, raspi 3
и 4x20 i2c LCD
и некоторые физическиеКнопки, моя процедура прерывания работает нормально.
Но я не могу понять, как заставить мою подпрограмму обработки прерываний запускать прерывания в любое время, начиная с StepperBee
, пока выполняется графический скрипт Tkinter
.
#!/usr/local/python
# StepperBee controller script for two stepper motors and 7inch touch screen
from tkinter import *
import tkinter.font as font
import usb.core
import time
win = Tk()
myFont = font.Font(family = 'Helvetica', size = 14, weight = 'bold')
dev = usb.core.find(idVendor=0x04d8, idProduct=0x005d)
if dev.is_kernel_driver_active(0): # This code close dev USB if active - and after adding
reattach = True # the non graphic code to tkinter environment I ned this...
dev.detach_kernel_driver(0) # got it from stackoverflow ;-)
if dev is None:
raise ValueError('Device not found')
else:
try:
dev.decatach_kernel_driver(0)
except:
pass
dev.set_configuration()
mode = 0b00000011 # set both stepper motors to full step mode
data = [16, mode]
dev.write(2, data)
Процедура прерывания работает только при запуске.Если при запуске реле прерывания разомкнуто, на экране появляется "Interrupt_low"
.Если при запуске реле прерывания замыкается, на экране появляется "Interrupt_high"
, после чего я не вижу прерываний.
data = [8]
dev.write(2, data)
status = dev.read(0x81, 6)
if status[5] == 64: # interrupt 4 - - out of 5
input = Message(win, text="Interrupt_high", width=250) # width is pixels
input.pack()
input.place(relx=0.8, rely=0.2, anchor="ne")
elif status[5] == 0:
input = Message(win, text="Interrupt_low", width=250) # width is pixels
input.pack()
input.place(relx=0.8, rely=0.2, anchor="ne")
def exitProgram():
print("Exit Button presset")
goOn = False
win.quit()
# data=[motor_nr, direction, timeLSB, timeMSB, stepsLSB, stepsMSB, outputs]
def writeStopMotorAll():
w = Message(win, text="All Stop", width=250)
w.pack()
data=[2, 0b0000, 0, 0, 0, 0, 0b00000000]
dev.write(2,data)
data=[4, 0b0000, 0, 0, 0, 0, 0b00000000]
dev.write(2,data)
def writeMotor1CW():
w = Message(win, text="Motor 1 CW", width=250)
w.pack()
data=[2, 0b0001, 15, 1, 255, 53, 0b00000000]
dev.write(2,data)
time.sleep(3)
def writeMotor1CCW():
w = Message(win, text="Motor 1 CCW", width=250)
w.pack()
data=[2, 0b0011, 15, 1, 250, 53, 0b00000000]
dev.write(2,data)
def writeMotor2CW():
w = Message(win, text="Motor 2 CW", width=250)
w.pack()
data=[4, 0b0100, 25, 1, 250, 10, 0b00000010]
dev.write(2,data)
def writeMotor2CCW():
w = Message(win, text="Motor 2 CCW", width=250)
w.pack()
data=[4, 0b1100, 25, 1, 250, 1, 0b00000100]
dev.write(2,data)
win.title(" OZ5RZ - Raspi StepperBee Controller Ver. 0.00.01")
win.geometry('800x400')
test = Message(win, text="TEST_BT", width=250)
test.pack()
test.place(relx=0.8, rely=0.1, anchor="ne")
exitButton = Button(win, text = "Exit", font = myFont, command = exitProgram, height = 1, width = 2)
exitButton.pack()
stopAllButton = Button(win, text = "Stop All", font = myFont, command = writeStopMotorAll, height = 1, width = 8)
stopAllButton.pack()
test1Button = Button(win, text = "Motor 1 CW", font = myFont, command = writeMotor1CW, height = 1, width = 8)
test1Button.pack()
test2Button = Button(win, text = "Motor 1 CCW", font = myFont, command = writeMotor1CCW, height = 1, width = 9)
test2Button.pack()
test3Button = Button(win, text = "Motor 2 CW", font = myFont, command = writeMotor2CW, height = 1, width = 8)
test3Button.pack()
test4Button = Button(win, text = "Motor 2 CCW", font = myFont, command = writeMotor2CCW, height = 1, width = 9)
test4Button.pack()
stopAllButton.place(relx=0.1, rely=0.3, anchor="center")
exitButton.place(relx=0.99, rely=1, anchor="se")
test1Button.place(relx=0.1, rely=0.8, anchor="center")
test2Button.place(relx=0.3, rely=0.8, anchor="center")
test3Button.place(relx=0.68, rely=0.8, anchor="center")
test4Button.place(relx=0.88, rely=0.8, anchor="center")
mainloop()