我正在使用 Phidget Bridge 4 输入来获取 2 个计量单元的信号。然后我想在信号之间进行加法和乘法但无法管理onVoltageRatioChange
功能。
首先,我尝试从函数返回信号onVoltageRatioChange
以便在内部使用它main
但没有成功..
然后,在函数内部,我尝试根据通道设置一个具有接收值的变量,然后在信号之间进行计算,但在这里我也遇到了一个错误:
错误:t = channel0 + channel1 UnboundLocalError:分配前引用了局部变量“channel0”
TIME_OUT = 5000 #5s beofre it throws a timeout exception
DATA_INTERVAL = 20 #1000ms sample frequency
A0 = 1
B0 = 0
offset = 0 # 10.45
channel0 = 0
channel1 = 0
def onVoltageRatioChange(self, voltageRatio):
Masse = (voltageRatio - (B0) ) / (A0) + offset
if(self.getChannel() == 0):
channel0 = voltageRatio
if(self.getChannel() == 1):
channel1 = voltageRatio
t = channel0 + channel1
print(t)
def main():
voltageRatioInput0 = VoltageRatioInput()
voltageRatioInput1 = VoltageRatioInput()
voltageRatioInput2 = VoltageRatioInput()
voltageRatioInput3 = VoltageRatioInput()
voltageRatioInput0.setChannel(0)
voltageRatioInput1.setChannel(1)
voltageRatioInput2.setChannel(2)
voltageRatioInput3.setChannel(3)
voltageRatioInput0.setOnVoltageRatioChangeHandler(onVoltageRatioChange)
voltageRatioInput1.setOnVoltageRatioChangeHandler(onVoltageRatioChange)
voltageRatioInput2.setOnVoltageRatioChangeHandler(onVoltageRatioChange)
voltageRatioInput3.setOnVoltageRatioChangeHandler(onVoltageRatioChange)
voltageRatioInput0.openWaitForAttachment(TIME_OUT)
voltageRatioInput1.openWaitForAttachment(TIME_OUT)
voltageRatioInput2.openWaitForAttachment(TIME_OUT)
voltageRatioInput3.openWaitForAttachment(TIME_OUT)
voltageRatioInput0.setDataInterval(DATA_INTERVAL)
voltageRatioInput1.setDataInterval(DATA_INTERVAL)
voltageRatioInput2.setDataInterval(DATA_INTERVAL)
voltageRatioInput3.setDataInterval(DATA_INTERVAL)
while True:
time.sleep(10)
voltageRatioInput0.close()
voltageRatioInput1.close()
voltageRatioInput2.close()
voltageRatioInput3.close()
main()