Code backup
This commit is contained in:
2026-05-10 16:59:01 +02:00
commit 368d6fafea
796 changed files with 315310 additions and 0 deletions
@@ -0,0 +1,25 @@
import serial
from datetime import datetime
PORT = "COM3" # oppure "/dev/ttyUSB0"
BAUDRATE = 115200
LOG_FILE = "serial_log.txt"
ser = serial.Serial(PORT, BAUDRATE, timeout=1)
print("In ascolto sulla seriale... Ctrl+C per uscire")
with open(LOG_FILE, "a", encoding="utf-8") as f:
try:
while True:
line = ser.readline().decode(errors="ignore").strip()
if line:
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")[:-3]
out = f"[{timestamp}] {line}"
print(out)
f.write(out + "\n")
f.flush()
except KeyboardInterrupt:
print("\nChiusura...")
finally:
ser.close()
@@ -0,0 +1,32 @@
import serial
import struct
import time
# configurazione porta
PORT = 'COM3' # cambia se serve
BAUD = 921600
# struttura Sample = 1 uint32 + 2 IMUData
# IMUData = 6 int16
# Formato struct: < = little-endian
fmt = '<I6h6h' # ts, imu1(6h), imu2(6h)
sample_size = struct.calcsize(fmt)
ser = serial.Serial(PORT, BAUD, timeout=1)
print(f"Listening on {PORT}...")
try:
while True:
data = ser.read(sample_size)
if len(data) != sample_size:
continue
s = struct.unpack(fmt, data)
ts = s[0]
imu1 = s[1:7]
imu2 = s[7:13]
print(f"{ts} | 1:{imu1} | 2:{imu2}")
except KeyboardInterrupt:
ser.close()
print("Closed")
@@ -0,0 +1,39 @@
import serial
import struct
# ---------------- CONFIG ----------------
PORT = 'COM3' # aggiorna con la tua COM
BAUD = 921600
fmt = '<I6h6h' # ts, imu1(6h), imu2(6h)
sample_size = struct.calcsize(fmt)
# ---------------- INIT ------------------
ser = serial.Serial(PORT, BAUD, timeout=1)
print(f"Listening on {PORT}...")
start_ts = None # primo timestamp ricevuto
try:
while True:
data = ser.read(sample_size)
if len(data) != sample_size:
continue # pacchetto incompleto
# ---- unpack del pacchetto ----
s = struct.unpack(fmt, data)
ts = s[0] # ts dell'ESP32 in microsecondi
imu1 = s[1:7]
imu2 = s[7:13]
# ---- calcolo timestamp relativo ----
if start_ts is None:
start_ts = ts
rel_time = (ts - start_ts) / 1_000_000 # in secondi
# ---- stampa dati ----
print(f"{rel_time:.6f}s | 1:{imu1} | 2:{imu2}")
except KeyboardInterrupt:
ser.close()
print("Closed")
@@ -0,0 +1,46 @@
import serial
import struct
import datetime
import time
# ---------------- CONFIG ----------------
PORT = 'COM3' # aggiorna con la tua COM
BAUD = 921600
fmt = '<I6h6h' # ts, imu1(6h), imu2(6h)
sample_size = struct.calcsize(fmt)
# ---------------- INIT ------------------
ser = serial.Serial(PORT, BAUD, timeout=1)
print(f"Listening on {PORT}...")
start_ts = None
start_time = None # tempo assoluto allinizio
try:
while True:
data = ser.read(sample_size)
if len(data) != sample_size:
continue # pacchetto incompleto
# ---- unpack del pacchetto ----
s = struct.unpack(fmt, data)
ts = s[0] # ts dell'ESP32 in microsecondi
imu1 = s[1:7]
imu2 = s[7:13]
# ---- inizializza riferimento tempo assoluto ----
if start_ts is None:
start_ts = ts
start_time = datetime.datetime.now()
# ---- calcola tempo assoluto basato sul ts progressivo ----
delta_us = ts - start_ts
abs_time = start_time + datetime.timedelta(microseconds=delta_us)
# ---- stampa dati con timestamp leggibile ----
print(f"{abs_time.strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]} | 1:{imu1} | 2:{imu2}")
except KeyboardInterrupt:
ser.close()
print("Closed")
@@ -0,0 +1,72 @@
import serial
import struct
import datetime
import csv
PORT = 'COM3'
BAUD = 921600
MAGIC = 0xABCD
FRAME_FMT = '<HI6h6hH'
FRAME_SIZE = struct.calcsize(FRAME_FMT)
def crc16(data: bytes) -> int:
c = 0
for b in data:
c ^= b
return c
ser = serial.Serial(PORT, BAUD, timeout=1)
buffer = bytearray()
start_ts = None
start_time = None
csv_file = open('imu_log.csv', 'w', newline='')
writer = csv.writer(csv_file)
writer.writerow([
'timestamp',
'imu1_ax','imu1_ay','imu1_az','imu1_gx','imu1_gy','imu1_gz',
'imu2_ax','imu2_ay','imu2_az','imu2_gx','imu2_gy','imu2_gz'
])
print("Listening...")
try:
while True:
buffer += ser.read(512)
while len(buffer) >= FRAME_SIZE:
if buffer[0:2] != MAGIC.to_bytes(2, 'little'):
buffer.pop(0)
continue
frame = buffer[:FRAME_SIZE]
buffer = buffer[FRAME_SIZE:]
unpacked = struct.unpack(FRAME_FMT, frame)
crc_rx = unpacked[-1]
crc_calc = crc16(frame[:-2])
if crc_rx != crc_calc:
continue # frame corrotto
ts_us = unpacked[1]
if start_ts is None:
start_ts = ts_us
start_time = datetime.datetime.now()
abs_time = start_time + datetime.timedelta(
microseconds=(ts_us - start_ts)
)
writer.writerow([
abs_time.strftime('%Y-%m-%d %H:%M:%S.%f')[:-3],
*unpacked[2:14]
])
except KeyboardInterrupt:
csv_file.close()
ser.close()
print("Stopped")
File diff suppressed because it is too large Load Diff