检测何时没有按下按钮PyQt

时间:2017-12-07 19:41:42

标签: python-3.x pyqt5

我有一个通过USB控制设备的GUI。 我设置它的方式基本上是两个按钮,前进和后退,当按下按钮时,该功能被传送到电机,当按钮被释放时,关闭信号被触发一次。

    def on_release():
        print('Off')
        self.off()

    def on_click():
        print('forward')
        self.forward()
    button = QPushButton('Cut', self)
    button.move(100,70) 
    button.pressed.connect(on_click)
    button.released.connect(on_release)

    def on_click():
        print('back')
        self.back()
    button = QPushButton('back', self)
    button.move(200,70) 
    button.pressed.connect(on_click)
    button.released.connect(on_release)

我最近遇到了一个有趣的故障模式,如果USB连接被暂停(在我的情况下,我使用GDB并点击断点,释放按钮,然后释放断点),此时按钮被释放,永远不会发送终止信号,电机将继续前进或后退。 (它可以通过单击后退或前进并释放,或通过完全杀死USB而杀死#34;)

我已经有一个保护措施(螺纹心跳信号),用于在断开USB连接的情况下关闭电机但是我想在一个特定USB的情况下更安全地使其失效传输失败。

我有办法检查是否没有按下任何按钮,所以我可以用它来触发关闭信号吗?

1 个答案:

答案 0 :(得分:0)

在{https://github.com/tjmarkham/python-stepper]的tjmarkham stepper.py 脚本中学习材料,以获得可以放在按钮后面的覆盆子Pi:

#CURRENT APPLICATION INFO
#200 steps/rev
#12V, 350mA
#Big Easy driver = 1/16 microstep mode
#Turn a 200 step motor left one full revolution: 3200

from time import sleep
import RPi.GPIO as gpio #https://pypi.python.org/pypi/RPi.GPIO
#import exitHandler #uncomment this and line 58 if using exitHandler

class stepper:
    #instantiate stepper 
    #pins = [stepPin, directionPin, enablePin]
    def __init__(self, pins):
        #setup pins
        self.pins = pins
        self.stepPin = self.pins[0]
        self.directionPin = self.pins[1]
        self.enablePin = self.pins[2]

        #use the broadcom layout for the gpio
        gpio.setmode(gpio.BCM)

        #set gpio pins
        gpio.setup(self.stepPin, gpio.OUT)
        gpio.setup(self.directionPin, gpio.OUT)
        gpio.setup(self.enablePin, gpio.OUT)

        #set enable to high (i.e. power is NOT going to the motor)
        gpio.output(self.enablePin, True)

        print("Stepper initialized (step=" + self.stepPin + ", direction=" + self.directionPin + ", enable=" + self.enablePin + ")")

    #clears GPIO settings
    def cleanGPIO(self):
        gpio.cleanup()

    #step the motor
    # steps = number of steps to take
    # dir = direction stepper will move
    # speed = defines the denominator in the waitTime equation: waitTime = 0.000001/speed. As "speed" is increased, the waitTime between steps is lowered
# stayOn = defines whether or not stepper should stay "on" or not. If stepper will need to receive a new step command immediately, this should be set to "True." Otherwise, it should remain at "False."

    def step(self, steps, dir, speed=1, stayOn=False):
        #set enable to low (i.e. power IS going to the motor)
        gpio.output(self.enablePin, False)

        #set the output to true for left and false for right
        turnLeft = True
        if (dir == 'right'):
            turnLeft = False;
        elif (dir != 'left'):
            print("STEPPER ERROR: no direction supplied")
            return False
        gpio.output(self.directionPin, turnLeft)

        stepCounter = 0

        waitTime = 0.000001/speed #waitTime controls speed

        while stepCounter < steps:
            #gracefully exit if ctr-c is pressed
            #exitHandler.exitPoint(True) #exitHandler.exitPoint(True, cleanGPIO)

            #turning the gpio on and off tells the easy driver to take one step
            gpio.output(self.stepPin, True)
            gpio.output(self.stepPin, False)
            stepCounter += 1

            #wait before taking the next step thus controlling rotation speed
            sleep(waitTime)

        if (stayOn == False):
            #set enable to high (i.e. power is NOT going to the motor)
            gpio.output(self.enablePin, True)

print("stepperDriver complete (turned " + dir + " " + str(steps) + " steps)")

<强> teststepper.py

from Stepper import Stepper

#stepper variables
#[stepPin, directionPin, enablePin]
testStepper = Stepper([22, 17, 23])

#test stepper
testStepper.step(3200, "right"); #steps, dir, speed, stayOn