def __init__(self, channel, channel_name, gate_src, timebase_src, edge=Edge.RISING): self._thread = None self._stop = False self._channel = channel self._data = np.zeros(0) self.sample_readies = 0 self.enabled = True # Create the task self._task = Task('task' + channel_name) # Add pulse width channel self._counter = self._task.ci_channels.add_ci_pulse_width_chan( channel, channel_name, units=TimeUnits.TICKS, starting_edge=edge) # Configure timing and data transfer mechanims self._task.timing.samp_timing_type = SampleTimingType.IMPLICIT self._counter.ci_data_xfer_mech = \ DataTransferActiveTransferMode.INTERRUPT # Configure the source and the gate self._counter.ci_ctr_timebase_src = timebase_src self._counter.ci_pulse_width_term = gate_src # Configure reader. self._task.in_stream.read_all_avail_samp = True self._reader = CounterReader(self._task.in_stream)
def angularposition(samples): counter = 'Dev1/ctr4' source_trigger = '/Dev1/RTSI0' encoder_decoding = EncoderType.X_4 encoder_zidxphase = EncoderZIndexPhase.AHIGH_BHIGH angle_units = AngleUnits.TICKS with nidaqmx.Task() as task: # DAQmxCreateCIAngEncoderChan(taskHandle,"Dev1/ctr0","",DAQmx_Val_X4,0,0.0,DAQmx_Val_AHighBHigh,DAQmx_Val_Degrees,24,0.0,"") task.ci_channels.add_ci_ang_encoder_chan(counter, "", encoder_decoding, False, 0, encoder_zidxphase, angle_units, 24, 0.0, "") # DAQmxCfgSampClkTiming(taskHandle,"/Dev1/PFI9",1000.0,DAQmx_Val_Rising,DAQmx_Val_ContSamps,1000) task.timing.cfg_samp_clk_timing(1000, source_trigger, sample_mode=AcquisitionType.FINITE, samps_per_chan=samples) reader = CounterReader(task.in_stream) task.start() print("Continuously reading. Press Ctrl+C to interrupt\n") data = np.zeros(samples) i = 0 while True: # DAQmxReadCounterF64(taskHandle,1000,10.0,data,1000,&read,0) data[i] = reader.read_one_sample_double(timeout=-1) i += 1 if i == samples - 1: break print('Finish') print(data)
def test_one_sample_pulse_freq(self, x_series_device, seed): # Reset the pseudorandom number generator with seed. random.seed(seed) frequency = random.uniform(1000, 10000) duty_cycle = random.uniform(0.2, 0.8) # Select random counters from the device. counters = random.sample(self._get_device_counters(x_series_device), 2) with nidaqmx.Task() as write_task, nidaqmx.Task() as read_task: write_task.co_channels.add_co_pulse_chan_freq( counters[0], freq=frequency, duty_cycle=duty_cycle) write_task.timing.cfg_implicit_timing( sample_mode=AcquisitionType.CONTINUOUS) read_task.ci_channels.add_ci_pulse_chan_freq(counters[1], min_val=1000, max_val=10000) read_task.ci_channels.all.ci_pulse_freq_term = ( '/{0}InternalOutput'.format(counters[0])) read_task.start() write_task.start() reader = CounterReader(read_task.in_stream) value_read = reader.read_one_sample_pulse_frequency() write_task.stop() assert numpy.isclose(value_read.freq, frequency, rtol=0.05) assert numpy.isclose(value_read.duty_cycle, duty_cycle, rtol=0.05)
def test_one_sample_uint32(self, x_series_device, seed): # Reset the pseudorandom number generator with seed. random.seed(seed) number_of_pulses = random.randint(2, 50) frequency = random.uniform(1000, 10000) # Select random counters from the device. counters = random.sample(self._get_device_counters(x_series_device), 2) with nidaqmx.Task() as write_task, nidaqmx.Task() as read_task: write_task.co_channels.add_co_pulse_chan_freq(counters[0], freq=frequency) write_task.timing.cfg_implicit_timing( samps_per_chan=number_of_pulses) read_task.ci_channels.add_ci_count_edges_chan(counters[1]) read_task.ci_channels.all.ci_count_edges_term = ( '/{0}InternalOutput'.format(counters[0])) reader = CounterReader(read_task.in_stream) read_task.start() write_task.start() write_task.wait_until_done(timeout=2) value_read = reader.read_one_sample_uint32() assert value_read == number_of_pulses
class FrequencyTask(BaseDAQTask): name = 'Frequency In' def _add_channel_fired(self): chan = MeasureFrequencyChannel() self.channels.append(chan) def read_data(self, arr=None): if self.reader is None: from nidaqmx.stream_readers import CounterReader self.reader = CounterReader(self._task.in_stream) #number_of_channels = len(self.channels) nsamp = self.samp_per_chan if arr is None: data = np.empty((nsamp, ), dtype=np.float64) else: data = arr dcycles = np.empty((nsamp, ), dtype=np.float64) nread = self.reader.read_many_sample_double( data, number_of_samples_per_channel=nsamp, timeout=self.timeout) nread = self.reader.read_many_sample_pulse_frequency( data, dcycles, number_of_samples_per_channel=nsamp, timeout=self.timeout) if nread != nsamp: return {} return {self.channels[0].phys_name: data}
def test_one_sample_pulse_time(self, x_series_device, seed): # Reset the pseudorandom number generator with seed. random.seed(seed) high_time = random.uniform(0.0001, 0.001) low_time = random.uniform(0.0001, 0.001) # Select random counters from the device. counters = random.sample(self._get_device_counters(x_series_device), 2) with nidaqmx.Task() as write_task, nidaqmx.Task() as read_task: write_task.co_channels.add_co_pulse_chan_time(counters[0], high_time=high_time, low_time=low_time) write_task.timing.cfg_implicit_timing( sample_mode=AcquisitionType.CONTINUOUS) read_task.ci_channels.add_ci_pulse_chan_time(counters[1], min_val=0.0001, max_val=0.001) read_task.ci_channels.all.ci_pulse_time_term = ( '/{0}InternalOutput'.format(counters[0])) read_task.start() write_task.start() reader = CounterReader(read_task.in_stream) value_read = reader.read_one_sample_pulse_time() write_task.stop() assert numpy.isclose(value_read.high_time, high_time, rtol=0.05) assert numpy.isclose(value_read.low_time, low_time, rtol=0.05)
def test_one_sample_double(self, x_series_device, seed): # Reset the pseudorandom number generator with seed. random.seed(seed) frequency = random.uniform(1000, 10000) # Select random counters from the device. counters = random.sample(self._get_device_counters(x_series_device), 2) with nidaqmx.Task() as write_task, nidaqmx.Task() as read_task: write_task.co_channels.add_co_pulse_chan_freq(counters[0], freq=frequency) write_task.timing.cfg_implicit_timing( sample_mode=AcquisitionType.CONTINUOUS) actual_frequency = write_task.co_channels.all.co_pulse_freq read_task.ci_channels.add_ci_freq_chan(counters[1], min_val=1000, max_val=10000) read_task.ci_channels.all.ci_freq_term = ( '/{0}InternalOutput'.format(counters[0])) reader = CounterReader(read_task.in_stream) read_task.start() write_task.start() value_read = reader.read_one_sample_double() numpy.testing.assert_allclose([value_read], [actual_frequency], rtol=0.05)
def test_multi_sample_uint32(self, x_series_device, seed): # Reset the pseudorandom number generator with seed. random.seed(seed) number_of_samples = random.randint(2, 50) frequency = random.uniform(1000, 10000) # Select random counters from the device. counters = random.sample(self._get_device_counters(x_series_device), 3) with nidaqmx.Task() as write_task, nidaqmx.Task() as read_task, \ nidaqmx.Task() as sample_clk_task: # Create a finite pulse train task that acts as the sample clock # for the read task and the arm start trigger for the write task. sample_clk_task.co_channels.add_co_pulse_chan_freq(counters[0], freq=frequency) actual_frequency = sample_clk_task.co_channels.all.co_pulse_freq sample_clk_task.timing.cfg_implicit_timing( samps_per_chan=number_of_samples) samp_clk_terminal = '/{0}InternalOutput'.format(counters[0]) write_task.co_channels.add_co_pulse_chan_freq( counters[1], freq=actual_frequency) write_task.timing.cfg_implicit_timing( samps_per_chan=number_of_samples) write_task.triggers.arm_start_trigger.trig_type = ( TriggerType.DIGITAL_EDGE) write_task.triggers.arm_start_trigger.dig_edge_edge = (Edge.RISING) write_task.triggers.arm_start_trigger.dig_edge_src = ( samp_clk_terminal) read_task.ci_channels.add_ci_count_edges_chan(counters[2], edge=Edge.RISING) read_task.ci_channels.all.ci_count_edges_term = ( '/{0}InternalOutput'.format(counters[1])) read_task.timing.cfg_samp_clk_timing( actual_frequency, source=samp_clk_terminal, active_edge=Edge.FALLING, samps_per_chan=number_of_samples) read_task.start() write_task.start() sample_clk_task.start() sample_clk_task.wait_until_done(timeout=2) reader = CounterReader(read_task.in_stream) values_read = numpy.zeros(number_of_samples, dtype=numpy.uint32) reader.read_many_sample_uint32( values_read, number_of_samples_per_channel=number_of_samples, timeout=2) expected_values = [i + 1 for i in range(number_of_samples)] assert values_read.tolist() == expected_values
def __init__(self, channel, channel_name): self._thread = None self._stop = False self._channel = channel self._data = np.zeros(0) self.sample_readies = 0 self.enabled = True # Create the task self._task = Task('task' + channel_name) # Configure reader. self._task.in_stream.read_all_avail_samp = True self._reader = CounterReader(self._task.in_stream)
def create_ci_task( self, name: str, chan_specs: dict, samp_clk_cnfg: dict = {}, timing_params: dict = {}, chan_xtra_params: dict = {}, clk_xtra_params: dict = {}, ): """Doc.""" task = ni.Task(new_task_name=name) chan = task.ci_channels.add_ci_count_edges_chan(**chan_specs) for key, val in chan_xtra_params.items(): setattr(chan, key, val) if samp_clk_cnfg: task.timing.cfg_samp_clk_timing(**samp_clk_cnfg) for key, val in timing_params.items(): setattr(task.timing, key, val) for key, val in clk_xtra_params.items(): setattr(task.timing, key, val) task.sr = CounterReader(task.in_stream) task.sr.verify_array_shape = False self.tasks.ci.append(task)
def count(high_time, low_time, samples): global Reader, data print('Configure internal connections..') system = System.local() system.connect_terms('/Dev1/PFI32', '/Dev1/RTSI0') with Task('ct1_task') as ct1_task, Task('timer') as timer_task: data = np.zeros(samples) # configure counter channel and task ct1 = ct1_task.ci_channels.add_ci_pulse_width_chan( 'Dev1/ctr2', 'counter', min_val=2, max_val=100000000, units=TimeUnits.TICKS) ct1_task.timing.cfg_implicit_timing(samps_per_chan=samples) ct1.ci_ctr_timebase_src = '/Dev1/PFI31' ct1.ci_pulse_width_term = '/Dev1/RTSI0' Reader = CounterReader(ct1_task.in_stream) ct1_task.register_every_n_samples_acquired_into_buffer_event( 1, sample_readies) ct1_task.start() timer_task.co_channels.add_co_pulse_chan_time( 'Dev1/ctr1', high_time=high_time, low_time=low_time) timer_task.timing.cfg_implicit_timing( sample_mode=AcquisitionType.FINITE, samps_per_chan=samples) timer_task.start() while not timer_task.is_task_done(): time.sleep(0.001) print(data) print(len(data))
class BaseChannel: """ Base class to implement a channel """ def __init__(self, channel, channel_name): self._thread = None self._stop = False self._channel = channel self._data = np.zeros(0) self.sample_readies = 0 self.enabled = True # Create the task self._task = Task('task' + channel_name) # Configure reader. self._task.in_stream.read_all_avail_samp = True self._reader = CounterReader(self._task.in_stream) # Configure timing def __del__(self): self._task.close() def start(self, samples): self._task.stop() self._data = np.zeros(samples) self.sample_readies = 0 if not self.enabled: return if samples == 1: self._task.timing.samp_quant_samp_per_chan = 2 else: self._task.timing.samp_quant_samp_per_chan = samples self._stop = False self._thread = threading.Thread(target=self._read, args=[samples]) self._task.start() self._thread.start() @property def data(self): return self._data[:self.sample_readies] @property def done(self): return self._task.is_task_done() def stop(self): self._stop = True self._task.stop() def _read(self, samples): i = 0 while not self._stop and samples != 0: self._data[i] = self._reader.read_one_sample_double(timeout=-1) i += 1 samples -= 1 self.sample_readies += 1 self._task.stop()
def test_pulse_ticks_1_samp(self, x_series_device, seed): # Reset the pseudorandom number generator with seed. random.seed(seed) high_ticks = random.randint(100, 1000) low_ticks = random.randint(100, 1000) starting_edge = random.choice([Edge.RISING, Edge.FALLING]) # Select random counters from the device. counters = random.sample(self._get_device_counters(x_series_device), 2) with nidaqmx.Task() as write_task, nidaqmx.Task() as read_task: write_task.co_channels.add_co_pulse_chan_ticks( counters[0], '/{0}/100kHzTimebase'.format(x_series_device.name), high_ticks=high_ticks, low_ticks=low_ticks) write_task.timing.cfg_implicit_timing( sample_mode=AcquisitionType.CONTINUOUS) read_task.ci_channels.add_ci_pulse_chan_ticks( counters[1], source_terminal='/{0}/100kHzTimebase'.format( x_series_device.name), min_val=100, max_val=1000) read_task.ci_channels.all.ci_pulse_ticks_term = ( '/{0}InternalOutput'.format(counters[0])) read_task.ci_channels.all.ci_pulse_ticks_starting_edge = ( starting_edge) read_task.start() write_task.start() reader = CounterReader(read_task.in_stream) value_read = reader.read_one_sample_pulse_ticks() write_task.stop() assert numpy.isclose(value_read.high_tick, high_ticks, rtol=0.05, atol=1) assert numpy.isclose(value_read.low_tick, low_ticks, rtol=0.05, atol=1)
class PulseCounter: """ Class to implement a pulse counter """ def __init__(self, channel, channel_name, gate_src, timebase_src, edge=Edge.RISING): self._data = None self.sample_readies = 0 self._channel = channel self._task = Task('task' + channel_name) self._counter = self._task.ci_channels.add_ci_pulse_width_chan( channel, channel_name, units=TimeUnits.TICKS, starting_edge=edge) self._counter.ci_ctr_timebase_src = timebase_src self._counter.ci_pulse_width_term = gate_src self._task.in_stream.read_all_avail_samp = True self._reader = CounterReader(self._task.in_stream) self._thread = None self._stop = False def __del__(self): self._task.close() def start(self, samples): self._data = np.zeros(samples) self._task.stop() self._data = np.zeros(samples) self._task.timing.samp_quant_samp_per_chan = samples self._task.timing.samp_timing_type = SampleTimingType.IMPLICIT self._counter.ci_data_xfer_mech = \ DataTransferActiveTransferMode.INTERRUPT self._thread = threading.Thread(target=self._read, args=[samples]) self._task.start() self._stop = False self._thread.start() @property def done(self): return self._task.is_task_done() def stop(self): self._stop = True self._task.stop() def _read(self, samples): i = 0 while not self._stop and samples != 0: self._data[i] = self._reader.read_one_sample_double(timeout=-1) i += 1 samples -= 1 @property def data(self): return self._data
def __init__(self, channel, channel_name, gate_src, timebase_src, edge=Edge.RISING): self._data = None self.sample_readies = 0 self._channel = channel self._task = Task('task' + channel_name) self._counter = self._task.ci_channels.add_ci_pulse_width_chan( channel, channel_name, units=TimeUnits.TICKS, starting_edge=edge) self._counter.ci_ctr_timebase_src = timebase_src self._counter.ci_pulse_width_term = gate_src self._task.in_stream.read_all_avail_samp = True self._reader = CounterReader(self._task.in_stream) self._thread = None self._stop = False
def test_multi_sample_double(self, x_series_device, seed): # Reset the pseudorandom number generator with seed. random.seed(seed) number_of_samples = random.randint(2, 50) frequency = random.uniform(1000, 10000) # Select random counters from the device. counters = random.sample(self._get_device_counters(x_series_device), 3) with nidaqmx.Task() as write_task, nidaqmx.Task() as read_task: write_task.co_channels.add_co_pulse_chan_freq(counters[1], freq=frequency) write_task.timing.cfg_implicit_timing( samps_per_chan=number_of_samples + 1) read_task.ci_channels.add_ci_freq_chan(counters[2], min_val=1000, max_val=10000, edge=Edge.RISING) read_task.ci_channels.all.ci_freq_term = ( '/{0}InternalOutput'.format(counters[1])) read_task.timing.cfg_implicit_timing( samps_per_chan=number_of_samples) read_task.start() write_task.start() write_task.wait_until_done(timeout=2) reader = CounterReader(read_task.in_stream) values_read = numpy.zeros(number_of_samples, dtype=numpy.float64) reader.read_many_sample_double( values_read, number_of_samples_per_channel=number_of_samples, timeout=2) expected_values = [frequency for _ in range(number_of_samples)] numpy.testing.assert_allclose(values_read, expected_values, rtol=0.05)
def read_data(self, arr=None): if self.reader is None: from nidaqmx.stream_readers import CounterReader self.reader = CounterReader(self._task.in_stream) #number_of_channels = len(self.channels) nsamp = self.samp_per_chan if arr is None: data = np.empty((nsamp, ), dtype=np.float64) else: data = arr dcycles = np.empty((nsamp, ), dtype=np.float64) nread = self.reader.read_many_sample_double( data, number_of_samples_per_channel=nsamp, timeout=self.timeout) nread = self.reader.read_many_sample_pulse_frequency( data, dcycles, number_of_samples_per_channel=nsamp, timeout=self.timeout) if nread != nsamp: return {} return {self.channels[0].phys_name: data}
def test_many_sample_pulse_ticks(self, x_series_device, seed): # Reset the pseudorandom number generator with seed. random.seed(seed) number_of_samples = random.randint(2, 50) # Select random counters from the device. counters = random.sample(self._get_device_counters(x_series_device), 2) with nidaqmx.Task() as write_task, nidaqmx.Task() as read_task: write_task.co_channels.add_co_pulse_chan_ticks( counters[0], '/{0}/100kHzTimebase'.format(x_series_device.name), idle_state=Level.HIGH) write_task.timing.cfg_implicit_timing( samps_per_chan=number_of_samples + 1) write_task.control(TaskMode.TASK_COMMIT) read_task.ci_channels.add_ci_pulse_chan_ticks( counters[1], source_terminal='/{0}/100kHzTimebase'.format( x_series_device.name), min_val=100, max_val=1000) read_task.ci_channels.all.ci_pulse_ticks_term = ( '/{0}InternalOutput'.format(counters[0])) read_task.timing.cfg_implicit_timing( samps_per_chan=number_of_samples) high_ticks_to_test = numpy.array([ random.randint(100, 1000) for _ in range(number_of_samples + 1) ], dtype=numpy.uint32) low_ticks_to_test = numpy.array([ random.randint(100, 1000) for _ in range(number_of_samples + 1) ], dtype=numpy.uint32) writer = CounterWriter(write_task.out_stream) reader = CounterReader(read_task.in_stream) writer.write_many_sample_pulse_ticks(high_ticks_to_test, low_ticks_to_test) read_task.start() write_task.start() high_ticks_read = numpy.zeros(number_of_samples, dtype=numpy.uint32) low_ticks_read = numpy.zeros(number_of_samples, dtype=numpy.uint32) reader.read_many_sample_pulse_ticks( high_ticks_read, low_ticks_read, number_of_samples_per_channel=number_of_samples, timeout=2) numpy.testing.assert_allclose(high_ticks_read, high_ticks_to_test[1:], rtol=0.05, atol=1) numpy.testing.assert_allclose(low_ticks_read, low_ticks_to_test[1:], rtol=0.05, atol=1)
def test_many_sample_pulse_freq(self, x_series_device, seed): # Reset the pseudorandom number generator with seed. random.seed(seed) number_of_samples = random.randint(2, 50) # Select random counters from the device. counters = random.sample(self._get_device_counters(x_series_device), 2) with nidaqmx.Task() as write_task, nidaqmx.Task() as read_task: write_task.co_channels.add_co_pulse_chan_freq( counters[0], idle_state=Level.HIGH) write_task.timing.cfg_implicit_timing( samps_per_chan=number_of_samples + 1) write_task.control(TaskMode.TASK_COMMIT) read_task.ci_channels.add_ci_pulse_chan_freq(counters[1], min_val=1000, max_val=10000) read_task.ci_channels.all.ci_pulse_freq_term = ( '/{0}InternalOutput'.format(counters[0])) read_task.timing.cfg_implicit_timing( samps_per_chan=number_of_samples) frequencies_to_test = numpy.array([ random.uniform(1000, 10000) for _ in range(number_of_samples + 1) ], dtype=numpy.float64) duty_cycles_to_test = numpy.array([ random.uniform(0.2, 0.8) for _ in range(number_of_samples + 1) ], dtype=numpy.float64) writer = CounterWriter(write_task.out_stream) reader = CounterReader(read_task.in_stream) writer.write_many_sample_pulse_frequency(frequencies_to_test, duty_cycles_to_test) read_task.start() write_task.start() frequencies_read = numpy.zeros(number_of_samples, dtype=numpy.float64) duty_cycles_read = numpy.zeros(number_of_samples, dtype=numpy.float64) reader.read_many_sample_pulse_frequency( frequencies_read, duty_cycles_read, number_of_samples_per_channel=number_of_samples, timeout=2) numpy.testing.assert_allclose(frequencies_read, frequencies_to_test[1:], rtol=0.05) numpy.testing.assert_allclose(duty_cycles_read, duty_cycles_to_test[1:], rtol=0.05)