-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfscv_daq.py
192 lines (150 loc) · 6.74 KB
/
fscv_daq.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
import time
import tables as tb
import numpy as np
import threading
try:
import nidaqmx
from nidaqmx.constants import AcquisitionType
REAL_DATA = True
except ModuleNotFoundError:
# If nidaqmx is not installed, random data is generated
REAL_DATA = False
class NIGrabber:
def __init__(self,
complevel = 5,
expectedrows = 500,
samples_per_scan = 1000,
rate = 100e3,
filename = 'datafile.h5',
):
self.samples_per_scan = samples_per_scan
self.rate = rate
self.n_scans_acquired = 0
self.running = False
self.delta_t_max = None
self.delta_t_min = None
self.delta_t = None
self.fileh = tb.open_file(filename, mode='w')
# Array for timestanps
self.array_ts = self.fileh.create_earray(self.fileh.root,
'array_ts',
tb.FloatAtom(),
(1, 0),
"Times",
expectedrows=expectedrows)
# Array for signal
filters = tb.Filters(complevel=complevel, complib='blosc')
self.array_scans = self.fileh.create_earray(self.fileh.root,
'array_scans',
tb.FloatAtom(),
(self.samples_per_scan, 0),
"Scans", filters=filters,
expectedrows=expectedrows)
# Array for command voltage
self.array_command = self.fileh.create_earray(self.fileh.root,
'array_command',
tb.FloatAtom(),
(self.samples_per_scan, 0),
"Command",
filters=filters,
expectedrows=expectedrows)
def callback(self, task_handle, every_n_samples_event_type,
number_of_samples, callback_data):
if REAL_DATA:
data = np.array(self.task.read(number_of_samples_per_channel=self.samples_per_scan))
else:
data = np.random.normal(size=(2, self.samples_per_scan))
data[1,:50] +=5
if not self.running:
return 0
t_now = time.perf_counter() - self.t0
# Append new data to data storage
self.array_ts.append(np.array([t_now])[np.newaxis])
self.array_command.append(data[0][:, np.newaxis])
self.array_scans.append(data[1][:, np.newaxis])
self.n_scans_acquired += 1
if self.n_scans_acquired > 1:
self.delta_t = t_now - self.lastUpdate
if self.n_scans_acquired == 2:
self.delta_t_max = self.delta_t
self.delta_t_min = self.delta_t
elif self.n_scans_acquired > 2:
if self.delta_t > self.delta_t_max:
self.delta_t_max = self.delta_t
if self.delta_t < self.delta_t_min:
self.delta_t_min = self.delta_t
self.lastUpdate = t_now
return 0
def start_grabbing(self):
self.running = True
self.n_scans_acquired = 0
if REAL_DATA:
self.task = nidaqmx.Task()
min_val = -10
max_val = 10
self.task.ai_channels.add_ai_voltage_chan("PXI1Slot4_2/ai0:1", min_val=min_val, max_val=max_val)
#self.task.timing.cfg_samp_clk_timing(1000, sample_mode=AcquisitionType.CONTINUOUS)
self.task.timing.cfg_samp_clk_timing(rate=self.rate,
sample_mode=AcquisitionType.FINITE,
samps_per_chan=self.samples_per_scan)
self.task.register_every_n_samples_acquired_into_buffer_event(
self.samples_per_scan, self.callback)
self.task.triggers.start_trigger.cfg_dig_edge_start_trig(
"/PXI1Slot4_2/PFI0")
self.task.triggers.start_trigger.retriggerable = True
else:
class phantom_data_task:
"""Single use phantom data generator."""
def __init__(self, callback_function, acuisition_period_sec=0.1):
self.callback_function = callback_function
self.acuisition_period_sec = acuisition_period_sec
self.generate = True
def acquire_random_data(self):
""" This continously calls the callback function """
while self.generate:
time.sleep(self.acuisition_period_sec)
if self.generate:
self.callback_function(None, None, None, None)
def close(self):
""" Stop calling the callback function """
self.generate=False
task_phantom = phantom_data_task(callback_function=self.callback,
acuisition_period_sec=self.samples_per_scan/self.rate)
phantom_thread = threading.Thread(target=task_phantom.acquire_random_data)
self.task_phantom = task_phantom
self.task = phantom_thread
# Save start time
self.array_ts.attrs['start_time'] = time.time()
self.array_ts.attrs['start_time_str'] = time.ctime()
self.t0 = time.perf_counter()
# Start grabbing
self.task.start()
def stop_grab(self):
self.running = False
this_filename = str(self.fileh.filename)
self.fileh.flush()
self.fileh.close()
print('saved: ', self.fileh.filename)
if REAL_DATA:
self.task.close()
else:
self.task_phantom.close()
return this_filename
class MyGui:
def __init__(self, grabber):
self.grabber = grabber
def doMeasurement(self):
for i in range(3):
print('Gui update', self.grabber.n_scans_acquired)
time.sleep(0.3)
print(self.grabber.delta_t_min)
print(self.grabber.delta_t)
print(self.grabber.delta_t_max)
self.grabber.stop_grab()
if __name__ == '__main__':
grabber = NIGrabber(samples_per_scan=1001, rate=200e3)
grab_thread = threading.Thread(target=grabber.start_grabbing)
grab_thread.start()
my_gui = MyGui(grabber)
gui_thread = threading.Thread(target=my_gui.doMeasurement)
gui_thread.start()