1

I am using this answer here and I have this code which get the gauge strain signal (2 channels) and then I apply a calibration step, then I print the 2 channels and the current time. But it looks like the print does not match the frequency I use in my code.

from time import sleep
from Phidget22.Phidget import *
from Phidget22.Devices.VoltageRatioInput import *
import time
import datetime

TIME_OUT = 5000 #5s beofre it throws a timeout exception 
DATA_INTERVAL = 50 #50ms sample frequency 

A0 = -6.128983223994E-06
B0 = -0.000059639277340

A1 = -6.101017778744E-06
B1 = -0.000286467338645

def onVoltageRatioChange0(self, voltageRatio):
    Masse = (voltageRatio - (B0) ) / (A0)
    self.masse = Masse

def onVoltageRatioChange1(self, voltageRatio):
    Masse = (voltageRatio - (B1) ) / (A1)
    self.masse = Masse

def results():
        voltageRatioInput0 = VoltageRatioInput()
        voltageRatioInput0.masse = 0
        voltageRatioInput0.setChannel(0)
        voltageRatioInput0.setOnVoltageRatioChangeHandler(onVoltageRatioChange0)
        voltageRatioInput0.openWaitForAttachment(TIME_OUT)
        voltageRatioInput0.setBridgeGain(BridgeGain.BRIDGE_GAIN_128)
        voltageRatioInput0.setDataInterval(DATA_INTERVAL)

        voltageRatioInput1 = VoltageRatioInput()
        voltageRatioInput1.masse = 0
        voltageRatioInput1.setChannel(1)
        voltageRatioInput1.setOnVoltageRatioChangeHandler(onVoltageRatioChange1)
        voltageRatioInput1.openWaitForAttachment(TIME_OUT)
        voltageRatioInput1.setBridgeGain(BridgeGain.BRIDGE_GAIN_128)
        voltageRatioInput1.setDataInterval(DATA_INTERVAL)

        print(str(datetime.datetime.now()) + " / " + str(voltageRatioInput0.masse) + " / " + str(voltageRatioInput1.masse))

        voltageRatioInput0.close()
        voltageRatioInput1.close()

if __name__ == '__main__':
    try:
        while True:
            results()

    except KeyboardInterrupt:
        print("Goodbye")
        pass

So normally using the voltageRatioInput0.setDataInterval(DATA_INTERVAL) it should print the value with a frequency of 20 hz. But it prints me this :

2019-11-22 10:24:59.460503 / -0.03847266852956798 / 0.2918630004986786
2019-11-22 10:25:00.099831 / -0.03695316689942101 / -0.02070779820379342
2019-11-22 10:25:00.772398 / -0.04029613574942367 / 0.28775155534154484
2019-11-22 10:25:01.420283 / -0.043487203384171676 / 0.25676361089420047

So clearly here I do not have 20 Hz...

RogerLM
  • 35
  • 6

1 Answers1

0

A lot of time is probably spent in your result() function. I cannot reproduce here, but a simple

python -m cProfile -s tottime your_program.py

should help you to see where most of the time is spent each loop iteration.

I guess that instanciation of a VoltageRatioInput() and VoltageRatioInput.close() are a good way to start. Could you try taking them outside the loop as in:

from Phidget22.Phidget import *
from Phidget22.Devices.VoltageRatioInput import *
import datetime

TIME_OUT = 5000 #5s beofre it throws a timeout exception 
DATA_INTERVAL = 50 #50ms sample frequency 

A0 = -6.128983223994E-06
B0 = -0.000059639277340

A1 = -6.101017778744E-06
B1 = -0.000286467338645

def onVoltageRatioChange0(self, voltageRatio):
    Masse = (voltageRatio - (B0) ) / (A0)
    self.masse = Masse

def onVoltageRatioChange1(self, voltageRatio):
    Masse = (voltageRatio - (B1) ) / (A1)
    self.masse = Masse

def results(voltageRatioInput0, voltageRatioInput1):
        voltageRatioInput0.masse = 0
        voltageRatioInput0.setChannel(0)
        voltageRatioInput0.setOnVoltageRatioChangeHandler(onVoltageRatioChange0)
        voltageRatioInput0.openWaitForAttachment(TIME_OUT)
        voltageRatioInput0.setBridgeGain(BridgeGain.BRIDGE_GAIN_128)
        voltageRatioInput0.setDataInterval(DATA_INTERVAL)

        voltageRatioInput1.masse = 0
        voltageRatioInput1.setChannel(1)
        voltageRatioInput1.setOnVoltageRatioChangeHandler(onVoltageRatioChange1)
        voltageRatioInput1.openWaitForAttachment(TIME_OUT)
        voltageRatioInput1.setBridgeGain(BridgeGain.BRIDGE_GAIN_128)
        voltageRatioInput1.setDataInterval(DATA_INTERVAL)

        print(str(datetime.datetime.now()) + " / " + str(voltageRatioInput0.masse) + " / " + str(voltageRatioInput1.masse))


if __name__ == '__main__':
    try:
        voltageRatioInput0 = VoltageRatioInput()
        voltageRatioInput1 = VoltageRatioInput()

        while True:
            results(voltageRatioInput0, voltageRatioInput1)

        voltageRatioInput0.close()
        voltageRatioInput1.close()

    except KeyboardInterrupt:
        print("Goodbye")
Tim
  • 2,052
  • 21
  • 30