Skip to content

Commit a8f847b

Browse files
author
Dilmi Wickramanayake
committed
Analog Input Filtering
1 parent f1395d6 commit a8f847b

File tree

4 files changed

+220
-53
lines changed

4 files changed

+220
-53
lines changed

examples/nidaqmx/nidaqmx_continuous_analog_input.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
panel = nipanel.create_panel(panel_script_path)
1212

1313
with nidaqmx.Task() as task:
14-
task.ai_channels.add_ai_voltage_chan("Dev1/ai0")
15-
task.ai_channels.add_ai_thrmcpl_chan("Dev1/ai1")
14+
task.ai_channels.add_ai_voltage_chan("Mod1/ai0")
15+
task.ai_channels.add_ai_thrmcpl_chan("Mod1/ai1")
1616
task.timing.cfg_samp_clk_timing(
1717
rate=1000.0, sample_mode=AcquisitionType.CONTINUOUS, samps_per_chan=3000
1818
)

examples/nidaqmx_analog_input_filtering/nidaqmx_analog_input_filtering.py

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,54 @@
44

55

66
import nidaqmx
7-
from nidaqmx.constants import AcquisitionType, ExcitationSource, Edge, Slope
8-
from settings_enum import DigitalEdge, PauseWhen, Slopes, AnalogPause
7+
from nidaqmx.constants import AcquisitionType, ExcitationSource, Edge, Slope, Timescale, TerminalConfiguration
8+
from settings_enum import DigitalEdge, PauseWhen, Slopes, AnalogPause, TerminalConfig
99
import nipanel
1010
import time
1111

1212
panel_script_path = Path(__file__).with_name("nidaqmx_analog_input_filtering_panel.py")
1313
panel = nipanel.create_panel(panel_script_path)
1414

1515

16-
1716
with nidaqmx.Task() as task:
18-
chan_voltage = task.ai_channels.add_ai_voltage_chan("Mod3/ai10")
19-
chan_strain_gage = task.ai_channels.add_ai_strain_gage_chan("Mod3/ai7", voltage_excit_source=ExcitationSource.EXTERNAL)
20-
chan_current = task.ai_channels.add_ai_current_chan("Mod3/ai9")
17+
terminal_config = TerminalConfig(panel.get_value("terminal_config", TerminalConfig.RSE.value))
18+
chan_voltage = task.ai_channels.add_ai_voltage_chan("Mod3/ai10", terminal_config=TerminalConfiguration.DEFAULT)
19+
chan_strain_gage = task.ai_channels.add_ai_strain_gage_chan("Mod3/ai7", voltage_excit_source=ExcitationSource.EXTERNAL, max_val=0.001, min_val=-0.001)
20+
chan_current = task.ai_channels.add_ai_current_chan("Mod3/ai9", max_val = 0.01, min_val= -0.01, ext_shunt_resistor_val=249)
2121
task.timing.cfg_samp_clk_timing(
22-
rate = 1000.0, sample_mode = AcquisitionType.CONTINUOUS, samps_per_chan = 3000
22+
rate = 1000.0, sample_mode = AcquisitionType.CONTINUOUS, samps_per_chan = 1000
2323
)
24-
24+
2525
task.timing.samp_clk_dig_fltr_min_pulse_width = 20.0e-6
2626
task.timing.samp_clk_dig_fltr_enable = True
2727
chan_voltage.ai_filter_response
28+
filter = panel.get_value("filter", "No Filtering")
29+
if filter == "No Filtering":
30+
chan_voltage.ai_filter_freq = 0.0
31+
chan_current.ai_filter_freq = 0.0
32+
chan_strain_gage.ai_filter_freq = 0.0
33+
34+
chan_voltage.ai_filter_order = 0
35+
chan_current.ai_filter_order = 0
36+
chan_strain_gage.ai_filter_order = 0
37+
38+
chan_voltage.ai_filter_response = panel.get_value("filter_response", "Comb")
39+
chan_current.ai_filter_response = panel.get_value("filter_response", "Comb")
40+
chan_strain_gage.ai_filter_response = panel.get_value("filter_response", "Comb")
41+
else:
42+
chan_voltage.ai_filter_freq = panel.get_value("filter_freq", 1000.0)
43+
chan_current.ai_filter_freq = panel.get_value("filter_freq", 1000.0)
44+
chan_strain_gage.ai_filter_freq = panel.get_value("filter_freq", 1000.0)
45+
46+
chan_voltage.ai_filter_order = panel.get_value("filter_order", 1)
47+
chan_current.ai_filter_order = panel.get_value("filter_order", 1)
48+
chan_strain_gage.ai_filter_order = panel.get_value("filter_order", 1)
49+
50+
chan_voltage.ai_filter_response = panel.get_value("filter_response", "Comb")
51+
chan_current.ai_filter_response = panel.get_value("filter_response", "Comb")
52+
chan_strain_gage.ai_filter_response = panel.get_value("filter_response", "Comb")
53+
54+
2855

