sgbaird commited on
Commit
569aba1
·
1 Parent(s): a6f907c

refactor: Improve code readability and performance in app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -26
app.py CHANGED
@@ -1,12 +1,3 @@
1
- """
2
- Fan control using EMC2101 and a Canakit RPi 5 fan
3
-
4
- https://chatgpt.com/share/e29312a2-a589-4a07-afb8-4778506a47e9
5
-
6
- https://github.com/AccelerationConsortium/ac-training-lab/tree/main/src/ac_training_lab/picow/fan-control
7
-
8
- """
9
-
10
  import json
11
  import queue
12
  import threading
@@ -24,15 +15,15 @@ st.title("Motor Control Panel")
24
  # Description and context
25
  st.markdown(
26
  """
27
- This application accesses a public test demo of a fan controller located in Send
28
- speed commands to the fan and visualize the RPM data.
29
-
30
- See
31
- [ac_training_lab/picow/fan-control](https://github.com/AccelerationConsortium/ac-training-lab/tree/main/src/ac_training_lab/picow/fan-control) # noqa: E501
32
- [[permalink](https://github.com/AccelerationConsortium/ac-training-lab/tree/db19bda95f2a81909da7dbcd7c5dc76e8d1ee6b7/src/ac_training_lab/picow/fan-control)] # noqa: E501
33
- for context.
34
-
35
- You may also be
36
  interested in the Acceleration Consortium's ["Hello World"
37
  microcourse](https://ac-microcourses.readthedocs.io/en/latest/courses/hello-world/index.html)
38
  for self-driving labs.
@@ -125,7 +116,6 @@ def plot_rpm_data(ax1, ax2, rpm_data):
125
  ax1.set_ylabel("RPM", fontsize=14)
126
  ax1.set_title("Motor RPM over Time", fontsize=16)
127
 
128
- # For any additional plots or processing in ax2, currently showing RPM data only.
129
  ax2.clear()
130
  ax2.plot(times, rpm_values, "-o", color="blue")
131
  ax2.set_ylim(0, max(1.1 * max(rpm_values), 1.1)) # Set fixed y-axis limits
@@ -133,24 +123,32 @@ def plot_rpm_data(ax1, ax2, rpm_data):
133
  ax2.set_ylabel("RPM (scaled)")
134
  ax2.set_title("RPM Data Stream")
135
 
136
- plt.pause(0.01)
137
  st.pyplot(fig)
138
 
139
 
140
  # Function to continuously receive RPM data
141
  def receive_rpm_data():
142
  rpm_data = []
 
143
  fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(10, 8))
144
  plt.ion()
145
 
146
  start_time = time.time()
 
 
147
  while True:
148
- data = rpm_data_queue.get(True)
149
- elapsed_time = time.time() - start_time
150
- data["time"] = elapsed_time
151
- rpm_data.append(data)
152
- plot_rpm_data(ax1, ax2, rpm_data)
153
- st.write("RPM Data Received:", data)
 
 
 
 
 
 
154
 
155
 
156
  # Start a background thread to receive RPM data
 
 
 
 
 
 
 
 
 
 
1
  import json
2
  import queue
3
  import threading
 
15
  # Description and context
16
  st.markdown(
17
  """
18
+ This application accesses a public test demo of a motor control located in
19
+ Toronto, ON, Canada (as of 2024-07-27). Send speed commands to the motor and
20
+ visualize the RPM data.
21
+
22
+ For more context, you
23
+ can refer to this [Colab
24
+ notebook](https://colab.research.google.com/github/sparks-baird/self-driving-lab-demo/blob/main/notebooks/4.2-paho-mqtt-colab-sdl-demo-test.ipynb)
25
+ and the [self-driving-lab-demo
26
+ project](https://github.com/sparks-baird/self-driving-lab-demo). You may also be
27
  interested in the Acceleration Consortium's ["Hello World"
28
  microcourse](https://ac-microcourses.readthedocs.io/en/latest/courses/hello-world/index.html)
29
  for self-driving labs.
 
116
  ax1.set_ylabel("RPM", fontsize=14)
117
  ax1.set_title("Motor RPM over Time", fontsize=16)
118
 
 
119
  ax2.clear()
120
  ax2.plot(times, rpm_values, "-o", color="blue")
121
  ax2.set_ylim(0, max(1.1 * max(rpm_values), 1.1)) # Set fixed y-axis limits
 
123
  ax2.set_ylabel("RPM (scaled)")
124
  ax2.set_title("RPM Data Stream")
125
 
 
126
  st.pyplot(fig)
127
 
128
 
129
  # Function to continuously receive RPM data
130
  def receive_rpm_data():
131
  rpm_data = []
132
+ global fig, ax1, ax2
133
  fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(10, 8))
134
  plt.ion()
135
 
136
  start_time = time.time()
137
+ timeout = 15 # Timeout duration in seconds
138
+
139
  while True:
140
+ try:
141
+ data = rpm_data_queue.get(timeout=timeout)
142
+ elapsed_time = time.time() - start_time
143
+ data["time"] = elapsed_time
144
+ rpm_data.append(data)
145
+ plot_rpm_data(ax1, ax2, rpm_data)
146
+ st.write("RPM Data Received:", data)
147
+ except queue.Empty:
148
+ st.error(
149
+ f"No RPM data received for {timeout} seconds. Please check the system."
150
+ )
151
+ break
152
 
153
 
154
  # Start a background thread to receive RPM data