Skip to content

Commit 5a027ba

Browse files
DilmiWickramanayakeDilmi Wickramanayakehellovolcano
authored
Add Analog Input Example (#85)
* Analog Input - Voltage and Thermocouple Single Task * Analog input graph * Analog input graph * merge main * Update nidaqmx_continuous_analog_input.py * Analog Input - Voltage and Thermocouple Signed-off-by: Dilmi Wickramanayake <[email protected]> * Fixing formatting issues Signed-off-by: Dilmi Wickramanayake <[email protected]> * add daqmx and streamlit_echarts to mypy override Signed-off-by: Valerie Gleason 👌 <[email protected]> * add daqmx and streamlit_echarts to mypy override Signed-off-by: Valerie Gleason 👌 <[email protected]> --------- Signed-off-by: Dilmi Wickramanayake <[email protected]> Signed-off-by: Valerie Gleason 👌 <[email protected]> Co-authored-by: Dilmi Wickramanayake <[email protected]> Co-authored-by: Valerie Gleason 👌 <[email protected]>
1 parent dcaf336 commit 5a027ba

File tree

7 files changed

+179
-6
lines changed

7 files changed

+179
-6
lines changed

.github/workflows/check_nipanel.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424
path: .venv
2525
key: nipanel-${{ runner.os }}-py${{ steps.setup-python.outputs.python-version }}-${{ hashFiles('poetry.lock') }}
2626
- name: Install nipanel
27-
run: poetry install -v
27+
run: poetry install -v --with examples,docs
2828
- name: Lint
2929
run: poetry run ni-python-styleguide lint
3030
- name: Mypy static analysis (Linux)

examples/nidaqmx/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Prerequisites
2+
===============
3+
Requires a Physical or Simulated Device : https://github.com/ni/nidaqmx-python/blob/master/README.rst (Getting Started Section)
4+
5+
## Sample
6+
7+
This is a nipanel example that displays an interactive Streamlit app and updates continuous analog input examples.
8+
9+
### Feature
10+
11+
- Supports various data types
12+
13+
### Required Software
14+
15+
- Python 3.9 or later
16+
17+
### Usage
18+
19+
Run `poetry run examples/nidaqmx_continuous_analog_input/nidaqmx_continuous_analog_input.py`
20+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""Data acquisition script that continuously acquires analog input data."""
2+
3+
from pathlib import Path
4+
5+
import nidaqmx
6+
from nidaqmx.constants import AcquisitionType
7+
8+
import nipanel
9+
10+
panel_script_path = Path(__file__).with_name("nidaqmx_continuous_analog_input_panel.py")
11+
panel = nipanel.create_panel(panel_script_path)
12+
13+
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")
16+
task.timing.cfg_samp_clk_timing(
17+
rate=1000.0, sample_mode=AcquisitionType.CONTINUOUS, samps_per_chan=3000
18+
)
19+
panel.set_value("sample_rate", task._timing.samp_clk_rate)
20+
task.start()
21+
try:
22+
print(f"\nPress Ctrl + C to stop")
23+
while True:
24+
data = task.read(number_of_samples_per_channel=1000)
25+
panel.set_value("voltage_data", data[0])
26+
panel.set_value("thermocouple_data", data[1])
27+
except KeyboardInterrupt:
28+
pass
29+
finally:
30+
task.stop()
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
"""Streamlit visualization script to display data acquired by nidaqmx_continuous_analog_input.py."""
2+
3+
import streamlit as st
4+
from streamlit_echarts import st_echarts
5+
6+
import nipanel
7+
8+
panel = nipanel.get_panel_accessor()
9+
10+
st.title("Analog Input - Voltage and Thermocouple in a Single Task")
11+
voltage_tab, thermocouple_tab = st.tabs(["Voltage", "Thermocouple"])
12+
13+
st.markdown(
14+
"""
15+
<style>
16+
div[data-baseweb="select"] {
17+
width: 190px !important; /* Adjust the width as needed */
18+
}
19+
</style>
20+
""",
21+
unsafe_allow_html=True,
22+
)
23+
24+
thermocouple_data = panel.get_value("thermocouple_data", [0.0])
25+
voltage_data = panel.get_value("voltage_data", [0.0])
26+
27+
sample_rate = panel.get_value("sample_rate", 0)
28+
29+
st.header("Voltage & Thermocouple")
30+
voltage_therm_graph = {
31+
"tooltip": {"trigger": "axis"},
32+
"legend": {"data": ["Voltage (V)", "Temperature (C)"]},
33+
"xAxis": {
34+
"type": "category",
35+
"data": [x / sample_rate for x in range(len(voltage_data))],
36+
"name": "Time",
37+
"nameLocation": "center",
38+
"nameGap": 40,
39+
},
40+
"yAxis": {
41+
"type": "value",
42+
"name": "Measurement",
43+
"nameRotate": 90,
44+
"nameLocation": "center",
45+
"nameGap": 40,
46+
},
47+
"series": [
48+
{
49+
"name": "voltage_amplitude",
50+
"type": "line",
51+
"data": voltage_data,
52+
"emphasis": {"focus": "series"},
53+
"smooth": True,
54+
"seriesLayoutBy": "row",
55+
},
56+
{
57+
"name": "thermocouple_amplitude",
58+
"type": "line",
59+
"data": thermocouple_data,
60+
"color": "red",
61+
"emphasis": {"focus": "series"},
62+
"smooth": True,
63+
"seriesLayoutBy": "row",
64+
},
65+
],
66+
}
67+
st_echarts(options=voltage_therm_graph, height="400px")
68+
69+
voltage_tab.header("Voltage")
70+
with voltage_tab:
71+
left_volt_tab, center_volt_tab, right_volt_tab = st.columns(3)
72+
with left_volt_tab:
73+
st.selectbox(options=["Dev1/ai0"], label="Physical Channels", disabled=True)
74+
st.selectbox(options=["Off"], label="Logging Modes", disabled=False)
75+
with center_volt_tab:
76+
st.selectbox(options=["-5"], label="Min Value")
77+
st.selectbox(options=["5"], label="Max Value")
78+
st.selectbox(options=["1000"], label="Samples per Loops", disabled=False)
79+
with right_volt_tab:
80+
st.selectbox(options=["default"], label="Terminal Configurations")
81+
st.selectbox(options=["OnboardClock"], label="Sample Clock Sources", disabled=False)
82+
83+
84+
thermocouple_tab.header("Thermocouple")
85+
with thermocouple_tab:
86+
left, middle, right = st.columns(3)
87+
with left:
88+
st.selectbox(options=["Dev1/ai1"], label="Physical Channel", disabled=True)
89+
st.selectbox(options=["0"], label="Min", disabled=False)
90+
st.selectbox(options=["100"], label="Max", disabled=False)
91+
st.selectbox(options=["Off"], label="Logging Mode", disabled=False)
92+
93+
with middle:
94+
st.selectbox(options=["Deg C"], label="Units", disabled=False)
95+
st.selectbox(options=["J"], label="Thermocouple Type", disabled=False)
96+
st.selectbox(options=["Constant Value"], label="CJC Source", disabled=False)
97+
st.selectbox(options=["1000"], label="Samples per Loop", disabled=False)
98+
with right:
99+
st.selectbox(options=["25"], label="CJC Value", disabled=False)
100+
st.selectbox(options=["OnboardClock"], label="Sample Clock Source", disabled=False)
101+
st.selectbox(options=[" "], label="Actual Sample Rate", disabled=True)

examples/simple_graph/simple_graph_panel.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
"""A Streamlit visualization panel for the simple_graph.py example script."""
22

33
import streamlit as st
4-
from streamlit_echarts import st_echarts # type: ignore
4+
from streamlit_echarts import st_echarts
55

66
import nipanel
77

8-
98
panel = nipanel.get_panel_accessor()
109

1110
st.set_page_config(page_title="Simple Graph Example", page_icon="📈", layout="wide")

poetry.lock

Lines changed: 20 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ protobuf = {version=">=4.21"}
1313
ni-measurement-plugin-sdk = {version=">=2.3"}
1414
typing-extensions = ">=4.13.2"
1515
streamlit = ">=1.24"
16+
streamlit-echarts = ">=0.4.0"
1617
nitypes = {version=">=0.1.0dev2", allow-prereleases=true}
1718
debugpy = ">=1.8.1"
1819

@@ -57,6 +58,7 @@ optional = true
5758

5859
[tool.poetry.group.examples.dependencies]
5960
streamlit-echarts = ">=0.4.0"
61+
nidaqmx = ">=0.8.0"
6062

6163
[build-system]
6264
requires = ["poetry-core>=1.8.0"]
@@ -76,9 +78,11 @@ strict = true
7678

7779
[[tool.mypy.overrides]]
7880
module = [
79-
# https://github.com/ni/hightime/issues/4 - Add type annotations
8081
"hightime.*",
8182
"grpc.framework.foundation.*",
83+
"streamlit_echarts.*",
84+
# https://github.com/ni/nidaqmx-python/issues/209 - Support type annotations
85+
"nidaqmx.*",
8286
]
8387
ignore_missing_imports = true
8488

@@ -93,4 +97,4 @@ testpaths = ["src/nipanel", "tests"]
9397

9498
[tool.pyright]
9599
include = ["examples/", "src/", "tests/"]
96-
exclude = ["src/ni/protobuf/types/", "src/ni/pythonpanel/v1/"]
100+
exclude = ["src/ni/protobuf/types/", "src/ni/pythonpanel/v1/","examples/nidaqmx/nidaqmx_continuous_analog_input.py"]

0 commit comments

Comments
 (0)