Просмотр исходного кода

added more data to the measurements

Lucas 7 месяцев назад
Родитель
Сommit
6dcbe665c6
3 измененных файлов с 57 добавлено и 33 удалено
  1. BIN
      __pycache__/dobluetooth.cpython-313.pyc
  2. 7 8
      dobluetooth.py
  3. 50 25
      graphing.py

BIN
__pycache__/dobluetooth.cpython-313.pyc


+ 7 - 8
dobluetooth.py

@@ -1,11 +1,10 @@
 import asyncio
 from bleak import BleakScanner, BleakClient
-import time
 
-dataarr = [0.0] * 6
-lastval = [0.0] * 6
+dataarr = [0.0] * 11
+lastval = [0.0] * 11
 
-target_name = "Long name works now"
+target_name = "MPU6050_BLE"
 target_address = None
 gclient = None
 
@@ -62,10 +61,10 @@ async def getData():
                         global dataarr
                         dataarr = convertToNumbers(datastr)
                         print("📡 Data:", dataarr)
-                        await asyncio.sleep(0.05)  # Optional: slow it down a bit
+                        await asyncio.sleep(0.05)
                     except Exception as e:
                         print("⚠ Read error:", e)
-                        break  # Break loop and reconnect
+                        break
         except Exception as e:
             print("⚠ Connection error:", e)
 
@@ -76,8 +75,8 @@ def convertToNumbers(stringlist) -> list[float]:
     try:
         dlist = stringlist.strip().split(",")
         dlist = [float(val) for val in dlist if val != '']
-        if len(dlist) >= 6:
-            return dlist[0:6]  # Already scaled: [gyroX, gyroY, gyroZ, accX, accY, accZ]
+        if len(dlist) >= 11:
+            return dlist[0:11]
         else:
             return lastval
     except Exception as e:

+ 50 - 25
graphing.py

@@ -23,9 +23,15 @@ acc_x_history = deque(maxlen=graph_length)
 acc_y_history = deque(maxlen=graph_length)
 acc_z_history = deque(maxlen=graph_length)
 
+# New values
+gyro_mag_history = deque(maxlen=graph_length)
+accel_mag_history = deque(maxlen=graph_length)
+pitch_history = deque(maxlen=graph_length)
+roll_history = deque(maxlen=graph_length)
+temp_history = deque(maxlen=graph_length)
 
 # Set up the display
-width, height = 1000, 550
+width, height = 1000, 750
 screen = pygame.display.set_mode((width, height))
 pygame.display.set_caption("MPU BLE Visualizer")
 
@@ -89,31 +95,39 @@ while running:
         data = dobluetooth.getDataArr()
         high_force : bool = False
 
-        if len(data) >= 6:
-            # Accelerometer values
+        if len(data) >= 11:
+            rotX, rotY, rotZ = data[0:3]
             accX, accY, accZ = data[3:6]
+            gyroMag, accelMag = data[6:8]
+            pitch, roll, temp = data[8:11]
+
+            # Add to history
+            gyro_x_history.append(rotX)
+            gyro_y_history.append(rotY)
+            gyro_z_history.append(rotZ)
             acc_x_history.append(accX)
             acc_y_history.append(accY)
             acc_z_history.append(accZ)
+            gyro_mag_history.append(gyroMag)
+            accel_mag_history.append(accelMag)
+            pitch_history.append(pitch)
+            roll_history.append(roll)
+            temp_history.append(temp)
 
-            acc_magnitude = math.sqrt(accX**2 + accY**2 + accZ**2)
-
-            if acc_magnitude > HIGH_FORCE_THRESHOLD:
+            if accelMag> HIGH_FORCE_THRESHOLD:
                 high_force = True
 
-            rotX, rotY, rotZ = data[0:3]
-            gyro_magnitude = math.sqrt(rotX**2 + rotY**2 + rotZ**2)
-
-            if gyro_magnitude < 10:
+            if gyroMag < 10:
                 gyro_condition = "🟢 Stable"
                 gyro_color = (0, 200, 0)
