Phidget Relay Issue

OS : Mac OS 11.6.4
PsychoPy version : PsychoPy-2021.2.3-macOS

What are you trying to achieve?:

I am trying to get PsychoPy to communicate with my Phidget (PhidgetInterfaceKit 0/0/8).

What did you try to make it work?:

So I followed the steps as listed on their site - Language - Python - Phidgets Support - and I was able to get the Phidget to work with the following code in a Jupyter notebook:

# test all lights
from Phidget22.Phidget import *
from Phidget22.Devices.DigitalOutput import *
import time

#Declare any event handlers here. These will be called every time the associated event occurs.

def main():
	#Create your Phidget channels
	house_light = DigitalOutput()
	hopper_light = DigitalOutput()
	hopper = DigitalOutput()

	#Set addressing parameters to specify which channel to open (if any)
	house_light.setChannel(1)
	hopper_light.setChannel(2)
	hopper.setChannel(4)

	#Assign any event handlers you need before calling open so that no events are missed.

	#Open your Phidgets and wait for attachment
	house_light.openWaitForAttachment(5000)
	hopper_light.openWaitForAttachment(5000)
	hopper.openWaitForAttachment(5000)

	#Do stuff with your Phidgets here or in your event handlers.
	house_light.setDutyCycle(1) #house_light_on
	hopper_light.setDutyCycle(1) #hopper_light_on 
	hopper.setDutyCycle(1) #hopper_up

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

	#Close your Phidgets once the program is done.
	house_light.close()
	hopper_light.close()
	hopper.close()

main()

Here is a video of it in action: Dropbox - IMG_7403.MOV - Simplify your life

Sweet, proof of concept done, now it was time to get PsychoPy on board. So I put the Phidget22 file here:

/Applications/PsychoPy.app/Contents/Resources/lib/python3.6/Phidget22Python

Now for my code in PsychoPy (Begin Experiment)

import os
import Phidget22Python

Now for my code in PsychoPy (Begin Routine)

#main code
from Phidget22Python import *
from Phidget22Python.Phidget22.Devices.DigitalOutput import *
import time

#Declare any event handlers here. These will be called every time the associated event occurs.

def main():
 #Create your Phidget channels
 house_light = DigitalOutput()
 hopper_light = DigitalOutput()
 hopper = DigitalOutput()

 #Set addressing parameters to specify which channel to open (if any)
 house_light.setChannel(1)
 hopper_light.setChannel(2)
 hopper.setChannel(4)

 #Open your Phidgets and wait for attachment
 house_light.openWaitForAttachment(5000)
 hopper_light.openWaitForAttachment(5000)
 hopper.openWaitForAttachment(5000)

Now for my code in PsychoPy (Each Frame):

#Declare any event handlers here. These will be called every time the associated event occurs.

def main():
 #Create your Phidget channels
 house_light = DigitalOutput()
 hopper_light = DigitalOutput()
 hopper = DigitalOutput()

 #Set addressing parameters to specify which channel to open (if any)
 house_light.setChannel(1)
 hopper_light.setChannel(2)
 hopper.setChannel(4)

 #Open your Phidgets and wait for attachment
 house_light.openWaitForAttachment(5000)
 hopper_light.openWaitForAttachment(5000)
 hopper.openWaitForAttachment(5000)

Code for each frame:

#Definitions 

#Hopper
def hopper_up():
    hopper.setDutyCycle(1) #hopper_up
def hopper_down():
    hopper.setDutyCycle(0) #hopper_down

#Hopper Light
def hopper_light_on():
    hopper_light.setDutyCycle(1) #hopper light on
def hopper_light_off():
    hopper_light.setDutyCycle(0) #hopper light off

#House Light
def house_light_on():
    house_light.setDutyCycle(1) #house light on
def house_light_off():
    house_light.setDutyCycle(0) #house light off

Ultimately, I will keep the house light on during the phase1, reward_noreward, phase2, end_phase components. So I will have a code component with some arbitrary time so that it stays on during trial (each frame):

if t <= 4000:
    house_light_on()

Then, for my reward routine I will have the following code to raise the hopper for the first 4 seconds then shut off (each frame):

if t <= 4:
    hopper_up()
if t >= 4:
    hopper_down()

What specifically went wrong when you tried that?:

I believe it has something to do with the “setup.py” not being run again as my Jupyter notebook has access to the one run in my terminal where I used the following command:

pip install Phidget22

Is there a way to run pip on PsychoPy? If not this should work after downloading the module (attached below) Language - Python - Phidgets Support (slide 4/8):

python setup.py install

Phidget22Python folder:

Thank you so much for your help!

If any of yall can help, I would really appreciate it!

PsychoPy has its own self-contained Python installation and doesn’t have direct access to the packages you install in the system-wide Python environment you’re using via Jupiter Notebooks. There are ways to link to externally-installed packages, though, as described here (ignore the outdated reference to Python 2.7):

https://www.psychopy.org/recipes/addCustomModules.html

I think there has been some recent mention of a mechanism for pip to work with PsychoPy directly, but not sure if that has actually been implemented.

1 Like

So for the path I would just put the folder: /Applications/PsychoPy.app/Contents/Resources/lib/python3.6/Phidget22Python ?

I guess if you’re actually installing the whole package folder there, then perhaps you don’t need to muck around with specifying a path, as your new package is in the same place as all the ones that come bundled with PsychoPy?

I don’t really know - give it a go and see what works.

You’ll know just by trying:

import Phidget22Python

Hi Adam,

I haven’t used phidgets but did make a video we never got round to putting on YouTube to help people use external python libs (you really only need the beginning minute and what happens at 3 mins 19). To use external python libraries you can put the folder containing that library in the same directory as your experiment and then use import MYMODULE where MYMODULE refers to the name of the module directly.

This video is how I used some external libs for other stuff (converting text to speech) - it’s of course a little different but the approach should be the same.

Hope this helps,
Becca

1 Like

For the upcoming major release (2022.2.x series) I’ll include the Phidgets22 within the Standalone release so that will solve that part of the issue for you. The next part is the use of your custom code. Have a look carefully at your code and try to work out what does what. Which bits do you want to be run /on every frame/ versus just at the beginning.

As it happens another user has recently been in touch and is working on a Phidgets Builder Component! So, at some stage I’m sure we’ll have a phidgets component that means you won’t need your code at all, but I don’t think that will be this summer release

best wishes
Jon

1 Like