Initial commit: PI_mikrokontroler changes

This commit is contained in:
2026-05-09 17:52:28 +02:00
committed by Robert Duszkiewicz
parent d668467c81
commit 3dd3565a6e
550 changed files with 153891 additions and 0 deletions

File diff suppressed because it is too large Load Diff

Binary file not shown.

After

Width:  |  Height:  |  Size: 113 KiB

View File

@@ -0,0 +1,164 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Konwersja .wmt do JSON i csv
# 22.03.2026 LK
# python3 reader2.py '/Users/lklich/Desktop/Adam Błachowicz' -a -f -d <-wszystkie, +podfoldery, usuwa stare wmt
from __future__ import annotations
import argparse
import pathlib
import struct
import json
import pandas as pd
from datetime import datetime
from typing import Tuple, Dict, Any
HEADER_FMT = "<3sHHIII" # 19 B
HEADER_SIZE = struct.calcsize(HEADER_FMT)
SAMPLE_FMT = "<IBhhhB" # 12 B
SAMPLE_SIZE = struct.calcsize(SAMPLE_FMT)
def read_wmt_one(path: pathlib.Path) -> Tuple[pd.DataFrame, Dict[str, Any]]:
blob = path.read_bytes()
if len(blob) < HEADER_SIZE:
raise ValueError(f"{path.name}: plik zbyt krótki ({len(blob)} B).")
magic, version, headerSize, sampleSize, start_unix, reccount = struct.unpack_from(HEADER_FMT, blob, 0)
if magic != b"WMT":
raise ValueError(f"{path.name}: niepoprawny magic {magic!r}.")
dt_obj = datetime.fromtimestamp(start_unix)
start_time_pl = dt_obj.strftime("%d.%m.%Y %H:%M:%S")
start_time_iso = dt_obj.isoformat()
data = blob[headerSize:]
nrec = len(data) // SAMPLE_SIZE
meta = {
"filename": path.name,
"full_path": str(path.absolute()),
"wmt_version": version,
"start_unix": start_unix,
"start_time_iso": start_time_iso,
"start_time_pl": start_time_pl,
"declared_reccount": reccount,
"actual_reccount": nrec,
"is_incomplete": len(data) % SAMPLE_SIZE != 0
}
rows = []
off = 0
for _ in range(nrec):
rec = data[off:off+SAMPLE_SIZE]
offset_us, sensor_id, x, y, z, ready = struct.unpack(SAMPLE_FMT, rec)
unix_ts = start_unix + (offset_us / 1_000_000.0)
rows.append((start_unix, offset_us, unix_ts, sensor_id, x, y, z, int(ready)))
off += SAMPLE_SIZE
df = pd.DataFrame(rows, columns=["start_unix", "offset_us", "unix_ts", "sensor_id", "x", "y", "z", "ready"])
df = df.astype({"sensor_id": "int32", "x": "int32", "y": "int32", "z": "int32", "ready": "int32"})
return df, meta
def save_outputs(df: pd.DataFrame, base_path: pathlib.Path, meta: Dict[str, Any], no_header: bool, suffix: str):
csv_path = base_path.with_suffix(suffix)
json_path = base_path.with_suffix(".json")
with open(csv_path, 'w', encoding='utf-8') as f:
f.write(f"# Plik: {meta.get('filename', 'N/A')}\n")
f.write(f"# Wersja WMT: {meta.get('wmt_version', 'N/A')}\n")
f.write(f"# Start: {meta.get('start_time_pl', 'N/A')} (Unix: {meta.get('start_unix', 0)})\n")
f.write(f"# Rekordy: {meta.get('actual_reccount', 0)}\n")
df.to_csv(f, index=False, header=not no_header)
with open(json_path, 'w', encoding='utf-8') as f:
json.dump(meta, f, indent=4, ensure_ascii=False)
def main():
ap = argparse.ArgumentParser(description="Konwersja .wmt -> CSV + JSON.")
ap.add_argument("input", help="Ścieżka do pliku lub katalogu")
ap.add_argument("-a", "--all", action="store_true", help="Przetwarzaj wszystkie pliki .wmt w katalogu")
ap.add_argument("-f", "--recursive", action="store_true", help="Przeszukuj podfoldery (wymaga -a)")
ap.add_argument("-o", "--overwrite", action="store_true", help="Nadpisuj istniejące pliki wyjściowe")
ap.add_argument("-d", "--delete", action="store_true", help="USUŃ plik .wmt po udanej konwersji")
ap.add_argument("--out-suffix", default=".csv", help="Sufiks CSV")
ap.add_argument("--concat", action="store_true", help="Scal dane do jednego pliku")
ap.add_argument("--no-header", action="store_true", help="CSV bez nagłówka kolumn")
args = ap.parse_args()
input_path = pathlib.Path(args.input)
files_to_process = []
if args.all:
if not input_path.is_dir():
print(f"[ERROR] Ścieżka {input_path} nie jest katalogiem!")
return
files_to_process = sorted(list(input_path.rglob("*.wmt") if args.recursive else input_path.glob("*.wmt")))
else:
if input_path.is_dir():
print(f"[ERROR] Podano katalog, ale brakuje -a.")
return
files_to_process = [input_path]
if not files_to_process:
print(f"[INFO] Brak plików do przetworzenia.")
return
dfs = []
combined_meta = []
successfully_processed = []
for p in files_to_process:
csv_check = p.with_suffix(args.out_suffix)
json_check = p.with_suffix(".json")
if not args.overwrite and not args.concat:
if csv_check.exists() or json_check.exists():
print(f"[SKIP] Pominięto {p.name} - pliki wyjściowe już istnieją.")
continue
try:
df, meta = read_wmt_one(p)
if args.concat:
df["_source_path"] = str(p.relative_to(input_path))
dfs.append(df)
combined_meta.append(meta)
successfully_processed.append(p)
else:
save_outputs(df, p, meta, args.no_header, args.out_suffix)
print(f"Przetworzono: {p.name}")
if args.delete:
p.unlink()
print(f" [DEL] Usunięto plik źródłowy: {p.name}")
except Exception as e:
print(f"[ERROR] Błąd w pliku {p.name}: {e}")
# Obsługa zapisu zbiorczego (concat)
if args.concat and dfs:
out_base = input_path / input_path.name if input_path.is_dir() else files_to_process[0]
big_df = pd.concat(dfs, ignore_index=True)
meta_summary = {
"description": "Scalony zestaw danych",
"files_count": len(combined_meta),
"sources": combined_meta
}
try:
save_outputs(big_df, out_base, meta_summary, args.no_header, args.out_suffix)
print(f"\nZapisano scalone dane do {out_base.with_suffix(args.out_suffix)}")
# Usuwamy pliki źródłowe dopiero po udanym scaleniu
if args.delete:
for p in successfully_processed:
p.unlink()
print(f" [DEL] Usunięto {len(successfully_processed)} plików źródłowych po scaleniu.")
except Exception as e:
print(f"[ERROR] Błąd zapisu pliku scalonego: {e}")
if __name__ == "__main__":
main()

