fix(minco_ws): step_blocker 兼容新版 sensor_msgs_py 的 read_points(此前启动即崩溃)
现象:整机 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 才生效。
This commit is contained in:
@@ -112,7 +112,15 @@ class StepBlocker(Node):
|
|||||||
|
|
||||||
def _cb(self, msg):
|
def _cb(self, msg):
|
||||||
raw = pc2.read_points(msg, field_names=('x', 'y', 'z'), skip_nans=True)
|
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:
|
if pts.size == 0:
|
||||||
pts = np.zeros((0, 3), dtype=np.float32)
|
pts = np.zeros((0, 3), dtype=np.float32)
|
||||||
elif pts.ndim == 1:
|
elif pts.ndim == 1:
|
||||||
|
|||||||
Reference in New Issue
Block a user