Efficient interfacing with Psychopy

Supporting 2.7 and 3.2+
Post Reply
ainsliej
Phidgetsian
Posts: 7
Joined: Tue Nov 12, 2019 3:34 am
Contact:

Efficient interfacing with Psychopy

Post by ainsliej »

Hi there,

I am new to working with Phidgets and with Python so this question may be very basic. I am attempting to write a program which takes voltage ratio info from 5 sensors (one for each finger) and uses this to control the position of a shape presented on screen using Psychopy.

I have a working code (see below), but it seems to be very slow and inefficient as I only see the cursors move ~2x per second. The code below is executed every frame. It takes the new voltage ratio, subtracts that from the baseline voltage ratio, and then uses this to calculate the new position variable. The position of the shape is then also updated each frame (code not shown here).

Code: Select all

newRthumb= voltageRatioRthumb.getVoltageRatio()
newRindex= voltageRatioRindex.getVoltageRatio()
newRmiddle= voltageRatioRmiddle.getVoltageRatio()
newRring= voltageRatioRring.getVoltageRatio()
newRlittle= voltageRatioRlittle.getVoltageRatio()

BLedRthumb=newRthumb-BLRthumb
BLedRindex=newRindex-BLRindex
BLedRmiddle=newRmiddle-BLRmiddle
BLedRring=newRring-BLRring
BLedRlittle=newRlittle-BLRlittle

PosRthumb =(BLedRthumb/0.000125)-0.75
PosRindex =(BLedRindex/0.000125)-0.75
PosRmiddle =(BLedRmiddle/0.000125)-0.75
PosRring =(BLedRring/0.000125)-0.75
PosRlittle =(BLedRlittle/0.000125)-0.75
Is there a better way to do this? I thought perhaps to use an event handler might be better, but I have struggled to use the event handler function to create a variable which I can then use outside the function. Also I should mention slow frame rate does not seem to be an inherent problem of the monitor, I can present other things at much higher time resolution.

Thanks in advance!
User avatar
mparadis
Site Admin
Posts: 959
Joined: Fri Oct 28, 2011 12:17 pm
Contact:

Re: Efficient interfacing with Psychopy

Post by mparadis »

I would certainly recommend doing this using events.

If you want to learn how to use variables inside event handlers, see this page and scroll to the "Linking Data to Events" section. There's some python code that shows you how to add your own variables to the channel object.
ainsliej
Phidgetsian
Posts: 7
Joined: Tue Nov 12, 2019 3:34 am
Contact:

Re: Efficient interfacing with Psychopy

Post by ainsliej »

Thanks for the quick reply

I can see how that would allow me to use the variable in the event, but what I need is for the variable created inside the event (i.e. the position of the shape) to always be present outside the event. Is it possible to pass variables out of the event?
User avatar
mparadis
Site Admin
Posts: 959
Joined: Fri Oct 28, 2011 12:17 pm
Contact:

Re: Efficient interfacing with Psychopy

Post by mparadis »

If you follow the process on that page, the variables that you bring in to the event handler will also be available outside of the event handler. There's no need to "pass them out" or return them. So you can declare your five variables, add them to the appropriate channels, and then set them equal to the voltageratio inside the event handler. Then, those variables will always have the most up-to-date voltage ratio.
ainsliej
Phidgetsian
Posts: 7
Joined: Tue Nov 12, 2019 3:34 am
Contact:

Re: Efficient interfacing with Psychopy

Post by ainsliej »

Im afraid I don't follow. I thought that you did not need to declare variables in Python? If I set the variable to a value outside of the Event then it appears to be fixed as this value. If I print the variable inside the event handler then I get the up-to-date value, but if I print it from outside then I only ever get the value I set it to initially. Sorry again, I am aware this is very basic Python and not necessarily specific to Phidgets.
User avatar
mparadis
Site Admin
Posts: 959
Joined: Fri Oct 28, 2011 12:17 pm
Contact:

Re: Efficient interfacing with Psychopy

Post by mparadis »

It should update the variable outside of the event. Here's a code sample I just made:

Code: Select all

from Phidget22.Phidget import *
from Phidget22.Devices.VoltageInput import *
import time

def onVoltageChange(self, voltage):
	self.count += 1
	print("Voltage: ", str(voltage), " Count (Inside): ", self.count)

def main():
	voltageInput0 = VoltageInput()

	voltageInput0.count = 0

	voltageInput0.setOnVoltageChangeHandler(onVoltageChange)

	voltageInput0.openWaitForAttachment(5000)

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

	print("Count (Outside): ", voltageInput0.count)

	voltageInput0.close()

main()
In this code, the "count" variable will increment each time the change event fires. At the end of the program when you press enter, it prints count and it matches the most recent value from inside the event.
Post Reply

Who is online

Users browsing this forum: No registered users and 20 guests