Added map endpoints for the Lumi app

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-16 18:22:17 +09:00
co-authored by Claude Opus 5.5
parent ed2c6bdafd
commit de2fb71c81
5 changed files with 324 additions and 2 deletions
+75
View File
@@ -0,0 +1,75 @@
"""Self-check of the map edits for the Lumi app (2026-11-16): save a marker where Lumi stands or at a tapped pose,
delete one (never the main dock), set Lumi's position on the map. Fake base, no robot: python tests/test_map_edit.py"""
import no_robot # noqa: F401 any connection to the robot fails the test
from fastapi import HTTPException
from api import server
sent = []
server.agv.request = lambda cmd, timeout=3.0: sent.append(cmd) or {"status": "OK", "results": {}}
server.agv.dock_marker = lambda: "Home"
server.agv.known_markers = lambda fresh=False: sent.append("refresh") if fresh else None
server.robot_free = lambda manual=False: None
def refused(fn, *a, code=409):
try:
fn(*a)
except HTTPException as e:
return e.status_code == code
return False
# where Lumi stands, and at a tapped pose (an existing name is moved there)
server.marker_save(server.MarkerBody(name="door_1"))
assert sent == ["/api/markers/insert?name=door_1&type=0&num=1", "refresh"], sent
sent.clear()
server.marker_save(server.MarkerBody(name="p11", x=1.5, y=-2.25, theta=0.5))
assert sent == ["/api/markers/insert_by_pose?name=p11&x=1.5&y=-2.25&theta=0.5&type=0", "refresh"], sent
sent.clear()
for bad in ("", "a b", "x/y", "로비", "a" * 51): # the base takes letters, digits, _ and - only
assert refused(server.marker_save, server.MarkerBody(name=bad), code=422), bad
assert sent == [], sent
# delete; never the main dock (docking needs it)
server.marker_delete(server.MarkerName(name="door_1"))
assert sent == ["/api/markers/delete?name=door_1", "refresh"], sent
sent.clear()
assert refused(server.marker_delete, server.MarkerName(name="Home"))
assert sent == [], sent
# Lumi's position on the map (relocalise), only while nothing runs
server.map_position(server.PointBody(x=-1.2, y=0.05, theta=-0.04))
assert sent == ["/api/position_adjust_by_pose?x=-1.2&y=0.05&theta=-0.04"], sent
sent.clear()
server.robot_free = lambda manual=False: (_ for _ in ()).throw(HTTPException(409, server.BUSY_MSG))
assert refused(server.map_position, server.PointBody(x=0, y=0))
assert sent == []
# the map picture in the app's dark look: unknown almost black, free space dark, walls light (same size)
import cv2 # noqa: E402
import numpy as np # noqa: E402
pic = np.full((4, 6), 205, np.uint8) # unknown
pic[1, 1:5] = 255 # free
pic[2, 2] = 0 # a wall
png = cv2.imencode(".png", pic)[1].tobytes()
server.agv.request = lambda cmd, timeout=3.0: {"results": {"map_name": "m", "floor": 1}}
server.urllib.request.urlopen = lambda url, timeout=10: type("R", (), {"read": lambda s: png, "__enter__": lambda s: s,
"__exit__": lambda s, *a: False})()
out = cv2.imdecode(np.frombuffer(server.map_image(dark=True).body, np.uint8), cv2.IMREAD_COLOR)
assert out.shape == (4, 6, 3), out.shape
assert tuple(out[0, 0]) == server.DARK_UNKNOWN[::-1] and tuple(out[1, 1]) == server.DARK_FREE[::-1], (out[0, 0], out[1, 1])
assert tuple(out[2, 2]) == server.DARK_WALL[::-1], out[2, 2]
assert server.map_image().body == png # as the base draws it, unless asked
# Lumi stands at a marker: the base takes its pose from it (after a map switch, the quickest way)
sent.clear()
server.agv.request = lambda cmd, timeout=3.0: sent.append(cmd) or {"status": "OK", "results": {}}
server.robot_free = lambda manual=False: None
server.map_position_marker(server.MarkerName(name="Home"))
assert sent == ["/api/position_adjust?marker=Home"], sent
assert refused(server.map_position_marker, server.MarkerName(name="a b"), code=422)
print("ok: map edits")
+50
View File
@@ -0,0 +1,50 @@
"""Self-check of reading the base's lidar for the Lumi app's position correction: the rosbridge WebSocket frames
(robot/rosbridge.py) and the scan as points around the base's centre (api/server.py /map/scan). Fake socket, no robot:
python tests/test_rosbridge.py"""
import json
import math
import struct
import no_robot # noqa: F401 any connection to the robot fails the test
from robot import rosbridge
from api import server
# a client frame is masked and carries the JSON; a server frame (unmasked, maybe split over reads) is read back
f = rosbridge.client_frame(b'{"op":"x"}')
assert f[0] == 0x81 and f[1] & 0x80 and len(f) == 2 + 4 + 10, f
mask, body = f[2:6], f[6:]
assert bytes(b ^ mask[i % 4] for i, b in enumerate(body)) == b'{"op":"x"}'
big = json.dumps({"msg": {"ranges": [1.0] * 300}}).encode()
server_frame = bytes([0x81, 126]) + struct.pack(">H", len(big)) + big
class Sock:
def __init__(self, data, step=50):
self.data, self.step = data, step
def recv(self, n):
out, self.data = self.data[:self.step], self.data[self.step:]
return out
msg, rest = rosbridge.read_message(Sock(server_frame + b"\x81\x02{}"), b"")
assert msg["msg"]["ranges"][0] == 1.0 and len(msg["msg"]["ranges"]) == 300, msg
assert rest == b"\x81\x02{}" or rest == b"", rest
# the scan as points (x ahead, y left, m) around the base's centre: the lidar sits LASER_X ahead of it
scan = {"angle_min": -math.pi / 2, "angle_increment": math.pi / 2, "range_min": 0.05, "range_max": 30.0,
"ranges": [1.0, 2.0, 1.0, None, 0.01, 50.0]}
pts = server.scan_points(scan, every=1)
assert len(pts) == 3, pts # None, too near and too far left out
assert pts[0] == [round(server.LASER_X, 3), -1.0], pts[0] # right of the lidar
assert pts[1] == [round(server.LASER_X + 2.0, 3), 0.0], pts[1] # ahead
assert pts[2] == [round(server.LASER_X, 3), 1.0], pts[2] # left
assert len(server.scan_points({**scan, "ranges": [1.0] * 6}, every=2)) == 3 # every 2nd
# /map/scan reads one message of the base's /scan
server.rosbridge.read_once = lambda topic, timeout=3.0: {"msg": scan} if topic == "/scan" else None
r = server.map_scan()
assert r["points"] == server.scan_points(scan) and r["frame"] == "base", r # every 2nd ray by default
print("ok: rosbridge and scan")