sl-webui f71dad5344 feat(arch): migrate all STM32/Mamba/BlackPill refs to ESP32 BALANCE/IO + fix roslib@1.4.0
Architecture change (2026-04-03): Mamba F722S (STM32F722) and BlackPill
replaced by ESP32 BALANCE (PID loop) and ESP32 IO (motors/sensors/comms).

- Update CLAUDE.md, docs, chassis BOM/ASSEMBLY, pinout, power-budget,
  wiring-diagram, TEAM.md, AUTONOMOUS_ARMING.md, docker-compose
- Update all ROS2 package comments, config labels, launch args
  (stm32_port→esp32_port, /dev/stm32-bridge→/dev/esp32-bridge)
- Update WebUI: stm32Mode→esp32Mode, stm32Version→esp32Version,
  "STM32 State/Mode" labels → "ESP32 State/Mode" (ControlMode, SettingsPanel)
- Add TODO(esp32-migration) markers on stm32_protocol.py and mamba_protocol.py
  binary frame layouts — pending ESP32 protocol spec from max
- Fix roslib CDN 1.3.0→1.4.0 in all 11 HTML panels (fixes ROS2 Humble
  rosbridge "Received a message without an op" incompatibility)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-04 08:25:24 -04:00

98 lines
3.1 KiB
Python

"""
Unit tests for ESP32 BALANCE telemetry parsing logic.
Run with: pytest jetson/ros2_ws/src/saltybot_bridge/test/test_parse.py
"""
import json
import pytest
# ── Minimal stub so we can test parsing without a ROS2 runtime ───────────────
def parse_frame(raw: bytes):
"""Mirror of the parsing logic in serial_bridge_node._parse_and_publish."""
text = raw.decode("ascii", errors="ignore").strip()
if not text:
return None
data = json.loads(text)
if "err" in data:
return {"type": "fault", "errno": data["err"]}
required = ("p", "r", "e", "ig", "m", "s", "y")
if not all(k in data for k in required):
raise ValueError(f"Incomplete frame: {data}")
return {
"type": "telemetry",
"pitch_deg": data["p"] / 10.0,
"roll_deg": data["r"] / 10.0,
"yaw_deg": data["y"] / 10.0,
"pid_error_deg": data["e"] / 10.0,
"integral": data["ig"] / 10.0,
"motor_cmd": int(data["m"]),
"state": int(data["s"]),
}
# ── Tests ─────────────────────────────────────────────────────────────────────
def test_normal_frame():
raw = b'{"p":125,"r":-30,"e":50,"ig":20,"m":350,"s":1,"y":0}\n'
result = parse_frame(raw)
assert result["type"] == "telemetry"
assert result["pitch_deg"] == pytest.approx(12.5)
assert result["roll_deg"] == pytest.approx(-3.0)
assert result["yaw_deg"] == pytest.approx(0.0)
assert result["pid_error_deg"] == pytest.approx(5.0)
assert result["integral"] == pytest.approx(2.0)
assert result["motor_cmd"] == 350
assert result["state"] == 1 # ARMED
def test_fault_frame():
raw = b'{"err":-1}\n'
result = parse_frame(raw)
assert result["type"] == "fault"
assert result["errno"] == -1
def test_zero_frame():
raw = b'{"p":0,"r":0,"e":0,"ig":0,"m":0,"s":0,"y":0}\n'
result = parse_frame(raw)
assert result["type"] == "telemetry"
assert result["pitch_deg"] == pytest.approx(0.0)
assert result["state"] == 0 # DISARMED
def test_tilt_fault_state():
raw = b'{"p":450,"r":0,"e":400,"ig":999,"m":1000,"s":2,"y":0}\n'
result = parse_frame(raw)
assert result["state"] == 2 # TILT_FAULT
assert result["pitch_deg"] == pytest.approx(45.0)
assert result["motor_cmd"] == 1000
def test_negative_motor_cmd():
raw = b'{"p":-100,"r":0,"e":-100,"ig":-50,"m":-750,"s":1,"y":10}\n'
result = parse_frame(raw)
assert result["motor_cmd"] == -750
assert result["pitch_deg"] == pytest.approx(-10.0)
def test_incomplete_frame_raises():
raw = b'{"p":100,"r":0}\n'
with pytest.raises(ValueError, match="Incomplete frame"):
parse_frame(raw)
def test_empty_line_returns_none():
assert parse_frame(b"\n") is None
assert parse_frame(b" \n") is None
def test_corrupt_frame_raises_json_error():
raw = b'not_json\n'
with pytest.raises(json.JSONDecodeError):
parse_frame(raw)