-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathLickSpout.py
executable file
·281 lines (239 loc) · 10 KB
/
LickSpout.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
from Database import *
from time import sleep
import numpy, socket
from Timer import *
from concurrent.futures import ThreadPoolExecutor
from importlib import util
from ThreadWorker import GetHWPoller
import serial
import sys
import platform
class Probe:
def __init__(self, logger):
self.logger = logger
self.probe1 = False
self.probe2 = False
self.ready = False
self.timer_probe1 = Timer()
self.timer_probe2 = Timer()
self.timer_ready = Timer()
self.__calc_pulse_dur(logger.reward_amount)
self.thread = ThreadPoolExecutor(max_workers=1)
def give_air(self, probe, duration, log=True):
pass
def give_liquid(self, probe, duration=False, log=True):
pass
def give_odor(self, odor_idx, duration, log=True):
pass
def lick(self):
if self.probe1:
self.probe1 = False
probe = 1
#print('Probe 1 activated')
elif self.probe2:
self.probe2 = False
probe = 2
#print('Probe 2 activated')
else:
probe = 0
return probe
def probe1_licked(self, channel):
self.probe1 = True
self.timer_probe1.start()
self.logger.log_lick(1)
#print('Probe 1 activated')
def probe2_licked(self, channel):
self.probe2 = True
self.timer_probe2.start()
self.logger.log_lick(2)
#print('Probe 2 activated')
def in_position(self):
return True, 0
def get_in_position(self):
pass
def get_off_position(self):
pass
def __calc_pulse_dur(self, reward_amount): # calculate pulse duration for the desired reward amount
self.liquid_dur = dict()
probes = (LiquidCalibration() & dict(setup=self.logger.setup)).fetch('probe')
for probe in list(set(probes)):
key = dict(setup=self.logger.setup, probe=probe)
dates = (LiquidCalibration() & key).fetch('date', order_by='date')
key['date'] = dates[-1] # use the most recent calibration
pulse_dur, pulse_num, weight = (LiquidCalibration.PulseWeight() & key).fetch('pulse_dur',
'pulse_num',
'weight')
self.liquid_dur[probe] = numpy.interp(reward_amount,
numpy.divide(weight, pulse_num),
pulse_dur)
def cleanup(self):
pass
class RPProbe(Probe):
def __init__(self, logger):
super(RPProbe, self).__init__(logger)
from RPi import GPIO
self.setup = int(''.join(list(filter(str.isdigit, socket.gethostname()))))
self.GPIO = GPIO
self.GPIO.setmode(self.GPIO.BCM)
self.GPIO.setup([17, 27, 9], self.GPIO.IN)
self.GPIO.setup([22, 23, 24, 25], self.GPIO.OUT, initial=self.GPIO.LOW)
self.channels = {'air': {1: 24, 2: 25},
'liquid': {1: 22, 2: 23},
'lick': {1: 17, 2: 27},
'start': {1: 9}} # 2
self.GPIO.add_event_detect(self.channels['lick'][2], self.GPIO.RISING, callback=self.probe2_licked, bouncetime=200)
self.GPIO.add_event_detect(self.channels['lick'][1], self.GPIO.RISING, callback=self.probe1_licked, bouncetime=200)
self.GPIO.add_event_detect(self.channels['start'][1], self.GPIO.BOTH, callback=self.position_change, bouncetime=50)
def give_air(self, probe, duration, log=True):
self.thread.submit(self.__pulse_out, self.channels['air'][probe], duration)
if log:
self.logger.log_air(probe)
def give_liquid(self, probe, duration=False, log=True):
if not duration:
duration = self.liquid_dur[probe]
self.thread.submit(self.__pulse_out, self.channels['liquid'][probe], duration)
if log:
self.logger.log_liquid(probe)
def give_odor(self, odor_idx, duration, log=True):
print('Odor %1d presentation for %d' % (odor_idx, duration))
channel_indexes = list(self.channels['air'][idx] for idx in odor_idx)
self.thread.submit(self.__pwd_out, channel_indexes, duration)
if log:
self.logger.log_odor(odor_idx)
def position_change(self, channel=0):
if self.GPIO.input(self.channels['start'][1]):
self.timer_ready.start()
self.ready = True
print('in position')
else:
self.ready = False
print('off position')
def in_position(self):
# handle missed events
ready = self.GPIO.input(self.channels['start'][1])
if self.ready != ready:
self.position_change()
if not self.ready:
ready_time = 0
else:
ready_time = self.timer_ready.elapsed_time()
return self.ready, ready_time
def __pwd_out(self, channel, duration, dutycycle=100, frequency=1):
for channel_index in channel:
self.GPIO.output(channel_index, self.GPIO.HIGH)
sleep(duration/1000)
for channel_index in channel:
self.GPIO.output(channel_index, self.GPIO.LOW)
def __pulse_out(self, channel, duration):
self.GPIO.output(channel, self.GPIO.HIGH)
sleep(duration/1000)
self.GPIO.output(channel, self.GPIO.LOW)
def cleanup(self):
self.GPIO.remove_event_detect(self.channels['lick'][1])
self.GPIO.remove_event_detect(self.channels['lick'][2])
self.GPIO.remove_event_detect(self.channels['start'][1])
self.GPIO.cleanup()
class SerialProbe(Probe):
def __init__(self, logger):
if platform.system() == 'Linux':
ser_port = '/dev/ttyUSB0'
else:
ser_port = '/dev/cu.UC-232AC'
self.serial = serial.serial_for_url(ser_port)
self.channels = {'out': {1: 'dtr', 2: 'rts'},
'in': {1: 'dsr', 2: 'cts'}}
self.serial.dtr = False # probe 1
self.serial.rts = False # place probe in position
setattr(self.serial, self.channels['out'][1], False) # read a byte from the hardware
setattr(self.serial, self.channels['out'][2], False) # read a byte from the hardware
super(SerialProbe, self).__init__(logger)
self.worker = GetHWPoller(0.001, self.poll_probe)
self.interlock = False # set to prohibit thread from accessing serial port
self.worker.start()
def give_liquid(self, probe, duration=False, log=True):
if not duration:
duration = self.liquid_dur[probe]
self.thread.submit(self.__pulse_out, probe, duration)
if log:
self.logger.log_liquid(probe)
def poll_probe(self):
if self.interlock:
return "interlock" # someone else is using serial port, wait till done!
self.interlock = True # set interlock so we won't be interrupted
response1 = getattr(self.serial, self.channels['in'][1]) # read a byte from the hardware
response2 = getattr(self.serial, self.channels['in'][2]) # read a byte from the hardware
self.interlock = False
if response1:
if self.timer_probe1.elapsed_time() > 200:
self.probe1_licked(1)
if response2:
if self.timer_probe2.elapsed_time() > 200:
self.probe2_licked(2)
def __pulse_out(self, probe, duration):
while self.interlock: # busy, wait for free, should timeout here
print("waiting for interlock")
sys.stdout.flush()
print('reward!')
self.interlock = True
setattr(self.serial, self.channels['out'][probe], True)
sleep(duration/1000)
setattr(self.serial, self.channels['out'][probe], False)
self.interlock = False
def in_position(self):
return self.ready
def cleanup(self):
self.worker.kill()
class SerialProbeOdor(SerialProbe):
def __init__(self, logger):
if platform.system() == 'Linux':
ser_port = '/dev/ttyUSB0'
else:
ser_port = '/dev/cu.UC-232AC'
self.serial = serial.serial_for_url(ser_port)
self.serial.dtr = False # probe 1
self.serial.rts = False # place probe in position
super(SerialProbe, self).__init__(logger)
self.worker = GetHWPoller(0.001, self.poll_probe)
self.interlock = False # set to prohibit thread from accessing serial port
self.worker.start()
def give_liquid(self, probe, duration=False, log=True):
if not duration:
duration = self.liquid_dur[probe]
self.thread.submit(self.__pulse_out, duration)
if log:
self.logger.log_liquid(probe)
def poll_probe(self):
if self.interlock:
return "interlock" # someone else is using serial port, wait till done!
self.interlock = True # set interlock so we won't be interrupted
response1 = self.serial.dsr # read a byte from the hardware
response2 = self.serial.cts # read a byte from the hardware
self.interlock = False
if response1:
if self.timer_probe1.elapsed_time() > 200:
self.probe1_licked(1)
if response2:
if self.timer_probe2.elapsed_time() > 200:
self.probe2_licked(2)
def __pulse_out(self, duration):
while self.interlock: # busy, wait for free, should timeout here
print("waiting for interlock")
sys.stdout.flush()
print('reward!')
self.interlock = True
self.serial.dtr = True
sleep(duration / 1000)
self.serial.dtr = False
self.interlock = False
def get_in_position(self):
if not self.ready:
self.serial.rts = True
self.ready = True
def get_off_position(self):
if self.ready:
self.serial.rts = False
self.ready = False
def in_position(self):
return self.ready
def cleanup(self):
self.worker.kill()