diff --git a/src/config.hpp b/src/config.hpp index 16ac5d2..caa55a2 100644 --- a/src/config.hpp +++ b/src/config.hpp @@ -70,7 +70,7 @@ /** * SLAM建图脚本路径 */ - constexpr const char *SLAM_SCRIPT_FILE = "/unitree/module/graph_pid_ws/0_unitree_slam.sh"; + constexpr const char *SLAM_SCRIPT_PATH = "/unitree/module/graph_pid_ws"; /** * SLAM建图log路径 diff --git a/src/config.hpp b/src/config.hpp index 16ac5d2..caa55a2 100644 --- a/src/config.hpp +++ b/src/config.hpp @@ -70,7 +70,7 @@ /** * SLAM建图脚本路径 */ - constexpr const char *SLAM_SCRIPT_FILE = "/unitree/module/graph_pid_ws/0_unitree_slam.sh"; + constexpr const char *SLAM_SCRIPT_PATH = "/unitree/module/graph_pid_ws"; /** * SLAM建图log路径 diff --git a/src/message_handler.cpp b/src/message_handler.cpp index 122a88c..29f09fe 100644 --- a/src/message_handler.cpp +++ b/src/message_handler.cpp @@ -1,4 +1,5 @@ -#include +#include +#include #include #include #include @@ -9,6 +10,8 @@ #include "methane_serial_port.hpp" #include "config.hpp" +namespace fs = std::filesystem; + int main() { //启动TCP服务端 std::thread service_thread([] { @@ -48,30 +51,23 @@ // 拉起第三方脚本 std::thread script_thread([] { - const auto cmd = ( - boost::format("setsid sh -c '%1%' > %2% 2>&1 &") - % Config::SLAM_SCRIPT_FILE - % Config::SLAM_LOG_FILE - ).str(); - std::system(cmd.c_str()); + fs::current_path(Config::SLAM_SCRIPT_PATH); + std::cout << "已切换到目录: " << fs::current_path() << std::endl; + for (const auto &entry: fs::directory_iterator(fs::current_path())) { + if (entry.is_regular_file() && entry.path().extension() == ".sh") { + const auto script_path = entry.path(); + std::cout << "找到 .sh 文件: " << script_path << std::endl; - // 等几秒钟,扫一遍日志确认启动成功 - std::this_thread::sleep_for(std::chrono::seconds(2)); - std::ifstream log(Config::SLAM_LOG_FILE); - std::string line; - bool ok = false; - while (std::getline(log, line)) { - // 确认启动是否成功 - if (line.find("process started") != std::string::npos) { - ok = true; + std::ostringstream cmd_ss; + cmd_ss << "./" + script_path.filename().string() + << " > " + << Config::SLAM_LOG_FILE + << " 2>&1"; + const std::string command = cmd_ss.str(); + std::system(command.c_str()); break; } } - if (ok) { - std::cout << "Unitree Slam service init success" << std::endl; - } else { - std::cerr << "[WARN] Unitree Slam service may not be ready yet" << std::endl; - } }); // 等待所有线程完成