From 4e48a403e5b5742f241c2a78103ef5c23fbe37d8 Mon Sep 17 00:00:00 2001 From: Alvin-lyq Date: Mon, 20 Apr 2026 09:59:02 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E7=BA=BF=E7=A8=8B=E9=80=80?= =?UTF-8?q?=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/autonomy/pl/include/pl/pl.hpp | 2 ++ src/autonomy/pl/src/pl.cpp | 4 +++- src/autonomy/pl/src/pl_node.cpp | 9 +++++++-- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/autonomy/pl/include/pl/pl.hpp b/src/autonomy/pl/include/pl/pl.hpp index 8e588f2..61f151c 100644 --- a/src/autonomy/pl/include/pl/pl.hpp +++ b/src/autonomy/pl/include/pl/pl.hpp @@ -70,4 +70,6 @@ extern std::string filename; extern int d_file; extern Direction drive_mode; extern TaskStatus task_status; +extern int thread_exit_flag; // 线程退出标志 + #endif \ No newline at end of file diff --git a/src/autonomy/pl/src/pl.cpp b/src/autonomy/pl/src/pl.cpp index 5400b9c..40a7ea6 100644 --- a/src/autonomy/pl/src/pl.cpp +++ b/src/autonomy/pl/src/pl.cpp @@ -37,6 +37,7 @@ int last_mode = 0; Direction drive_mode = Direction::STRAIGHT_D; TaskStatus task_status = TaskStatus::PENDING; +int thread_exit_flag = 0; // 线程退出标志,0=继续运行,1=需要退出 // 返回单位:m double ntzx_GPS_length(double lonti1, double lati1, double lonti2, double lati2) @@ -202,13 +203,14 @@ Direction straight_or_turn(double cur_direction, double des_direction, int thres void PL_ProcThread() { usleep(70000); + thread_exit_flag = 0; // 重置退出标志 int road_pos = 0; int des_pos = 3; int direction_pos = 20; auto next_time = std::chrono::steady_clock::now(); - while (1) + while (!thread_exit_flag) // 检查线程退出标志 { next_time += std::chrono::milliseconds(50); diff --git a/src/autonomy/pl/src/pl_node.cpp b/src/autonomy/pl/src/pl_node.cpp index 2be7df4..6ee1327 100644 --- a/src/autonomy/pl/src/pl_node.cpp +++ b/src/autonomy/pl/src/pl_node.cpp @@ -110,8 +110,13 @@ class pl_node : public rclcpp::Node } else if (is_start == 1 && msg->task_status == 0) { - pthread_cancel(pl_thread_t); - LOG_INFO("pl_thread_t is canceled"); + thread_exit_flag = 1; // 设置线程退出标志 + LOG_INFO("请求线程退出..."); + + // 等待线程正常退出 + pthread_join(pl_thread_t, NULL); + LOG_INFO("线程已正常退出"); + is_start = 0; task_status = TaskStatus::COMPLETED; }