Skip to content

Commit

Permalink
Add Counter Input examples (#581)
Browse files Browse the repository at this point in the history
* Add Counter Input examples

* reworked as per comments

* reworked on comments part 2
  • Loading branch information
WayneDroid authored May 17, 2024
1 parent 1bc5df5 commit 6ffec7b
Show file tree
Hide file tree
Showing 8 changed files with 154 additions and 41 deletions.
21 changes: 0 additions & 21 deletions examples/counter_in/ci_count_edges.py

This file was deleted.

19 changes: 0 additions & 19 deletions examples/counter_in/ci_pulse_freq.py

This file was deleted.

32 changes: 32 additions & 0 deletions examples/counter_in/cnt_dig_event.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""Example of counter input edge count operation.
This example demonstrates how to count digital events on a
Counter Input Channel. The Initial Count, Count Direction, and
Edge are all configurable.
"""

import nidaqmx
from nidaqmx.constants import CountDirection, Edge

with nidaqmx.Task() as task:
channel = task.ci_channels.add_ci_count_edges_chan(
"Dev1/ctr0",
edge=Edge.RISING,
initial_count=0,
count_direction=CountDirection.COUNT_UP,
)
channel.ci_count_edges_term = "/Dev1/PFI8"

print("Continuously polling. Press Ctrl+C to stop.")
task.start()

try:
edge_counts = 0
while True:
edge_counts = task.read()
print(f"Acquired count: {edge_counts:n}", end="\r")
except KeyboardInterrupt:
pass
finally:
task.stop()
print(f"\nAcquired {edge_counts:n} total counts.")
36 changes: 36 additions & 0 deletions examples/counter_in/cont_cnt_buff_event_ext_clk.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"""Example of counter input edge count operation.
This example demonstrates how to count buffered digital events
on a Counter Input Channel. The Initial Count, Count Direction,
Edge, and Sample Clock Source are all configurable.
"""

import nidaqmx
from nidaqmx.constants import AcquisitionType, CountDirection, Edge

with nidaqmx.Task() as task:
channel = task.ci_channels.add_ci_count_edges_chan(
"Dev1/ctr0",
edge=Edge.RISING,
initial_count=0,
count_direction=CountDirection.COUNT_UP,
)
task.timing.cfg_samp_clk_timing(
1000, source="/Dev1/PFI9", sample_mode=AcquisitionType.CONTINUOUS
)
channel.ci_count_edges_term = "/Dev1/PFI8"

print("Continuously polling. Press Ctrl+C to stop.")
task.start()

try:
total_read = 0
while True:
edge_counts = task.read(number_of_samples_per_channel=1000)
total_read += len(edge_counts)
print(f"Acquired data: {len(edge_counts)} samples. Total {total_read}.", end="\r")
except KeyboardInterrupt:
pass
finally:
task.stop()
print(f"\nAcquired {total_read} total samples.")
36 changes: 36 additions & 0 deletions examples/counter_in/cont_read_buff_freq.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"""Example of counter input frequency acquisition.
This example demonstrates how to continuously measure buffered frequency
using one counter on a Counter Input Channel. The Edge, Minimum Value
and Maximum Value are all configurable.
"""

import nidaqmx
from nidaqmx.constants import AcquisitionType, Edge, FrequencyUnits


with nidaqmx.Task() as task:
channel = task.ci_channels.add_ci_freq_chan(
"Dev1/ctr0",
min_val=2.0,
max_val=100000.0,
units=FrequencyUnits.HZ,
edge=Edge.RISING,
)
channel.ci_freq_term = "/Dev1/PFI8"
task.timing.cfg_implicit_timing(sample_mode=AcquisitionType.CONTINUOUS)

print("Continuously polling. Press Ctrl+C to stop.")
task.start()

try:
total_read = 0
while True:
frequencies = task.read(number_of_samples_per_channel=1000)
total_read += len(frequencies)
print(f"Acquired data: {len(frequencies)} samples. Total {total_read}.", end="\r")
except KeyboardInterrupt:
pass
finally:
task.stop()
print(f"\nAcquired {total_read} total samples.")
27 changes: 27 additions & 0 deletions examples/counter_in/read_freq.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""Example of counter input frequency acquisition.
This example demonstrates how to measure frequency using one
counter on a Counter Input Channel. The Edge, Minimum Value and
Maximum Value are all configurable.
"""

import nidaqmx
from nidaqmx.constants import Edge, FrequencyUnits


with nidaqmx.Task() as task:
channel = task.ci_channels.add_ci_freq_chan(
"Dev1/ctr0",
min_val=2.0,
max_val=100000.0,
units=FrequencyUnits.HZ,
edge=Edge.RISING,
)
channel.ci_freq_term = "/Dev1/PFI8"

task.start()

data = task.read()
print(f"Acquired frequency: {data:.2f} Hz")

task.stop()
22 changes: 22 additions & 0 deletions examples/counter_in/read_pulse_freq.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"""Example of CI pulse frequency operation.
This example demonstrates how to configure a pulse measurement to acquire frequency and duty cycle.
"""

import nidaqmx
from nidaqmx.constants import FrequencyUnits

with nidaqmx.Task() as task:
channel = task.ci_channels.add_ci_pulse_chan_freq(
"Dev1/ctr0", "", min_val=2.0, max_val=100000.0, units=FrequencyUnits.HZ
)
channel.ci_pulse_freq_term = "/Dev1/PFI8"

task.start()

data = task.read()
print(f"Acquired data:")
print(f"Frequency: {data.freq:.2f} Hz")
print(f"Duty cycle: {(data.duty_cycle * 100):.2f}%")

task.stop()
2 changes: 1 addition & 1 deletion tests/acceptance/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def test___shipping_example___run___no_errors(example_path: Path, system):
pytest.skip(
f"Device {device_name} does not have physical channel {physical_channel_name}"
)
if example_path.name == "ci_pulse_freq.py":
if example_path.name == "read_pulse_freq.py":
pytest.skip("Example times out if there is no signal.")
if re.search(r"\binput\(|\bKeyboardInterrupt\b", example_source):
pytest.skip("Example waits for keyboard input.")
Expand Down

0 comments on commit 6ffec7b

Please sign in to comment.