我正在尝试创建一个简单的程序来模拟供暖通风应用中的可变风扇。下面的脚本仅模仿基于风扇启动和启动的管道压力读数(以英寸水柱表示)。基本上,风扇会启动,管道压力将为零。当电动机加速时,风管压力将每10秒缓慢上升,每10秒将产生.1“ WC的值。当风扇的风管压力设定值为1.5时,程序将生成一个大约1.3-1.7的随机数,以模仿风扇在设定点附近徘徊。希望这是有道理的!
我觉得这可以写得更好一些,但形式最简单:
import time
from numpy.random import seed
from numpy.random import randint
# seed random number generator
seed(1)
#generate random number to mimic fan hovering at setpoint
def ductRandStatic():
value = randint(13, 17, 20)
return value * .1
ductStaticStart = 0
ductStaticEnd = 1.5
#mimic fan ramping up to setpoint slowly
def startFan():
static = ductStaticStart + .1
time.sleep(10)
if static < ductStaticEnd:
static = ductRandStatic()
else:
static = static
print(static)
while True:
startFan()
由于某种原因,该程序仅打印我认为随机数生成器...我希望它能每10秒打印一次模拟风管压力的值...
[1.5 1.6 1.6 1.5 1.4 1.4 1.4 1.6 1.3 1.3 1.4 1.6 1.3 1.5 1.3 1.3 1.4 1.6
1.4 1.5]
答案 0 :(得分:0)
我认为您想摆脱startFan()
函数并将其大部分代码移入主循环,以便static
可以记住其值:
# initialize to 0.1
static = 0.1
# mimic fan ramping up to setpoint slowly
while True:
if static < 1.5:
static = ductRandStatic()
print(static)
time.sleep(10)
另外,您对randint()
的调用是错误的-应该有两个整数,但是要传递三个整数。
答案 1 :(得分:0)
您的仿真有两个阶段:启动(一系列固定值)和维持(您的随机变化)。这样的代码:
from time import sleep
from numpy.random import randint
def ductRandStatic():
return randint(13, 17) * 0.1
delay = 10
for i in range(16):
pressure = i * 0.1
print(pressure)
sleep(delay)
while True:
print(ductRandStatic() )
sleep(delay)
输出:
0.0
0.1
0.2
0.30000000000000004
0.4
0.5
0.6000000000000001
0.7000000000000001
0.8
0.9
1.0
1.1
1.2000000000000002
1.3
1.4000000000000001
1.5
1.5
1.6
1.4000000000000001
1.4000000000000001
1.4000000000000001
1.6
1.4000000000000001
1.4000000000000001
1.6
1.4000000000000001
1.3
1.4000000000000001
1.3
1.3
1.5
1.3
^CTraceback (most recent call last):
File "so.py", line 18, in <module>
sleep(delay)
KeyboardInterrupt