-
Notifications
You must be signed in to change notification settings - Fork 48
/
app_tclab.py
126 lines (109 loc) · 4.05 KB
/
app_tclab.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import streamlit as st
import pandas as pd
import numpy as np
import time
import tclab
# Check for hardware availability and set up TCLab
hardware = False
# Uncomment when running locally to use the TCLab
#tclab_hardware = st.checkbox("Use TCLab hardware", value=False)
#hardware = tclab_hardware.value
# Initialize session state variables for data persistence
if "collecting_data" not in st.session_state:
st.session_state.collecting_data = False
st.session_state.timestamps = []
st.session_state.time = []
st.session_state.T1_list = []
st.session_state.T2_list = []
st.session_state.Q1_list = []
st.session_state.Q2_list = []
# Sidebar for controls to save space
st.sidebar.title("TCLab Controls")
Q1 = st.sidebar.slider("Heater 1 (%)", 0, 100, 0)
Q2 = st.sidebar.slider("Heater 2 (%)", 0, 100, 0)
if hardware:
LED = st.sidebar.slider("LED Brightness (%)", 0, 100, 0)
# Add image under controls
url = "https://apmonitor.com/pdc/uploads/Main/tclab_transparent.png"
# In case URL isn't available
try:
st.sidebar.image(url, caption="TCLab Device")
except:
pass
# Create empty placeholder for the real-time line chart
combined_chart = st.empty()
# Start/Stop buttons
if st.button("Start Data Collection"):
st.session_state.collecting_data = True
st.session_state.timestamps = []
st.session_state.time = []
st.session_state.T1_list = []
st.session_state.T2_list = []
st.session_state.Q1_list = []
st.session_state.Q2_list = []
if hardware:
TCLab = tclab.setup(connected=True)
else:
TCLab = tclab.setup(connected=False, speedup=10)
st.session_state.lab = TCLab()
if st.button("Stop Data Collection"):
st.session_state.collecting_data = False
st.session_state.lab.close()
# Main loop for data collection
if st.session_state.collecting_data:
with st.session_state.lab:
# Start data collection loop
for t in tclab.clock(600): # Collect data for up to 600 seconds
if not st.session_state.collecting_data:
break # Exit loop if Stop button is pressed
# Read temperatures
T1 = st.session_state.lab.T1
T2 = st.session_state.lab.T2
# Update data lists in session state
if not st.session_state.time:
st.session_state.time.append(0)
else:
st.session_state.time.append(st.session_state.time[-1]+1)
st.session_state.timestamps.append(t)
st.session_state.Q1_list.append(Q1)
st.session_state.T1_list.append(T1)
st.session_state.Q2_list.append(Q2)
st.session_state.T2_list.append(T2)
# Update heaters and LED based on slider values
st.session_state.lab.Q1(Q1)
st.session_state.lab.Q2(Q2)
if hardware:
st.session_state.lab.LED(LED)
# Update DataFrame with new data
data = pd.DataFrame({
"Time": st.session_state.time,
"Q1": st.session_state.Q1_list,
"T1": st.session_state.T1_list,
"Q2": st.session_state.Q2_list,
"T2": st.session_state.T2_list
})
# Update real-time line chart with both temperature and heater data
combined_chart.line_chart(data[["Q1", "T1", "Q2", "T2"]])
# Small pause
if not hardware:
time.sleep(0.1)
# Convert data to DataFrame for downloading
if len(st.session_state.timestamps) > 0:
data = pd.DataFrame({
"Time (s)": np.arange(0,len(st.session_state.timestamps)),
"Q1": st.session_state.Q1_list,
"T1": st.session_state.T1_list,
"Q2": st.session_state.Q2_list,
"T2": st.session_state.T2_list
})
# Download button for the data
csv = data.to_csv(index=False)
st.download_button(
label="Download Data as CSV",
data=csv,
file_name="tclab_data.csv",
mime="text/csv"
)
st.session_state.collecting_data = False
else:
st.write("No data collected yet.")