如何完全重启python程序

2024-04-26 20:22:01 发布

您现在位置:Python中文网/ 问答频道 /正文

在一个项目中,我使用了树莓Pi(运行dexter industries modified raspbian)和砖块Pi来运行乐高汽车。我用python编写了一个程序,效果非常好,但是如果没有按下压力传感器,我需要整个程序反复运行。我尝试在while下调用函数sensorValue()(它检测压力传感器是否被按下)正确:。但一旦我这么做了,事情就变得很奇怪了。它只会无限期地继续重复,即使我按下传感器,重复出现的0也会变成1,但它不会调用我需要它运行的下一个函数。你知道吗

请帮助,这是我第一次实际使用python来编写任何东西,我是一个巨大的初学者,所以任何帮助都非常感谢。你知道吗

再次感谢

from BrickPi import *

BrickPiSetup()
BrickPi.MotorEnable[PORT_A] = 1
BrickPi.SensorType[PORT_4] = TYPE_SENSOR_TOUCH

BrickPiSetupSensors()

def sensorValue():
    result = BrickPiUpdateValues()
    if not result :
        print BrickPi.Sensor[PORT_4]
    time.sleep(.01)
    if BrickPi.Sensor[PORT_4] == 0:

def programBody():

    print ("program rest/pause")
    BrickPi.MotorSpeed[PORT_A] = 0
    BrickPiUpdateValues()
    time.sleep(3) 

    print ("reminder/alarm = 200 value")
    BrickPi.MotorSpeed[PORT_A] = 200
    ot = time.time()
    while(time.time() - ot <3):
        BrickPiUpdateValues()
        time.sleep(.01)

    print ("reminder/alarm = 125 value")
    BrickPi.MotorSpeed[PORT_A] = 125
    ot = time.time()
    while(time.time() - ot <3):
        BrickPiUpdateValues()
        time.sleep(.01)

sensorValue()  #I would put while True: here but...

if BrickPi.Sensor[PORT_4]:
    print "program successfully initiatied"
    programBody()

Tags: 程序iftimeportpisleep传感器ot
1条回答
网友
1楼 · 发布于 2024-04-26 20:22:01

试试这个

import BrickPi,time

BrickPiSetup()
BrickPi.MotorEnable[PORT_A] = 1
BrickPi.SensorType[PORT_4] = TYPE_SENSOR_TOUCH

BrickPiSetupSensors()

z = 0
def mainprogram():
    print ("running")
    while x == 1:
        z = z + 1
        print ("the plate has been pressed for %s seconds" % z)
        time.sleep(1)

while True:
    time.sleep(.1)
    if BrickPi.Sensor[PORT_4]:
        print "program successfully initiatied"
        mainprogram()

相关问题 更多 >