-            elif gyro_magnitude < 75:
+            elif gyroMag < 75:
                 gyro_condition = "🟡 Moving"
                 gyro_color = (255, 200, 0)
             else:
                 gyro_condition = "🔴 High Rotation"
                 gyro_color = (255, 50, 50)
 
+
             # Create the font surface
             gyro_status_surface = warning_font.render(f"Gyro Status: {gyro_condition}", True, gyro_color)
 
@@ -123,12 +137,6 @@ while running:
             # Blit it to the screen
             screen.blit(gyro_status_surface, gyro_status_rect)
 
-            # Add to history
-            gyro_mag_history.append(gyro_magnitude)
-            gyro_x_history.append(rotX)
-            gyro_y_history.append(rotY)
-            gyro_z_history.append(rotZ)
-
             for i in range(6):
                 val = abs(data[i])
 
@@ -184,15 +192,32 @@ while running:
     screen.blit(font.render("Gyro X/Y/Z (°/s)", True, (0, 0, 0)), (10, gyro_top - 15))
     screen.blit(font.render("Accel X/Y/Z (g)", True, (0, 0, 0)), (10, accel_top - 15))
 
-    # Gyroscope graph
-    draw_graph(screen, gyro_x_history, (255, 0, 0), gyro_top, graph_height, height_scale=10)
-    draw_graph(screen, gyro_y_history, (0, 255, 0), gyro_top, graph_height, height_scale=10)
-    draw_graph(screen, gyro_z_history, (0, 0, 255), gyro_top, graph_height, height_scale=10)
+    # Pitch and Roll
+    pitch_top = accel_top + graph_spacing
+    screen.blit(font.render("Pitch / Roll (°)", True, (0, 0, 0)), (10, pitch_top - 15))
+
+    # Temp + Mags
+    mags_top = pitch_top + graph_spacing
+    screen.blit(font.render("Temp (°C), AccelMag (g), GyroMag (°/s)", True, (0, 0, 0)), (10, mags_top - 15))
+
+    # Gyroscope graph (°/s)
+    draw_graph(screen, gyro_x_history, (255, 0, 0), gyro_top, graph_height, height_scale=5)
+    draw_graph(screen, gyro_y_history, (0, 255, 0), gyro_top, graph_height, height_scale=5)
+    draw_graph(screen, gyro_z_history, (0, 0, 255), gyro_top, graph_height, height_scale=5)
+
+    # Accelerometer graph (g)
+    draw_graph(screen, acc_x_history, (255, 128, 0), accel_top, graph_height, height_scale=30)
+    draw_graph(screen, acc_y_history, (0, 200, 200), accel_top, graph_height, height_scale=30)
+    draw_graph(screen, acc_z_history, (128, 0, 255), accel_top, graph_height, height_scale=30)
+
+    # Pitch and Roll (°)
+    draw_graph(screen, pitch_history, (100, 100, 255), pitch_top, graph_height, height_scale=1.5)
+    draw_graph(screen, roll_history, (255, 100, 100), pitch_top, graph_height, height_scale=1.5)
 
-    # Accelerometer graph
-    draw_graph(screen, acc_x_history, (255, 128, 0), accel_top, graph_height, height_scale=25)
-    draw_graph(screen, acc_y_history, (0, 200, 200), accel_top, graph_height, height_scale=25)
-    draw_graph(screen, acc_z_history, (128, 0, 255), accel_top, graph_height, height_scale=25)
+    # Temp + Mags
+    draw_graph(screen, temp_history, (200, 0, 0), mags_top, graph_height, height_scale=3)
+    draw_graph(screen, accel_mag_history, (0, 150, 255), mags_top, graph_height, height_scale=30)
+    draw_graph(screen, gyro_mag_history, (100, 255, 100), mags_top, graph_height, height_scale=5)
 
     pygame.display.flip()
     clock.tick(60)