| 
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  | 
 | 7 | +import nidaqmx.system  | 
6 | 8 | from nidaqmx.constants import (  | 
7 | 9 |     AcquisitionType,  | 
8 | 10 |     CurrentShuntResistorLocation,  | 
9 | 11 |     CurrentUnits,  | 
10 | 12 |     Edge,  | 
11 | 13 |     ExcitationSource,  | 
12 | 14 |     FilterResponse,  | 
13 |  | -    LoggingMode,  | 
14 |  | -    LoggingOperation,  | 
15 | 15 |     Slope,  | 
16 | 16 |     StrainGageBridgeType,  | 
17 | 17 |     TerminalConfiguration,  | 
 | 
22 | 22 | panel_script_path = Path(__file__).with_name("nidaqmx_analog_input_filtering_panel.py")  | 
23 | 23 | panel = nipanel.create_panel(panel_script_path)  | 
24 | 24 | panel.set_value("is_running", False)  | 
 | 25 | + | 
 | 26 | +system = nidaqmx.system.System.local()  | 
 | 27 | + | 
 | 28 | +channel_name = []  | 
 | 29 | +for dev in system.devices:  | 
 | 30 | +    for chan in dev.ai_physical_chans:  | 
 | 31 | +        channel_name.append(chan.name)  | 
 | 32 | +panel.set_value("channel_name", channel_name)  | 
 | 33 | +trigger_sources = []  | 
 | 34 | +for dev in system.devices:  | 
 | 35 | +    if hasattr(dev, "terminals"):  | 
 | 36 | +        for term in dev.terminals:  | 
 | 37 | +            trigger_sources.append(term)  | 
 | 38 | +panel.set_value("trigger_sources", trigger_sources)  | 
25 | 39 | try:  | 
26 | 40 |     print(f"Panel URL: {panel.panel_url}")  | 
27 | 41 |     print(f"Waiting for the 'Run' button to be pressed...")  | 
28 | 42 |     print(f"(Press Ctrl + C to quit)")  | 
29 | 43 |     while True:  | 
30 | 44 |         while not panel.get_value("run_button", False):  | 
31 | 45 |             panel.set_value("is_running", False)  | 
 | 46 | +            # time.sleep(0.1)  | 
32 | 47 |         panel.set_value("is_running", True)  | 
33 | 48 |         panel.set_value("stop_button", False)  | 
34 | 49 | 
 
  | 
35 | 50 |         # How to use nidaqmx: https://nidaqmx-python.readthedocs.io/en/stable/  | 
36 | 51 |         with nidaqmx.Task() as task:  | 
 | 52 | + | 
37 | 53 |             chan_type = panel.get_value("chan_type", "1")  | 
38 | 54 | 
 
  | 
39 | 55 |             if chan_type == "2":  | 
40 | 56 |                 chan = task.ai_channels.add_ai_current_chan(  | 
41 |  | -                    "Mod3/ai10",  | 
 | 57 | +                    panel.get_value("physical_channel", ""),  | 
42 | 58 |                     max_val=panel.get_value("max_value_current", 0.01),  | 
43 | 59 |                     min_val=panel.get_value("min_value_current", -0.01),  | 
44 | 60 |                     ext_shunt_resistor_val=panel.get_value("shunt_resistor_value", 249.0),  | 
 | 
50 | 66 | 
 
  | 
51 | 67 |             elif chan_type == "3":  | 
52 | 68 |                 chan = task.ai_channels.add_ai_strain_gage_chan(  | 
53 |  | -                    "Mod3/ai10",  | 
 | 69 | +                    panel.get_value("physical_channel", ""),  | 
54 | 70 |                     nominal_gage_resistance=panel.get_value("gage_resistance", 350.0),  | 
55 | 71 |                     voltage_excit_source=ExcitationSource.EXTERNAL,  # Only mode that works  | 
56 | 72 |                     max_val=panel.get_value("max_value_strain", 0.001),  | 
 | 
66 | 82 |                 )  | 
67 | 83 |             else:  | 
68 | 84 |                 chan = task.ai_channels.add_ai_voltage_chan(  | 
69 |  | -                    "Mod3/ai10",  | 
 | 85 | +                    panel.get_value("physical_channel", ""),  | 
70 | 86 |                     terminal_config=panel.get_value(  | 
71 | 87 |                         "terminal_configuration", TerminalConfiguration.DEFAULT  | 
72 | 88 |                     ),  | 
 | 
79 | 95 |                 sample_mode=AcquisitionType.CONTINUOUS,  | 
80 | 96 |                 samps_per_chan=panel.get_value("total_samples", 100),  | 
81 | 97 |             )  | 
82 |  | -            panel.set_value("actual_sample_rate", task._timing.samp_clk_rate)  | 
 | 98 | +            panel.set_value("actual_sample_rate", task.timing.samp_clk_rate)  | 
83 | 99 |             panel.set_value("sample_rate", panel.get_value("rate", 100.0))  | 
84 | 100 | 
 
  | 
85 |  | -            task.in_stream.configure_logging(  | 
86 |  | -                file_path=panel.get_value("tdms_file_path", "data.tdms"),  | 
87 |  | -                logging_mode=panel.get_value("logging_mode", LoggingMode.OFF),  | 
88 |  | -                operation=LoggingOperation.OPEN_OR_CREATE,  | 
89 |  | -            )  | 
90 | 101 |             if panel.get_value("filter", "Filter") == "Filter":  | 
91 | 102 |                 chan.ai_filter_enable = True  | 
92 | 103 |                 chan.ai_filter_freq = panel.get_value("filter_freq", 0.0)  | 
 | 
109 | 120 |                     trigger_slope=panel.get_value("slope", Slope.FALLING),  | 
110 | 121 |                     trigger_level=panel.get_value("level", 0.0),  | 
111 | 122 |                 )  | 
 | 123 | + | 
112 | 124 |             if trigger_type == "2":  | 
113 | 125 |                 task.triggers.start_trigger.cfg_dig_edge_start_trig(  | 
114 | 126 |                     trigger_source="/Dev2/PFI0", trigger_edge=panel.get_value("edge", Edge.FALLING)  | 
 | 
0 commit comments