2956
panel.set_value("source", [task.triggers.pause_trigger.dig_lvl_src])
3057
panel.set_value("analog_source", [task.triggers.start_trigger.anlg_edge_src])
@@ -49,7 +76,11 @@
4976
print(f"Press Ctrl + C to stop")
5077

5178
while True:
79+
date = panel.get_value("date", None)
80+
time_input = panel.get_value("time", None)
5281

82+
hysteriresis = panel.get_value("hysteriesis", 0.0)
83+
task.triggers.start_trigger.cfg_time_start_trig(when = date, timescale=Timescale.USE_HOST)
5384
level = panel.get_value("level", 0.0)
5485
new_slope = Slopes(panel.get_value("slope",Slopes.FALLING.value))
5586
if slope != new_slope:
@@ -59,6 +90,7 @@
5990
task.triggers.start_trigger.cfg_anlg_edge_start_trig(trigger_source="APFI0", trigger_level=level, trigger_slope= Slope.RISING)
6091
else:
6192
task.triggers.start_trigger.cfg_anlg_edge_start_trig(trigger_source="APFI0", trigger_level=level, trigger_slope= Slope.FALLING)
93+
task.triggers.start_trigger.anlg_edge_hyst = hysteriresis
6294
task.start()
6395
slope = new_slope
6496

@@ -88,21 +120,7 @@
88120

89121

90122
task.triggers.start_trigger.cfg_dig_edge_start_trig
91-
# task.triggers.start_trigger.cfg_dig_edge_start_trig(trigger_source="/Dev2/te1/SampleClock")
92-
# task.triggers.start_trigger.cfg_time_start_trig(10)
93-
# task.triggers.start_trigger.cfg_anlg_edge_start_trig(trigger_source="APFI0")
94-
# Configure a digital pause trigger (example)
95-
# Replace "PFI0" with your actual trigger source if needed
96-
97-
# task.triggers.pause_trigger.trig_type = TriggerType.DIGITAL_LEVEL
98-
# task.triggers.pause_trigger.dig_lvl_src = "/Dev2/PFI0"
99-
100-
# task.triggers.pause_trigger.dig_lvl_when
101-
# task.triggers.pause_trigger.trig_type = TriggerType.ANALOG_LEVEL
102-
# task.triggers.pause_trigger.anlg_lvl_src
103-
# task.triggers.pause_trigger.anlg_lvl_when
104-
105-
123+
106124
data = task.read(number_of_samples_per_channel=100)
107125
panel.set_value("voltage_data", data[0])
108126
panel.set_value("current_data", data[2])

examples/nidaqmx_analog_input_filtering/nidaqmx_analog_input_filtering_panel.py

Lines changed: 167 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import streamlit as st
44
from streamlit_echarts import st_echarts
5-
from settings_enum import DigitalEdge, PauseWhen, Slopes, AnalogPause
5+
from settings_enum import DigitalEdge, PauseWhen, Slopes, AnalogPause, TerminalConfig
66
import time
77
import nipanel
88

@@ -13,8 +13,6 @@
1313
panel = nipanel.get_panel_accessor()
1414

1515

