Files
VSAG_SENTRY_SIM_27RMUL/tools/smoke_probe.py
T

106 lines
3.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
"""冒烟探针:等 Nav2 完全激活 → 收几帧过滤后的点云 → 发导航目标并等结果。"""
import sys
import time
import rclpy
from rclpy.node import Node
from rclpy.action import ActionClient
from rclpy.qos import QoSProfile, ReliabilityPolicy
from sensor_msgs.msg import PointCloud2
from geometry_msgs.msg import PoseStamped
from nav2_msgs.action import NavigateToPose
from lifecycle_msgs.srv import GetState
from lifecycle_msgs.msg import State
rclpy.init()
node = Node('smoke_probe', parameter_overrides=[])
node.set_parameters([rclpy.parameter.Parameter('use_sim_time',
rclpy.Parameter.Type.BOOL, True)])
got = {'cloud': 0, 'pts': 0}
def on_cloud(msg):
got['cloud'] += 1
got['pts'] = msg.width * msg.height
qos = QoSProfile(depth=3, reliability=ReliabilityPolicy.BEST_EFFORT)
node.create_subscription(PointCloud2, '/scan/points_filtered', on_cloud, qos)
def wait_active(name, timeout):
"""等某个 lifecycle 节点进入 ACTIVE(id=3)。"""
cli = node.create_client(GetState, f'/{name}/get_state')
if not cli.wait_for_service(timeout_sec=10.0):
return f'服务 /{name}/get_state 不存在'
deadline = time.time() + timeout
while time.time() < deadline:
fut = cli.call_async(GetState.Request())
rclpy.spin_until_future_complete(node, fut, timeout_sec=5.0)
if fut.done() and fut.result() is not None:
st = fut.result().current_state
if st.id == State.PRIMARY_STATE_ACTIVE:
return None
print('[probe] %s = %s,继续等…' % (name, st.label))
time.sleep(2)
return f'{name} {timeout} 秒内没到 ACTIVE'
print('[probe] 等 Nav2 生命周期节点激活…')
for name in ('bt_navigator', 'planner_server', 'controller_server'):
err = wait_active(name, 180)
if err:
print('[probe] ❌', err)
sys.exit(2)
print('[probe] ✅ %s ACTIVE' % name)
AC = ActionClient(node, NavigateToPose, 'navigate_to_pose')
if not AC.wait_for_server(timeout_sec=30.0):
print('[probe] ❌ navigate_to_pose 动作服务器不存在')
sys.exit(2)
print('[probe] ✅ 动作服务器就绪')
t0 = time.time()
while time.time() - t0 < 25 and got['cloud'] < 3:
rclpy.spin_once(node, timeout_sec=0.2)
print('[probe] /scan/points_filtered: %d 帧, 最后一帧 %d 点 %s'
% (got['cloud'], got['pts'], '✅' if got['cloud'] else '❌ 一帧都没收到'))
goal = NavigateToPose.Goal()
goal.pose = PoseStamped()
goal.pose.header.frame_id = 'map'
goal.pose.header.stamp = node.get_clock().now().to_msg()
goal.pose.pose.position.x = 2.0
goal.pose.pose.position.y = 2.0
goal.pose.pose.orientation.w = 1.0
print('[probe] 发导航目标 (2.0, 2.0) …')
handle = None
for attempt in range(1, 13):
send = AC.send_goal_async(goal)
rclpy.spin_until_future_complete(node, send, timeout_sec=30)
handle = send.result()
if handle is not None and handle.accepted:
print('[probe] 目标已接受(第 %d 次尝试)' % attempt)
break
print('[probe] 第 %d 次被拒绝,5 秒后重试…' % attempt)
time.sleep(5)
handle = None
if handle is None:
print('[probe] ❌ 目标始终被拒绝')
sys.exit(3)
res = handle.get_result_async()
rclpy.spin_until_future_complete(node, res, timeout_sec=300)
if not res.done():
print('[probe] ❌ 300 秒内没拿到结果')
sys.exit(4)
status = res.result().status
print('[probe] ★ NavigateToPose status = %d (%s)'
% (status, 'SUCCEEDED' if status == 4 else '不是 SUCCEEDED'))
rclpy.shutdown()
sys.exit(0 if status == 4 else 5)