Skip to content

Commit a82f608

Browse files
author
Dilmi Wickramanayake
committed
Completed Task Settings
1 parent 0a8da81 commit a82f608

File tree

4 files changed

+364
-349
lines changed

4 files changed

+364
-349
lines changed

examples/nidaqmx_analog_input_filtering/nidaqmx_analog_input_filtering.py

Lines changed: 123 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -3,117 +3,147 @@
33
from pathlib import Path
44

55
import nidaqmx
6-
from nidaqmx.constants import AcquisitionType, ExcitationSource, Edge, Slope, Timescale, TerminalConfiguration, FilterResponse
7-
8-
from settings_enum import DigitalEdge, PauseWhen, Slopes, AnalogPause, TerminalConfig, ShuntLocation
96
import numpy as numpy
7+
from nidaqmx.constants import (
8+
AcquisitionType,
9+
CurrentShuntResistorLocation,
10+
CurrentUnits,
11+
Edge,
12+
ExcitationSource,
13+
FilterResponse,
14+
LoggingMode,
15+
LoggingOperation,
16+
Slope,
17+
StrainGageBridgeType,
18+
TerminalConfiguration,
19+
Timescale,
20+
)
21+
from settings_enum import AnalogPause, PauseWhen
22+
1023
import nipanel
11-
import time
1224

1325
panel_script_path = Path(__file__).with_name("nidaqmx_analog_input_filtering_panel.py")
1426
panel = nipanel.create_panel(panel_script_path)
27+
panel.set_value("is_running", False)
28+
try:
29+
print(f"Panel URL: {panel.panel_url}")
30+
print(f"Waiting for the 'Run' button to be pressed...")
31+
print(f"(Press Ctrl + C to quit)")
32+
while True:
33+
while not panel.get_value("run_button", False):
34+
panel.set_value("is_running", False)
35+
panel.set_value("is_running", True)
36+
panel.set_value("stop_button", False)
37+
38+
with nidaqmx.Task() as task:
39+
chan_type = panel.get_value("chan_type", "1")
40+
if chan_type == "1":
41+
chan = task.ai_channels.add_ai_voltage_chan(
42+
"Mod3/ai10",
43+
terminal_config=panel.get_value(
44+
"terminal_configuration", TerminalConfiguration.DEFAULT
45+
),
46+
max_val=panel.get_value("max_value_voltage", 5.0),
47+
min_val=panel.get_value("min_value_voltage", -5.0),
48+
)
49+
chan.ai_filter_freq = panel.get_value("filter_freq", 0.0)
50+
chan.ai_filter_response = panel.get_value("filter_response", FilterResponse.COMB)
51+
chan.ai_filter_order = 1
52+
53+
elif chan_type == "2":
54+
chan = task.ai_channels.add_ai_current_chan(
55+
"Mod3/ai10",
56+
max_val=panel.get_value("max_value_current", 0.01),
57+
min_val=panel.get_value("min_value_current", -0.01),
58+
ext_shunt_resistor_val=panel.get_value("shunt_resistor_value", 249.0),
59+
shunt_resistor_loc=panel.get_value(
60+
"shunt_location", CurrentShuntResistorLocation.EXTERNAL
61+
),
62+
units=panel.get_value("units", CurrentUnits.AMPS),
63+
)
64+
chan.ai_filter_freq = panel.get_value("filter_freq", 0.0)
65+
chan.ai_filter_response = panel.get_value("filter_response", FilterResponse.COMB)
66+
chan.ai_filter_order = 1
1567