16-
tabs = st.tabs(["Voltage", "Current", "Strain Gage"])
17-
1816
st.markdown(
1917
"""
2018
<style>
@@ -43,24 +41,169 @@
4341

4442
sample_rate = panel.get_value("sample_rate", 0.0)
4543

46-
col_chart, settings = st.columns([1,1.5]) # Chart wider than buttons
47-
with settings:
44+
left_col, right_col = st.columns(2)
45+
with right_col:
46+
with st.container(border=True):
47+
48+
st.title("Channel Settings")
49+
st.selectbox(options=["Mod3/ai10"], index=0, label="Physical Channels", disabled=True)
50+
51+
terminal_config = st.selectbox(
52+
"terminal_config",
53+
options=[(e.name, e.value) for e in TerminalConfig],
54+
format_func=lambda x: x[0],
55+
index=0,
56+
)
57+
terminal_config = TerminalConfig[terminal_config[0]]
58+
panel.set_value("terminal_config", terminal_config)
59+
60+
st.title("Timing Settings")
61+
st.selectbox("Sample Clock Source", options=["Onboard Clock"], index=0)
62+
st.number_input("Sample Rate", value=1000, min_value=1, step=1)
63+
st.number_input("Number of Samples", value=100, min_value=1, step=1)
64+
st.selectbox("Actual Sample Rate", options=["placeholder"], disabled=True)
65+
66+
st.title("Logging Settings")
67+
st.selectbox("Logging Mode", options=["Off", "Log and Read"], index=0)
68+
st.text_input("TDMS File Path", value="data.tdms")
69+
70+
st.title("Filtering Settings")
71+
filter = st.selectbox("Filter", options = ["No Filtering", "Filter"])
72+
filter_freq = st.number_input("Filtering Frequency", value = 1000)
73+
filter_order = st.number_input("Filter Order", min_value=0, max_value=1)
74+
filter_response = st.selectbox("Filter Response", options=["Comb","Butterworth", "Brickwall", "Bessel"])
75+
st.selectbox("Actual Filter Frequency", options=["placeholder"], disabled=True)
76+
st.selectbox("Actual Filter Order", options=[filter_order], disabled=True)
77+
st.selectbox("Actual Filter Response", options=[filter_response], disabled=True)
78+
79+
80+
with left_col:
81+
with st.container(border=True):
82+
tabs = st.tabs(["Voltage", "Current", "Strain Gage"])
83+
with tabs[0]:
84+
st.title("Voltage Data")
85+
channel_left, channel_right = st.columns(2)
86+
with channel_left:
87+
st.number_input(
88+
"Min Value",
89+
value=-5.0,
90+
step=0.1,
91+
key="min_value_voltage",
92+
)
93+
st.number_input(
94+
"Max Value",
95+
value=5.0,
96+
step=0.1,
97+
key="max_value_voltage",
98+
)
99+
100+
with tabs[1]:
101+
st.title("Current Data")
102+
channel_left, channel_right = st.columns(2)
103+
with channel_left:
104+
st.selectbox(options=["default", "Internal", "External"], index=0, label="shunt resistor location", disabled=False)
105+
st.selectbox(options=["Amps", "From Custom"], label="Units", index=0, disabled=False)
106+
st.number_input(
107+
"Min Value",
108+
value=-0.01,
109+
step=0.001,
110+
key="min_value_current",
111+
)
112+
panel.set_value("min_value_current", "min_value_current")
113+
st.number_input(
114+
"Max Value",
115+
value=0.01,
116+
step=0.001,
117+
key="max_value_current",
118+
)
119+
panel.set_value("max_value_current", "max_value_current")
120+
st.number_input(
121+
"Shunt Resistor Value",
122+
value=249.0,
123+
step=1.0,
124+
key="shunt_resistor_value",
125+
)
126+
panel.set_value("shunt_resistor_value", "shunt_resistor_value")
127+
with tabs[2]:
128+
st.title("Strain Gage Data")
129+
channel_left, channel_right = st.columns(2)
130+
with channel_left:
131+
st.number_input(
132+
"Min Value",
133+
value=-0.001,
134+
step=0.0001,
135+
key="min_value_strain",
136+
)
137+
panel.set_value("min_value_strain", "min_value_strain")
138+
st.number_input(
139+
"Max Value",
140+
value=0.001,
141+
step=0.0001,
142+
key="max_value_strain",
143+
)
144+
panel.set_value("max_value_strain", "max_value_strain")
145+
st.title("strain gage information")
146+
st.number_input(
147+
"Gage Factor",
148+
value=2.0,
149+
step=1,
150+
key="gage_factor",
151+
)
152+
panel.set_value("gage_factor", "gage_factor")
153+
st.number_input(
154+
"nominal gage resistance",
155+
value=350.0,
156+
step=1.0,
157+
key="nominal_resistance",
158+
)
159+
panel.set_value("nominal_resistance", "nominal_resistance")
160+
st.number_input(
161+
"poisson ratio",
162+
value=0.3,
163+
step=1.0,
164+
key="poisson_ratio",
165+
)
166+
panel.set_value("poisson_ratio", "poisson_ratio")
167+
st.title("bridge information")
168+
st.selectbox(label="strain configuration", options=["Full Bridge I", "Quarter Bridge", "Half Bridge II", "Half Bridge I", "Full Bridge III", "Full Bridge II"], key="strain_config",index=0, disabled=False)
169+
panel.set_value("strain_config", "strain_config")
170+
st.number_input(
171+
"lead wire resistance",
172+
value=0.0,
173+
step=1.0,
174+
key="lead_wire_resistance",
175+
)
176+
panel.set_value("lead_wire_resistance", "lead_wire_resistance")
177+
st.number_input(
178+
"initial bridge voltage",
179+
value=0.0,
180+
step=1.0,
181+
key="initial_bridge_voltage",
182+
)
183+
panel.set_value("initial_bridge_voltage", "initial_bridge_voltage")
184+
st.selectbox(label="voltage excitation source", key = "voltage_excit",options=["External"], disabled=True)
185+
panel.set_value("voltage_excitation_source", "voltage_excit")
186+
st.number_input(
187+
"voltage excitation value",
188+
value=2.5,
189+
step=1.0,
190+
key="voltage_excitation_value",
191+
)
192+
panel.set_value("voltage_excitation_value", "voltage_excitation_value")
193+
48194
tab1, tab2, tab3, tab4, tab5, tab6, tab7, tab8 = st.tabs(["No Trigger","Digital Start","Digital Pause","Digital Reference","Analog Start","Analog Pause", "Analog Reference", "Time Start"])
49195
with tab1:
50196
st.write("To enable triggers, select a tab above, and configure the settings. \n Not all hardware supports all trigger types. Refer to your device documentation for more information.")
51197
with tab2:
52-
nipanel.enum_selectbox(
53-
198+
st.selectbox("Source->", source)
199+
edge_digital = st.selectbox(
200+
"Edge",
201+
options=[(e.name, e.value) for e in DigitalEdge],
202+
format_func=lambda x: x[0],
203+
index=0,
54204
)
55-
# st.selectbox("Source->", source)
56-
# edge_digital = st.selectbox(
57-
# "Edge",
58-
# options=[(e.name, e.value) for e in DigitalEdge],
59-
# format_func=lambda x: x[0],
60-
# index=0,
61-
# )
62-
# edge_digital = DigitalEdge[edge_digital[0]]
63-
# panel.set_value("edge_digital", edge_digital)
205+
edge_digital = DigitalEdge[edge_digital[0]]
206+
panel.set_value("edge_digital", edge_digital)
64207

65208
with tab3:
66209
st.selectbox("Source-", source)
@@ -104,13 +247,10 @@
104247
with tab7:
105248
st.write("This trigger type is not supported in continuous sample timing. Refer to your device documentation for more information on which triggers are supported.")
106249
with tab8:
107-
if st.button("Select time Manually"):
108-
time = st.time_input("When?")
109-
st.write(time)
250+
date = st.date_input("Date", value=None)
251+
time_input = st.time_input("Time", value=None)
110252
st.write("If time is not manually set, task will begin after 10 seconds")
111253

112-
113-
with tabs[0]:
114254
graph = {
115255
"animation": False,
116256
"tooltip": {"trigger": "axis"},
@@ -140,9 +280,10 @@
140280
},
141281
],
142282
}
283+
with tabs[0]:
143284
st_echarts(options=graph, height="400px", key="graph", width="50%")
144-
with tabs[1]:
145-
graph_2= {
285+
286+
graph_2= {
146287
"animation": False,
147288
"tooltip": {"trigger": "axis"},
148289
"legend": {"data": ["Voltage (V)"]},
@@ -171,10 +312,9 @@
171312
},
172313
],
173314
}
315+
with tabs[1]:
174316
st_echarts(options=graph, height="400px", key="graph_2", width="50%")
175-
176-
with tabs[2]:
177-
graph_3 = {
317+
graph_3 = {
178318
"animation": False,
179319
"tooltip": {"trigger": "axis"},
180320
"legend": {"data": ["Voltage (V)"]},
@@ -203,6 +343,7 @@
203343
},
204344
],
205345
}
346+
with tabs[2]:
206347
st_echarts(options=graph_3, height="400px", key="graph_3", width="50%")
207348

208349

examples/nidaqmx_analog_input_filtering/settings_enum.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
12
"""Enumeration for amplitude values used in the simple graph example."""
23

34
import enum
4-
5+
from nidaqmx.constants import Edge, Slope, Timescale, TerminalConfiguration
56
class DigitalEdge(enum.StrEnum):
67
FALLING = "FALLING"
78
RISING = "RISING"
@@ -16,4 +17,11 @@ class Slopes(enum.StrEnum):
1617

1718
class AnalogPause(enum.StrEnum):
1819
ABOVE = "ABOVE"
19-
BELOW = "BELOW"
20+
BELOW = "BELOW"
21+
22+
class TerminalConfig(enum.StrEnum):
23+
DEFAULT = "DEFAULT"
24+
RSE = "RSE"
25+
NRSE = "NRSE"
26+
DIFF = "DIFF"
27+
PSEUDO_DIFF = "PSEUDO_DIFF"

0 commit comments

Comments
 (0)