From 4449dc62d19438a10391a305f4362fec92eb56ad Mon Sep 17 00:00:00 2001 From: Mori <1625251923@qq.com> Date: Wed, 20 Aug 2025 20:59:09 +0800 Subject: [PATCH] fix up by cursor --- .vscode/settings.json | 3 +++ main.py | 6 ++++-- utils/image_raw.py | 35 +++++++++++++++++++++++------------ 3 files changed, 30 insertions(+), 14 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..9ddf6b2 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "cmake.ignoreCMakeListsMissing": true +} \ No newline at end of file diff --git a/main.py b/main.py index fd5cf0b..cd2a156 100755 --- a/main.py +++ b/main.py @@ -165,7 +165,6 @@ def main(): print("\n程序被用户中断") except Exception as e: import traceback - import sys exc_type, exc_value, exc_tb = sys.exc_info() tb = traceback.extract_tb(exc_tb) if tb: @@ -300,7 +299,10 @@ class Robot_Ctrl(object): self.runing = 0 self.rec_thread.join() self.send_thread.join() - # self.image_processor.destroy() + try: + self.image_processor.destroy() + except Exception as e: + print(f"销毁图像处理器失败: {e}") # 销毁 MarkerRunner if hasattr(self, 'marker_runner') and self.marker_runner is not None: try: diff --git a/utils/image_raw.py b/utils/image_raw.py index a7b4de6..217cfcf 100755 --- a/utils/image_raw.py +++ b/utils/image_raw.py @@ -255,7 +255,7 @@ class ImageProcessor: self.image_subscriber = ImageSubscriber() self.spin_thread = None self.running = True - # self.log = get_logger("图像处理器") + self.log = get_logger("图像处理器") # 异步 QR 码扫描相关 self.scan_thread = None @@ -288,17 +288,28 @@ class ImageProcessor: # self.image_subscriber.destroy_node() def destroy(self): - if not self._ros_initialized: - return - - self._shutdown_flag = True - if self.spin_thread: - self.spin_thread.join(timeout=1.0) - - self.image_subscriber.stop_camera() - self.image_subscriber.destroy_node() - rclpy.shutdown() - self._ros_initialized = False + """优雅关闭:停止扫描与spin线程,释放ROS资源(不在此shutdown全局)。""" + try: + self.running = False + self.stop_async_scan() + except Exception: + pass + + try: + if self.spin_thread and self.spin_thread.is_alive(): + self.spin_thread.join(timeout=1.0) + except Exception: + pass + + try: + self.image_subscriber.stop_camera() + except Exception: + pass + + try: + self.image_subscriber.destroy_node() + except Exception: + pass def get_current_image(self, text = 'rgb'): # INFO 默认返回 rgb 图像