Skip to content

Commit

Permalink
Add and update AI examples. (#566)
Browse files Browse the repository at this point in the history
* added and updated AI examples

* reworked as per given comments

* fixed style error

* added pprint in event example

* reworked and file renamed as per suggestions

* remind user before start task in event example

* reduced samples per chan

* reduced samples per chan for event example

* print total read in cont examples

* removed samps_per_chan for cont examples

* added :f for single read examples
  • Loading branch information
WayneDroid authored Apr 25, 2024
1 parent 5e0380b commit db57411
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 32 deletions.
32 changes: 0 additions & 32 deletions examples/analog_in/ai_voltage_sw_timed.py

This file was deleted.

27 changes: 27 additions & 0 deletions examples/analog_in/cont_voltage_acq_int_clk.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""Example of analog input voltage acquisition.
This example demonstrates how to acquire a continuous amount of data
using the DAQ device's internal clock.
"""

import nidaqmx
from nidaqmx.constants import AcquisitionType

with nidaqmx.Task() as task:
task.ai_channels.add_ai_voltage_chan("Dev1/ai0")
task.timing.cfg_samp_clk_timing(1000.0, sample_mode=AcquisitionType.CONTINUOUS)
task.start()
print("Running task. Press Ctrl+C to stop.")

try:
total_read = 0
while True:
data = task.read(number_of_samples_per_channel=1000)
read = len(data)
total_read += read
print(f"Acquired data: {read} samples. Total {total_read}.", end="\r")
except KeyboardInterrupt:
pass
finally:
task.stop()
print(f"\nAcquired {total_read} total samples.")
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"""Example of analog input voltage acquisition with events.
This example demonstrates how to use Every N Samples events to
acquire a continuous amount of data using the DAQ device's
internal clock. The Every N Samples events indicate when data is
available from DAQmx.
"""

import nidaqmx
from nidaqmx.constants import AcquisitionType


def main():
"""Continuously acquires data using an Every N Samples event."""
total_read = 0
with nidaqmx.Task() as task:

def callback(task_handle, every_n_samples_event_type, number_of_samples, callback_data):
"""Callback function for reading singals."""
nonlocal total_read
read = len(task.read(number_of_samples_per_channel=number_of_samples))
total_read += read
print(f"Acquired data: {read} samples. Total {total_read}.", end="\r")

return 0

task.ai_channels.add_ai_voltage_chan("Dev1/ai0")
task.timing.cfg_samp_clk_timing(1000.0, sample_mode=AcquisitionType.CONTINUOUS)
task.register_every_n_samples_acquired_into_buffer_event(1000, callback)
task.start()

input("Running task. Press Enter to stop.\n")

task.stop()
print(f"\nAcquired {total_read} total samples.")


if __name__ == "__main__":
main()
16 changes: 16 additions & 0 deletions examples/analog_in/thrmcpl_sample.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""Example of analog input temperature acquisition.
This example demonstrates how to acquire thermocouple measurement using
software timing.
"""

import nidaqmx
from nidaqmx.constants import ThermocoupleType, TemperatureUnits

with nidaqmx.Task() as task:
task.ai_channels.add_ai_thrmcpl_chan(
"Dev1/ai0", units=TemperatureUnits.DEG_C, thermocouple_type=ThermocoupleType.K
)

data = task.read()
print(f"Acquired data: {data:f}")
15 changes: 15 additions & 0 deletions examples/analog_in/voltage_acq_int_clk.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"""Example of analog input voltage acquisition.
This example demonstrates how to acquire a finite amount
of data using the DAQ device's internal clock.
"""

import nidaqmx
from nidaqmx.constants import AcquisitionType, READ_ALL_AVAILABLE

with nidaqmx.Task() as task:
task.ai_channels.add_ai_voltage_chan("Dev1/ai0")
task.timing.cfg_samp_clk_timing(1000.0, sample_mode=AcquisitionType.FINITE, samps_per_chan=50)

data = task.read(READ_ALL_AVAILABLE)
print("Acquired data: [" + ", ".join(f"{value:f}" for value in data) + "]")
12 changes: 12 additions & 0 deletions examples/analog_in/voltage_sample.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
"""Example of analog input voltage acquisition.
This example demonstrates how to acquire a voltage measurement using software timing.
"""

import nidaqmx

with nidaqmx.Task() as task:
task.ai_channels.add_ai_voltage_chan("Dev1/ai0")

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

0 comments on commit db57411

Please sign in to comment.