View File

@@ -0,0 +1,154 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
wmt_reader.py — parser plików .wmt z nagłówkiem WMT i eksport do CSV
Nagłówek (packed, little-endian; 19 B):
char[3] magic // "WMT"
uint16 version
uint16 headerSize // rozmiar nagłówka w bajtach (powinno być 19)
uint32 sampleSize // rozmiar rekordu Sample (powinno być 11)
uint32 timestamp // Unix time startu akwizycji
uint32 reccount // liczba rekordów w pliku
Rekord Sample (packed, little-endian; 11 B):
int32 ident
int16 x, y, z
bool ready // 1 bajt
Wymagania:
- Czytaj .wmt i zapisuj CSV.
- Kolumny x,y,z mają być typu całkowitego (nie skalujemy tych wartości ani ich nie modyfikujemy).
- Opcjonalnie można dodać kolumny x_g,y_g,z_g poprzez --range-g (x,y,z pozostają bez zmian).
"""
from __future__ import annotations
import argparse
import struct
import pathlib
import pandas as pd
# ------------------------------ Format pliku ----------------------------------
HEADER_FMT = "<3sHHIII" # magic(3s), version(H), headerSize(H), sampleSize(I), timestamp(I), reccount(I)
HEADER_SIZE = struct.calcsize(HEADER_FMT) # oczekiwane 19
# Nowy rekord Sample: ident(int32), x(int16), y(int16), z(int16), ready(bool-1B)
SAMPLE_FMT = "<ihhh?" # 4 + 2 + 2 + 2 + 1 = 11 B
SAMPLE_SIZE_EXPECTED = struct.calcsize(SAMPLE_FMT) # 11
# --------------------------------- I/O ---------------------------------------
def read_wmt(path: str) -> pd.DataFrame:
"""
Wczytuje plik .wmt (z nagłówkiem WMT) do DataFrame.
Kolumny: timestamp, ident, x, y, z, ready
Gwarantujemy, że x,y,z są typu całkowitego (int32 w DataFrame), bez skalowania.
"""
p = pathlib.Path(path)
blob = p.read_bytes()
# --- Nagłówek ---
if len(blob) < HEADER_SIZE:
raise ValueError(f"{p.name}: za krótki nagłówek ({len(blob)} B).")
magic, version, headerSize, sampleSize, start_unix, reccount = struct.unpack_from(HEADER_FMT, blob, 0)
if magic != b"WMT":
raise ValueError(f"{p.name}: nieprawidłowa sygnatura magic={magic!r} (oczekiwano b'WMT').")
if headerSize != HEADER_SIZE:
# Tolerujemy różnicę, ale ostrzegamy; dalej użyjemy headerSize jako offsetu danych
print(f"[WARN] {p.name}: headerSize={headerSize}, spodziewano {HEADER_SIZE}.")
if sampleSize != SAMPLE_SIZE_EXPECTED:
raise ValueError(f"{p.name}: sampleSize={sampleSize}, oczekiwano {SAMPLE_SIZE_EXPECTED} (nowy format Sample=11B).")
# --- Dane rekordów ---
data_off = headerSize # początek rekordów wg nagłówka
if len(blob) < data_off:
raise ValueError(f"{p.name}: uszkodzony headerSize={headerSize} (większy niż plik).")
data = blob[data_off:]
if len(data) % sampleSize != 0:
# to nie musi być błąd krytyczny (np. przerwany zapis), ale ostrzeżmy
print(f"[WARN] {p.name}: długość danych {len(data)} nie jest wielokrotnością sampleSize={sampleSize}.")
nrec = len(data) // sampleSize
if reccount and reccount != nrec:
print(f"[INFO] {p.name}: reccount w nagłówku = {reccount}, policzone rekordy = {nrec}.")
rows = []
off = 0
for _ in range(nrec):
rec = data[off:off + sampleSize]
ident, x, y, z, ready = struct.unpack(SAMPLE_FMT, rec)
rows.append((
start_unix, # timestamp startu akwizycji z nagłówka
ident, x, y, z,
bool(ready),
))
off += sampleSize
df = pd.DataFrame(
rows,
columns=["timestamp", "ident", "x", "y", "z", "ready"]
)
# Wymuszenie typu całkowitego dla x,y,z bez zmiany wartości
df = df.astype({"x": "int32", "y": "int32", "z": "int32", "ident": "int32"})
# ready pozostaje bool
return df
# ---------------------------- Konwersja do jednostek g ------------------------
def to_g(df: pd.DataFrame, range_g: float) -> pd.DataFrame:
"""
Konwersja int -> g przy założeniu: Q = 32768 / range_g => g = value / Q
(Dopisuje kolumny x_g,y_g,z_g; NIE modyfikuje x,y,z.)
"""
if range_g is None or range_g <= 0:
raise ValueError("range_g musi być dodatni (np. 2, 4, 8, 16).")
q = 32768.0 / float(range_g)
out = df.copy()
out["x_g"] = out["x"] / q
out["y_g"] = out["y"] / q
out["z_g"] = out["z"] / q
return out
# -------------------------------------- CLI ----------------------------------
def main() -> None:
ap = argparse.ArgumentParser(description="Czytaj pliki .wmt (WMT, Sample 11B) i eksportuj do CSV.")
ap.add_argument("inputs", nargs="+", help="Ścieżki do plików .wmt (jeden lub wiele).")
ap.add_argument("--range-g", type=float, default=None,
help="Jeśli podasz (np. 2,4,8,16), doda kolumny x_g,y_g,z_g (x,y,z pozostają całkowite).")
ap.add_argument("--out-suffix", default=".csv",
help="Sufiks wyjściowy (domyślnie .csv).")
ap.add_argument("--concat", action="store_true",
help="Jeśli ustawione, łączy wszystkie wejścia w JEDEN plik wynikowy (pierwszy.EXT).")
ap.add_argument("--no-header", action="store_true",
help="Zapisz CSV bez nagłówka.")
args = ap.parse_args()
dfs = []
for in_path in args.inputs:
df = read_wmt(in_path)
if args.range_g is not None:
df = to_g(df, args.range_g)
if args.concat:
df["_source"] = str(pathlib.Path(in_path).name)
dfs.append(df)
else:
out_path = str(pathlib.Path(in_path).with_suffix(args.out_suffix))
df.to_csv(out_path, index=False, header=not args.no_header)
print(f"Zapisano {out_path} ({len(df)} rekordów)")
if args.concat and dfs:
out_path = str(pathlib.Path(args.inputs[0]).with_suffix(args.out_suffix))
big = pd.concat(dfs, ignore_index=True)
big.to_csv(out_path, index=False, header=not args.no_header)
print(f"Zapisano scalony {out_path} ({len(big)} rekordów z {len(dfs)} plików)")
if __name__ == "__main__":
main()

View File

@@ -0,0 +1,30 @@
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
# Wczytanie danych z pominięciem komentarzy (pierwsze 4 linie)
df = pd.read_csv('00000002.csv', comment='#')
# Obliczenie magnitudy
df['magnitude'] = np.sqrt(df['x']**2 + df['y']**2 + df['z']**2)
# Przeliczenie czasu na względny (od 0)
start_time = df['unix_ts'].min()
df['relative_time'] = df['unix_ts'] - start_time
# Tworzenie wykresu
fig, ax = plt.subplots(figsize=(12, 6))
for sensor_id in sorted(df['sensor_id'].unique()):
sensor_data = df[df['sensor_id'] == sensor_id]
ax.plot(sensor_data['relative_time'], sensor_data['magnitude'],
label=f'Sensor {sensor_id}', linewidth=0.8, alpha=0.8)
ax.set_xlabel('Czas [s] (od startu)')
ax.set_ylabel('Magnituda (jednostki surowe)')
ax.set_title('Porównanie magnitudy przyspieszenia (Sensor 0 vs Sensor 1)')
ax.legend()
ax.grid(True, linestyle='--', alpha=0.7)
plt.tight_layout()
plt.savefig('magnitude_comparison.png')

View File

@@ -0,0 +1,110 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Konwerter .wmt (nowy format próbek 12 B) -> CSV.
# python3 wmt_reader.py '/Users/katalog/00000001.wmt' -a -f -d <-wszystkie, +podfoldery, usuwa stare wmt
Plik .wmt:
Header (packed LE, 19 B):
char[3] magic="WMT"
uint16 version
uint16 headerSize
uint32 sampleSize (oczekiwane 12)
uint32 timestamp (Unix start akwizycji, sekundy)
uint32 reccount (liczba rekordów; może być 0)
Sample (packed LE, 12 B):
uint32 offset_us (µs od startu akwizycji)
uint8 sensor_id (0..6)
int16 x, y, z
uint8 ready (0/1)
CSV:
start_unix, offset_us, unix_ts, sensor_id, x, y, z, ready
"""
from __future__ import annotations
import argparse
import pathlib
import struct
import pandas as pd
from datetime import datetime
HEADER_FMT = "<3sHHIII" # 19 B
HEADER_SIZE = struct.calcsize(HEADER_FMT)
SAMPLE_FMT = "<IBhhhB" # 12 B: u32, u8, i16, i16, i16, u8
SAMPLE_SIZE = struct.calcsize(SAMPLE_FMT) # 12
def read_wmt_one(path: str) -> pd.DataFrame:
p = pathlib.Path(path)
blob = p.read_bytes()
if len(blob) < HEADER_SIZE:
raise ValueError(f"{p.name}: plik zbyt krótki ({len(blob)} B).")
magic, version, headerSize, sampleSize, start_unix, reccount = struct.unpack_from(HEADER_FMT, blob, 0)
if magic != b"WMT":
raise ValueError(f"{p.name}: niepoprawny magic {magic!r} (oczekiwano b'WMT').")
if sampleSize != SAMPLE_SIZE:
raise ValueError(f"{p.name}: sampleSize={sampleSize}, oczekiwano {SAMPLE_SIZE} (obsługiwany jest TYLKO nowy format 12 B).")
if len(blob) < headerSize:
raise ValueError(f"{p.name}: headerSize={headerSize} większy niż rozmiar pliku.")
# Wyświetlenie informacji o nagłówku
start_time = datetime.fromtimestamp(start_unix)
print(f"\n=== Plik: {p.name} ===")
print(f"Czas rozpoczęcia akwizycji: {start_time} (Unix: {start_unix})")
print(f"Liczba próbek zadeklarowana w nagłówku: {reccount}")
data = blob[headerSize:]
if len(data) % SAMPLE_SIZE != 0:
print(f"[WARN] {p.name}: długość danych {len(data)} nie jest wielokrotnością {SAMPLE_SIZE} (plik może być niepełny).")
nrec = len(data) // SAMPLE_SIZE
if reccount and reccount != nrec:
print(f"[INFO] {p.name}: reccount={reccount}, policzono={nrec}.")
else:
print(f"Policzono faktyczną liczbę próbek: {nrec}")
rows = []
off = 0
for _ in range(nrec):
rec = data[off:off+SAMPLE_SIZE]
offset_us, sensor_id, x, y, z, ready = struct.unpack(SAMPLE_FMT, rec)
unix_ts = start_unix + (offset_us / 1_000_000.0) # sekundy z ułamkiem
rows.append((start_unix, offset_us, unix_ts, sensor_id, x, y, z, int(ready)))
off += SAMPLE_SIZE
df = pd.DataFrame(rows, columns=["start_unix", "offset_us", "unix_ts", "sensor_id", "x", "y", "z", "ready"])
df = df.astype({"sensor_id": "int32", "x": "int32", "y": "int32", "z": "int32", "ready": "int32"})
return df
def main():
ap = argparse.ArgumentParser(description="Konwersja .wmt (nowy format 12 B) -> CSV.")
ap.add_argument("inputs", nargs="+", help="Ścieżki do plików .wmt")
ap.add_argument("--out-suffix", default=".csv", help="Sufiks wyjściowy (domyślnie .csv)")
ap.add_argument("--concat", action="store_true", help="Scal wszystkie wejścia do jednego CSV (na bazie pierwszego pliku)")
ap.add_argument("--no-header", action="store_true", help="Zapisz CSV bez nagłówka")
args = ap.parse_args()
dfs = []
for in_path in args.inputs:
df = read_wmt_one(in_path)
if args.concat:
df["_source"] = str(pathlib.Path(in_path).name)
dfs.append(df)
else:
out_path = str(pathlib.Path(in_path).with_suffix(args.out_suffix))
df.to_csv(out_path, index=False, header=not args.no_header)
print(f"Zapisano {out_path} ({len(df)} rekordów)")
if args.concat and dfs:
out_path = str(pathlib.Path(args.inputs[0]).with_suffix(args.out_suffix))
big = pd.concat(dfs, ignore_index=True)
big.to_csv(out_path, index=False, header=not args.no_header)
print(f"\nZapisano scalony {out_path} ({len(big)} rekordów z {len(dfs)} plików)")
if __name__ == "__main__":
main()