-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtailwithdrawal.py
More file actions
173 lines (147 loc) · 5.33 KB
/
Copy pathtailwithdrawal.py
File metadata and controls
173 lines (147 loc) · 5.33 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
162
163
164
165
166
167
168
169
170
171
172
173
#!/usr/bin/env python
import time
import RPi.GPIO as GPIO
import os
import glob
import serial
import sys
import datetime
import operator
from config import COMMAND_RFIDs, USER_RFIDs, SPEED_RFIDs
## disable saving data if keys are detected as RFID
## check within rat variability before continue to next rat
## temp probe DS18B20
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')[0]
device_file = device_folder + '/w1_slave'
# path to data file
idfile=open("/home/pi/deviceid")
device=idfile.read()
device=device.strip()
today=datetime.date.today()
td=str(today)
file_dir = "/home/pi/Pies/tailwithdrawl/"
# create the directory if not already exist
if not os.path.isdir(file_dir):
os.makedirs(file_dir)
datafile = file_dir + "tailimmersion_" + td + ".csv"
# flags
startflag = "\x02"
endflag = "\x03"
latency={}
savedata=1 # default to save data
def setupGPIO():
GPIO.setmode(GPIO.BOARD) # Numbers GPIOs by physical location
GPIO.setup(Tail, GPIO.IN, pull_up_down=GPIO.PUD_UP)
def read_temp_raw():
f = open(device_file, 'r')
lines = f.readlines()
f.close()
return lines
def read_temp():
lines = read_temp_raw()
while lines[0].strip()[-3:] != 'YES':
time.sleep(0.01)
lines = read_temp_raw()
equals_pos = lines[1].find('t=')
if equals_pos != -1:
temp_string = lines[1][equals_pos+2:]
temp_c = float(temp_string) / 1000.0
return temp_c
Tail=12
setupGPIO()
spin_speed = input("Please enter a mixing speed (1-7): ")[-4:]
if spin_speed in SPEED_RFIDs.keys():
spin_speed = SPEED_RFIDs[spin_speed]
user=input("TailTimer started.\nPlease enter your name:\n")[-4:]
if user in USER_RFIDs.keys():
user = USER_RFIDs[user]
print ("\nWelcome, " + user + "\n")
targettemp=input("Please enter the target temp in C, or scan any key for 48C.\n")
if not targettemp.isdigit():
targettemp=48
templo=int(targettemp)-0.25
temphi=int(targettemp)+0.25
print ("\n\nProgram started, user is " + user + " target temp range: (" + str(templo) + " - " + str(temphi) + ")\n" )
print ("Data are saved in " + datafile+"\n")
print ("Please test the wire.")
ratid="00fbtest"
def save_data(fname, line):
with open(fname, "a") as f:
f.write(line)
print("data saved to " + fname)
while True:
time.sleep(0.01)
tail_out= GPIO.input(Tail)
if (tail_out==True):
temp1=read_temp()
if (temp1>temphi):
print ("Temp: " + str(temp1) + " Too hot!!")
elif (temp1<templo):
print ("Temp: " + str(temp1) + " Too cold!!")
elif ratid[0:4] != "00fb":
print ("Temp: " + str(temp1) + " please test " + ratid)
else:
print ("Temp: " + str(temp1) + "!!! "+ ratid +" is not a rat ID. ")
else:
sTime=time.time()
print ("Timer started\n")
while (tail_out==False):
tail_out= GPIO.input(Tail)
time.sleep(0.02)
elapsed=time.time()-sTime + 0.4 # calibrated at 2s and 10s found this consistent system error
if (elapsed>10):
print ("Maximum latency of 10 s reached\n");
tail_out=True
elapsed=round(elapsed, 3)
temp2=read_temp()
temp=round((temp1+temp2)/2, 3)
now0=datetime.datetime.now()
now=now0.strftime("%Y-%m-%d\t%H:%M")
line=ratid+"\t" + now + "\t"+ str(elapsed) + "\t"+ str(temp) + "\t" + str(user) + "\t" + spin_speed + "\n"
if (ratid in latency.keys()):
latency[ratid] += str(elapsed) + ", "
else:
latency[ratid] = str(elapsed) + ", "
print ("Rat is "+ ratid+", latency = "+ latency[ratid])
ind_latency = latency[ratid].split(", ")
if (len(ind_latency) == 2) and (abs(float(ind_latency[0]) - float(ind_latency[1])) > 1):
print("the latency difference between the first and second test is greater than 1 seconds. please test the same rat again.")
force_test = True
if force_test:
next_id = ""
else:
next_id=input("Choose one of the following:\n \"n\" for new rat,\n\"d\" to delete this trial,\n\"a\" to test the current rat again\n\"e\" to end the run\n")[-4:]
## RFID equivalants
if next_id in COMMAND_RFIDs.keys():
next_id = COMMAND_RFIDs[next_id]
if (next_id =="d"):
print ("Data deleted as requested\n")
next_id="a" # then test the same rat again
savedata=0
if elapsed<1.5:
print ("!!! latency < 1.5 sec, Data not saved");
savedata=0
next_id="a"
if temp> temphi or temp<templo:
print ("!!! temperature not in target range, Data not saved")
next_id="a"
savedata=0
if ratid[0:4]=="00fb" or ratid[0:4]=="3200":
print ("!!! RFID is not a valid rat ID, Data not saved");
next_id="n"
savedata=0
if savedata:
save_data(datafile, line)
if (next_id=="e"):
print ("Exit prgram\n")
sys.exit()
savedata=1
print ("Please wait 10 seconds\n")
time.sleep(10)
if (next_id=="n"):
ratid=input("Please enter new rat ID\n")
else:
print ("Please test the same rat again\n")