#!/usr/bin/env python3 """Isolated live relay test. No Gazebo, TF, or real actuator subscribers.""" import argparse import os from pathlib import Path import signal import subprocess import time import uuid def main(): parser=argparse.ArgumentParser(description=__doc__) parser.add_argument('--binary',type=Path,default=Path(__file__).resolve().parents[1]/'build/bin/cmd_vel_relay') args=parser.parse_args() os.environ.update(ROS_DOMAIN_ID=str(130+int(uuid.uuid4().hex[:4],16)%40), FASTDDS_BUILTIN_TRANSPORTS='UDPv4',ROS2CLI_DISABLE_DAEMON='1') import rclpy from geometry_msgs.msg import Twist from std_msgs.msg import Bool from action_msgs.srv import CancelGoal rclpy.init();node=rclpy.create_node('test_motion_interlock') received=[];cancellations=[] subscription=node.create_subscription(Twist,'/model/sentry/cmd_vel',lambda m:received.append((time.monotonic(),m.linear.x)),100) health=node.create_publisher(Bool,'/field_localization/navigation_valid',1) final=node.create_publisher(Twist,'/cmd_vel',1) upstream=node.create_publisher(Twist,'/cmd_vel_smoothed',1) raw=node.create_publisher(Twist,'/cmd_vel_nav',1) def cancel(req,response): cancellations.append(time.monotonic());response.return_code=0 return response services=[node.create_service(CancelGoal,name+'/_action/cancel_goal',cancel) for name in ['/navigate_to_pose','/navigate_through_poses']] process=subprocess.Popen([str(args.binary),'--ros-args','-p','navigation_mode:=true'], start_new_session=True,stdout=subprocess.DEVNULL,stderr=subprocess.PIPE,text=True) checks=0 def check(ok,message): nonlocal checks checks+=1 if not ok: raise RuntimeError(message) print('PASS',message,flush=True) def drive(duration,valid=True,publish_health=True,publish_final=True): until=time.monotonic()+duration while time.monotonic()=before and v==0] check(bool(zeros) and min(zeros)-before<0.1,'invalid health stops within 100 ms') check(len(cancellations)>=2,'navigation action servers receive cancellation') drive(0.5) before=time.monotonic();drive(0.6,publish_health=False) zeros=[t for t,v in received if t>=before+0.3 and v==0] check(bool(zeros) and min(zeros)-before<0.4,'stale health stops within 400 ms') drive(0.5) before=time.monotonic();drive(0.6,publish_final=False) zeros=[t for t,v in received if t>=before+0.3 and v==0] check(bool(zeros) and min(zeros)-before<0.4,'stale final command stops despite upstream traffic') check(process.poll() is None,'relay remains alive') print(f'{checks} live checks passed; health_stop_ms={(min(t for t,v in received if t>=before+0.3 and v==0)-before)*1000:.3f}') finally: if process.poll() is None:os.killpg(process.pid,signal.SIGINT) try:process.wait(timeout=5) except subprocess.TimeoutExpired:os.killpg(process.pid,signal.SIGKILL);process.wait() _=subscription,services node.destroy_node();rclpy.shutdown() if __name__=='__main__':main()