-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlj.py
146 lines (123 loc) · 5.93 KB
/
lj.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
import sys
from datetime import datetime
import sqlite3 as sql
import time
import u3
import utilities as ut
#--------This module connects to the Labjack and records the data to SQLite-----------------------------------
def labjack():
conn = sql.connect("labjackdb.db")
c = conn.cursor()
with conn:
c.execute("SELECT run_id FROM run_number ORDER BY run_id DESC LIMIT 1")
lastid = c.fetchone()[0]
c.execute("DELETE FROM temp_readings")
c.execute("SELECT * FROM preferences ") # Get current preferences
prefs = c.fetchall()[0]
max_requests = prefs[2]
scan_frequency = prefs[3]
factor0 = prefs[5]
factor1 = prefs[6]
factor2 = prefs[7]
factor3 = prefs[8]
factor4 = prefs[9]
factor5 = prefs[10]
factor6 = prefs[11]
max5 = prefs[25]
max6 = prefs[26]
state = ut.if_recording(c)
d = None
d = u3.U3()
d.configU3()
d.getCalibrationData()
d.configIO(
EnableCounter0 = True,
FIOAnalog = 15,
TimerCounterPinOffset=4
)
d.streamConfig(
NumChannels = 6,
InternalStreamClockFrequency = 0,
Resolution = 3,
ScanInterval = 1,
PChannels = [0,1,2,3,210,224],
NChannels = [31,31,32,31,31,31],
ScanFrequency = scan_frequency,
)
time_stamp = datetime.now()
try:
d.streamStop()
except:
print('error on streamStop')
while True:
try:
d.streamStart()
for r in d.streamData(): # I assume r stands for raw data
if r is not None: # basically prints an error if there is no raw data
#---------------------------------------------------------------------#
#--- The following only runs once when the recording state changes ---#
#---------------------------------------------------------------------#
if state != ut.if_recording(c):
d.streamStop()
d.configU3()
d.getCalibrationData()
d.configIO(
EnableCounter0 = True,
FIOAnalog = 15,
TimerCounterPinOffset=4
)
d.streamConfig(
NumChannels = 6,
InternalStreamClockFrequency = 0,
Resolution = 3,
ScanInterval = 1,
PChannels = [0,1,2,3,210,224],
NChannels = [31,31,32,31,31,31],
ScanFrequency = scan_frequency,
)
d.streamStart()
with conn:
c.execute("DELETE FROM temp_readings")
c.execute("SELECT run_id FROM run_number ORDER BY run_id DESC LIMIT 1")
lastid = c.fetchone()[0]
c.execute("SELECT * FROM preferences ") # Get current preferences
prefs = c.fetchall()[0]
max_requests = prefs[2]
scan_frequency = prefs[3]
factor0 = prefs[5]
factor1 = prefs[6]
factor2 = prefs[7]
factor3 = prefs[8]
factor4 = prefs[9]
factor5 = prefs[10]
factor6 = prefs[11]
max5 = prefs[25]
max6 = prefs[26]
state = ut.if_recording(c)
table_name = "dac_readings" if state == True else "temp_readings"
#-----------------------------------------------------------------------------------#
#-- The following updates readings with user calibrations #
#-----------------------------------------------------------------------------------#
time = int(datetime.now().strftime('%s%f'))
ain0 = (sum(r['AIN0'])/len(r['AIN0']) * factor0)
ain1 = (sum(r['AIN1'])/len(r['AIN1']) * factor1)
ain2 = (sum(r['AIN2'])/len(r['AIN2']) * factor2)
#ain3 = (sum(r['AIN3'])/len(r['AIN3']) * factor3)
ain3 = (10**(sum(r['AIN3'])/len(r['AIN3'])-6.125)*1000) # Edwards APGX-H Vacuum Gauge
counts32 = ((r['AIN224'][-1] << 16) + r['AIN210'][-1]) * factor4
with conn:
c.execute("SELECT s1, s2 FROM sliderpos")
s1, s2 = c.fetchone()
c.execute(f"""
INSERT INTO {table_name} (run_id, time, ain0, ain1, ain2, ain3, ain4, s1, s2)
VALUES ({1 if state == False else str(lastid)},{str(time)},{str(ain0)}, {str(ain1)}, {str(ain2)}, {str(ain3)}, {str(counts32)}, {str(s1)},{str(s2)})
""")
d.writeRegister(5000, (s1 / factor5)) # Analogue out slider calibration
d.writeRegister(5002, (s2 / factor6)) # Analogue out slider calibration
except Exception as e:
d.streamStop()
d.close()
print('Exception in LJ, program ended', e)
break
#while True:
#labjack()