需要使用此脚本获取平均值和最大值



我遇到了一些问题。首先,考虑到风速似乎很低,特别是与附近的其他气象站相比(显然我所在的地区有很多爱好者(。光是看着树、旗帜、灌木和小动物在我的院子里飞过,我就知道1.6英里/小时有点低。内部测试一切正常,当我在外部运行测试脚本时,它会在旋转时接收信号。

第二个问题是总是报告";平均值;以及";max";速度完全相同。我试着调整间隔,但无论我投入多长时间,它们总是以相同的数字报告。

from gpiozero import Button
import requests
import time
import math
import statistics
import database
wind_count = 0
radius_cm = 9.0
wind_interval = 5
ADJUSTMENT = 1.18
interval = 60
gust = 0
def spin():
global wind_count
wind_count = wind_count + 1
def calculate_speed(time_sec):
global wind_count
global gust
circumference_cm = (2 * math.pi) * radius_cm
rotations = wind_count / 2.0
dist_cm = circumference_cm * rotations
dist_km = (circumference_cm * rotations) / 100000
dist_mi = dist_km * 0.621371
mi_per_sec = dist_mi / time_sec
mi_per_hour = mi_per_sec * 3600
return mi_per_hour * ADJUSTMENT
def reset_wind():
global wind_count
wind_count = 0
def reset_gust():
global gust
gust = 0
wind_speed_sensor = Button(16)
wind_speed_sensor.when_activated = spin
while True:
print("Starting Weather Sensor Read Loop...")
start_time = time.time()
while time.time() - start_time <= interval:
print("Start timed loop...")
wind_start_time = time.time()
reset_wind()
reset_gust()
store_speeds = []
time.sleep(wind_interval)
final_speed = calculate_speed(wind_interval)
store_speeds.append(final_speed)
wind_gust_speed = (max(store_speeds))
wind_speed = (statistics.mean(store_speeds))
print(wind_average, wind_speed)

当我评论出";store_speeds=[]";第一个循环的速度报告是相同的;max";读到";不同的";比平均值高。这仍然困扰着我,因为为什么在第一个循环中它们是一样的?当wind_interval设置为5,间隔设置为60时,我认为在60秒的时间内采集5秒样本,给我12个样本来获得平均值和最大值,这是错误的吗?

我的目标是,每次它报告时,我都会得到一个平均值和最大值;循环;如果可能的话,而不是脚本在中断/停止之前保持运行的时间的平均值/最大值。

以下是工作和更正的代码:

from gpiozero import Button
import time
import math
import statistics
wind_count = 0
radius_cm = 9.0
wind_interval = 5
interval = 30
CM_IN_A_KM = 100000.0
SECS_IN_AN_HOUR = 3600
ADJUSTMENT = 1.18
store_speeds = []
def reset_wind():
global wind_count
wind_count = 0
def spin():
global wind_count
wind_count = wind_count + 1
def calculate_speed(time_sec):
global wind_count
circumference_cm = (2 * math.pi) * radius_cm
rotations = wind_count / 2.0
dist_km = (circumference_cm * rotations) / CM_IN_A_KM
km_per_sec = dist_km / time_sec
km_per_hour = km_per_sec * SECS_IN_AN_HOUR
mi_per_hour = km_per_hour * 0.6214

return mi_per_hour * ADJUSTMENT

wind_speed_sensor = Button(16)
wind_speed_sensor.when_pressed = spin
while True:
store_speeds = []
for _ in range (interval//wind_interval):
reset_wind()
#reset_gust()
time.sleep(wind_interval)  # counter is spinning in background
final_speed = calculate_speed(wind_interval)
store_speeds.append(final_speed)

wind_gust = max(store_speeds)
wind_speed = statistics.mean(store_speeds)
print(wind_speed, wind_gust)

最新更新