我是一名初学者,在main_odroid.py中运行代码时遇到了以下错误:
'NameError:未定义名称'alarmthresh5'
当我运行此代码时:
from threading import Thread
def func3():
Import main_odroid
global alarmthresh5
alarmthresh5 = 1.45 * stddev2
[...]
if floats[1] > alarmthresh5:
wpi.digitalWrite(1, 1)
[...]
Thread(target = func3).start()
[...]
[…]表示为清晰起见我排除的其余(无关)代码
我使用了搜索功能,尝试了不同的答案,但没有解决问题。 谢谢,我对你的答案很感兴趣
编辑:我认为变量alarmthresh5的作用域是全局的,因为我在func3中键入了“global alarmthresh5”
EDIT2:整个代码
import subprocess
from subprocess import Popen, PIPE
import os
import odroid_wiringpi as wpi
from collections import deque
import time
import datetime
from threading import Thread
from math import sqrt
sysdamping = 10
freqdamping = 14
totalbins = 2000
#Functions
def average(p): return sum(p) / float(len(p))
def variance(p): return list(map(lambda x: (x - average(p))**2, p))
def std_dev(p): return sqrt(average(variance(p)))
wpi.wiringPiSetup()
wpi.pinMode(1, 1)
wpi.pinMode(5, 1)
wpi.pinMode(27, 1)
wpi.pinMode(31, 1)
wpi.pinMode(23, 1)
wpi.pinMode(11, 1)
wpi.digitalWrite(23, 0)
wpi.digitalWrite(11, 0)
wpi.pinMode(2, 1)
wpi.pinMode(7, 1)
wpi.digitalWrite(2, 0)
wpi.digitalWrite(7, 0)
wpi.pinMode(9, 1)
wpi.digitalWrite(9, 0)
time.sleep(0.1)
wpi.pullUpDnControl(30, 2)
os.system("killall -9 soapy_power -q")
time.sleep(0.1)
def func2():
exec(open("/home/odroid/rtl-power-fftw/build/deactiveer.py").read())
def func3():
import main_odroid
global alarmthresh1
global alarmthresh2
global alarmthresh3
global alarmthresh4
global alarmthresh5
global Lijst
alarmthresh1 = (average(rolling[i]) + 1.45 * stddev2)
alarmthresh2 = alarmthresh1 + 2
alarmthresh3 = alarmthresh2 + 2
alarmthresh4 = alarmthresh3 + 2
alarmthresh5 = alarmthresh4 + 2
Lijst = [381843000.0, 382242000.0, 382245000.0, 382248000.0, 382254000.0, 382257000.0, 382305000.0, 382308000.0, 382320000.0, 382323000.0, 382326000.0, 382389000.0, 382392000.0, 382557000.0, 382560000.0, 382671000.0, 383157000.0, 383454000.0, 383457000.0, 383469000.0, 383472000.0, 383997000.0, 384000000.0, 384003000.0, 384006000.0]
def func4():
import main_odroid
global stddev2
stddev2 = std_dev(sweep)
def func1():
cmd = ["/home/odroid/.local/bin/soapy_power", "-f", "433M", "-r", "6M", "--force-rate", "-q", "-c", "-b", "2000", "-F", "rtl_power_fftw"]
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, bufsize=1)
rolling = []
i = 0
rolling_avg = deque([])
sweep = deque([], maxlen=1500)
stddev = 12
totalbins = 2000
# Let's see what's going on with rtl_power_fftw
for line in iter(p.stdout.readline, b''):
#Ignore garbage output
if not (b'#' in line or not line.strip()):
floats = list(map(float, line.split()))
# Create 2D array
if len(rolling) < totalbins:
rolling.append(deque([]))
rolling[i].append(floats[1])
sweep.append(floats[1])
# Let's start filtering...
if len(rolling[i]) >= freqdamping:
rolling[i].popleft()
print("ref 5")
input_state3 = wpi.digitalRead(9)
if floats[1] > alarmthresh5 and 380820000.0 < floats[0] < 384570000.0 and floats[0] not in Lijst: # Led 5
wpi.digitalWrite(1, 1)
if input_state3 == 0:
wpi.digitalWrite(9, 1) # Buzzer proxy
wpi.digitalWrite(31, 1) # Buzzer ON
print(round(floats[0]), round(floats[1]), alarmthresh5, "ref2")
Thread(target = func2).start()
if floats[1] > alarmthresh1 and floats[0] in [380310000.0, 381444000.0, 381579000.0, 381780000.0, 381783000.0, 382110000.0, 382836000.0, 382881000.0, 382884000.0, 382887000.0, 382890000.0, 382893000.0, 383037000.0, 383085000.0, 383340000.0, 383460000.0, 384258000.0, 384261000.0, 384264000.0]:
if input_state3 == 0:
wpi.digitalWrite(9, 1) # Buzzer proxy
wpi.digitalWrite(31, 1) # Buzzer ON
print(round(floats[0]), round(floats[1]), alarmthresh1, "ref1")
Thread(target = func2).start()
if floats[1] > alarmthresh4 and 380820000.0 < floats[0] < 384570000.0 and floats[0] not in Lijst: # Led 4
wpi.digitalWrite(7, 1)
if floats[1] > alarmthresh3 and 380820000.0 < floats[0] < 384570000.0 and floats[0] not in Lijst: # Led 3
wpi.digitalWrite(23, 1)
if floats[1] > alarmthresh2 and 380820000.0 < floats[0] < 384570000.0 and floats[0] not in Lijst: # Led 2
wpi.digitalWrite(5, 1)
if floats[1] > alarmthresh1 and 380820000.0 < floats[0] < 384570000.0 and floats[0] not in Lijst: # Led 1
input_state = wpi.digitalRead(27)
if input_state == 0:
wpi.digitalWrite(27, 1)
Thread(target = func2).start()
# Increment or reset indexer (i)
if i < totalbins - i: i = i + 1
else:
i = 0
Thread(target = func4).start() # Set stddev2
Thread(target = func3).start() # Set alarmthresh1 - alarmthresh5
print("ref 6")
p.stdout.close()
p.wait()
Thread(target = func1).start()
Ref 6在Ref 5之前打印,因此func3在调用alarmthresh5之前启动
可以指定变量alarmthresh5的范围吗。尝试仅在函数定义中添加变量,类似于:def func3(alarmthresh5=ValueYouWantOutput)
相关问题 更多 >
编程相关推荐