0

我有一个 Raspberry Pi 和一顶感知帽。我正在尝试创建一个循环,显示从感测帽(温度和方向)到 GUI 的输出,并每秒自动更新。因此,将打开一个屏幕,其中包含 Pi 提供的信息。但是我的代码只有在我关闭屏幕时才会刷新。我希望它每秒自动刷新。

这是我的代码(有点长);我刚刚开始使用 Python:

from sense_hat import SenseHat
from datetime import date, datetime

from datetime import datetime
import datetime
from time import sleep
import time
from guizero import App, Text

sense=SenseHat()
sense.set_imu_config(True, True, True) # accelerometer, magnetometer , gyroscope sense.clear() myFile=open('PiTempData.txt','a') myFile.write(str(['datetime', 'temp','pres','hum','itch_degree','roll_degree','yaw_degree','acc_x_raw','acc_y_raw','acc_z_raw','acc_x','acc_y','acc_z','compass']))

while True:
    t= sense.get_temperature()-20
    p = sense.get_pressure()
    h= sense.get_humidity()

orientation = sense.get_orientation_degrees()

pitch = orientation["pitch"]
roll = orientation["roll"]
yaw = orientation["yaw"]

accel_only = sense.get_accelerometer()
raw = sense.get_accelerometer_raw()
acceleration = sense.get_accelerometer_raw()
x = acceleration['x']
y = acceleration['y']
z = acceleration['z']
x1 = acceleration['x']
y1 = acceleration['y']
z1 = acceleration['z']
x2=round(x1, 0)
y2=round(y1, 0)
z2=round(z1, 0)
raw1 = sense.get_compass_raw()
gyro_only = sense.get_gyroscope()

north = sense.get_compass()

print ("")
print ("The current time",datetime.datetime.now())
print ("")
print ("Temp:%.i c, Pressure = %.0f. RH= %.0f" % (t,p,h))
print ("")
print ("Orientation")
print("pitch {0} roll {1} yaw {2}".format(pitch, roll, yaw))
print ("")
print ("Accelerometer")
print("x=%s, y=%s, z=%s" % (x, y, z))
print("x=%s, y=%s, z=%s" % (x2, y2, z2))
print ("")
print("compass")
print("North: %s" % north)
time.sleep(0.5)

myFile=open('PiTempData.txt','a')

myFile.write("\n")
myFile.write(str(datetime.datetime.now()))
myFile.write(str(","))
myFile.write(str ((t,p,h)))
myFile.write(str(","))
myFile.write(str((pitch,roll,yaw)))
myFile.write(str(","))
myFile.write(str((x, y, z)))
myFile.write(str(","))
myFile.write(str((x2,y2,z2)))
myFile.write(str(","))
myFile.write(str(north))


app=App(title= "IMU")
welcom_message=Text(app,text=" ")
welcom_message=Text(app,text="temp=")
welcom_message=Text(app,text=t)
welcom_message=Text(app,text=" ")
welcom_message=Text(app,text="pitch=")
welcom_message=Text(app,text=pitch)
welcom_message=Text(app,text=" ")
welcom_message=Text(app,text="roll=")
welcom_message=Text(app,text=roll)
welcom_message=Text(app,text=" ")
welcom_message=Text(app,text="yaw=")
welcom_message=Text(app,text=yaw)
welcom_message=Text(app,text=" ")
welcom_message=Text(app,text="Accelerometer x=")
welcom_message=Text(app,text=x)
welcom_message=Text(app,text=" ")
welcom_message=Text(app,text="Accelerometer y=")
welcom_message=Text(app,text=y)
welcom_message=Text(app,text=" ")
welcom_message=Text(app,text="Accelerometer z=")
welcom_message=Text(app,text=z)
welcom_message=Text(app,text=" ")
welcom_message=Text(app,text="Accelerometer x=")
welcom_message=Text(app,text=x2)
welcom_message=Text(app,text=" ")
welcom_message=Text(app,text="Accelerometer y=")
welcom_message=Text(app,text=y2)
welcom_message=Text(app,text=" ")
welcom_message=Text(app,text="Accelerometer z=")
welcom_message=Text(app,text=z2)
welcom_message=Text(app,text=" ")
welcom_message=Text(app,text="compass=")
welcom_message=Text(app,text=north)

app.display()
4

1 回答 1

0

app.display()您必须注册一个将定期调用的回调之前:

welcom_message.after(1000, update_label)

def update_label():
    welcom_message.set(read_sensor()) # here read the sensor data on every callback call
    # recursive call
    welcom_message.after(1000, update_label)

但是,您的代码存在一个问题:

while True:
    t= sense.get_temperature()-20
    p = sense.get_pressure()
    h= sense.get_humidity()

脚本中间的这个while循环看起来是无限的,即你的程序永远不会经过这个循环。我相信app.display()实际上是阻塞 - 它应该阻止你的程序返回。因此,我建议摆脱这个while循环,如上所示注册一个回调,并在该回调中查询传感器以显示要显示的值。

于 2020-08-20T13:20:43.307 回答