fix(sim): clean up interrupted runs and document usage and migration

Prevent ROS cleanup errors from skipping owned Gazebo process teardown.
Reject occupied master ports before launching nodes and stop the stack when
its GUI closes. Add --gui to the automatic LIO navigation checks.

Expand the root README and add a beginner guide covering manual goals,
automatic tests, restart troubleshooting, MID360 assumptions and staged
migration to real hardware. Preserve lifecycle and GUI validation evidence.

Validation: three navigation restart cycles, two busy-port rejection cases,
SIGINT/SIGHUP cleanup and a GUI straight-route check. Current source hashes,
Python syntax, documentation shell syntax and local links verified. Hardware
migration remains unverified; no push performed.
This commit is contained in:
2026-11-08 22:44:02 +08:00
parent 664eaa81e5
commit ca61aa63b5
21 changed files with 1022 additions and 59 deletions
+44 -7
View File
@@ -17,6 +17,7 @@
"""
import argparse
import atexit
import json
import math
import os
@@ -171,6 +172,27 @@ def sim_env(extra=None):
return env
def shutdown_probe(probe):
"""ROS may already be shut down by Ctrl+C; never block process cleanup."""
errors = []
if probe is not None:
if rclpy.ok():
try:
probe.send(0., 0., 0.)
except Exception as exc:
errors.append('stop command: ' + repr(exc))
try:
probe.destroy_node()
except Exception as exc:
errors.append('destroy node: ' + repr(exc))
if rclpy.ok():
try:
rclpy.shutdown()
except Exception as exc:
errors.append('ROS shutdown: ' + repr(exc))
return errors
class Sim(object):
"""一次仿真:启动 → 就绪 → 收尾。只碰自己创建的进程。"""
@@ -182,6 +204,21 @@ class Sim(object):
self.known = {}
self.teardown = {}
self.owner_token = None
self._stopping = False
self._old_hup = None
# Also clean up if a caller raises before its normal finally/stop path.
atexit.register(self.stop)
try:
self._old_hup = signal.getsignal(signal.SIGHUP)
signal.signal(signal.SIGHUP, self._on_hangup)
except ValueError: # Signal installation is limited to the main thread.
pass
def _on_hangup(self, signum, frame):
# Closing a test terminal normally sends SIGHUP. Unwind the caller's
# finally block instead of abandoning its detached launch children.
if not self._stopping:
raise KeyboardInterrupt('Test terminal closed (SIGHUP)')
def owns(self, pid):
if not self.owner_token or pid in (0, 1, os.getpid()):
@@ -273,6 +310,9 @@ class Sim(object):
return ''
def stop(self):
if self.teardown:
return self.teardown
self._stopping = True
port = 11355
sigterm, sigkill = [], []
t0 = time.monotonic()
@@ -323,6 +363,9 @@ class Sim(object):
'never_touched_unknown_pid': True}
if self._fh:
self._fh.close()
atexit.unregister(self.stop)
if self._old_hup is not None and signal.getsignal(signal.SIGHUP) == self._on_hangup:
signal.signal(signal.SIGHUP, self._old_hup)
return self.teardown
@@ -1511,13 +1554,7 @@ def main():
'samples': probe.odom[::10]}, f, ensure_ascii=False, indent=1)
res['tf'] = {'tf': dict(probe.tf_pairs), 'tf_static': dict(probe.tf_static_pairs)}
finally:
try:
probe.send(0.0, 0.0, 0.0)
probe.destroy_node()
except Exception: # noqa: BLE001
pass
if rclpy.ok():
rclpy.shutdown()
res['ros_cleanup_errors'] = shutdown_probe(probe)
res['teardown'] = sim.stop()
log('[teardown] %r' % res['teardown'])