16-
print(f"Panel URL: {panel.panel_url}")
17-
while True:
18-
with nidaqmx.Task() as task:
19-
if panel.get_value("run_button", False):
20-
print(f"...")
21-
else:
22-
placeholder = panel.get_value("placeholder", "1")
23-
if placeholder == "1":
24-
chan = task.ai_channels.add_ai_voltage_chan("Mod3/ai10", terminal_config=TerminalConfiguration.DEFAULT, max_val=panel.get_value("max_value_voltage", 5.0), min_val=panel.get_value("min_value_voltage", -5.0))
25-
chan.ai_filter_freq = panel.get_value("filter_freq",0.0)
26-
27-
# chan.ai_filter_order = panel.get_value("filter_order", 0)
28-
29-
elif placeholder == "2":
30-
chan = task.ai_channels.add_ai_current_chan("Mod3/ai10", max_val = panel.get_value("max_value_current", 0.01), min_val= panel.get_value("min_value_current", -0.01), ext_shunt_resistor_val= panel.get_value("shunt_resistor_value", 249.0))
31-
elif placeholder == "3":
32-
chan = task.ai_channels.add_ai_strain_gage_chan("Mod3/ai10", nominal_gage_resistance= panel.get_value("gage_resistance", 350.0), voltage_excit_source=ExcitationSource.EXTERNAL,
33-
max_val=panel.get_value("max_value_strain", 0.001), min_val=panel.get_value("min_value_strain", -0.001), poisson_ratio=panel.get_value("poisson_ratio",0.3), lead_wire_resistance=panel.get_value("wire_resistance",0.0),
34-
initial_bridge_voltage=panel.get_value("initial_voltage", 0.0), gage_factor=panel.get_value("gage_factor",2.0), voltage_excit_val=panel.get_value("voltage_excitation_value",0.0))
68+
elif chan_type == "3":
69+
chan = task.ai_channels.add_ai_strain_gage_chan(
70+
"Mod3/ai10",
71+
nominal_gage_resistance=panel.get_value("gage_resistance", 350.0),
72+
voltage_excit_source=ExcitationSource.EXTERNAL, # Only mode that works
73+
max_val=panel.get_value("max_value_strain", 0.001),
74+
min_val=panel.get_value("min_value_strain", -0.001),
75+
poisson_ratio=panel.get_value("poisson_ratio", 0.3),
76+
lead_wire_resistance=panel.get_value("wire_resistance", 0.0),
77+
initial_bridge_voltage=panel.get_value("initial_voltage", 0.0),
78+
gage_factor=panel.get_value("gage_factor", 2.0),
79+
voltage_excit_val=panel.get_value("voltage_excitation_value", 0.0),
80+
strain_config=panel.get_value(
81+
"strain_configuration", StrainGageBridgeType.FULL_BRIDGE_I
82+
),
83+
)
84+
85+
chan.ai_filter_freq = panel.get_value("filter_freq", 0.0)
86+
chan.ai_filter_response = panel.get_value("filter_response", FilterResponse.COMB)
87+
chan.ai_filter_order = 1
3588

3689
task.timing.cfg_samp_clk_timing(
37-
rate = 1000.0, sample_mode = AcquisitionType.CONTINUOUS, samps_per_chan = 1000)
38-
90+
rate=1000.0,
91+
sample_mode=AcquisitionType.CONTINUOUS,
92+
samps_per_chan=1000,
93+
)
94+
task.in_stream.configure_logging(
95+
file_path=panel.get_value("tdms_file_path", "data.tdms"),
96+
logging_mode=panel.get_value("logging_mode", LoggingMode.OFF),
97+
operation=LoggingOperation.OPEN_OR_CREATE,
98+
)
99+
39100
task.timing.samp_clk_dig_fltr_min_pulse_width = 20.0e-6
40101
task.timing.samp_clk_dig_fltr_enable = True
41-
42-
43-
44-
panel.set_value("source", [task.triggers.pause_trigger.dig_lvl_src])
45-
panel.set_value("analog_source", [task.triggers.start_trigger.anlg_edge_src])
46102

47103
panel.set_value("sample_rate", task._timing.samp_clk_rate)
48104

49-
slope = Slopes(panel.get_value("slope",Slopes.FALLING.value))
105+
task.triggers.start_trigger.cfg_anlg_edge_start_trig(
106+
trigger_source="APFI0",
107+
trigger_slope=panel.get_value("slope", Slope.FALLING),
108+
trigger_level=panel.get_value("level", 0.0),
109+
)
110+
task.triggers.start_trigger.cfg_dig_edge_start_trig(
111+
trigger_source="/Dev2/PFI0", trigger_edge=panel.get_value("edge", Edge.FALLING)
112+
)
113+
task.triggers.start_trigger.anlg_edge_hyst = hysteriresis = panel.get_value(
114+
"hysteriesis", 0.0
115+
)
50116

