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!