Skip to content

Commit b7abc39

Browse files
author
Dilmi Wickramanayake
committed
Analog Input - Voltage and Thermocouple Single Task
1 parent f05a081 commit b7abc39

File tree

5 files changed

+394
-2
lines changed

5 files changed

+394
-2
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""This example demonstrates how to open/update a Streamlit application using nipanel package."""
2+
3+
import nipanel
4+
import nidaqmx
5+
import pathlib
6+
import time
7+
8+
script_path = pathlib.Path(__file__)
9+
panel_script_path = str(script_path.with_name("nidaqmx_continuous_analog_input_panel.py"))
10+
11+
panel = nipanel.StreamlitPanel(
12+
panel_id="nidaqmx_continuous_analog_input_panel",
13+
streamlit_script_path=panel_script_path,
14+
)
15+
16+
data_arr = []
17+
with nidaqmx.Task() as task:
18+
task.ai_channels.add_ai_voltage_chan("Mod1/ai2")
19+
task.ai_channels.add_ai_thrmcpl_chan("Mod1/ai3")
20+
try:
21+
total_read = 0
22+
while True:
23+
data = task.read(number_of_samples_per_channel=3)
24+
read = len(data)
25+
total_read += read
26+
27+
data_arr.append(data)
28+
time.sleep(1)
29+
panel.set_value("amplitude",data_arr[-1][-1])
30+
panel.set_value("Volts",data_arr[-1][0])
31+
except KeyboardInterrupt:
32+
pass
33+
finally:
34+
task.stop()
35+
print(f"\nAcquired {total_read} total samples.")
36+
37+
38+
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
"""Streamlit application script for displaying values using nipanel package."""
2+
import nipanel
3+
import streamlit as st
4+
import streamlit.components.v1 as components
5+
from streamlit_echarts import st_echarts
6+
7+
panel = nipanel.StreamlitPanelValueAccessor(panel_id="nidaqmx_continuous_analog_input_panel")
8+
9+
add_refresh_component = components.declare_component(
10+
"panelRefreshComponent",
11+
url=f"http://localhost:42001/panels/refresh/{panel.panel_id}",)
12+
add_refresh_component()
13+
14+
15+
st.title("Analog Input - Voltage and Thermocouple in a Single Task")
16+
voltage_tab, thermocouple_tab = st.tabs(["Voltage", "Thermocouple"])
17+
18+
st.markdown(
19+
"""
20+
<style>
21+
div[data-baseweb="select"] {
22+
width: 190px !important; /* Adjust the width as needed */
23+
}
24+
</style>
25+
""",
26+
unsafe_allow_html=True
27+
)
28+
29+
30+
list_of_therm_amp = panel.get_value("amplitude")
31+
list_of_voltage_amp = panel.get_value("Volts")
32+
33+
if "therm_history" not in st.session_state:
34+
st.session_state.therm_history = []
35+
if "volts_history" not in st.session_state:
36+
st.session_state.volts_history = []
37+
38+
for therm_amp in list_of_therm_amp:
39+
st.session_state.therm_history.append(therm_amp)
40+
for voltage_amp in list_of_voltage_amp:
41+
st.session_state.volts_history.append(voltage_amp)
42+
43+
therm_amp_graph = {
44+
"tooltip": {"trigger": "axis"},
45+
"legend": {"data": ["thermocouple_amplitude"]},
46+
"xAxis": {
47+
"type": "category",
48+
"data":list(range(len(st.session_state.therm_history))),
49+
"name": "Time"
50+
},
51+
"yAxis": {
52+
"type": "value",
53+
"name": "Thermocouple Amplitude"
54+
},
55+
"series": [
56+
{
57+
"name": "thermocouple_amplitude",
58+
"type": "line",
59+
"data": st.session_state.therm_history,
60+
"color": "red"
61+
},
62+
63+
64+
],
65+
}
66+
st_echarts(options=therm_amp_graph, height="400px")
67+
68+
voltage_amp_graph = {
69+
"tooltip": {"trigger": "axis"},
70+
"legend": {"data": ["voltage_amplitude"]},
71+
"xAxis": {
72+
"type": "category",
73+
"data": list(range(len(st.session_state.volts_history))),
74+
"name": "Time"
75+
},
76+
"yAxis": {
77+
"type": "value",
78+
"name": "Voltage Amplitude"
79+
},
80+
"series": [
81+
{
82+
"name": "voltage_amplitude",
83+
"type": "line",
84+
"data": st.session_state.volts_history,
85+
},
86+
87+
],
88+
}
89+
st_echarts(options=voltage_amp_graph, height="400px")
90+
91+
st.header("Voltage & Thermocouple")
92+
voltage_therm_graph = {
93+
"tooltip": {"trigger": "axis"},
94+
"legend": {"data": ["voltage_amplitude", "thermocouple_amplitude"]},
95+
"xAxis": {
96+
"type": "category",
97+
"data": list(range(len(st.session_state.volts_history))),
98+
"name": "Time"
99+
},
100+
"yAxis": {
101+
"type": "value",
102+
"name": "Voltage and Thermocouple Amplitude"
103+
},
104+
"series": [
105+
{
106+
"name": "voltage_amplitude",
107+
"type": "line",
108+
"data": st.session_state.volts_history,
109+
"emphasis": {"focus":"series"},
110+
"smooth": True,
111+
"seriesLayoutBy": "row",
112+
},
113+
{
114+
"name": "thermocouple_amplitude",
115+
"type": "line",
116+
"data": st.session_state.therm_history,
117+
"color": "red",
118+
"emphasis": {"focus":"series"},
119+
"smooth": True,
120+
"seriesLayoutBy": "row",
121+
},
122+
],
123+
}
124+
st_echarts(options=voltage_therm_graph, height="400px")
125+
126+
with voltage_tab:
127+
left_volt_tab, center_volt_tab, right_volt_tab = st.columns(3)
128+
with left_volt_tab:
129+
st.selectbox(options=["Mod1/ai2"], label="Physical Channels", disabled=True)
130+
st.selectbox(options=["Off"], label="Logging Modes", disabled=False)
131+
with center_volt_tab:
132+
st.selectbox(options=["-5"],label="Min Value")
133+
st.selectbox(options=["5"],label="Max Value")
134+
st.selectbox(options=["1000"], label="Samples per Loops", disabled=False)
135+
with right_volt_tab:
136+
st.selectbox(options=["default"], label="Terminal Configurations")
137+
st.selectbox(options=["OnboardClock"], label="Sample Clock Sources", disabled=False)
138+
139+
140+
thermocouple_tab.header("Thermocouple")
141+
with thermocouple_tab:
142+
left, middle, right = st.columns(3)
143+
with left:
144+
st.selectbox(options=["Mod1/ai3"], label="Physical Channel", disabled=True)
145+
st.selectbox(options=["0"], label="Min", disabled=False)
146+
st.selectbox(options=["100"], label="Max", disabled=False)
147+
st.selectbox(options=["Off"], label="Logging Mode", disabled=False)
148+
149+
with middle:
150+
st.selectbox(options=["Deg C"], label = "Units", disabled=False)
151+
st.selectbox(options=["J"], label="Thermocouple Type", disabled=False)
152+
st.selectbox(options=["Constant Value"], label="CJC Source", disabled=False)
153+
st.selectbox(options=["1000"], label="Samples per Loop", disabled=False)
154+
with right:
155+
st.selectbox(options=["25"],label="CJC Value", disabled=False)
156+
st.selectbox(options=["OnboardClock"], label="Sample Clock Source", disabled=False)
157+
st.selectbox(options=[" "], label="Actual Sample Rate", disabled=True)
158+
159+
160+
161+
162+
163+
164+
165+
166+
167+
168+

examples/sample/sample.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
streamlit_script_path=panel_script_path,
1313
)
1414
panel.set_value("sample_string", "Hello, World!")
15-
panel.set_value("sample_int", 42)
15+
panel.set_value("sample_int", 6)
1616
panel.set_value("sample_float", 3.14)
1717
panel.set_value("sample_bool", True)
1818
panel.set_value("float_values", [1.1, 2.2, 3.3])

0 commit comments

Comments
 (0)