Skip to content

Commit 1bfa9ad

Browse files
author
Dilmi Wickramanayake
committed
merge main
1 parent f2c476a commit 1bfa9ad

File tree

2 files changed

+22
-72
lines changed

2 files changed

+22
-72
lines changed

examples/nidaqmx_continuous_analog_input/nidaqmx_continuous_analog_input.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
"""This is the logic that will read/set values to panel."""
1+
"""Streamlit application script for displaying values from nidaqmx_continuous_analog_input.py"""
22

33
import pathlib
4-
import time
54

65
import nidaqmx
6+
from nidaqmx.constants import AcquisitionType
7+
78

89
import nipanel
910

@@ -13,25 +14,27 @@
1314
panel = nipanel.StreamlitPanel(
1415
panel_id="nidaqmx_continuous_analog_input_panel",
1516
streamlit_script_path=panel_script_path,
17+
1618
)
1719

18-
data_arr = []
1920
with nidaqmx.Task() as task:
21+
# User needs simulated hardware to run script, and user should use NI MAX to setup cDAQ1 Model NI 9201
2022
task.ai_channels.add_ai_voltage_chan("Mod1/ai2")
2123
task.ai_channels.add_ai_thrmcpl_chan("Mod1/ai3")
24+
task.timing.cfg_samp_clk_timing(
25+
rate=1000.0, # Sample rate in Hz
26+
sample_mode=AcquisitionType.CONTINUOUS,
27+
samps_per_chan=3000 # Buffer size per channel
28+
)
29+
task.start()
2230
try:
2331
total_read = 0
2432
while True:
25-
data = task.read(number_of_samples_per_channel=3)
26-
read = len(data)
27-
total_read += read
28-
29-
data_arr.append(data)
30-
time.sleep(1)
31-
panel.set_value("amplitude", data_arr[-1][-1])
32-
panel.set_value("Volts", data_arr[-1][0])
33+
print(f"\nPress Ctrl + C to stop")
34+
data = task.read(number_of_samples_per_channel=1000)
35+
panel.set_value("voltage_data", data[0])
36+
panel.set_value("thermocouple_data", data[1])
3337
except KeyboardInterrupt:
3438
pass
3539
finally:
3640
task.stop()
37-
print(f"\nAcquired {total_read} total samples.")

examples/nidaqmx_continuous_analog_input/nidaqmx_continuous_analog_input_panel.py

Lines changed: 7 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
1-
"""This is the panel that will use the logic."""
1+
"""Streamlit application script for displaying values using nipanel package."""
22

33
import streamlit as st
4-
import streamlit.components.v1 as components
54
from streamlit_echarts import st_echarts
65

76
import nipanel
87

98
panel = nipanel.StreamlitPanelValueAccessor(panel_id="nidaqmx_continuous_analog_input_panel")
109

11-
add_refresh_component = components.declare_component(
12-
"panelRefreshComponent",
13-
url=f"http://localhost:42001/panels/refresh/{panel.panel_id}",
14-
)
15-
add_refresh_component()
10+
panel = nipanel.initialize_panel()
1611

1712

1813
st.title("Analog Input - Voltage and Thermocouple in a Single Task")
@@ -30,81 +25,33 @@
3025
)
3126

3227

33-
list_of_therm_amp = panel.get_value("amplitude")
34-
list_of_voltage_amp = panel.get_value("Volts")
35-
36-
if "therm_history" not in st.session_state:
37-
st.session_state.therm_history = []
38-
if "volts_history" not in st.session_state:
39-
st.session_state.volts_history = []
28+
list_of_therm_amp = panel.get_value("thermocouple_data", [0.0])
29+
list_of_voltage_amp = panel.get_value("voltage_data", [0.0])
4030

41-
for therm_amp in list_of_therm_amp:
42-
st.session_state.therm_history.append(therm_amp)
43-
for voltage_amp in list_of_voltage_amp:
44-
st.session_state.volts_history.append(voltage_amp)
45-
46-
therm_amp_graph = {
47-
"tooltip": {"trigger": "axis"},
48-
"legend": {"data": ["thermocouple_amplitude"]},
49-
"xAxis": {
50-
"type": "category",
51-
"data": list(range(len(st.session_state.therm_history))),
52-
"name": "Time",
53-
},
54-
"yAxis": {"type": "value", "name": "Thermocouple Amplitude"},
55-
"series": [
56-
{
57-
"name": "thermocouple_amplitude",
58-
"type": "line",
59-
"data": st.session_state.therm_history,
60-
"color": "red",
61-
},
62-
],
63-
}
64-
st_echarts(options=therm_amp_graph, height="400px")
65-
66-
voltage_amp_graph = {
67-
"tooltip": {"trigger": "axis"},
68-
"legend": {"data": ["voltage_amplitude"]},
69-
"xAxis": {
70-
"type": "category",
71-
"data": list(range(len(st.session_state.volts_history))),
72-
"name": "Time",
73-
},
74-
"yAxis": {"type": "value", "name": "Voltage Amplitude"},
75-
"series": [
76-
{
77-
"name": "voltage_amplitude",
78-
"type": "line",
79-
"data": st.session_state.volts_history,
80-
},
81-
],
82-
}
83-
st_echarts(options=voltage_amp_graph, height="400px")
8431

8532
st.header("Voltage & Thermocouple")
8633
voltage_therm_graph = {
8734
"tooltip": {"trigger": "axis"},
8835
"legend": {"data": ["voltage_amplitude", "thermocouple_amplitude"]},
8936
"xAxis": {
9037
"type": "category",
91-
"data": list(range(len(st.session_state.volts_history))),
38+
"data": list(range(len(list_of_voltage_amp))),
9239
"name": "Time",
9340
},
9441
"yAxis": {"type": "value", "name": "Voltage and Thermocouple Amplitude"},
9542
"series": [
9643
{
9744
"name": "voltage_amplitude",
9845
"type": "line",
99-
"data": st.session_state.volts_history,
46+
"data": list_of_voltage_amp,
10047
"emphasis": {"focus": "series"},
10148
"smooth": True,
10249
"seriesLayoutBy": "row",
10350
},
10451
{
10552
"name": "thermocouple_amplitude",
10653
"type": "line",
107-
"data": st.session_state.therm_history,
54+
"data": list_of_therm_amp,
10855
"color": "red",
10956
"emphasis": {"focus": "series"},
11057
"smooth": True,

0 commit comments

Comments
 (0)