【问题标题】:Detect when no buttons are being pushed PyQt检测何时没有按下按钮 PyQt
【发布时间】:2017-12-07 19:41:42
【问题描述】:

我有一个通过 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来终止它)

我已经有保护措施(线程心跳信号)在 USB 连接被切断的情况下关闭电机,但我想让这个失败更安全一点,以防一个特定的 USB 关闭传输失败了。

有没有办法让我检查是否没有按下按钮,以便我可以使用它来触发关闭信号?

【问题讨论】:

  • 检查 [stackoverflow.com/questions/19508450/… 或者使用带有 trinamic 2120+ 步进驱动器的 arduino。信号下降 == 板上的电机停止模式、过载等。 [watterott.com/de/SilentStepStick]
  • 您可以定义按钮在按下时通过 USB 或步进驱动器给出的步骤。例如,button1 = 每个按下事件 100 步。按钮 2 = 10 步,按钮 3 = 1 步...在后一种情况下,这意味着您需要连续按下它才能继续进行。如果释放... 1 个额外的步骤完成,然后停止信号自动传输(在步进驱动程序或您的 pyscript 中)。
  • 另一个非常有用的链接是 [zapmaker.org/projects/grbl-controller-3-0/],他们展示了如何为控制 3d 打印机的 arduino 板制作 qt gui。这里 [zapmaker.org/wp-content/uploads/2012/12/mac.png] 显示向左或向右移动的 1 步长。

标签: python-3.x pyqt5


【解决方案1】:

来自 tjmarkham stepper.py 脚本的学习材料 [https://github.com/tjmarkham/python-stepper] 用于可以放在按钮后面的树莓派:

#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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-06
    • 1970-01-01
    相关资源
    最近更新 更多