forked from Ulm-IQO/qudi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ni_x_series_in_streamer.py
1158 lines (1002 loc) · 49.5 KB
/
ni_x_series_in_streamer.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
"""
This file contains the qudi hardware module to use a National Instruments X-series card as mixed
signal input data streamer.
Qudi is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Qudi is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Qudi. If not, see <http://www.gnu.org/licenses/>.
Copyright (c) the Qudi Developers. See the COPYRIGHT.txt file at the
top-level directory of this distribution and at <https://github.com/Ulm-IQO/qudi/>
"""
import copy
import numpy as np
import ctypes
import time
import nidaqmx as ni
from nidaqmx._lib import lib_importer # Due to NIDAQmx C-API bug needed to bypass property getter
from nidaqmx.stream_readers import AnalogMultiChannelReader, CounterReader
from core.module import Base
from core.configoption import ConfigOption
from core.util.helpers import natural_sort
from interface.data_instream_interface import DataInStreamInterface, DataInStreamConstraints
from interface.data_instream_interface import StreamingMode, StreamChannelType, StreamChannel
class NIXSeriesInStreamer(Base, DataInStreamInterface):
"""
A National Instruments device that can detect and count digital pulses and measure analog
voltages as data stream.
!!!!!! NI USB 63XX, NI PCIe 63XX and NI PXIe 63XX DEVICES ONLY !!!!!!
See [National Instruments X Series Documentation](@ref nidaq-x-series) for details.
Example config for copy-paste:
nicard_6343_instreamer:
module.Class: 'ni_x_series_in_streamer.NIXSeriesInStreamer'
device_name: 'Dev1'
digital_sources: # optional
- 'PFI15'
analog_sources: # optional
- 'ai0'
- 'ai1'
# external_sample_clock_source: 'PFI0' # optional
# external_sample_clock_frequency: 1000 # optional
adc_voltage_range: [-10, 10] # optional
max_channel_samples_buffer: 10000000 # optional
read_write_timeout: 10 # optional
"""
# config options
_device_name = ConfigOption(name='device_name', default='Dev1', missing='warn')
_digital_sources = ConfigOption(name='digital_sources', default=tuple(), missing='info')
_analog_sources = ConfigOption(name='analog_sources', default=tuple(), missing='info')
_external_sample_clock_source = ConfigOption(
name='external_sample_clock_source', default=None, missing='nothing')
_external_sample_clock_frequency = ConfigOption(
name='external_sample_clock_frequency', default=None, missing='nothing')
_adc_voltage_range = ConfigOption('adc_voltage_range', default=(-10, 10), missing='info')
_max_channel_samples_buffer = ConfigOption(
'max_channel_samples_buffer', default=25e6, missing='info')
_rw_timeout = ConfigOption('read_write_timeout', default=10, missing='nothing')
# Hardcoded data type
__data_type = np.float64
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# NIDAQmx device handle
self._device_handle = None
# Task handles for NIDAQmx tasks
self._di_task_handles = list()
self._ai_task_handle = None
self._clk_task_handle = None
# nidaqmx stream reader instances to help with data acquisition
self._di_readers = list()
self._ai_reader = None
# Internal settings
self.__sample_rate = -1.0
self.__stream_length = -1
self.__buffer_size = -1
self.__use_circular_buffer = False
self.__streaming_mode = None
# Data buffer
self._data_buffer = np.empty(0, dtype=self.__data_type)
self._has_overflown = False
# List of all available counters and terminals for this device
self.__all_counters = tuple()
self.__all_digital_terminals = tuple()
self.__all_analog_terminals = tuple()
# currently active channels
self.__active_channels = tuple()
# Stored hardware constraints
self._constraints = None
return
def on_activate(self):
"""
Starts up the NI-card and performs sanity checks.
"""
# Check if device is connected and set device to use
dev_names = ni.system.System().devices.device_names
if self._device_name.lower() not in set(dev.lower() for dev in dev_names):
raise Exception('Device name "{0}" not found in list of connected devices: {1}\n'
'Activation of NIXSeriesInStreamer failed!'
''.format(self._device_name, dev_names))
for dev in dev_names:
if dev.lower() == self._device_name.lower():
self._device_name = dev
break
self._device_handle = ni.system.Device(self._device_name)
self.__all_counters = tuple(
ctr.split('/')[-1] for ctr in self._device_handle.co_physical_chans.channel_names if
'ctr' in ctr.lower())
self.__all_digital_terminals = tuple(
term.rsplit('/', 1)[-1].lower() for term in self._device_handle.terminals if 'PFI' in term)
self.__all_analog_terminals = tuple(
term.rsplit('/', 1)[-1].lower() for term in self._device_handle.ai_physical_chans.channel_names)
# Check digital input terminals
if self._digital_sources:
source_set = set(self._extract_terminal(src) for src in self._digital_sources)
invalid_sources = source_set.difference(set(self.__all_digital_terminals))
if invalid_sources:
self.log.error(
'Invalid digital source terminals encountered. Following sources will '
'be ignored:\n {0}\nValid digital input terminals are:\n {1}'
''.format(', '.join(natural_sort(invalid_sources)),
', '.join(self.__all_digital_terminals)))
self._digital_sources = natural_sort(source_set.difference(invalid_sources))
# Check analog input channels
if self._analog_sources:
source_set = set(self._extract_terminal(src) for src in self._analog_sources)
invalid_sources = source_set.difference(set(self.__all_analog_terminals))
if invalid_sources:
self.log.error('Invalid analog source channels encountered. Following sources will '
'be ignored:\n {0}\nValid analog input channels are:\n {1}'
''.format(', '.join(natural_sort(invalid_sources)),
', '.join(self.__all_analog_terminals)))
self._analog_sources = natural_sort(source_set.difference(invalid_sources))
# Check if all input channels fit in the device
if len(self._digital_sources) > 3:
raise Exception(
'Too many digital channels specified. Maximum number of digital channels is 3.')
if len(self._analog_sources) > 16:
raise Exception(
'Too many analog channels specified. Maximum number of analog channels is 16.')
# Check if there are any valid input channels left
if not self._analog_sources and not self._digital_sources:
raise Exception('No valid analog or digital sources defined in config. '
'Activation of NIXSeriesInStreamer failed!')
# Create constraints
self._constraints = DataInStreamConstraints()
self._constraints.digital_channels = tuple(
StreamChannel(name=src,
type=StreamChannelType.DIGITAL,
unit='counts') for src in self._digital_sources)
self._constraints.analog_channels = tuple(
StreamChannel(name=src,
type=StreamChannelType.ANALOG,
unit='V') for src in self._analog_sources)
self._constraints.analog_sample_rate.min = self._device_handle.ai_min_rate
self._constraints.analog_sample_rate.max = self._device_handle.ai_max_multi_chan_rate
self._constraints.analog_sample_rate.step = 1
self._constraints.analog_sample_rate.unit = 'Hz'
# FIXME: What is the minimum frequency for the digital counter timebase?
self._constraints.digital_sample_rate.min = 0.1
self._constraints.digital_sample_rate.max = self._device_handle.ci_max_timebase
self._constraints.digital_sample_rate.step = 0.1
self._constraints.digital_sample_rate.unit = 'Hz'
self._constraints.combined_sample_rate = self._constraints.analog_sample_rate
self._constraints.read_block_size.min = 1
self._constraints.read_block_size.max = int(self._max_channel_samples_buffer)
self._constraints.read_block_size.step = 1
# TODO: Implement FINITE streaming mode
self._constraints.streaming_modes = (StreamingMode.CONTINUOUS,) # , StreamingMode.FINITE)
self._constraints.data_type = np.float64
self._constraints.allow_circular_buffer = True
# Check external sample clock source
if self._external_sample_clock_source is not None:
new_name = self._extract_terminal(self._external_sample_clock_source)
if new_name in self.__all_digital_terminals:
self._external_sample_clock_source = new_name
else:
self.log.error('No valid source terminal found for external_sample_clock_source '
'"{0}". Falling back to internal sampling clock.'
''.format(self._external_sample_clock_source))
self._external_sample_clock_source = None
# Check external sample clock frequency
if self._external_sample_clock_source is None:
self._external_sample_clock_frequency = None
elif self._external_sample_clock_frequency is None:
self.log.error('External sample clock source supplied but no clock frequency. '
'Falling back to internal clock instead.')
self._external_sample_clock_source = None
elif not self._clk_frequency_valid(self._external_sample_clock_frequency):
if self._analog_sources:
self.log.error('External sample clock frequency requested ({0:.3e}Hz) is out of '
'bounds. Please choose a value between {1:.3e}Hz and {2:.3e}Hz.'
' Value will be clipped to the closest boundary.'
''.format(self._external_sample_clock_frequency,
self._constraints.combined_sample_rate.min,
self._constraints.combined_sample_rate.max))
self._external_sample_clock_frequency = min(
self._external_sample_clock_frequency,
self._constraints.combined_sample_rate.max)
self._external_sample_clock_frequency = max(
self._external_sample_clock_frequency,
self._constraints.combined_sample_rate.min)
else:
self.log.error('External sample clock frequency requested ({0:.3e}Hz) is out of '
'bounds. Please choose a value between {1:.3e}Hz and {2:.3e}Hz.'
' Value will be clipped to the closest boundary.'
''.format(self._external_sample_clock_frequency,
self._constraints.digital_sample_rate.min,
self._constraints.digital_sample_rate.max))
self._external_sample_clock_frequency = min(
self._external_sample_clock_frequency,
self._constraints.digital_sample_rate.max)
self._external_sample_clock_frequency = max(
self._external_sample_clock_frequency,
self._constraints.digital_sample_rate.min)
self.terminate_all_tasks()
if self._external_sample_clock_frequency is not None:
self.__sample_rate = float(self._external_sample_clock_frequency)
else:
self.__sample_rate = self._constraints.combined_sample_rate.min
self.__data_type = np.float64
self.__stream_length = -1
self.__buffer_size = max(self._max_channel_samples_buffer, 1000000)
self.__use_circular_buffer = False
self.__streaming_mode = StreamingMode.CONTINUOUS
self.__active_channels = tuple()
# Reset data buffer
self._data_buffer = np.empty(0, dtype=self.__data_type)
self._has_overflown = False
return
def on_deactivate(self):
""" Shut down the NI card.
"""
self.terminate_all_tasks()
# Free memory if possible while module is inactive
self._data_buffer = np.empty(0, dtype=self.__data_type)
return
@property
def sample_rate(self):
"""
The currently set sample rate
@return float: current sample rate in Hz
"""
return self.__sample_rate
@sample_rate.setter
def sample_rate(self, rate):
if self._check_settings_change():
if not self._clk_frequency_valid(rate):
if self._analog_sources:
min_val = self._constraints.combined_sample_rate.min
max_val = self._constraints.combined_sample_rate.max
else:
min_val = self._constraints.digital_sample_rate.min
max_val = self._constraints.digital_sample_rate.max
self.log.warning(
'Sample rate requested ({0:.3e}Hz) is out of bounds. Please choose '
'a value between {1:.3e}Hz and {2:.3e}Hz. Value will be clipped to '
'the closest boundary.'.format(rate, min_val, max_val))
rate = max(min(max_val, rate), min_val)
self.__sample_rate = float(rate)
return
@property
def data_type(self):
"""
Read-only property to return the currently set data type
@return type: current data type
"""
return self.__data_type
@property
def buffer_size(self):
"""
The currently set buffer size.
Buffer size corresponds to the number of samples per channel that can be buffered. So the
actual buffer size in bytes can be estimated by:
buffer_size * number_of_channels * size_in_bytes(data_type)
@return int: current buffer size in samples per channel
"""
return self.__buffer_size
@buffer_size.setter
def buffer_size(self, size):
if self._check_settings_change():
size = int(size)
if size > self._max_channel_samples_buffer:
self.log.error('buffer_size to set ({0}) is larger than maximum allowed buffer '
'size of {1:d} samples per channel.'
''.format(size, self._max_channel_samples_buffer))
return
elif size < 1:
self.log.error('Buffer size smaller than 1 makes no sense. Tried to set {0} as '
'buffer size and failed.'.format(size))
return
self.__buffer_size = int(size)
self._init_buffer()
return
@property
def use_circular_buffer(self):
"""
A flag indicating if circular sample buffering is being used or not.
@return bool: indicate if circular sample buffering is used (True) or not (False)
"""
return self.__use_circular_buffer
@use_circular_buffer.setter
def use_circular_buffer(self, flag):
if self._check_settings_change():
if flag and not self._constraints.allow_circular_buffer:
self.log.error('Circular buffer not allowed for this hardware module.')
return
self.__use_circular_buffer = bool(flag)
return
@property
def streaming_mode(self):
"""
The currently configured streaming mode Enum.
@return StreamingMode: Finite (StreamingMode.FINITE) or continuous
(StreamingMode.CONTINUOUS) data acquisition
"""
return self.__streaming_mode
@streaming_mode.setter
def streaming_mode(self, mode):
if self._check_settings_change():
mode = StreamingMode(mode)
if mode not in self._constraints.streaming_modes:
self.log.error('Unknown streaming mode "{0}" encountered.\nValid modes are: {1}.'
''.format(mode, self._constraints.streaming_modes))
return
self.__streaming_mode = mode
return
@property
def number_of_channels(self):
"""
Read-only property to return the currently configured number of data channels.
@return int: the currently set number of channels
"""
return len(self.__active_channels)
@property
def active_channels(self):
"""
The currently configured data channel properties.
Returns a dict with channel names as keys and corresponding StreamChannel instances as
values.
@return dict: currently active data channel properties with keys being the channel names
and values being the corresponding StreamChannel instances.
"""
constr = self._constraints
return(*(ch.copy() for ch in constr.digital_channels if ch.name in self.__active_channels),
*(ch.copy() for ch in constr.analog_channels if ch.name in self.__active_channels))
@active_channels.setter
def active_channels(self, channels):
if self._check_settings_change():
avail_channels = tuple(ch.name for ch in self.available_channels)
if any(ch not in avail_channels for ch in channels):
self.log.error('Invalid channel to stream from encountered ({0}).\nValid channels '
'are: {1}'
''.format(tuple(channels), tuple(self.available_channels)))
return
self.__active_channels = tuple(channels)
return
@property
def available_channels(self):
"""
Read-only property to return the currently used data channel properties.
Returns a dict with channel names as keys and corresponding StreamChannel instances as
values.
@return tuple: data channel properties for all available channels with keys being the
channel names and values being the corresponding StreamChannel instances.
"""
return (*(ch.copy() for ch in self._constraints.digital_channels),
*(ch.copy() for ch in self._constraints.analog_channels))
@property
def available_samples(self):
"""
Read-only property to return the currently available number of samples per channel ready
to read from buffer.
@return int: Number of available samples per channel
"""
if not self.is_running:
return 0
if self._ai_task_handle is None:
# avail_samples = self._di_task_handles[0].in_stream.total_samp_per_chan_acquired - \
# self._di_task_handles[0].in_stream.curr_read_pos
return self._di_task_handles[0].in_stream.avail_samp_per_chan
else:
# avail_samples = self._ai_task_handle.in_stream.total_samp_per_chan_acquired - \
# self._ai_task_handle.in_stream.curr_read_pos
return self._ai_task_handle.in_stream.avail_samp_per_chan
@property
def stream_length(self):
"""
Property holding the total number of samples per channel to be acquired by this stream.
This number is only relevant if the streaming mode is set to StreamingMode.FINITE.
@return int: The number of samples to acquire per channel. Ignored for continuous streaming.
"""
return self.__stream_length
@stream_length.setter
def stream_length(self, length):
if self._check_settings_change():
length = int(length)
if length < 1:
self.log.error('Stream_length must be a positive integer >= 1.')
return
self.__stream_length = length
return
@property
def is_running(self):
"""
Read-only flag indicating if the data acquisition is running.
@return bool: Data acquisition is running (True) or not (False)
"""
return self._ai_reader is not None or self._di_readers
@property
def buffer_overflown(self):
"""
Read-only flag to check if the read buffer has overflown.
In case of a circular buffer it indicates data loss.
In case of a non-circular buffer the data acquisition should have stopped if this flag is
coming up.
Flag will only be reset after starting a new data acquisition.
@return bool: Flag indicates if buffer has overflown (True) or not (False)
"""
return self._has_overflown
@property
def all_settings(self):
"""
Read-only property to return a dict containing all current settings and values that can be
configured using the method "configure". Basically returns the same as "configure".
@return dict: Dictionary containing all configurable settings
"""
return {'sample_rate': self.__sample_rate,
'streaming_mode': self.__streaming_mode,
'active_channels': self.active_channels,
'stream_length': self.__stream_length,
'buffer_size': self.__buffer_size,
'use_circular_buffer': self.__use_circular_buffer}
def configure(self, sample_rate=None, streaming_mode=None, active_channels=None,
stream_length=None, buffer_size=None, use_circular_buffer=None):
"""
Method to configure all possible settings of the data input stream.
@param float sample_rate: The sample rate in Hz at which data points are acquired
@param StreamingMode streaming_mode: The streaming mode to use (finite or continuous)
@param iterable active_channels: Iterable of channel names (str) to be read from.
@param int stream_length: In case of a finite data stream, the total number of
samples to read per channel
@param int buffer_size: The size of the data buffer to pre-allocate in samples per channel
@param bool use_circular_buffer: Use circular buffering (True) or stop upon buffer overflow
(False)
@return dict: All current settings in a dict. Keywords are the same as kwarg names.
"""
if self._check_settings_change():
# Handle sample rate change
if sample_rate is not None:
self.sample_rate = sample_rate
# Handle streaming mode change
if streaming_mode is not None:
self.streaming_mode = streaming_mode
# Handle active channels
if active_channels is not None:
self.active_channels = active_channels
# Handle total number of samples
if stream_length is not None:
self.stream_length = stream_length
# Handle buffer size
if buffer_size is not None:
self.buffer_size = buffer_size
# Handle circular buffer flag
if use_circular_buffer is not None:
self.use_circular_buffer = use_circular_buffer
return self.all_settings
def get_constraints(self):
"""
Return the constraints on the settings for this data streamer.
@return DataInStreamConstraints: Instance of DataInStreamConstraints containing constraints
"""
return self._constraints.copy()
def start_stream(self):
"""
Start the data acquisition and data stream.
@return int: error code (0: OK, -1: Error)
"""
if self.is_running:
self.log.warning('Unable to start input stream. It is already running.')
return 0
if (self._init_sample_clock() + self._init_digital_tasks() + self._init_analog_task()) != 0:
return -1
self._init_buffer()
try:
self._clk_task_handle.start()
except ni.DaqError:
self.log.exception('Error while starting sample clock task.')
self.terminate_all_tasks()
return -1
if self._ai_task_handle is not None:
try:
self._ai_task_handle.start()
except ni.DaqError:
self.log.exception('Error while starting analog input task.')
self.terminate_all_tasks()
return -1
try:
for task in self._di_task_handles:
task.start()
except ni.DaqError:
self.log.exception('Error while starting digital counter tasks.')
self.terminate_all_tasks()
return -1
return 0
def stop_stream(self):
"""
Stop the data acquisition and data stream.
@return int: error code (0: OK, -1: Error)
"""
if self.is_running:
self.terminate_all_tasks()
return 0
def read_data_into_buffer(self, buffer, number_of_samples=None):
"""
Read data from the stream buffer into a 1D/2D numpy array given as parameter.
In case of a single data channel the numpy array can be either 1D or 2D. In case of more
channels the array must be 2D with the first index corresponding to the channel number and
the second index serving as sample index:
buffer.shape == (self.number_of_channels, number_of_samples)
The numpy array must have the same data type as self.data_type.
If number_of_samples is omitted it will be derived from buffer.shape[1]
This method will not return until all requested samples have been read or a timeout occurs.
@param numpy.ndarray buffer: The numpy array to write the samples to
@param int number_of_samples: optional, number of samples to read per channel. If omitted,
this number will be derived from buffer axis 1 size.
@return int: Number of samples per channel read into buffer; negative value indicates error
(e.g. read timeout)
"""
if not self.is_running:
self.log.error('Unable to read data. Device is not running.')
return -1
if not isinstance(buffer, np.ndarray) or buffer.dtype != self.__data_type:
self.log.error('buffer must be numpy.ndarray with dtype {0}. Read failed.'
''.format(self.__data_type))
return -1
if buffer.ndim == 2:
if buffer.shape[0] != self.number_of_channels:
self.log.error('Configured number of channels ({0:d}) does not match first '
'dimension of 2D buffer array ({1:d}).'
''.format(self.number_of_channels, buffer.shape[0]))
return -1
number_of_samples = buffer.shape[1] if number_of_samples is None else number_of_samples
buffer = buffer.flatten()
elif buffer.ndim == 1:
if number_of_samples is None:
number_of_samples = buffer.size // self.number_of_channels
else:
self.log.error('Buffer must be a 1D or 2D numpy.ndarray.')
return -1
if number_of_samples < 1:
return 0
# Check for buffer overflow
if self.available_samples > self.buffer_size:
self._has_overflown = True
try:
write_offset = 0
# Read digital channels
for i, reader in enumerate(self._di_readers):
# read the counter value. This function is blocking.
read_samples = reader.read_many_sample_double(
buffer[write_offset:],
number_of_samples_per_channel=number_of_samples,
timeout=self._rw_timeout)
if read_samples != number_of_samples:
return -1
write_offset += number_of_samples
# Read analog channels
if self._ai_reader is not None:
read_samples = self._ai_reader.read_many_sample(
buffer[write_offset:],
number_of_samples_per_channel=number_of_samples,
timeout=self._rw_timeout)
if read_samples != number_of_samples:
return -1
except ni.DaqError:
self.log.exception('Getting samples from streamer failed.')
return -1
return read_samples
def read_available_data_into_buffer(self, buffer):
"""
Read data from the stream buffer into a 1D/2D numpy array given as parameter.
In case of a single data channel the numpy array can be either 1D or 2D. In case of more
channels the array must be 2D with the first index corresponding to the channel number and
the second index serving as sample index:
buffer.shape == (self.number_of_channels, number_of_samples)
The numpy array must have the same data type as self.data_type.
This method will read all currently available samples into buffer. If number of available
samples exceed buffer size, read only as many samples as fit into the buffer.
@param numpy.ndarray buffer: The numpy array to write the samples to
@return int: Number of samples per channel read into buffer; negative value indicates error
(e.g. read timeout)
"""
avail_samples = min(buffer.size // self.number_of_channels, self.available_samples)
return self.read_data_into_buffer(buffer=buffer, number_of_samples=avail_samples)
def read_data(self, number_of_samples=None):
"""
Read data from the stream buffer into a 2D numpy array and return it.
The arrays first index corresponds to the channel number and the second index serves as
sample index:
return_array.shape == (self.number_of_channels, number_of_samples)
The numpy arrays data type is the one defined in self.data_type.
If number_of_samples is omitted all currently available samples are read from buffer.
This method will not return until all requested samples have been read or a timeout occurs.
@param int number_of_samples: optional, number of samples to read per channel. If omitted,
all available samples are read from buffer.
@return numpy.ndarray: The read samples
"""
if not self.is_running:
self.log.error('Unable to read data. Device is not running.')
return np.empty((0, 0), dtype=self.__data_type)
if number_of_samples is None:
read_samples = self.read_available_data_into_buffer(self._data_buffer)
if read_samples < 0:
return np.empty((0, 0), dtype=self.__data_type)
else:
read_samples = self.read_data_into_buffer(self._data_buffer,
number_of_samples=number_of_samples)
if read_samples != number_of_samples:
return np.empty((0, 0), dtype=self.__data_type)
total_samples = self.number_of_channels * read_samples
return self._data_buffer[:total_samples].reshape((self.number_of_channels,
number_of_samples))
def read_single_point(self):
"""
This method will initiate a single sample read on each configured data channel.
In general this sample may not be acquired simultaneous for all channels and timing in
general can not be assured. Us this method if you want to have a non-timing-critical
snapshot of your current data channel input.
May not be available for all devices.
The returned 1D numpy array will contain one sample for each channel.
@return numpy.ndarray: 1D array containing one sample for each channel. Empty array
indicates error.
"""
if not self.is_running:
self.log.error('Unable to read data. Device is not running.')
return np.empty(0, dtype=self.__data_type)
try:
# Read digital channels
for i, reader in enumerate(self._di_readers):
# read the counter value. This function is blocking.
if self.__data_type == np.float64:
self._data_buffer[i] = reader.read_one_sample_double(timeout=self._rw_timeout)
else:
self._data_buffer[i] = reader.read_one_sample_uint32(timeout=self._rw_timeout)
# Read analog channels
if self._ai_reader is not None:
self._ai_reader.read_one_sample(self._data_buffer[len(self._di_readers):],
timeout=self._rw_timeout)
except ni.DaqError:
self.log.exception('Getting samples from data stream failed.')
return np.empty(0, dtype=self.__data_type)
return self._data_buffer[:self.number_of_channels]
# =============================================================================================
def _init_sample_clock(self):
"""
If no external clock is given, configures a counter to provide the sample clock for all
channels.
@return int: error code (0: OK, -1: Error)
"""
# Return if sample clock is externally supplied
if self._external_sample_clock_source is not None:
return 0
if self._clk_task_handle is not None:
self.log.error('Sample clock task is already running. Unable to set up a new clock '
'before you close the previous one.')
return -1
# Try to find an available counter
for src in self.__all_counters:
# Check if task by that name already exists
task_name = 'SampleClock_{0:d}'.format(id(self))
try:
task = ni.Task(task_name)
except ni.DaqError:
self.log.exception('Could not create task with name "{0}".'.format(task_name))
return -1
# Try to configure the task
try:
task.co_channels.add_co_pulse_chan_freq(
'/{0}/{1}'.format(self._device_name, src),
freq=self.__sample_rate,
idle_state=ni.constants.Level.LOW)
task.timing.cfg_implicit_timing(
sample_mode=ni.constants.AcquisitionType.CONTINUOUS)
except ni.DaqError:
self.log.exception('Error while configuring sample clock task.')
try:
del task
except NameError:
pass
return -1
# Try to reserve resources for the task
try:
task.control(ni.constants.TaskMode.TASK_RESERVE)
except ni.DaqError:
# Try to clean up task handle
try:
task.close()
except ni.DaqError:
pass
try:
del task
except NameError:
pass
# Return if no counter could be reserved
if src == self.__all_counters[-1]:
self.log.exception('Error while setting up clock. Probably because no free '
'counter resource could be reserved.')
return -1
continue
break
self._clk_task_handle = task
return 0
def _init_digital_tasks(self):
"""
Set up tasks for digital event counting.
@return int: error code (0:OK, -1:error)
"""
digital_channels = tuple(
ch.name for ch in self.active_channels if ch.type == StreamChannelType.DIGITAL)
if not digital_channels:
return 0
if self._di_task_handles:
self.log.error('Digital counting tasks have already been generated. '
'Setting up counter tasks has failed.')
self.terminate_all_tasks()
return -1
if self._clk_task_handle is None and self._external_sample_clock_source is None:
self.log.error(
'No sample clock task has been generated and no external clock source specified. '
'Unable to create digital counting tasks.')
self.terminate_all_tasks()
return -1
if self._external_sample_clock_source:
clock_channel = '/{0}/{1}'.format(self._device_name, self._external_sample_clock_source)
sample_freq = float(self._external_sample_clock_frequency)
else:
clock_channel = '/{0}InternalOutput'.format(self._clk_task_handle.channel_names[0])
sample_freq = float(self._clk_task_handle.co_channels.all.co_pulse_freq)
# Set up digital counting tasks
for i, chnl in enumerate(digital_channels):
chnl_name = '/{0}/{1}'.format(self._device_name, chnl)
task_name = 'PeriodCounter_{0}'.format(chnl)
# Try to find available counter
for ctr in self.__all_counters:
ctr_name = '/{0}/{1}'.format(self._device_name, ctr)
try:
task = ni.Task(task_name)
except ni.DaqError:
self.log.error('Could not create task with name "{0}"'.format(task_name))
self.terminate_all_tasks()
return -1
try:
task.ci_channels.add_ci_period_chan(
ctr_name,
min_val=0,
max_val=100000000,
units=ni.constants.TimeUnits.TICKS,
edge=ni.constants.Edge.RISING)
# NOTE: The following two direct calls to C-function wrappers are a
# workaround due to a bug in some NIDAQmx.lib property getters. If one of
# these getters is called, it will mess up the task timing.
# This behaviour has been confirmed using pure C code.
# nidaqmx will call these getters and so the C function is called directly.
try:
lib_importer.windll.DAQmxSetCIPeriodTerm(
task._handle,
ctypes.c_char_p(ctr_name.encode('ascii')),
ctypes.c_char_p(clock_channel.encode('ascii')))
lib_importer.windll.DAQmxSetCICtrTimebaseSrc(
task._handle,
ctypes.c_char_p(ctr_name.encode('ascii')),
ctypes.c_char_p(chnl_name.encode('ascii')))
except:
lib_importer.cdll.DAQmxSetCIPeriodTerm(
task._handle,
ctypes.c_char_p(ctr_name.encode('ascii')),
ctypes.c_char_p(clock_channel.encode('ascii')))
lib_importer.cdll.DAQmxSetCICtrTimebaseSrc(
task._handle,
ctypes.c_char_p(ctr_name.encode('ascii')),
ctypes.c_char_p(chnl_name.encode('ascii')))
task.timing.cfg_implicit_timing(
sample_mode=ni.constants.AcquisitionType.CONTINUOUS,
samps_per_chan=self.__buffer_size)
except ni.DaqError:
try:
del task
except NameError:
pass
self.terminate_all_tasks()
self.log.exception('Something went wrong while configuring digital counter '
'task for channel "{0}".'.format(chnl))
return -1
try:
task.control(ni.constants.TaskMode.TASK_RESERVE)
except ni.DaqError:
try:
task.close()
except ni.DaqError:
self.log.exception('Unable to close task.')
try:
del task
except NameError:
self.log.exception('Some weird namespace voodoo happened here...')
if ctr == self.__all_counters[-1]:
self.log.exception('Unable to reserve resources for digital counting task '
'of channel "{0}". No available counter found!'
''.format(chnl))
self.terminate_all_tasks()
return -1
continue
try:
self._di_readers.append(CounterReader(task.in_stream))
self._di_readers[-1].verify_array_shape = False
except ni.DaqError:
self.log.exception(
'Something went wrong while setting up the digital counter reader for '
'channel "{0}".'.format(chnl))
self.terminate_all_tasks()
try:
task.close()
except ni.DaqError:
self.log.exception('Unable to close task.')
try:
del task
except NameError:
self.log.exception('Some weird namespace voodoo happened here...')
return -1
self._di_task_handles.append(task)
break
return 0
def _init_analog_task(self):
"""
Set up task for analog voltage measurement.
@return int: error code (0:OK, -1:error)
"""
analog_channels = tuple(
ch.name for ch in self.active_channels if ch.type == StreamChannelType.ANALOG)
if not analog_channels:
return 0
if self._ai_task_handle:
self.log.error(
'Analog input task has already been generated. Unable to set up analog in task.')
self.terminate_all_tasks()
return -1
if self._clk_task_handle is None and self._external_sample_clock_source is None:
self.log.error(
'No sample clock task has been generated and no external clock source specified. '
'Unable to create analog voltage measurement tasks.')
self.terminate_all_tasks()
return -1
if self._external_sample_clock_source:
clock_channel = '/{0}/{1}'.format(self._device_name, self._external_sample_clock_source)
sample_freq = float(self._external_sample_clock_frequency)
else:
clock_channel = '/{0}InternalOutput'.format(self._clk_task_handle.channel_names[0])
sample_freq = float(self._clk_task_handle.co_channels.all.co_pulse_freq)
# Set up analog input task
task_name = 'AnalogIn_{0:d}'.format(id(self))