51-
if slope == "Rising":
52-
task.triggers.start_trigger.cfg_anlg_edge_start_trig(trigger_source="APFI0", trigger_level=0, trigger_slope= Slope.RISING)
53-
else:
54-
task.triggers.start_trigger.cfg_anlg_edge_start_trig(trigger_source="APFI0", trigger_level=0, trigger_slope= Slope.FALLING)
55-
56-
digital_edge = DigitalEdge(panel.get_value("edge_digital", DigitalEdge.FALLING.value))
57-
if digital_edge == "RISING":
58-
task.triggers.start_trigger.cfg_dig_edge_start_trig(trigger_source="/Dev2/PFI0", trigger_edge=Edge.RISING)
59-
else:
60-
task.triggers.start_trigger.cfg_dig_edge_start_trig(trigger_source="/Dev2/PFI0", trigger_edge=Edge.FALLING)
61117
try:
118+
62119
task.start()
63-
while True:
64-
hysteriresis = panel.get_value("hysteriesis", 0.0)
65-
# task.triggers.start_trigger.cfg_time_start_trig(when = date, timescale=Timescale.USE_HOST)
66-
level = panel.get_value("level", 0.0)
67-
new_slope = Slopes(panel.get_value("slope",Slopes.FALLING.value))
68-
if slope != new_slope:
69-
task.stop()
70-
71-
if new_slope == "Rising":
72-
task.triggers.start_trigger.cfg_anlg_edge_start_trig(trigger_source="APFI0", trigger_level=level, trigger_slope= Slope.RISING)
73-
else:
74-
task.triggers.start_trigger.cfg_anlg_edge_start_trig(trigger_source="APFI0", trigger_level=level, trigger_slope= Slope.FALLING)
75-
task.triggers.start_trigger.anlg_edge_hyst = hysteriresis
76-
task.start()
77-
slope = new_slope
78-
79-
analog_pause = AnalogPause(panel.get_value("analog_pause", AnalogPause.ABOVE.value))
80-
81-
if analog_pause == "ABOVE":
82-
task.triggers.pause_trigger.anlg_lvl_when.ABOVE
83-
else:
84-
task.triggers.pause_trigger.anlg_lvl_when.BELOW
85-
86-
pause_when = PauseWhen(panel.get_value("pause_when",PauseWhen.HIGH.value))
87-
if pause_when == "High":
88-
task.triggers.pause_trigger.dig_lvl_when.HIGH
89-
else:
90-
task.triggers.pause_trigger.dig_lvl_when.LOW
91-
92-
new_edge = DigitalEdge(panel.get_value("edge_digital", DigitalEdge.FALLING.value))
93-
if new_edge != digital_edge:
94-
task.stop()
95-
if new_edge == "RISING":
96-
task.triggers.start_trigger.cfg_dig_edge_start_trig(trigger_source="/Dev2/PFI0", trigger_edge=Edge.RISING)
97-
else:
98-
task.triggers.start_trigger.cfg_dig_edge_start_trig(trigger_source="/Dev2/PFI0", trigger_edge=Edge.FALLING)
99-
task.start()
100-
digital_edge = new_edge
101-
102-
103-
task.triggers.start_trigger.cfg_dig_edge_start_trig
104-
105-
if panel.get_value("is_running", True):
106-
data = task.read(number_of_samples_per_channel=100)
107-
panel.set_value("voltage_data", data)
108-
panel.set_value("current_data", data)
109-
panel.set_value("strain_data", data)
110-
else:
111-
break
120+
level = panel.get_value("level", 0.0)
121+
analog_pause = AnalogPause(panel.get_value("analog_pause", AnalogPause.ABOVE.value))
122+
123+
if analog_pause == "ABOVE":
124+
task.triggers.pause_trigger.anlg_lvl_when.ABOVE
125+
else:
126+
task.triggers.pause_trigger.anlg_lvl_when.BELOW
127+
128+
pause_when = PauseWhen(panel.get_value("pause_when", PauseWhen.HIGH.value))
129+
if pause_when == "High":
130+
task.triggers.pause_trigger.dig_lvl_when.HIGH
131+
else:
132+
task.triggers.pause_trigger.dig_lvl_when.LOW
133+
134+
while not panel.get_value("stop_button", False):
135+
data = task.read(number_of_samples_per_channel=100)
136+
panel.set_value("voltage_data", data)
137+
panel.set_value("current_data", data)
138+
panel.set_value("strain_data", data)
139+
112140
except KeyboardInterrupt:
113-
raise
141+
pass
114142
finally:
115143
task.stop()
116144
panel.set_value("is_running", False)
145+
panel.set_value("run_button", False)
146+
117147

118-
119-
148+
except KeyboardInterrupt:
149+
pass

0 commit comments

Comments
 (0)