1

I have a PhidgetBridge which is connected to 2 gauge strain. I got the signal and make the calibration using this code. But when I run it, it displays me only one print, or I'd like to have many prints (with this code, I'd like the data to be printed with a data interval of 50ms (so 20 Hz)).

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 #1000ms 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 main():

        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(voltageRatioInput0.masse) + " / " + str(voltageRatioInput1.masse))

        try:
            input("Press Enter to Stop\n")

        except (Exception, KeyboardInterrupt):

            pass

        voltageRatioInput0.close()
        voltageRatioInput1.close()


main()
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
RogerLM
  • 35
  • 6
  • You probably want to include a `while` loop or some other sort of loop whose termination condition depends on the input. – davidlowryduda Nov 21 '19 at 15:44
  • What is the current behavior? What is expected instead? Please add some context, since that code depends will not run without the connected electronics. – alec_djinn Nov 21 '19 at 15:45
  • Notice that when you use input() the loop will block until an input is received. See my answer for an alternative that doesn't block. – alexisdevarennes Nov 21 '19 at 15:56

3 Answers3

0

You just need an infinite loop (while True) that will run until a KeyboardInterrupt exception is thrown. Furthermore, in order to run print the new results every 50 milisseconds, you just need to add sleep(0.05) in the end of the while loop:

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 #1000ms 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(voltageRatioInput0.masse) + " / " + str(voltageRatioInput1.masse))

        voltageRatioInput0.close()
        voltageRatioInput1.close()

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

            # Finally sleep for 50ms
            sleep(0.05)

    except KeyboardInterrupt:
        print("Goodbye")
        pass
Giorgos Myrianthous
  • 36,235
  • 20
  • 134
  • 156
  • Thank you! It works, but it looks like the data are not printed with a 50ms interval, it's much slowly... have idea why ? – RogerLM Nov 22 '19 at 08:23
  • Since normally I use setDataInterval(DATA_INTERVAL) to set the frequency of the signal, do I still need it since now you make me use sleep(0.05) ? – RogerLM Nov 22 '19 at 08:26
  • @RogerLM If this is already handled by the function `setDataInterval(DATA_INTERVAL)` feel free to skip that line. – Giorgos Myrianthous Nov 22 '19 at 09:19
0

I've adapted your code to use a While loop:

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  # 1000ms 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 main():
    while True:

        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(voltageRatioInput0.masse) + " / " +
            str(voltageRatioInput1.masse))

        try:
            input("Press Enter to Stop\n")
        except (Exception, KeyboardInterrupt):
            pass

        voltageRatioInput0.close()
        voltageRatioInput1.close()
        sleep(0.05)

main()

But your code will halt on every iteration and wait for input so a better implementation would be:

import os
import sys
import select
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  # 1000ms 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 main():
    while True:

        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(voltageRatioInput0.masse) + " / " +
            str(voltageRatioInput1.masse))

        voltageRatioInput0.close()
        voltageRatioInput1.close()

        os.system('cls' if os.name == 'nt' else 'clear')
        print("I'm doing stuff. Press Enter to stop me!")

        if sys.stdin in select.select([sys.stdin], [], [], 0)[0]:
            break

        time.sleep(0.05)  # wait 50ms

main()

This doesn't block and will break the loop on enter.

alexisdevarennes
  • 5,437
  • 4
  • 24
  • 38
0

You probably want to do something like this:

import time

while True:
    try:

        print('my code output') #substitute this line with your code

        time.sleep(0.05) #50 ms interval

    except KeyboardInterrupt:
        answer = input("Quit? Y/N")
        if answer.upper() == 'Y':
            break

print('Program terminated by the user')
alec_djinn
  • 10,104
  • 8
  • 46
  • 71