Skip to content

Commit 4d3baff

Browse files
author
Harry Coureau
committed
fix: preserve Q10 room labels and sample header calibration
1 parent 583bba1 commit 4d3baff

10 files changed

Lines changed: 45 additions & 89 deletions

File tree

‎roborock/diagnostics.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ def reset(self) -> None:
106106
"ipAddress",
107107
"ipAdress",
108108
"wifiName",
109+
"rawName",
109110
"lat",
110111
"long",
111112
}

‎roborock/map/b01_grid_layers.py‎

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -220,58 +220,6 @@ def solve_calibration(
220220
return best[1]
221221

222222

223-
def solve_calibration_with_origin(
224-
layers: GridLayers,
225-
points: list[tuple[float, float]],
226-
origin: tuple[float, float],
227-
*,
228-
resolutions: Iterable[float],
229-
y_signs: Iterable[int] = (1, -1),
230-
min_on_floor: float = 0.5,
231-
) -> GridCalibration | None:
232-
"""Fit resolution + Y orientation around a *known* grid-pixel origin.
233-
234-
Unlike :func:`solve_calibration`, the pixel origin ``(ox, oy)`` is fixed --
235-
e.g. read straight from the Q10 grid-frame header -- so this only sweeps the
236-
candidate ``resolutions`` and ``y_signs`` and keeps the placement landing the
237-
most ``points`` on floor. With the expensive 2-D offset slide gone, far fewer
238-
points are needed to confirm the fit, so it works from a short path rather
239-
than a dense clean. Returns ``None`` if no candidate lands a ``min_on_floor``
240-
fraction of points on floor (e.g. the origin or points are bogus).
241-
"""
242-
if not points:
243-
return None
244-
w, h = layers.width, layers.height
245-
ox, oy = origin
246-
cells = _calibration_cells(layers)
247-
248-
best: tuple[float, GridCalibration] | None = None
249-
for resolution in resolutions:
250-
if resolution <= 0:
251-
continue
252-
for y_sign in y_signs:
253-
on_floor = 0
254-
blocked = 0
255-
for x, y in points:
256-
px = int(x / resolution + ox)
257-
py = int(oy - y_sign * y / resolution)
258-
if not (0 <= px < w and 0 <= py < h):
259-
blocked += 1
260-
continue
261-
cell_type = cells[py * w + px]
262-
if cell_type == _CalibrationCell.FLOOR:
263-
on_floor += 1
264-
elif cell_type == _CalibrationCell.BLOCKED:
265-
blocked += 1
266-
score = on_floor - 1.5 * blocked
267-
if best is None or score > best[0]:
268-
best = (score, GridCalibration(float(resolution), float(ox), float(oy), y_sign))
269-
270-
if best is None or best[0] < len(points) * min_on_floor:
271-
return None
272-
return best[1]
273-
274-
275223
def decompose_grid(
276224
width: int,
277225
height: int,

‎roborock/map/b01_q10_map_parser.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ class Q10Room(RoborockBase):
119119
@property
120120
def name(self) -> str:
121121
"""User friendly room name (firmware ``rr_`` defaults are normalized)."""
122+
if not self.raw_name.startswith("rr_"):
123+
return self.raw_name
122124
return self.raw_name.removeprefix("rr_").replace("_", " ").strip().title()
123125

124126

‎roborock/map/b01_q10_render.py‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
from roborock.exceptions import RoborockException
2929

3030
from .b01_grid_layers import (
31+
LAYER_FLOOR,
3132
GridCalibration,
3233
GridLayers,
3334
solve_calibration,
@@ -43,6 +44,7 @@
4344
Q10Point,
4445
Q10Room,
4546
Q10TracePacket,
47+
classify_q10_cell,
4648
erased_packet,
4749
)
4850
from .b01_q10_overlays import ZONE_TYPE_NO_GO, ZONE_TYPE_NO_MOP, Q10Zone
@@ -186,11 +188,11 @@ def _trace_calibration_from_header_metadata(
186188
origin_y=origin[1],
187189
y_sign=1,
188190
)
189-
return calibration if _trace_projects_onto_floor(packet.layers, points, calibration) else None
191+
return calibration if _trace_projects_onto_floor(packet, points, calibration) else None
190192

191193

192194
def _trace_projects_onto_floor(
193-
layers: GridLayers,
195+
packet: Q10MapPacket,
194196
points: Sequence[Q10Point],
195197
calibration: GridCalibration,
196198
) -> bool:
@@ -203,9 +205,9 @@ def _trace_projects_onto_floor(
203205
for point in sample:
204206
pixel_x, pixel_y = calibration.world_to_pixel(point.x, point.y)
205207
column, row = math.floor(pixel_x), math.floor(pixel_y)
206-
if not (0 <= column < layers.width and 0 <= row < layers.height):
208+
if not (0 <= column < packet.width and 0 <= row < packet.height):
207209
continue
208-
if layers.cell_class(layers.grid[row * layers.width + column]) == "floor":
210+
if classify_q10_cell(packet.grid[row * packet.width + column]) == LAYER_FLOOR:
209211
on_floor += 1
210212
return on_floor >= len(sample) * 0.5
211213

‎tests/devices/traits/b01/q10/test_map.py‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def test_update_from_map_packet_populates_image_and_rooms() -> None:
8686

8787
assert trait.image_content is not None
8888
assert trait.image_content[:8] == b"\x89PNG\r\n\x1a\n"
89-
assert {room.id: room.name for room in trait.rooms} == {2: "Living Room", 3: "Bedroom"}
89+
assert {room.id: room.name for room in trait.rooms} == {2: "Living Room", 3: "bedroom"}
9090
assert len(updates) == 1
9191

9292

@@ -235,7 +235,7 @@ async def test_subscribe_loop_routes_map_push(
235235
message_queue.put_nowait(parse_map_packet(FIXTURE.read_bytes()))
236236

237237
await _wait_for(lambda: q10_api.map.image_content is not None)
238-
assert {room.id: room.name for room in q10_api.map.rooms} == {2: "Living Room", 3: "Bedroom"}
238+
assert {room.id: room.name for room in q10_api.map.rooms} == {2: "Living Room", 3: "bedroom"}
239239

240240

241241
async def test_subscribe_loop_routes_trace_push(
@@ -659,6 +659,8 @@ def test_docking_clears_room_and_ignores_late_fixture_trace(status: YXDeviceStat
659659
assert trait.current_room is None
660660
trait.update_from_trace_packet(trace)
661661
assert trait.current_room == packet.rooms[1]
662+
663+
662664
async def test_create_forwards_obstacle_drawables_to_rendering(fake_channel: FakeB01Q10Channel) -> None:
663665
"""The public Q10 factory config changes the rendered image."""
664666
enabled = create(fake_channel, map_parser_config=B01Q10MapParserConfig(drawables=[Drawable.OBSTACLES]))

‎tests/map/test_b01_grid_layers.py‎

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
GridCalibration,
1313
decompose_grid,
1414
solve_calibration,
15-
solve_calibration_with_origin,
1615
)
1716

1817

@@ -125,27 +124,3 @@ def test_solve_calibration_returns_none_when_unfittable() -> None:
125124
# Points so far apart no resolution keeps them on the 6x6 floor block.
126125
points = [(0.0, 0.0), (1000.0, 0.0), (0.0, 1000.0)]
127126
assert solve_calibration(layers, points, resolutions=[2.0]) is None
128-
129-
130-
def test_solve_calibration_with_origin_fits_resolution_from_short_path() -> None:
131-
"""With a known origin only resolution is fit, so a tiny path suffices."""
132-
layers = _floor_block_layers()
133-
true = GridCalibration(2.0, 3.0, 8.0, 1)
134-
points = [true.pixel_to_world(px, py) for px, py in [(4, 7), (6, 5)]] # two points
135-
cal = solve_calibration_with_origin(layers, points, (true.origin_x, true.origin_y), resolutions=[1.0, 2.0, 3.0])
136-
assert cal is not None
137-
assert (cal.resolution, cal.origin_x, cal.origin_y, cal.y_sign) == (2.0, 3.0, 8.0, 1)
138-
139-
140-
def test_solve_calibration_with_origin_returns_none_off_floor() -> None:
141-
"""A wrong origin that lands the path off floor is rejected, not forced."""
142-
layers = _floor_block_layers()
143-
true = GridCalibration(2.0, 3.0, 8.0, 1)
144-
points = [true.pixel_to_world(px, py) for px, py in [(4, 7), (6, 5)]]
145-
# Origin shoved into the background corner: every point lands off floor.
146-
assert solve_calibration_with_origin(layers, points, (0.0, 0.0), resolutions=[2.0]) is None
147-
148-
149-
def test_solve_calibration_with_origin_returns_none_without_points() -> None:
150-
layers = _floor_block_layers()
151-
assert solve_calibration_with_origin(layers, [], (3.0, 8.0), resolutions=[2.0]) is None

‎tests/map/test_b01_q10_map_parser.py‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ def test_packet_layers_decompose_q10_fixture() -> None:
153153
"""The Q10 synthetic fixture splits into floor + per-room layers."""
154154
layers = parse_map_packet(_payload()).layers
155155
assert layers.class_counts.get(LAYER_FLOOR) == 26
156-
assert {room.id: room.name for room in layers.rooms} == {2: "Living Room", 3: "Bedroom"}
156+
assert {room.id: room.name for room in layers.rooms} == {2: "Living Room", 3: "bedroom"}
157157

158158
living = layers.render_room(2, (255, 0, 0, 255))
159159
image = Image.open(io.BytesIO(living))
@@ -202,9 +202,9 @@ def test_parse_map_packet_dimensions_straddling_256() -> None:
202202

203203

204204
def test_room_name_normalization() -> None:
205-
"""Firmware ``rr_`` default names are normalized; custom names are titled."""
205+
"""Firmware ``rr_`` defaults are normalized; custom names stay verbatim."""
206206
assert Q10Room(id=2, raw_name="rr_living_room", pixel_value=8, pixel_count=9).name == "Living Room"
207-
assert Q10Room(id=3, raw_name="bedroom", pixel_value=12, pixel_count=9).name == "Bedroom"
207+
assert Q10Room(id=3, raw_name="bedroom", pixel_value=12, pixel_count=9).name == "bedroom"
208208

209209

210210
def test_room_pixel_count_matches_grid() -> None:
@@ -627,3 +627,8 @@ def test_maximum_counted_obstacle_table_is_bounded() -> None:
627627
packet = parse_map_packet(FIXTURE.read_bytes() + _carpet_tail(8, 6, bytes(48)) + table)
628628
assert packet.obstacles == [Q10Obstacle(-10, 20)] * 255
629629
assert packet.skip_cleaning_points == []
630+
631+
632+
@pytest.mark.parametrize("label", ["Salle d’eau", "Owner's bedroom", "lower_case", " Study ", ""])
633+
def test_custom_room_name_preserved(label: str) -> None:
634+
assert Q10Room(id=3, raw_name=label, pixel_value=12, pixel_count=9).name == label

‎tests/map/test_b01_q10_render.py‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ def test_map_data_from_packet_populates_room_names_and_carpet_map() -> None:
149149
carpet[20] = 3
150150
packet = replace(_packet(), carpet_mask=bytes(carpet))
151151
map_data = _map_data_from_packet(packet, config=CONFIG)
152-
assert map_data.additional_parameters["room_names"] == {2: "Living Room", 3: "Bedroom"}
152+
assert map_data.additional_parameters["room_names"] == {2: "Living Room", 3: "bedroom"}
153153
assert map_data.carpet_map == {10, 20}
154154

155155

@@ -386,6 +386,8 @@ def test_current_room_requires_pose_calibration_and_matching_metadata() -> None:
386386
assert resolve_q10_current_room(packet, Q10TracePacket(points=[Q10Point(100, 60)])) is None
387387
packet = replace(packet, header_calibration=HEADER, rooms=[])
388388
assert resolve_q10_current_room(packet, Q10TracePacket(points=[Q10Point(100, 60)])) is None
389+
390+
389391
@pytest.mark.parametrize("origin", [(0, 500), (100, 600)])
390392
def test_render_obstacle_at_header_anchored_position(origin: tuple[int, int]) -> None:
391393
"""A marker is placed using 50 raw units per pixel, with a top-down image."""
@@ -430,3 +432,15 @@ def render(drawables: list[Drawable], source: Q10MapPacket = packet) -> bytes:
430432
assert render([Drawable.OBSTACLES]) != render([])
431433
assert render([]) == render([], replace(packet, obstacles=[]))
432434
assert render([Drawable.OBSTACLES], replace(packet, obstacles=[])) == render([], replace(packet, obstacles=[]))
435+
436+
437+
def test_resolve_current_room_valid_header_avoids_grid_decomposition(monkeypatch: pytest.MonkeyPatch) -> None:
438+
"""Reading the current room with a valid header only samples raw grid cells."""
439+
packet = replace(_packet(), header_calibration=HEADER)
440+
trace = Q10TracePacket(points=[Q10Point(100, 60)])
441+
442+
def fail_decomposition(*args: object, **kwargs: object) -> None:
443+
pytest.fail("Current-room lookup decomposed the full occupancy grid")
444+
445+
monkeypatch.setattr("roborock.map.b01_q10_map_parser.decompose_grid", fail_decomposition)
446+
assert resolve_q10_current_room(packet, trace) == packet.rooms[1]

‎tests/protocols/test_b01_q10_protocol.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ def test_decode_message_map_packet() -> None:
141141
message = _message(MAP_FIXTURE.read_bytes(), RoborockMessageProtocol.MAP_RESPONSE)
142142
decoded = decode_message(message)
143143
assert isinstance(decoded, Q10MapPacket)
144-
assert {room.id: room.name for room in decoded.rooms} == {2: "Living Room", 3: "Bedroom"}
144+
assert {room.id: room.name for room in decoded.rooms} == {2: "Living Room", 3: "bedroom"}
145145

146146

147147
@pytest.mark.parametrize(

‎tests/test_diagnostics.py‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import pytest
44

5-
from roborock.diagnostics import Diagnostics, redact_device_uid, redact_topic_name
5+
from roborock.diagnostics import REDACTED, Diagnostics, redact_device_data, redact_device_uid, redact_topic_name
66

77

88
def test_empty_diagnostics():
@@ -99,3 +99,10 @@ def test_redact_device(duid: str, expected: str) -> None:
9999

100100
redacted = redact_device_uid(duid)
101101
assert redacted == expected
102+
103+
104+
def test_redact_q10_raw_room_name() -> None:
105+
"""Custom Q10 room labels are private in exported diagnostics."""
106+
assert redact_device_data(
107+
{"map": {"rooms": [{"rawName": "Owner’s bedroom"}], "currentRoom": {"rawName": "Salle d’eau"}}}
108+
) == {"map": {"rooms": [{"rawName": REDACTED}], "currentRoom": {"rawName": REDACTED}}}

0 commit comments

Comments
 (0)