如何完全重启 python 程序
How to completely restart a python program
对于一个项目,我使用了 Raspberry Pi(运行ning dexter industries 修改 raspbian)和 Brick Pi 到 运行 乐高电机。我用 python 编写了一个程序,它工作得很好,但如果没有按下压力传感器,我需要整个程序重复 运行。我尝试在 True: 下调用函数 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()
试试这个
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()
对于一个项目,我使用了 Raspberry Pi(运行ning dexter industries 修改 raspbian)和 Brick Pi 到 运行 乐高电机。我用 python 编写了一个程序,它工作得很好,但如果没有按下压力传感器,我需要整个程序重复 运行。我尝试在 True: 下调用函数 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()
试试这个
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()