## Making computation with 2 channels using python

Supporting 2.6 and up
YLM
Phidgetsian
Posts: 10
Joined: Mon Nov 18, 2019 9:38 am
Contact:

### Making computation with 2 channels using python

Hi,

I used the generated python code to get the signal of 2 channels, I modify it a little bit and did some calibration. Now that I have my 2 channels independently, I'd like to use them together in order to make some computation. Here is the code :

Code: Select all

`from Phidget22.Phidget import *from Phidget22.Devices.VoltageRatioInput import *import timeimport datetimeTIME_OUT = 5000 #5s before it throws a timeout exception DATA_INTERVAL = 1000 #1000ms data interval # Calibration factorA0 = -6.128983223994E-06B0 = -0.000059639277340A1 = -6.101017778744E-06B1 = -0.000286467338645def onVoltageRatioChange0(self, voltageRatio):   Masse = (voltageRatio - (B0) ) / (A0)   print(str(Masse))def onVoltageRatioChange1(self, voltageRatio):   Masse = (voltageRatio - (B1) ) / (A1)   print(str(Masse))   def main():      voltageRatioInput0 = VoltageRatioInput()      voltageRatioInput0.setChannel(0)      voltageRatioInput0.setOnVoltageRatioChangeHandler(onVoltageRatioChange0)      voltageRatioInput0.openWaitForAttachment(TIME_OUT)      voltageRatioInput0.setBridgeGain(BridgeGain.BRIDGE_GAIN_128)      voltageRatioInput0.setDataInterval(DATA_INTERVAL)      voltageRatioInput1 = VoltageRatioInput()      voltageRatioInput1.setChannel(1)      voltageRatioInput1.setOnVoltageRatioChangeHandler(onVoltageRatioChange1)      voltageRatioInput1.openWaitForAttachment(TIME_OUT)      voltageRatioInput1.setBridgeGain(BridgeGain.BRIDGE_GAIN_128)      voltageRatioInput1.setDataInterval(DATA_INTERVAL)      try:         input("Press Enter to Stop\n")      except (Exception, KeyboardInterrupt):         pass`

I have tried to do :

Code: Select all

`Masse0 = voltageRatioInput0.setOnVoltageRatioChangeHandler(onVoltageRatioChange0)`

To then called Masse0.Masse but it returns me None...

I am not so good with python code, and I do not know how to call each Masse from the 2 function onVoltageRatioChange0 and 1 to use them inside my main and make some computation using the 2 channels...

Thank you

Posts: 715
Joined: Fri Oct 28, 2011 12:17 pm
Contact:

### Re: Making computation with 2 channels using python

The change events do not return any variable on their own. In python, in order to gain access to these values outside of the event, you need to create a new variable and assign it to the handler. I've updated your code sample to do this for each channel, and print both mass values when the program quits to prove that both values are available in main:

Code: Select all

`from Phidget22.Phidget import *from Phidget22.Devices.VoltageRatioInput import *import timeimport datetimeTIME_OUT = 5000 #5s before it throws a timeout exceptionDATA_INTERVAL = 1000 #1000ms data interval# Calibration factorA0 = -6.128983223994E-06B0 = -0.000059639277340A1 = -6.101017778744E-06B1 = -0.000286467338645def onVoltageRatioChange0(self, voltageRatio):   Masse = (voltageRatio - (B0) ) / (A0)   self.masse = Masse   print(str(Masse))def onVoltageRatioChange1(self, voltageRatio):   Masse = (voltageRatio - (B1) ) / (A1)   self.masse = Masse   print(str(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)            try:         input("Press Enter to Stop\n")      except (Exception, KeyboardInterrupt):         pass      print(str(voltageRatioInput0.masse) + " / " + str(voltageRatioInput1.masse))      voltageRatioInput0.close()main()`

YLM
Phidgetsian
Posts: 10
Joined: Mon Nov 18, 2019 9:38 am
Contact:

### Re: Making computation with 2 channels using python

Great. Thank you !

YLM
Phidgetsian
Posts: 10
Joined: Mon Nov 18, 2019 9:38 am
Contact:

### Re: Making computation with 2 channels using python

With my previous code, with the

Code: Select all

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

I had the value in my console using my DATA_INTERVAL until I press 'Enter'. Now with the print of 2 Channels inside the main I only have one print and then the code stop.. Do you know why ?

Thank you

Posts: 715
Joined: Fri Oct 28, 2011 12:17 pm
Contact:

### Re: Making computation with 2 channels using python

I'm not sure why yours is only printing one event- using the code I posted, mine keeps printing until enter is pressed.

jdecoux
Labview Developer
Posts: 117
Joined: Mon Nov 13, 2017 10:20 am
Contact:

### Re: Making computation with 2 channels using python

Only getting one event with that code sounds like you may be using IDLE.

IDLE has problems dealing with multiple threads (which are needed for events), so we recommend against using IDLE for development with Phidgets. Try with another IDE, or running the program from the command line.

YLM
Phidgetsian
Posts: 10
Joined: Mon Nov 18, 2019 9:38 am
Contact:

### Re: Making computation with 2 channels using python

curently I am writing my code inside Sublime Text and then I run the code inside the terminal using

Code: Select all

`python3 mycode.py`

This is not the good solution ?

Thank you

jdecoux
Labview Developer
Posts: 117
Joined: Mon Nov 13, 2017 10:20 am
Contact:

### Re: Making computation with 2 channels using python

That should work then...

To clarify, do you see more than one set of data printed to the screen before you press ENTER?

If so, the code provided is working as intended. The data being printed while waiting for input is happening in the Event Handlers, which you can think of as happening in the background at the same time as the rest of your code.

The final print statement only happens once as it is written to only print once, to demonstrate how you would use voltageRatioInput0.masse and voltageRatioInput1.masse in your main code.