fix(nav): stop on invalid localization and enforce final velocity path

This commit is contained in:
mzx committed 2026-11-22 11:43:13 +08:00
1 parent b772174c1c
commit 28ac7c488e
24 files changed
+1236 -161

No files matched your search

+101
View File
@@ -0,0 +1,101 @@
#!/usr/bin/env python3
"""Supervise only our Nav2 process group; bounded recovery before navigation becomes active."""
import argparse
import json
import os
from pathlib import Path
import signal
import subprocess
import sys
import tempfile
import time
def stop_owned(process):
if process is None:
return
# Each child is started in a new session. No process-name matching or shared-memory cleanup.
for signum, timeout in ((signal.SIGINT, 8), (signal.SIGTERM, 2), (signal.SIGKILL, 1)):
try:
os.killpg(process.pid, signum)
except ProcessLookupError:
break
deadline = time.monotonic()+timeout
while time.monotonic() < deadline:
process.poll()
try:
os.killpg(process.pid, 0)
except ProcessLookupError:
return
time.sleep(0.05)
process.poll()
def supervise(command, checker, attempts, timeout):
navigation = None
observer = None
try:
with tempfile.TemporaryDirectory(prefix='sentry-nav2-') as directory:
for attempt in range(1, attempts+1):
result_path = Path(directory)/f'{attempt}.json'
print(f'[Nav2 supervisor] startup attempt {attempt}/{attempts}', flush=True)
navigation = subprocess.Popen(command, start_new_session=True)
observer = subprocess.Popen(
checker+['--activate', '--timeout', str(timeout), '--result', str(result_path)],
start_new_session=True)
while navigation.poll() is None and observer.poll() is None:
time.sleep(0.1)
if navigation.poll() is not None:
print('[Nav2 supervisor] Nav2 process exited before readiness; aborting', flush=True)
return 1
result = json.loads(result_path.read_text()) if result_path.is_file() else {}
if observer.returncode == 0 and result.get('passed'):
print(f'[Nav2 supervisor] ready on attempt {attempt}; startup recovery disabled', flush=True)
return navigation.wait()
stop_owned(observer)
observer = None
stop_owned(navigation)
navigation = None
if not result.get('retryable') or attempt == attempts:
print('[Nav2 supervisor] startup failed; no further retry (navigator active/unknown, '
'or attempt limit reached)', flush=True)
return 1
print('[Nav2 supervisor] navigator confirmed UNCONFIGURED/INACTIVE; '
'recreating only owned Nav2 processes. LIO/Gazebo continue running.', flush=True)
return 1
finally:
stop_owned(observer)
stop_owned(navigation)
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--attempts', type=int, default=3)
parser.add_argument('--timeout', type=float, default=120)
parser.add_argument('launch_arguments', nargs=argparse.REMAINDER)
args = parser.parse_args()
if args.attempts < 1 or args.attempts > 3 or args.timeout <= 0:
parser.error('attempts must be 1..3; timeout must be positive')
arguments = args.launch_arguments
if arguments and arguments[0] == '--':
arguments = arguments[1:]
if not arguments:
parser.error('Nav2 bringup arguments required after --')
def interrupted(signum, frame):
signal.signal(signal.SIGINT, signal.SIG_IGN)
signal.signal(signal.SIGTERM, signal.SIG_IGN)
raise KeyboardInterrupt
signal.signal(signal.SIGINT, interrupted)
signal.signal(signal.SIGTERM, interrupted)
try:
return supervise(['ros2', 'launch', 'nav2_bringup', 'bringup_launch.py', *arguments],
[sys.executable, str(Path(__file__).with_name('nav2_startup.py'))],
args.attempts, args.timeout)
except KeyboardInterrupt:
return 130
if __name__ == '__main__':
raise SystemExit(main())