我有一个具有这种结构的麦克风类:
class Microphone(object): # Microphone class
def __init__(self, x, y, limit):
self.X = x
self.Y = y
self.low_limit = limit
self.counter = 0以及与麦克风实例列表一起工作的函数。
def knock_calibration_2d(port, microphones, W):
print " ------------------------- Knock calibration ----------------------"
i = 0
while True:
if port.in_waiting > 0:
msg = str(port.readline()).strip()
if msg.startswith("ACK:"): # received an ACK message
continue
elif msg.startswith("A"): # received an ADC value
words = msg.split(' ')
microphones[(ord(msg[1]) - ord('0'))] = int(words[1])
i = i + 1
if i == len(microphones):
break
print "Raw calibration counter values: " + str([e for e in microphones])
L = W / math.sqrt(2) # W=47
T = 0.0
for j in range(1, len(microphones)):
T = T + (microphones[j].counter - microphones[0].counter)
T = T / (len(microphones) - 1)
C = L / T
print "Normalized calibration counter values: " + str([e for e in microphones])
print "L=" + str(L) + " T=" + str(T) + " C=" + str(C)
return 1使用下面的表达式,我总是会得到"AttributeError:' int‘对象没有属性’计数器‘“错误消息,同时,我确信麦克风列表只包含麦克风对象,而不是带有计数器属性的int。这里会有什么问题?我用完整的代码这里创建了一个pastebin。我和anaconda和python2.7的解释器一起工作
T=T+ (microphonesj.counter - microphones.counter)
下面是我定义列表的主要功能:
def main():
microphones = [
Microphone( 0, 0, 50),
Microphone( W/2, W/2, 50),
Microphone( W/2, -W/2, 50),
Microphone( -W/2, -W/2, 50),
Microphone( -W/2, W/2, 50)
]
C = knock_calibration_2d(port, microphones, W)发布于 2018-06-23 12:26:30
在你的knock_calibration_2d函数中,第10~12行,你改变麦克风的元素。
如果消息以“A”开头,则使用Int对象替换元素
https://stackoverflow.com/questions/51001009
复制相似问题