|
1 | 1 | """Data acquisition script that continuously acquires analog input data.""" |
2 | 2 |
|
| 3 | +import time |
3 | 4 | from pathlib import Path |
4 | 5 |
|
5 | 6 | import nidaqmx |
6 | | -from nidaqmx.constants import AcquisitionType |
| 7 | +from nidaqmx.constants import ( |
| 8 | + AcquisitionType, |
| 9 | + TerminalConfiguration, |
| 10 | + CJCSource, |
| 11 | + TemperatureUnits, |
| 12 | + ThermocoupleType, |
| 13 | + LoggingMode, |
| 14 | + LoggingOperation, |
| 15 | +) |
7 | 16 |
|
8 | 17 | import nipanel |
9 | 18 |
|
10 | 19 | panel_script_path = Path(__file__).with_name("nidaqmx_continuous_analog_input_panel.py") |
11 | 20 | panel = nipanel.create_panel(panel_script_path) |
12 | 21 |
|
13 | | -# How to use nidaqmx: https://nidaqmx-python.readthedocs.io/en/stable/ |
14 | | -with nidaqmx.Task() as task: |
15 | | - task.ai_channels.add_ai_voltage_chan("Dev1/ai0") |
16 | | - task.ai_channels.add_ai_thrmcpl_chan("Dev1/ai1") |
17 | | - task.timing.cfg_samp_clk_timing( |
18 | | - rate=1000.0, sample_mode=AcquisitionType.CONTINUOUS, samps_per_chan=3000 |
19 | | - ) |
20 | | - panel.set_value("sample_rate", task._timing.samp_clk_rate) |
21 | | - task.start() |
| 22 | +try: |
22 | 23 | print(f"Panel URL: {panel.panel_url}") |
23 | | - try: |
24 | | - print(f"Press Ctrl + C to stop") |
25 | | - while True: |
26 | | - data = task.read( |
27 | | - number_of_samples_per_channel=1000 # pyright: ignore[reportArgumentType] |
| 24 | + print(f"Waiting for the 'Run' button to be pressed...") |
| 25 | + print(f"(Press Ctrl + C to quit)") |
| 26 | + while True: |
| 27 | + panel.set_value("run_button", False) |
| 28 | + while not panel.get_value("run_button", False): |
| 29 | + time.sleep(0.1) |
| 30 | + |
| 31 | + # How to use nidaqmx: https://nidaqmx-python.readthedocs.io/en/stable/ |
| 32 | + with nidaqmx.Task() as task: |
| 33 | + task.ai_channels.add_ai_voltage_chan( |
| 34 | + physical_channel="Dev1/ai0", |
| 35 | + min_val=panel.get_value("voltage_min_value", -5.0), |
| 36 | + max_val=panel.get_value("voltage_max_value", 5.0), |
| 37 | + terminal_config=panel.get_value( |
| 38 | + "terminal_configuration", TerminalConfiguration.DEFAULT |
| 39 | + ), |
| 40 | + ) |
| 41 | + task.ai_channels.add_ai_thrmcpl_chan( |
| 42 | + "Dev1/ai1", |
| 43 | + min_val=panel.get_value("thermocouple_min_value", 0.0), |
| 44 | + max_val=panel.get_value("thermocouple_max_value", 100.0), |
| 45 | + units=panel.get_value("thermocouple_units", TemperatureUnits.DEG_C), |
| 46 | + thermocouple_type=panel.get_value("thermocouple_type", ThermocoupleType.K), |
| 47 | + cjc_source=panel.get_value( |
| 48 | + "thermocouple_cjc_source", CJCSource.CONSTANT_USER_VALUE |
| 49 | + ), |
| 50 | + cjc_val=panel.get_value("thermocouple_cjc_val", 25.0), |
| 51 | + ) |
| 52 | + task.timing.cfg_samp_clk_timing( |
| 53 | + rate=panel.get_value("sample_rate_input", 1000.0), |
| 54 | + sample_mode=AcquisitionType.CONTINUOUS, |
| 55 | + samps_per_chan=panel.get_value("samples_per_channel", 3000), |
28 | 56 | ) |
29 | | - panel.set_value("voltage_data", data[0]) |
30 | | - panel.set_value("thermocouple_data", data[1]) |
31 | | - except KeyboardInterrupt: |
32 | | - pass |
33 | | - finally: |
34 | | - task.stop() |
| 57 | + task.in_stream.configure_logging( |
| 58 | + file_path=panel.get_value("tdms_file_path", "data.tdms"), |
| 59 | + logging_mode=panel.get_value("logging_mode", LoggingMode.OFF), |
| 60 | + operation=LoggingOperation.OPEN_OR_CREATE, |
| 61 | + ) |
| 62 | + panel.set_value("sample_rate", task._timing.samp_clk_rate) |
| 63 | + try: |
| 64 | + print(f"Starting data acquisition...") |
| 65 | + task.start() |
| 66 | + panel.set_value("is_running", True) |
| 67 | + |
| 68 | + panel.set_value("stop_button", False) |
| 69 | + while not panel.get_value("stop_button", False): |
| 70 | + data = task.read( |
| 71 | + number_of_samples_per_channel=1000 # pyright: ignore[reportArgumentType] |
| 72 | + ) |
| 73 | + panel.set_value("voltage_data", data[0]) |
| 74 | + panel.set_value("thermocouple_data", data[1]) |
| 75 | + except KeyboardInterrupt: |
| 76 | + raise |
| 77 | + finally: |
| 78 | + print(f"Stopping data acquisition...") |
| 79 | + task.stop() |
| 80 | + panel.set_value("is_running", False) |
| 81 | + |
| 82 | +except KeyboardInterrupt: |
| 83 | + pass |
0 commit comments