【发布时间】:2021-06-05 15:01:17
【问题描述】:
我遇到了这个问题,我无法从函数 run_sensor 中读取变量 bpm 和 spo2(我需要这些另一个脚本中的两个变量)。如果有人可以帮助我,谢谢。
class HeartRateMonitor(object):
LOOP_TIME = 0.10
def __init__(self, print_raw=False, print_result=False):
self.bpm = 0
if print_raw is True:
print('IR, Red')
self.print_raw = print_raw
self.print_result = print_result
def run_sensor(self):
sensor = MAX30102()
ir_data = []
red_data = []
bpms = []
# run until told to stop
while not self._thread.stopped:
# check if any data is available
num_bytes = sensor.get_data_present()
if num_bytes > 0:
# grab all the data and stash it into arrays
if len(ir_data) == 100:
bpm, valid_bpm, spo2, valid_spo2 = hrcalc.calc_hr_and_spo2(ir_data, red_data) # <-------------- here
if valid_bpm:
bpms.append(bpm)
while len(bpms) > 4:
bpms.pop(0)
self.bpm = np.mean(bpms)
if (np.mean(ir_data) < 50000 and np.mean(red_data) < 50000):
self.bpm = 0
if self.print_result:
print("Finger not detected")
if self.print_result:
print("BPM: {0}, SpO2: {1}".format(round(self.bpm), round(spo2)))
【问题讨论】:
-
你的缩进错误
-
另一个脚本是什么?如果它在另一个文件中,您应该重新考虑您的设计。也许
bpm和spo2应该是类的属性?您正在尝试在另一个脚本中使用函数的局部变量,这是无法完成的。答案给出了使用global的(糟糕的)建议,这可能有效,但通常不是一个好主意。 -
另一个脚本将这两个变量用于 Web 仪表板,当我尝试使用全局时,它会给我错误或值保持为 0。
-
我尝试将 bpm 和 spo2 分配给其他变量,但它一直为 0
标签: python multithreading function class