From 80711d985ce5ff3a26150cef8a393232af3500fe Mon Sep 17 00:00:00 2001 From: Alvin-lyq Date: Fri, 8 May 2026 10:19:31 +0800 Subject: [PATCH] =?UTF-8?q?fix=20bug=20:=20=E4=B8=8A=E6=8A=A5=E5=8D=A1?= =?UTF-8?q?=E9=A1=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/base/mc/src/mc.cpp | 2 +- .../mqtt_report/include/mqtt_report/mqtt_client.hpp | 7 +++++++ .../mqtt_report/src/node/mqtt_vehicle_node.cpp | 2 +- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/base/mc/src/mc.cpp b/src/base/mc/src/mc.cpp index 45dcd47..bd66453 100644 --- a/src/base/mc/src/mc.cpp +++ b/src/base/mc/src/mc.cpp @@ -72,7 +72,7 @@ int main(int argc, char** argv) auto node = rclcpp::Node::make_shared("can_driver_node"); LOG_INFO("Starting mc package..."); - auto pub = node->create_publisher("can_data", 10); + auto pub = node->create_publisher("can_data", 100); auto sub = node->create_subscription("mc_ctrl", 10, mcCtrlCallback); auto vid_sub = node->create_subscription("/vehicle/identity", rclcpp::QoS(1).transient_local().reliable(), vehicleIdentityCallback); diff --git a/src/communication/mqtt_report/include/mqtt_report/mqtt_client.hpp b/src/communication/mqtt_report/include/mqtt_report/mqtt_client.hpp index 06cf8fd..54af120 100644 --- a/src/communication/mqtt_report/include/mqtt_report/mqtt_client.hpp +++ b/src/communication/mqtt_report/include/mqtt_report/mqtt_client.hpp @@ -70,6 +70,9 @@ class MQTTClientWrapper bool publish(const std::string& topic, const std::string& payload, int qos = 0, bool retained = false) { + // 快速检查连接状态,避免不必要的锁竞争 + if (!connected_) return false; + std::lock_guard lock(mtx_); if (!client_ || !connected_) return false; @@ -195,6 +198,10 @@ class MQTTClientWrapper { if (!connected_) { + // 尝试连接前先释放锁,给 publish 线程机会 + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + if (stop_flag_) break; + std::lock_guard lock(mtx_); doConnect(); } diff --git a/src/communication/mqtt_report/src/node/mqtt_vehicle_node.cpp b/src/communication/mqtt_report/src/node/mqtt_vehicle_node.cpp index 89ca9b2..8d1e406 100644 --- a/src/communication/mqtt_report/src/node/mqtt_vehicle_node.cpp +++ b/src/communication/mqtt_report/src/node/mqtt_vehicle_node.cpp @@ -37,7 +37,7 @@ class VehicleReportNode : public rclcpp::Node { // CAN subscription_ = this->create_subscription( - "can_data", 10, std::bind(&VehicleReportNode::topic_callback, this, std::placeholders::_1)); + "can_data", 100, std::bind(&VehicleReportNode::topic_callback, this, std::placeholders::_1)); // Identity identity_sub_ = this->create_subscription(