-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathliqrew.py
More file actions
163 lines (127 loc) · 4.31 KB
/
liqrew.py
File metadata and controls
163 lines (127 loc) · 4.31 KB
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
import time
import logging
from gpiozero import DigitalOutputDevice
from gpiozero.pins.mock import MockFactory
from abc import ABC, abstractmethod
from arch import isRaspberryPI
logger = logging.getLogger('HALLiqReward')
pf = None if isRaspberryPI() else MockFactory()
liqRewPIN = 26
class LiquidRewardTempl(ABC):
def __init__(self, drop_amount: int = 1):
self.drop_amount = drop_amount
@abstractmethod
def is_open(self) -> bool:
''' return True if the reward is continusly running '''
raise NotImplementedError
@abstractmethod
def open(self):
''' give continously liquid reward '''
raise NotImplementedError
@abstractmethod
def close(self):
''' stop giving continously liquid reward '''
raise NotImplementedError
@abstractmethod
def drop(self, N=None):
''' release drop_amount of liquid reward '''
raise NotImplementedError
@abstractmethod
def set_drop_amount(self, amount: int):
''' define the amount of reward '''
raise NotImplementedError
@abstractmethod
def get_drop_amount(self):
''' return the amount of reward '''
raise NotImplementedError
@abstractmethod
def _close(self):
''' close instance '''
raise NotImplementedError
def __str__(self):
return 'Drop Amount {:d}'.format(self.drop_amount)
#####################################################
## Lee Valve
#####################################################
class LeeValve(LiquidRewardTempl):
def __init__(self, drop_amount: int = 1):
self.valve = DigitalOutputDevice(liqRewPIN, pin_factory=pf)
self.set_drop_amount(drop_amount)
def set_drop_amount(self, drop_amount: int):
''' the drop amount is a multiple of 10 ms '''
self.open_time = float(0.01*drop_amount)
def get_drop_amount(self) -> int:
return int(self.open_time/0.01)
def is_open(self):
return self.valve.value == 1
def open(self):
self.valve.on()
def close(self):
self.valve.off()
def drop(self,N=None):
logger.info('Drop delivered')
if isinstance(N,int) and N > 1:
self.valve.blink(on_time=float(N*0.01), n=1, background=True)
else:
self.valve.blink(on_time=self.open_time, n=1)
def _close(self):
self.valve.close()
def __str__(self):
return 'Drop open time: {:03d} ms'.format(self.get_drop_amount()*10)
#####################################################
## Lee Pump 12/24 V micro dose
#####################################################
class LeePump(LiquidRewardTempl):
def __init__(self, drop_amount: int = 1):
super().__init__(drop_amount)
self.pump = DigitalOutputDevice(liqRewPIN, pin_factory=pf)
def set_drop_amount(self, drop_amount: int):
self.drop_amount = drop_amount
def get_drop_amount(self):
return self.drop_amount
def is_open(self):
return self.pump.value == 1
def open(self):
self.pump.blink(on_time=.1, off_time=.1, background=True)
def close(self):
self.pump.off()
def drop(self,N=None):
logger.info('Drop delivered')
if isinstance(N,int) and N > 1:
self.pump.blink(on_time=.1, off_time=.1, n=N, background=True)
else:
self.pump.blink(on_time=.1, off_time=.1, n=self.drop_amount, background=True)
def _close(self):
self.pump.close()
def __str__(self):
return 'Drop amount: {:03d} ul'.format(self.drop_amount*10)
if __name__=='__main__':
logging.basicConfig(filename='logs/liqrewtest.log', filemode='w+', level=logging.DEBUG,
format='%(asctime)s.%(msecs)03d@@%(name)s@@%(levelname)s@@%(message)s',
datefmt='%Y/%m/%d||%H:%M:%S'
)
logger.info('Liquid Reward Test')
val = LeeValve()
val.open()
time.sleep(.5)
val.close()
val.set_drop_amount(2)
val.drop()
print('a b a {}'.format(val))
val._close()
pump = LeePump()
pump.open()
time.sleep(5)
pump.close()
pump.set_drop_amount(2)
pump.drop()
print('a b a {}'.format(pump))
pump._close()
pump = LeePump2()
pump.open()
time.sleep(5)
pump.close()
pump.set_drop_amount(2)
pump.drop()
print('a b a {}'.format(pump))
pump._close()