Skip to content

Commit

Permalink
release of new GPIO modules
Browse files Browse the repository at this point in the history
  • Loading branch information
JensBlack committed Sep 22, 2020
1 parent e53cfa0 commit 28cef2b
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 136 deletions.
42 changes: 37 additions & 5 deletions experiments/base/stimulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,35 @@ def __init__(self):
self._name = 'BaseStimulation'
self._parameter_dict = dict(TYPE='str',
PORT='str',
IP = 'str',
STIM_TIME='float')
self._settings_dict = get_stimulation_settings(self._name, self._parameter_dict)
self._running = False
self._stim_device = self._setup_device(self._settings_dict['TYPE'], self._settings_dict['PORT'])
self._stim_device = self._setup_device(self._settings_dict['TYPE'], self._settings_dict['PORT'], self._settings_dict['IP'])

@staticmethod
def _setup_device(type, port):
def _setup_device(type, port, ip):
device = None
if type == 'NI':
from experiments.utils.DAQ_output import DigitalModDevice
device = DigitalModDevice(port)

if type == 'RASPBERRY':
from experiments.utils.gpio_control import DigitalPiDevice
device = DigitalPiDevice(port)

if type == 'RASP_NETWORK':
from experiments.utils.gpio_control import DigitalPiDevice
if ip is not None:
device = DigitalPiDevice(port, ip)
else:
raise ValueError('IP required for remote GPIO control.')

if type == 'ARDUINO':
from experiments.utils.gpio_control import DigitalArduinoDevice
device = DigitalArduinoDevice(port)


return device

def stimulate(self):
Expand Down Expand Up @@ -72,23 +89,38 @@ class RewardDispenser(BaseStimulation):
def __init__(self):
self._name = 'RewardDispenser'
self._parameter_dict = dict(TYPE = 'str',
IP = 'str',
STIM_PORT= 'str',
REMOVAL_PORT = 'str',
STIM_TIME = 'float',
REMOVAL_TIME = 'float')
self._settings_dict = get_stimulation_settings(self._name, self._parameter_dict)
self._running = False
self._stim_device = self._setup_device(self._settings_dict['TYPE'], self._settings_dict['STIM_PORT'])
self._removal_device = self._setup_device(self._settings_dict['TYPE'], self._settings_dict['REMOVAL_PORT'])
self._stim_device = self._setup_device(self._settings_dict['TYPE'], self._settings_dict['STIM_PORT'],
self._settings_dict['IP'])
self._removal_device = self._setup_device(self._settings_dict['TYPE'], self._settings_dict['REMOVAL_PORT'],
self._settings_dict['IP'])


@staticmethod
def _setup_device(type, port):
def _setup_device(type, port, ip):
device = None
if type == 'NI':
from experiments.utils.DAQ_output import DigitalModDevice
device = DigitalModDevice(port)

if type == 'RASPBERRY':
from experiments.utils.gpio_control import DigitialPiBoardDevice
device = DigitialPiBoardDevice(port)

if type == 'RASP_NETWORK':
from experiments.utils.gpio_control import DigitialPiBoardDevice
if ip is not None:
device = DigitialPiBoardDevice(port, ip)
else:
raise ValueError('IP required for remote GPIO control.')


return device

def stimulate(self):
Expand Down
8 changes: 8 additions & 0 deletions experiments/configs/default_config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,20 @@ STIMULATION = BaseStimulation
;[STIMULATION]

[BaseStimulation]
; can be NI, RASPBERRY or RASP_NETWORK
TYPE = NI
;only used in RASP_NETWORK
IP = None
;PORT parameter is used for all (Port from DAQ, PIN from Raspberry, or serial port from Arduino)
PORT = Dev1/PFI6
STIM_TIME = 3.5

[RewardDispenser]
; can be NI, RASPBERRY, RASP_NETWORK or ARDUINO
TYPE = NI
;only used in RASP_NETWORK
IP = None
;PORT parameter is used for all (Port from DAQ, PIN from Raspberry, or serial port from Arduino)
STIM_PORT = Dev1/PFI6
REMOVAL_PORT = Dev1/PFI5
STIM_TIME = 3.5
Expand Down
66 changes: 66 additions & 0 deletions experiments/utils/gpio_control.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
from gpiozero import DigitalOutputDevice
from gpiozero.pins.pigpio import PiGPIOFactory

import serial



class DigitalPiDevice:
"""
Digital modulated devices in combination with Raspberry Pi GPIO
Setup: https://gpiozero.readthedocs.io/en/stable/remote_gpio.html
"""

def __init__(self, PIN, BOARD_IP: str = None):

"""
:param BOARD_IP: IP adress of board connected to the Device
"""
if BOARD_IP is not None:
self._factory = PiGPIOFactory(host = BOARD_IP)
self._device = DigitalOutputDevice(PIN, pin_factory = self._factory)
else:
self._factory = None
self._device = DigitalOutputDevice(PIN)
self._running = False

def turn_on(self):
self._device.on()
self._running = True

def turn_off(self):
self._device.off()
self._running = False

def toggle(self):
self._device.toggle()
self._running = self._device.is_active


class DigitalArduinoDevice:
"""
Digital modulated devices in combination with Arduino boards connected via USB
setup: https://pythonforundergradengineers.com/python-arduino-LED.html
"""

def __init__(self, PORT):
"""
:param PORT: USB PORT of the arduino board
"""
self._device = serial.Serial(PORT, baudrate=9600)
self._running = False

def turn_on(self):
self._device.write(b'H')
self._running = True

def turn_off(self):
self._device.write(b'L')
self._running = False

def toggle(self):
if self._running:
self.turn_off()
else:
self.turn_on()
130 changes: 0 additions & 130 deletions experiments/utils/readme.md

This file was deleted.

5 changes: 4 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
gpiozero
pigpio
pyserial
nidaqmx>=0.5.7
click>=7.0
opencv-python>=3.4.5.20
numpy>=1.14.5
pandas>=0.21.0
matplotlib>=3.0.3
scikit-image>=0.14.2
scipy>=1.1.0
scipy>=1.1.0

0 comments on commit 28cef2b

Please sign in to comment.