|
| 1 | +"""Streamlit visualization script to display data acquired by nidaqmx_continuous_analog_input.py.""" |
| 2 | + |
| 3 | +from typing import cast |
| 4 | + |
| 5 | +import hightime as ht |
| 6 | +import streamlit as st |
| 7 | +from nidaqmx.constants import ( |
| 8 | + TerminalConfiguration, |
| 9 | +) |
| 10 | +from nitypes.waveform import AnalogWaveform |
| 11 | +from streamlit_echarts import st_echarts |
| 12 | + |
| 13 | +import nipanel |
| 14 | +from nipanel.controls import enum_selectbox |
| 15 | + |
| 16 | +st.set_page_config( |
| 17 | + page_title="NI-DAQmx - Analog Input - Voltage with FFT", page_icon="📈", layout="wide" |
| 18 | +) |
| 19 | + |
| 20 | +st.markdown( |
| 21 | + """ |
| 22 | + <style> |
| 23 | + div[data-baseweb="select"] { |
| 24 | + max-width: 250px !important; |
| 25 | + } |
| 26 | + div.stNumberInput { |
| 27 | + max-width: 250px !important; |
| 28 | + } |
| 29 | + div.stTextInput { |
| 30 | + max-width: 250px !important; |
| 31 | + } |
| 32 | + </style> |
| 33 | + """, |
| 34 | + unsafe_allow_html=True, |
| 35 | +) |
| 36 | + |
| 37 | + |
| 38 | +def _click_start() -> None: |
| 39 | + panel.set_value("is_running", True) |
| 40 | + |
| 41 | + |
| 42 | +def _click_stop() -> None: |
| 43 | + panel.set_value("is_running", False) |
| 44 | + |
| 45 | + |
| 46 | +panel = nipanel.get_streamlit_panel_accessor() |
| 47 | + |
| 48 | +st.header("NI-DAQmx - Analog Input - Voltage with FFT") |
| 49 | +if panel.get_value("is_running", False): |
| 50 | + st.button(r"⏹️ Stop", key="stop_button", on_click=_click_stop) |
| 51 | +else: |
| 52 | + st.button(r"▶️ Run", key="run_button", on_click=_click_start) |
| 53 | + |
| 54 | +sample_rate = panel.get_value("sample_rate", 0.0) |
| 55 | + |
| 56 | +# Create two-column layout for the entire interface |
| 57 | +left_column, right_column = st.columns([1, 1]) |
| 58 | + |
| 59 | +# Left column - Channel tabs and Timing Settings |
| 60 | +with left_column: |
| 61 | + # Channel Settings tabs |
| 62 | + with st.container(border=True): |
| 63 | + st.header("Channel Settings") |
| 64 | + voltage_tab = st.tabs(["Voltage"])[0] |
| 65 | + |
| 66 | + voltage_tab.header("Voltage") |
| 67 | + with voltage_tab: |
| 68 | + channel_left_column, channel_right_column = st.columns(2) |
| 69 | + with channel_left_column: |
| 70 | + st.selectbox( |
| 71 | + options=panel.get_value("available_voltage_channels", [""]), |
| 72 | + index=0, |
| 73 | + label="Voltage Channel", |
| 74 | + disabled=panel.get_value("is_running", False), |
| 75 | + key="voltage_channel", |
| 76 | + ) |
| 77 | + st.number_input( |
| 78 | + "Min Value", |
| 79 | + value=-5.0, |
| 80 | + step=0.1, |
| 81 | + disabled=panel.get_value("is_running", False), |
| 82 | + key="voltage_min_value", |
| 83 | + ) |
| 84 | + st.number_input( |
| 85 | + "Max Value", |
| 86 | + value=5.0, |
| 87 | + step=0.1, |
| 88 | + disabled=panel.get_value("is_running", False), |
| 89 | + key="voltage_max_value", |
| 90 | + ) |
| 91 | + with channel_right_column: |
| 92 | + enum_selectbox( |
| 93 | + panel, |
| 94 | + label="Terminal Configuration", |
| 95 | + value=TerminalConfiguration.DEFAULT, |
| 96 | + disabled=panel.get_value("is_running", False), |
| 97 | + key="terminal_configuration", |
| 98 | + ) |
| 99 | + |
| 100 | + # Timing Settings section in left column |
| 101 | + with st.container(border=True): |
| 102 | + st.header("Timing Settings") |
| 103 | + timing_left_column, timing_right_column = st.columns(2) |
| 104 | + with timing_left_column: |
| 105 | + st.selectbox( |
| 106 | + options=["OnboardClock"], |
| 107 | + label="Sample Clock Source", |
| 108 | + disabled=True, |
| 109 | + ) |
| 110 | + st.number_input( |
| 111 | + "Sample Rate", |
| 112 | + value=1000.0, |
| 113 | + step=100.0, |
| 114 | + min_value=1.0, |
| 115 | + disabled=panel.get_value("is_running", False), |
| 116 | + key="sample_rate_input", |
| 117 | + ) |
| 118 | + with timing_right_column: |
| 119 | + st.number_input( |
| 120 | + "Samples to Read", |
| 121 | + value=1000, |
| 122 | + step=100, |
| 123 | + min_value=10, |
| 124 | + disabled=panel.get_value("is_running", False), |
| 125 | + key="samples_per_channel", |
| 126 | + ) |
| 127 | + st.text_input( |
| 128 | + label="Actual Sample Rate", |
| 129 | + value=str(sample_rate) if sample_rate else "", |
| 130 | + disabled=True, |
| 131 | + ) |
| 132 | + |
| 133 | +# Right column - Graph |
| 134 | +with right_column: |
| 135 | + if panel.get_value("daq_error", "") != "": |
| 136 | + st.error( |
| 137 | + f"There was an error running the script. Fix the issue and click Run again. \n\n {panel.get_value('daq_error', '')}" |
| 138 | + ) |
| 139 | + else: |
| 140 | + with st.container(border=True): |
| 141 | + # Voltage Data Graph section |
| 142 | + st.header("Acquired Data") |
| 143 | + |
| 144 | + voltage_waveform = panel.get_value("voltage_waveform", AnalogWaveform()) |
| 145 | + if voltage_waveform.sample_count == 0: |
| 146 | + time_labels = ["00:00:00.000"] |
| 147 | + else: |
| 148 | + timestamps = cast( |
| 149 | + list[ht.datetime], |
| 150 | + list(voltage_waveform.timing.get_timestamps(0, voltage_waveform.sample_count)), |
| 151 | + ) |
| 152 | + time_labels = [ |
| 153 | + f"{ts.hour:02d}:{ts.minute:02d}:{ts.second:02d}.{ts.microsecond//1000:03d}" |
| 154 | + for ts in timestamps |
| 155 | + ] |
| 156 | + |
| 157 | + voltage_graph = { |
| 158 | + "animation": False, |
| 159 | + "tooltip": {"trigger": "axis"}, |
| 160 | + "legend": {"data": [voltage_waveform.units]}, |
| 161 | + "xAxis": { |
| 162 | + "type": "category", |
| 163 | + "data": time_labels, |
| 164 | + "name": "Time", |
| 165 | + "nameLocation": "center", |
| 166 | + "nameGap": 40, |
| 167 | + }, |
| 168 | + "yAxis": { |
| 169 | + "type": "value", |
| 170 | + "name": "Measurement", |
| 171 | + "nameRotate": 90, |
| 172 | + "nameLocation": "center", |
| 173 | + "nameGap": 40, |
| 174 | + }, |
| 175 | + "series": [ |
| 176 | + { |
| 177 | + "name": voltage_waveform.units, |
| 178 | + "type": "line", |
| 179 | + "data": list(voltage_waveform.scaled_data), |
| 180 | + "emphasis": {"focus": "series"}, |
| 181 | + "smooth": True, |
| 182 | + "seriesLayoutBy": "row", |
| 183 | + }, |
| 184 | + ], |
| 185 | + } |
| 186 | + st_echarts(options=voltage_graph, height="300px", key="voltage_graph") |
| 187 | + |
| 188 | + frequencies = panel.get_value("fft_freqs", [0.0]) |
| 189 | + magnitudes = panel.get_value("fft_mags", [0.0]) |
| 190 | + fft_data = [{"value": [x, y]} for x, y in zip(frequencies, magnitudes)] |
| 191 | + |
| 192 | + fft_graph = { |
| 193 | + "animation": False, |
| 194 | + "tooltip": {"trigger": "axis"}, |
| 195 | + "legend": {"data": ["Magnitude"]}, |
| 196 | + "xAxis": { |
| 197 | + "type": "value", |
| 198 | + "name": "Frequency", |
| 199 | + "nameLocation": "center", |
| 200 | + "nameGap": 40, |
| 201 | + }, |
| 202 | + "yAxis": { |
| 203 | + "type": "value", |
| 204 | + "name": "Magnitude", |
| 205 | + "nameRotate": 90, |
| 206 | + "nameLocation": "center", |
| 207 | + "nameGap": 40, |
| 208 | + }, |
| 209 | + "series": [ |
| 210 | + { |
| 211 | + "name": "Magnitude", |
| 212 | + "type": "line", |
| 213 | + "data": fft_data, |
| 214 | + "emphasis": {"focus": "series"}, |
| 215 | + "smooth": True, |
| 216 | + "seriesLayoutBy": "row", |
| 217 | + }, |
| 218 | + ], |
| 219 | + } |
| 220 | + st_echarts(options=fft_graph, height="300px", key="fft_graph") |
0 commit comments