【问题标题】:Is there a method for making a function run continuously until a user tells it to stop?有没有一种方法可以让函数连续运行,直到用户告诉它停止?
【发布时间】:2020-07-29 05:49:33
【问题描述】:

我目前正在尝试在 Python 3 中为树莓派编写一个程序,以控制位于桌子上的一组线性致动器,这将改变腿的长度并允许桌面倾斜。桌子的一个特点是自动调平功能,以确保桌面始终保持水平,并且如果它检测到它不是通过从板载加速器中提取数据,它将自我纠正。自动级别功能工作得非常好,但是我很难在开和关条件下进行编程。理想情况下,在控制台中键入 a(1) 将调用 autolevel 函数并使其持续运行,直到键入 a(0) 将其停止。我设法通过键入 a(1) 来运行该函数,但是一旦接受该命令,程序就会停止侦听进一步的命令并无休止地循环。当前状态的代码发布在下面。

def a(n):
    while n== 1:
        acceleration = sense.get_accelerometer_raw()
        x = acceleration['x']
        y = acceleration['y']
        z = acceleration['z']
        
        x=round(x, 3)
        y=round(y, 3)
        z=round(z, 3)
        
        if x >0.00:
            GPIO.output(12,0)
            GPIO.output(13,1)#leg 1 retract
            GPIO.output(15,0)
            GPIO.output(29,1)#leg 2 retract
            GPIO.output(31,1)
            GPIO.output(32,0)#leg 3 extend
            GPIO.output(33,1)
            GPIO.output(36,0)#leg 4 extend
            sense.show_message('Correcting forward tilt')
            
        elif x <0.00:
            GPIO.output(12,1)
            GPIO.output(13,0)#leg 1 extend
            GPIO.output(15,1)
            GPIO.output(29,0)#leg 2 extend
            GPIO.output(31,0)
            GPIO.output(32,1)#leg 3 retract
            GPIO.output(33,0)
            GPIO.output(36,1)#leg 4 retract
            sense.show_message('Correcting rearward tilt')
            
        else:
            if y >0.00:
                GPIO.output(12,0)
                GPIO.output(13,1)#leg 1 retract
                GPIO.output(15,1)
                GPIO.output(29,0)#leg 2 extend
                GPIO.output(31,0)
                GPIO.output(32,1)#leg 3 retract
                GPIO.output(33,1)
                GPIO.output(36,0)#leg 4 extend
                sense.show_message('Correcting right side tilt')
            elif y <0.00:
                GPIO.output(12,1)
                GPIO.output(13,0)#leg 1 extend
                GPIO.output(15,0)
                GPIO.output(29,1)#leg 2 retract
                GPIO.output(31,1)
                GPIO.output(32,0)#leg 3 extend
                GPIO.output(33,0)
                GPIO.output(36,1)#leg 4 retract
                sense.show_message('Correcting left side tilt')
            else:
                sense.show_message("No tilt to correct")
                print ("Stand currently level, standing by for reactive adjustment.")

【问题讨论】:

  • 这可能会有所帮助:stackoverflow.com/questions/49550355/…
  • 我认为这可能符合我正在寻找的内容,但我不确定如何修改代码以使其不只是默认开启。我认为在自动关卡运行时无法操作桌面的升高和降低命令。理想情况下,这将是用户自己打开和关闭的功能。
  • @JacobCaraig 你尝试过多线程方式吗?

标签: python function loops input while-loop


【解决方案1】:

你可以使用多线程, 以下是如何将其合并到:

您将被要求输入值,在您的情况下将是“退出”关键字。 让我知道是否有效。

import threading 

flag=False
def a1(): 
    global flag
    a= flag
    #if(a==True):
    while (!a):
        acceleration = sense.get_accelerometer_raw()
        x = acceleration['x']
        y = acceleration['y']
        z = acceleration['z']
        #paste the rest of your code here
   
    
def loopBreak(): 
    """ 
    function to exit the loop
    """
    global flag
    inp=input("Enter value")
    if inp=="exit":
      flag =True


if __name__ == "__main__": 
    # creating thread 
    t1 = threading.Thread(target=a1) 
    t2 = threading.Thread(target=loopBreak) 
  
    # starting thread 1 
    t1.start() 
    # starting thread 2 
    t2.start() 
  
    # wait until thread 1 is completely executed 
    t1.join() 
    # wait until thread 2 is completely executed 
    t2.join() 
    print(flag)
    # both threads completely executed 
    print("Done!") 

【讨论】:

  • 很遗憾提供的代码不起作用,它在没有用户输入的情况下自动初始化了自动调平程序并且不接受退出命令。
  • @JacobCaraig 你是如何执行你的脚本的?您是否还在终端或您正在运行的任何地方收到“输入值”提示?我做了一个小编辑,忘记添加了。您需要稍微修改 while 部分。检查它现在是否有效。
  • 我正在使用 Thonny 中的“运行”功能执行脚本。我还进行了您建议的修改,但得到了相同的结果。
  • @JacobCaraig 你可以尝试使用命令提示符/终端运行一次吗?
猜你喜欢
  • 2013-08-25
  • 1970-01-01
  • 2021-08-19
  • 1970-01-01
  • 2023-03-13
  • 1970-01-01
  • 2011-03-07
  • 2023-03-15
  • 1970-01-01
相关资源
最近更新 更多