简体   繁体   English

如何完全重启python程序

[英]How to completely restart a python program

For a project, i used a Raspberry Pi (running dexter industries modified raspbian) and Brick Pi to run lego motors. 对于一个项目,我使用了Raspberry Pi(运行dexter industries的改进型raspbian)和Brick Pi来运行乐高电机。 I wroted a program with python and it works great and all, but i need the entire program to run repeatedly if the pressure sensor was not pressed. 我用python编写了一个程序,它可以很好地工作,但是如果没有按下压力传感器,我需要整个程序重复运行。 I tried calling the function sensorValue() (which detects whether the pressure sensor was being pushed) under while True:. 我尝试在True:下调用函数sensorValue()(检测压力传感器是否被按下)。 But once i did that stuff became weird. 但是一旦我做到了,那东西就会变得很奇怪。 It would just continue to repeat indefinitely and even if i pushed the sensor, the recurring 0 would turn to 1 but it wouldn't call the next function i need it to run. 它只会继续无限期地重复,即使我按下传感器,重复出现的0也会变成1,但不会调用下一个我需要运行的函数。

Please help, this is my first time actually using python to write anything and i am a massive beginner so any help is GREATLY APPRECIATED. 请帮忙,这是我第一次真正使用python编写任何东西,我是一个初学者,所以非常感谢。

Thanks Again 再次感谢

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()

try this 尝试这个

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM