From 7e31f1451111537c984c8fb9673e2a558c28f5c5 Mon Sep 17 00:00:00 2001 From: walunt5 Date: Sat, 24 Oct 2026 21:47:49 +0800 Subject: [PATCH] =?UTF-8?q?fix(minco=5Fws):=20step=5Fblocker=20=E5=85=BC?= =?UTF-8?q?=E5=AE=B9=E6=96=B0=E7=89=88=20sensor=5Fmsgs=5Fpy=20=E7=9A=84=20?= =?UTF-8?q?read=5Fpoints=EF=BC=88=E6=AD=A4=E5=89=8D=E5=90=AF=E5=8A=A8?= =?UTF-8?q?=E5=8D=B3=E5=B4=A9=E6=BA=83=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 现象:整机 launch 起来后 step_blocker 立刻退出 TypeError: Cannot cast array data from dtype({'names':['x','y','z'],...itemsize:48}) to dtype('float32') at step_blocker_node.py: pts = np.asarray(list(raw), dtype=np.float32) 根因:本机 sensor_msgs_py.point_cloud2.read_points 的返回类型标注是 np.ndarray (**结构化数组**),不是旧版的'元组生成器',故 list(raw) 得到 np.void,无法 astype。 后果:step_blocker 一挂,/cloud_registered_for_scan 就没有发布者, scan_planner 订阅到死话题 —— 表面症状是'地图空的',而非报错。 修法:先判 isinstance(raw, np.ndarray) and raw.dtype.names,用 raw['x']/['y']/['z'] 堆叠后转 float32;否则走旧的 list(raw) 分支,兼容两代 API。 注意本包是 ament_cmake + install(PROGRAMS),install 侧脚本是**拷贝**, 改完必须 colcon build --packages-select step_blocker 才生效。 --- .../step_blocker/step_blocker/step_blocker_node.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/minco_ws/src/scan/step_blocker/step_blocker/step_blocker_node.py b/minco_ws/src/scan/step_blocker/step_blocker/step_blocker_node.py index 5653d0f..75d599f 100755 --- a/minco_ws/src/scan/step_blocker/step_blocker/step_blocker_node.py +++ b/minco_ws/src/scan/step_blocker/step_blocker/step_blocker_node.py @@ -112,7 +112,15 @@ class StepBlocker(Node): def _cb(self, msg): raw = pc2.read_points(msg, field_names=('x', 'y', 'z'), skip_nans=True) - pts = np.asarray(list(raw), dtype=np.float32) + # sensor_msgs_py 有两代返回类型: + # 旧版 -> 元组生成器,可用 np.asarray(list(raw)) + # 新版(Humble 2023 之后的更新)-> 结构化 np.ndarray, + # list() 出来是 np.void,直接 astype(float32) 会 TypeError。 + if isinstance(raw, np.ndarray) and raw.dtype.names: + pts = (np.stack([raw['x'], raw['y'], raw['z']], axis=-1).astype(np.float32) + if raw.size else np.zeros((0, 3), dtype=np.float32)) + else: + pts = np.asarray(list(raw), dtype=np.float32) if pts.size == 0: pts = np.zeros((0, 3), dtype=np.float32) elif pts.ndim == 1: