SHA256
grafica v1
This commit is contained in:
1 parent
204b88d96f
commit
48eeb7f944
4 files changed
+225
-23546
No files matched your search
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
# Base: Hilos y datos buffer de datos compartidos
|
# Base: Hilos y datos buffer de datos compartidos
|
||||||
import queue
|
import queue
|
||||||
|
import threading
|
||||||
|
|
||||||
from src.cliente_ws import websocket_cliente
|
from src.cliente_ws import websocket_cliente
|
||||||
from src.graficar import graficar
|
from src.graficar import graficar
|
||||||
@@ -17,21 +18,18 @@ data_clean = queue.Queue()
|
|||||||
data_grafica = queue.Queue()
|
data_grafica = queue.Queue()
|
||||||
i_paquetes = queue.Queue()
|
i_paquetes = queue.Queue()
|
||||||
buffer_size = queue.Queue()
|
buffer_size = queue.Queue()
|
||||||
|
data_guardar = queue.Queue()
|
||||||
|
|
||||||
# def debug_print():
|
# def debug_print():
|
||||||
# while True:
|
# while True:
|
||||||
# data = data_clean.get()
|
# raw = data_grafica.get()
|
||||||
# # raw = data_grafica.get()
|
# print(raw)
|
||||||
# print(data)
|
|
||||||
# # print(raw)
|
|
||||||
# # print(i)
|
|
||||||
|
|
||||||
|
|
||||||
hilo_1 = websocket_cliente(raw_data, i_paquetes, ip_server)
|
hilo_1 = websocket_cliente(raw_data, i_paquetes, ip_server)
|
||||||
hilo_2 = data_procesor(raw_data, data_clean, data_grafica, buffer_size)
|
hilo_2 = data_procesor(raw_data, data_clean, data_grafica, buffer_size, data_guardar)
|
||||||
# hilo_3 = threading.Thread(target=debug_print)
|
# hilo_3 = threading.Thread(target=debug_print)
|
||||||
hilo_4 = guardar_datos(data_clean)
|
hilo_4 = guardar_datos(data_guardar)
|
||||||
|
|
||||||
hilo_1.daemon = True
|
hilo_1.daemon = True
|
||||||
hilo_2.daemon = True
|
hilo_2.daemon = True
|
||||||
|
|||||||
+16
-47
@@ -7,71 +7,40 @@ from pyqtgraph.Qt import QtCore
|
|||||||
def graficar(cola_clean, cola_filtrada, cola_buffer_size):
|
def graficar(cola_clean, cola_filtrada, cola_buffer_size):
|
||||||
app = QtWidgets.QApplication([])
|
app = QtWidgets.QApplication([])
|
||||||
win = pg.GraphicsLayoutWidget(show=True, title="Monitor de Señal")
|
win = pg.GraphicsLayoutWidget(show=True, title="Monitor de Señal")
|
||||||
p1 = win.addPlot(title="Señal Cruda y Filtrada (Historial de 1s)")
|
p1 = win.addPlot(title="Señal Cruda y Filtrada (Bloques de 1s)")
|
||||||
p1.addLegend()
|
p1.addLegend()
|
||||||
|
|
||||||
curve1 = p1.plot(pen=pg.mkPen("r", width=1), name="Cruda")
|
curve1 = p1.plot(pen=pg.mkPen("r", width=1), name="Cruda")
|
||||||
curve2 = p1.plot(pen=pg.mkPen("b", width=2), name="Filtrada")
|
curve2 = p1.plot(pen=pg.mkPen("b", width=2), name="Filtrada")
|
||||||
|
|
||||||
# Empezamos con los buffers nulos.
|
|
||||||
# La gráfica esperará pasivamente a que el hilo de procesamiento defina el tamaño.
|
|
||||||
buffer_clean = None
|
|
||||||
buffer_filtrado = None
|
|
||||||
|
|
||||||
def update():
|
def update():
|
||||||
nonlocal buffer_clean, buffer_filtrado
|
# Vaciamos la cola de tamaño por si llega,
|
||||||
|
# aunque ya no necesitamos inicializar arreglos vacíos.
|
||||||
|
while not cola_buffer_size.empty():
|
||||||
|
cola_buffer_size.get()
|
||||||
|
|
||||||
# 1. VERIFICAR TAMAÑO DEL BUFFER
|
# Extraemos el último bloque de 1s de la señal cruda
|
||||||
# Si el procesador mandó un nuevo tamaño (al inicio, o si cambia la tasa de muestreo)
|
|
||||||
if not cola_buffer_size.empty():
|
|
||||||
# Extraemos el valor y nos aseguramos de que sea un entero
|
|
||||||
tamano_buffer = int(cola_buffer_size.get())
|
|
||||||
|
|
||||||
# Inicializamos (o reinicializamos) los arreglos de NumPy
|
|
||||||
buffer_clean = np.zeros(tamano_buffer)
|
|
||||||
buffer_filtrado = np.zeros(tamano_buffer)
|
|
||||||
print(
|
|
||||||
f"[*] Gráfica inicializada: Buffer de {tamano_buffer} muestras configurado."
|
|
||||||
)
|
|
||||||
|
|
||||||
# Si el buffer aún no existe (el ESP32 no ha mandado suficientes datos),
|
|
||||||
# abortamos este ciclo de dibujo y esperamos al siguiente.
|
|
||||||
if buffer_clean is None:
|
|
||||||
return
|
|
||||||
|
|
||||||
# 2. PROCESAR SEÑAL CRUDA
|
|
||||||
actualizar_cruda = False
|
actualizar_cruda = False
|
||||||
|
datos_crudos = None
|
||||||
while not cola_clean.empty():
|
while not cola_clean.empty():
|
||||||
datos_nuevos = np.array(cola_clean.get())
|
datos_crudos = np.array(cola_clean.get())
|
||||||
n = len(datos_nuevos)
|
|
||||||
|
|
||||||
# Precaución por si un paquete llega más grande que el buffer entero
|
|
||||||
if n > len(buffer_clean):
|
|
||||||
datos_nuevos = datos_nuevos[-len(buffer_clean) :]
|
|
||||||
n = len(datos_nuevos)
|
|
||||||
|
|
||||||
buffer_clean[:-n] = buffer_clean[n:]
|
|
||||||
buffer_clean[-n:] = datos_nuevos
|
|
||||||
actualizar_cruda = True
|
actualizar_cruda = True
|
||||||
|
|
||||||
if actualizar_cruda:
|
if actualizar_cruda and datos_crudos is not None:
|
||||||
curve1.setData(buffer_clean)
|
curve1.setData(datos_crudos)
|
||||||
|
|
||||||
# 3. PROCESAR SEÑAL FILTRADA
|
# Extraemos el último bloque de 1s de la señal filtrada
|
||||||
actualizar_filtrada = False
|
actualizar_filtrada = False
|
||||||
|
datos_filtrados = None
|
||||||
while not cola_filtrada.empty():
|
while not cola_filtrada.empty():
|
||||||
nuevo_filtrado = np.array(cola_filtrada.get())
|
datos_filtrados = np.array(cola_filtrada.get())
|
||||||
|
|
||||||
# Validación de seguridad por si el tamaño cambió en medio de una iteración
|
|
||||||
if len(nuevo_filtrado) == len(buffer_filtrado):
|
|
||||||
buffer_filtrado = nuevo_filtrado
|
|
||||||
actualizar_filtrada = True
|
actualizar_filtrada = True
|
||||||
|
|
||||||
if actualizar_filtrada:
|
if actualizar_filtrada and datos_filtrados is not None:
|
||||||
curve2.setData(buffer_filtrado)
|
curve2.setData(datos_filtrados)
|
||||||
|
|
||||||
timer = QtCore.QTimer()
|
timer = QtCore.QTimer()
|
||||||
timer.timeout.connect(update)
|
timer.timeout.connect(update)
|
||||||
timer.start(50) # 20 FPS para un barrido fluido
|
timer.start(50) # Revisa la cola cada 50ms, pero la gráfica cambiará cada 1s
|
||||||
|
|
||||||
app.exec_()
|
app.exec_()
|
||||||
@@ -21,7 +21,7 @@ def procesar(samples, T):
|
|||||||
f_dominante = f_positivo[np.argmax(magnitudes)]
|
f_dominante = f_positivo[np.argmax(magnitudes)]
|
||||||
array_data = pd.Series(samples)
|
array_data = pd.Series(samples)
|
||||||
window_size = max(1, int(f_dominante))
|
window_size = max(1, int(f_dominante))
|
||||||
media_movil = array_data.rolling(window=window_size).mean()
|
media_movil = array_data.rolling(window=window_size).mean().bfill().values
|
||||||
return media_movil
|
return media_movil
|
||||||
|
|
||||||
|
|
||||||
@@ -43,31 +43,34 @@ class data_procesor(threading.Thread):
|
|||||||
bandera_analisis = False
|
bandera_analisis = False
|
||||||
buffer_fft = []
|
buffer_fft = []
|
||||||
|
|
||||||
def __init__(self, cola_raw, cola_clean, cola_graficar, cola_tamaño):
|
def __init__(self, cola_raw, cola_clean, cola_graficar, cola_tamaño, cola_guardar):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.cola_raw = cola_raw
|
self.cola_raw = cola_raw
|
||||||
self.cola_clean = cola_clean
|
self.cola_clean = cola_clean
|
||||||
self.cola_graficar = cola_graficar
|
self.cola_graficar = cola_graficar
|
||||||
self.cola_tamaño = cola_tamaño
|
self.cola_tamaño = cola_tamaño
|
||||||
|
self.cola_guardar = cola_guardar
|
||||||
self.size_array = 0
|
self.size_array = 0
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
self.periodo = 0
|
self.periodo = 0
|
||||||
while True:
|
while True:
|
||||||
raw = self.cola_raw.get()
|
raw = self.cola_raw.get()
|
||||||
data_procesor.buffer_fft.extend(raw)
|
# Convertir a volts
|
||||||
|
volts = bit_voltio(raw)
|
||||||
|
data_procesor.buffer_fft.extend(volts)
|
||||||
if not data_procesor.bandera_analisis:
|
if not data_procesor.bandera_analisis:
|
||||||
self.size_array = len(raw)
|
self.size_array = len(raw)
|
||||||
self.periodo = 1 / (self.size_array * 100)
|
self.periodo = 1 / (self.size_array * 100)
|
||||||
self.total_size = 100 * self.size_array
|
self.total_size = 100 * self.size_array
|
||||||
self.cola_tamaño.put(self.total_size)
|
self.cola_tamaño.put(self.total_size)
|
||||||
|
data_procesor.bandera_analisis = True
|
||||||
if len(data_procesor.buffer_fft) >= self.total_size:
|
if len(data_procesor.buffer_fft) >= self.total_size:
|
||||||
senal_original = np.array(data_procesor.buffer_fft)
|
senal_original = np.array(data_procesor.buffer_fft)
|
||||||
data_clean = procesar(data_procesor.buffer_fft, self.periodo)
|
data_clean = procesar(data_procesor.buffer_fft, self.periodo)
|
||||||
self.cola_graficar.put(data_clean)
|
self.cola_graficar.put(data_clean)
|
||||||
|
self.cola_clean.put(senal_original)
|
||||||
|
self.cola_guardar.put(senal_original)
|
||||||
tiempo = calcular_desfase(senal_original, data_clean, self.periodo)
|
tiempo = calcular_desfase(senal_original, data_clean, self.periodo)
|
||||||
print(f"Desface: {tiempo:.6f}s")
|
print(f"Desface: {tiempo:.6f}s")
|
||||||
data_procesor.buffer_fft.clear()
|
data_procesor.buffer_fft.clear()
|
||||||
|
|
||||||
# DEBUG print(self.size_array)
|
|
||||||
self.cola_clean.put(bit_voltio(raw))
|
|
||||||
Reference in new issue
Block a user