diff --git a/src/message_handler.cpp b/src/message_handler.cpp index 29f09fe..cf1448e 100644 --- a/src/message_handler.cpp +++ b/src/message_handler.cpp @@ -15,25 +15,17 @@ int main() { //启动TCP服务端 std::thread service_thread([] { - try { - boost::asio::io_service io; - TcpService::getInstance().start(Config::TCP_SERVICE_LOCALE_PORT); - io.run(); - } catch (const boost::wrapexcept &e) { - std::cerr << "MessageHandler [TcpService]" << e.what() << std::endl; - } + boost::asio::io_service io; + TcpService::getInstance().start(Config::TCP_SERVICE_LOCALE_PORT); + io.run(); }); // 启动TCP客户端 std::thread client_thread([] { - try { - boost::asio::io_service io; - TcpClient client; - client.connect(Config::REMOTE_SERVICE_IP, Config::TCP_SERVICE_REMOTE_PORT); - io.run(); - } catch (const boost::wrapexcept &e) { - std::cerr << "MessageHandler [TcpClient]" << e.what() << std::endl; - } + boost::asio::io_service io; + TcpClient client; + client.connect(Config::REMOTE_SERVICE_IP, Config::TCP_SERVICE_REMOTE_PORT); + io.run(); }); //启动甲烷数据查询串口线程 @@ -45,19 +37,18 @@ sp.read_from_port(); io.run(); } catch (std::exception &e) { - std::cerr << "MessageHandler [MethaneSerialPort]" << e.what() << std::endl; + std::cerr << "\033[33m" + "MessageHandler [MethaneSerialPort]" << e.what() + << "\033[0m" << std::endl; } }); // 拉起第三方脚本 std::thread script_thread([] { 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::ostringstream cmd_ss; cmd_ss << "./" + script_path.filename().string() << " > " diff --git a/src/message_handler.cpp b/src/message_handler.cpp index 29f09fe..cf1448e 100644 --- a/src/message_handler.cpp +++ b/src/message_handler.cpp @@ -15,25 +15,17 @@ int main() { //启动TCP服务端 std::thread service_thread([] { - try { - boost::asio::io_service io; - TcpService::getInstance().start(Config::TCP_SERVICE_LOCALE_PORT); - io.run(); - } catch (const boost::wrapexcept &e) { - std::cerr << "MessageHandler [TcpService]" << e.what() << std::endl; - } + boost::asio::io_service io; + TcpService::getInstance().start(Config::TCP_SERVICE_LOCALE_PORT); + io.run(); }); // 启动TCP客户端 std::thread client_thread([] { - try { - boost::asio::io_service io; - TcpClient client; - client.connect(Config::REMOTE_SERVICE_IP, Config::TCP_SERVICE_REMOTE_PORT); - io.run(); - } catch (const boost::wrapexcept &e) { - std::cerr << "MessageHandler [TcpClient]" << e.what() << std::endl; - } + boost::asio::io_service io; + TcpClient client; + client.connect(Config::REMOTE_SERVICE_IP, Config::TCP_SERVICE_REMOTE_PORT); + io.run(); }); //启动甲烷数据查询串口线程 @@ -45,19 +37,18 @@ sp.read_from_port(); io.run(); } catch (std::exception &e) { - std::cerr << "MessageHandler [MethaneSerialPort]" << e.what() << std::endl; + std::cerr << "\033[33m" + "MessageHandler [MethaneSerialPort]" << e.what() + << "\033[0m" << std::endl; } }); // 拉起第三方脚本 std::thread script_thread([] { 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::ostringstream cmd_ss; cmd_ss << "./" + script_path.filename().string() << " > " diff --git a/src/methane_serial_port.cpp b/src/methane_serial_port.cpp index a97193c..7bace4c 100644 --- a/src/methane_serial_port.cpp +++ b/src/methane_serial_port.cpp @@ -81,8 +81,9 @@ port_.set_option(serial_port_base::character_size(8)); port_.set_option(serial_port_base::parity(serial_port_base::parity::none)); port_.set_option(serial_port_base::stop_bits(serial_port_base::stop_bits::one)); - - std::cout << "MethaneSerialPort init success" << std::endl; + std::cout << "\033[32m" + << "MethaneSerialPort init success" + << "\033[0m" << std::endl; start_timer(); } diff --git a/src/message_handler.cpp b/src/message_handler.cpp index 29f09fe..cf1448e 100644 --- a/src/message_handler.cpp +++ b/src/message_handler.cpp @@ -15,25 +15,17 @@ int main() { //启动TCP服务端 std::thread service_thread([] { - try { - boost::asio::io_service io; - TcpService::getInstance().start(Config::TCP_SERVICE_LOCALE_PORT); - io.run(); - } catch (const boost::wrapexcept &e) { - std::cerr << "MessageHandler [TcpService]" << e.what() << std::endl; - } + boost::asio::io_service io; + TcpService::getInstance().start(Config::TCP_SERVICE_LOCALE_PORT); + io.run(); }); // 启动TCP客户端 std::thread client_thread([] { - try { - boost::asio::io_service io; - TcpClient client; - client.connect(Config::REMOTE_SERVICE_IP, Config::TCP_SERVICE_REMOTE_PORT); - io.run(); - } catch (const boost::wrapexcept &e) { - std::cerr << "MessageHandler [TcpClient]" << e.what() << std::endl; - } + boost::asio::io_service io; + TcpClient client; + client.connect(Config::REMOTE_SERVICE_IP, Config::TCP_SERVICE_REMOTE_PORT); + io.run(); }); //启动甲烷数据查询串口线程 @@ -45,19 +37,18 @@ sp.read_from_port(); io.run(); } catch (std::exception &e) { - std::cerr << "MessageHandler [MethaneSerialPort]" << e.what() << std::endl; + std::cerr << "\033[33m" + "MessageHandler [MethaneSerialPort]" << e.what() + << "\033[0m" << std::endl; } }); // 拉起第三方脚本 std::thread script_thread([] { 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::ostringstream cmd_ss; cmd_ss << "./" + script_path.filename().string() << " > " diff --git a/src/methane_serial_port.cpp b/src/methane_serial_port.cpp index a97193c..7bace4c 100644 --- a/src/methane_serial_port.cpp +++ b/src/methane_serial_port.cpp @@ -81,8 +81,9 @@ port_.set_option(serial_port_base::character_size(8)); port_.set_option(serial_port_base::parity(serial_port_base::parity::none)); port_.set_option(serial_port_base::stop_bits(serial_port_base::stop_bits::one)); - - std::cout << "MethaneSerialPort init success" << std::endl; + std::cout << "\033[32m" + << "MethaneSerialPort init success" + << "\033[0m" << std::endl; start_timer(); } diff --git a/src/robotic_arm_serial_port.cpp b/src/robotic_arm_serial_port.cpp index fce59b3..daa7a4c 100644 --- a/src/robotic_arm_serial_port.cpp +++ b/src/robotic_arm_serial_port.cpp @@ -13,6 +13,9 @@ port_.set_option(serial_port_base::character_size(8)); port_.set_option(serial_port_base::parity(serial_port_base::parity::none)); port_.set_option(serial_port_base::stop_bits(serial_port_base::stop_bits::one)); + std::cout << "\033[32m" + << "RoboticArmSerialPort init success" + << "\033[0m" << std::endl; } std::vector RoboticArmSerialPort::read_from_port(const std::vector &command) { diff --git a/src/message_handler.cpp b/src/message_handler.cpp index 29f09fe..cf1448e 100644 --- a/src/message_handler.cpp +++ b/src/message_handler.cpp @@ -15,25 +15,17 @@ int main() { //启动TCP服务端 std::thread service_thread([] { - try { - boost::asio::io_service io; - TcpService::getInstance().start(Config::TCP_SERVICE_LOCALE_PORT); - io.run(); - } catch (const boost::wrapexcept &e) { - std::cerr << "MessageHandler [TcpService]" << e.what() << std::endl; - } + boost::asio::io_service io; + TcpService::getInstance().start(Config::TCP_SERVICE_LOCALE_PORT); + io.run(); }); // 启动TCP客户端 std::thread client_thread([] { - try { - boost::asio::io_service io; - TcpClient client; - client.connect(Config::REMOTE_SERVICE_IP, Config::TCP_SERVICE_REMOTE_PORT); - io.run(); - } catch (const boost::wrapexcept &e) { - std::cerr << "MessageHandler [TcpClient]" << e.what() << std::endl; - } + boost::asio::io_service io; + TcpClient client; + client.connect(Config::REMOTE_SERVICE_IP, Config::TCP_SERVICE_REMOTE_PORT); + io.run(); }); //启动甲烷数据查询串口线程 @@ -45,19 +37,18 @@ sp.read_from_port(); io.run(); } catch (std::exception &e) { - std::cerr << "MessageHandler [MethaneSerialPort]" << e.what() << std::endl; + std::cerr << "\033[33m" + "MessageHandler [MethaneSerialPort]" << e.what() + << "\033[0m" << std::endl; } }); // 拉起第三方脚本 std::thread script_thread([] { 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::ostringstream cmd_ss; cmd_ss << "./" + script_path.filename().string() << " > " diff --git a/src/methane_serial_port.cpp b/src/methane_serial_port.cpp index a97193c..7bace4c 100644 --- a/src/methane_serial_port.cpp +++ b/src/methane_serial_port.cpp @@ -81,8 +81,9 @@ port_.set_option(serial_port_base::character_size(8)); port_.set_option(serial_port_base::parity(serial_port_base::parity::none)); port_.set_option(serial_port_base::stop_bits(serial_port_base::stop_bits::one)); - - std::cout << "MethaneSerialPort init success" << std::endl; + std::cout << "\033[32m" + << "MethaneSerialPort init success" + << "\033[0m" << std::endl; start_timer(); } diff --git a/src/robotic_arm_serial_port.cpp b/src/robotic_arm_serial_port.cpp index fce59b3..daa7a4c 100644 --- a/src/robotic_arm_serial_port.cpp +++ b/src/robotic_arm_serial_port.cpp @@ -13,6 +13,9 @@ port_.set_option(serial_port_base::character_size(8)); port_.set_option(serial_port_base::parity(serial_port_base::parity::none)); port_.set_option(serial_port_base::stop_bits(serial_port_base::stop_bits::one)); + std::cout << "\033[32m" + << "RoboticArmSerialPort init success" + << "\033[0m" << std::endl; } std::vector RoboticArmSerialPort::read_from_port(const std::vector &command) { diff --git a/src/slam_wrapper.cpp b/src/slam_wrapper.cpp index 2dade74..4aa69cc 100644 --- a/src/slam_wrapper.cpp +++ b/src/slam_wrapper.cpp @@ -122,7 +122,9 @@ }); serial_port_thread.detach(); } catch (const boost::wrapexcept &e) { - std::cerr << "SlamWrapper [RoboticArmSerialPort]" << e.what() << std::endl; + std::cerr << "\033[31m" + << "SlamWrapper [RoboticArmSerialPort]" << e.what() + << "\033[0m" << std::endl; } // pub @@ -190,7 +192,9 @@ } config_file.close(); - std::cout << "SlamWrapper init success, has " << _armCommands.size() << " arm commands " << std::endl; + std::cout << "\033[32m" + << "SlamWrapper init success, has " << _armCommands.size() << " arm commands " + << "\033[0m" << std::endl; } //casic start @@ -398,9 +402,7 @@ end_ = seq->data().find(";", begin_); str_ = seq->data().substr(begin_ + 9, end_ - begin_ - 9); if (const int feedback_ = atoi(str_.c_str()); feedback_ == 0 || feedback_ == -1) { - std::cout << "\033[1;31m" - << "Command execution failed with index = " << index_ << "." - << "\033[0m"; + std::cout << "Command execution failed with index = " << index_ << "." << std::endl; } std::cout << notice_ << std::endl; } else if (index_ == 10001) { @@ -419,22 +421,22 @@ pause_navigation(); //倒计时结束后恢复巡检 std::thread countdown_thread([this, command] { - try { - std::cout << "发送机械臂转动指令,倒计时开始,请等待10秒" << std::endl; - _serialPortPtr->write(command); - std::this_thread::sleep_for(std::chrono::seconds(5)); + std::cout << "\033[37m" + "发送机械臂转动指令,倒计时开始,请等待10秒" + << "\033[0m" << std::endl; + _serialPortPtr->write(command); + std::this_thread::sleep_for(std::chrono::seconds(5)); - //延迟5~10秒更新节点 - TcpService::getInstance().update_node(_current_node); + //延迟5~10秒更新节点 + TcpService::getInstance().update_node(_current_node); - std::this_thread::sleep_for(std::chrono::seconds(5)); + std::this_thread::sleep_for(std::chrono::seconds(5)); - recover_navigation(); - std::cout << "倒计时结束,恢复机械臂初始位置,恢复巡检"; - _serialPortPtr->write(default_command); - } catch (const std::exception &e) { - std::cerr << "线程执行过程中发生异常: " << e.what() << std::endl; - } + recover_navigation(); + std::cout << "\033[37m" + "倒计时结束,恢复机械臂初始位置,恢复巡检" + << "\033[0m" << std::endl; + _serialPortPtr->write(default_command); }); countdown_thread.join(); } diff --git a/src/message_handler.cpp b/src/message_handler.cpp index 29f09fe..cf1448e 100644 --- a/src/message_handler.cpp +++ b/src/message_handler.cpp @@ -15,25 +15,17 @@ int main() { //启动TCP服务端 std::thread service_thread([] { - try { - boost::asio::io_service io; - TcpService::getInstance().start(Config::TCP_SERVICE_LOCALE_PORT); - io.run(); - } catch (const boost::wrapexcept &e) { - std::cerr << "MessageHandler [TcpService]" << e.what() << std::endl; - } + boost::asio::io_service io; + TcpService::getInstance().start(Config::TCP_SERVICE_LOCALE_PORT); + io.run(); }); // 启动TCP客户端 std::thread client_thread([] { - try { - boost::asio::io_service io; - TcpClient client; - client.connect(Config::REMOTE_SERVICE_IP, Config::TCP_SERVICE_REMOTE_PORT); - io.run(); - } catch (const boost::wrapexcept &e) { - std::cerr << "MessageHandler [TcpClient]" << e.what() << std::endl; - } + boost::asio::io_service io; + TcpClient client; + client.connect(Config::REMOTE_SERVICE_IP, Config::TCP_SERVICE_REMOTE_PORT); + io.run(); }); //启动甲烷数据查询串口线程 @@ -45,19 +37,18 @@ sp.read_from_port(); io.run(); } catch (std::exception &e) { - std::cerr << "MessageHandler [MethaneSerialPort]" << e.what() << std::endl; + std::cerr << "\033[33m" + "MessageHandler [MethaneSerialPort]" << e.what() + << "\033[0m" << std::endl; } }); // 拉起第三方脚本 std::thread script_thread([] { 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::ostringstream cmd_ss; cmd_ss << "./" + script_path.filename().string() << " > " diff --git a/src/methane_serial_port.cpp b/src/methane_serial_port.cpp index a97193c..7bace4c 100644 --- a/src/methane_serial_port.cpp +++ b/src/methane_serial_port.cpp @@ -81,8 +81,9 @@ port_.set_option(serial_port_base::character_size(8)); port_.set_option(serial_port_base::parity(serial_port_base::parity::none)); port_.set_option(serial_port_base::stop_bits(serial_port_base::stop_bits::one)); - - std::cout << "MethaneSerialPort init success" << std::endl; + std::cout << "\033[32m" + << "MethaneSerialPort init success" + << "\033[0m" << std::endl; start_timer(); } diff --git a/src/robotic_arm_serial_port.cpp b/src/robotic_arm_serial_port.cpp index fce59b3..daa7a4c 100644 --- a/src/robotic_arm_serial_port.cpp +++ b/src/robotic_arm_serial_port.cpp @@ -13,6 +13,9 @@ port_.set_option(serial_port_base::character_size(8)); port_.set_option(serial_port_base::parity(serial_port_base::parity::none)); port_.set_option(serial_port_base::stop_bits(serial_port_base::stop_bits::one)); + std::cout << "\033[32m" + << "RoboticArmSerialPort init success" + << "\033[0m" << std::endl; } std::vector RoboticArmSerialPort::read_from_port(const std::vector &command) { diff --git a/src/slam_wrapper.cpp b/src/slam_wrapper.cpp index 2dade74..4aa69cc 100644 --- a/src/slam_wrapper.cpp +++ b/src/slam_wrapper.cpp @@ -122,7 +122,9 @@ }); serial_port_thread.detach(); } catch (const boost::wrapexcept &e) { - std::cerr << "SlamWrapper [RoboticArmSerialPort]" << e.what() << std::endl; + std::cerr << "\033[31m" + << "SlamWrapper [RoboticArmSerialPort]" << e.what() + << "\033[0m" << std::endl; } // pub @@ -190,7 +192,9 @@ } config_file.close(); - std::cout << "SlamWrapper init success, has " << _armCommands.size() << " arm commands " << std::endl; + std::cout << "\033[32m" + << "SlamWrapper init success, has " << _armCommands.size() << " arm commands " + << "\033[0m" << std::endl; } //casic start @@ -398,9 +402,7 @@ end_ = seq->data().find(";", begin_); str_ = seq->data().substr(begin_ + 9, end_ - begin_ - 9); if (const int feedback_ = atoi(str_.c_str()); feedback_ == 0 || feedback_ == -1) { - std::cout << "\033[1;31m" - << "Command execution failed with index = " << index_ << "." - << "\033[0m"; + std::cout << "Command execution failed with index = " << index_ << "." << std::endl; } std::cout << notice_ << std::endl; } else if (index_ == 10001) { @@ -419,22 +421,22 @@ pause_navigation(); //倒计时结束后恢复巡检 std::thread countdown_thread([this, command] { - try { - std::cout << "发送机械臂转动指令,倒计时开始,请等待10秒" << std::endl; - _serialPortPtr->write(command); - std::this_thread::sleep_for(std::chrono::seconds(5)); + std::cout << "\033[37m" + "发送机械臂转动指令,倒计时开始,请等待10秒" + << "\033[0m" << std::endl; + _serialPortPtr->write(command); + std::this_thread::sleep_for(std::chrono::seconds(5)); - //延迟5~10秒更新节点 - TcpService::getInstance().update_node(_current_node); + //延迟5~10秒更新节点 + TcpService::getInstance().update_node(_current_node); - std::this_thread::sleep_for(std::chrono::seconds(5)); + std::this_thread::sleep_for(std::chrono::seconds(5)); - recover_navigation(); - std::cout << "倒计时结束,恢复机械臂初始位置,恢复巡检"; - _serialPortPtr->write(default_command); - } catch (const std::exception &e) { - std::cerr << "线程执行过程中发生异常: " << e.what() << std::endl; - } + recover_navigation(); + std::cout << "\033[37m" + "倒计时结束,恢复机械臂初始位置,恢复巡检" + << "\033[0m" << std::endl; + _serialPortPtr->write(default_command); }); countdown_thread.join(); } diff --git a/src/tcp_client.cpp b/src/tcp_client.cpp index c712f81..b4f0676 100644 --- a/src/tcp_client.cpp +++ b/src/tcp_client.cpp @@ -169,8 +169,11 @@ continue; } - std::cout << "TcpClient::connected to " << server_ip << ":" << server_port - << ", waiting for command ..." << std::endl; + std::cout << "\033[32m" + << "TcpClient connected to " + << server_ip << ":" << server_port + << ", waiting for command ..." + << "\033[0m" << std::endl; //连接成功后把自己的编号发送给Tcp Service const std::string dog_code = std::string("devCode:") + Config::DOG_CODE; @@ -183,11 +186,15 @@ receive_thread.join(); } catch (const std::exception &e) { if (attempt == _max_retries - 1) { - std::cout << "TcpClient::connect failed: " << e.what() << std::endl; + std::cout << "\033[31m" + << "TcpClient connect " << e.what() + << "\033[0m" << std::endl; throw; } - std::cout << "TcpClient::connect failed! " - << "Attempting to connect (" << attempt + 1 << "/" << _max_retries << ")..." << std::endl; + std::cout << "\033[33m" + << "TcpClient connect failed! " + << "Attempting to connect (" << attempt + 1 << "/" << _max_retries << ")..." + << "\033[0m" << std::endl; std::this_thread::sleep_for(std::chrono::milliseconds(_retry_interval)); } } diff --git a/src/message_handler.cpp b/src/message_handler.cpp index 29f09fe..cf1448e 100644 --- a/src/message_handler.cpp +++ b/src/message_handler.cpp @@ -15,25 +15,17 @@ int main() { //启动TCP服务端 std::thread service_thread([] { - try { - boost::asio::io_service io; - TcpService::getInstance().start(Config::TCP_SERVICE_LOCALE_PORT); - io.run(); - } catch (const boost::wrapexcept &e) { - std::cerr << "MessageHandler [TcpService]" << e.what() << std::endl; - } + boost::asio::io_service io; + TcpService::getInstance().start(Config::TCP_SERVICE_LOCALE_PORT); + io.run(); }); // 启动TCP客户端 std::thread client_thread([] { - try { - boost::asio::io_service io; - TcpClient client; - client.connect(Config::REMOTE_SERVICE_IP, Config::TCP_SERVICE_REMOTE_PORT); - io.run(); - } catch (const boost::wrapexcept &e) { - std::cerr << "MessageHandler [TcpClient]" << e.what() << std::endl; - } + boost::asio::io_service io; + TcpClient client; + client.connect(Config::REMOTE_SERVICE_IP, Config::TCP_SERVICE_REMOTE_PORT); + io.run(); }); //启动甲烷数据查询串口线程 @@ -45,19 +37,18 @@ sp.read_from_port(); io.run(); } catch (std::exception &e) { - std::cerr << "MessageHandler [MethaneSerialPort]" << e.what() << std::endl; + std::cerr << "\033[33m" + "MessageHandler [MethaneSerialPort]" << e.what() + << "\033[0m" << std::endl; } }); // 拉起第三方脚本 std::thread script_thread([] { 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::ostringstream cmd_ss; cmd_ss << "./" + script_path.filename().string() << " > " diff --git a/src/methane_serial_port.cpp b/src/methane_serial_port.cpp index a97193c..7bace4c 100644 --- a/src/methane_serial_port.cpp +++ b/src/methane_serial_port.cpp @@ -81,8 +81,9 @@ port_.set_option(serial_port_base::character_size(8)); port_.set_option(serial_port_base::parity(serial_port_base::parity::none)); port_.set_option(serial_port_base::stop_bits(serial_port_base::stop_bits::one)); - - std::cout << "MethaneSerialPort init success" << std::endl; + std::cout << "\033[32m" + << "MethaneSerialPort init success" + << "\033[0m" << std::endl; start_timer(); } diff --git a/src/robotic_arm_serial_port.cpp b/src/robotic_arm_serial_port.cpp index fce59b3..daa7a4c 100644 --- a/src/robotic_arm_serial_port.cpp +++ b/src/robotic_arm_serial_port.cpp @@ -13,6 +13,9 @@ port_.set_option(serial_port_base::character_size(8)); port_.set_option(serial_port_base::parity(serial_port_base::parity::none)); port_.set_option(serial_port_base::stop_bits(serial_port_base::stop_bits::one)); + std::cout << "\033[32m" + << "RoboticArmSerialPort init success" + << "\033[0m" << std::endl; } std::vector RoboticArmSerialPort::read_from_port(const std::vector &command) { diff --git a/src/slam_wrapper.cpp b/src/slam_wrapper.cpp index 2dade74..4aa69cc 100644 --- a/src/slam_wrapper.cpp +++ b/src/slam_wrapper.cpp @@ -122,7 +122,9 @@ }); serial_port_thread.detach(); } catch (const boost::wrapexcept &e) { - std::cerr << "SlamWrapper [RoboticArmSerialPort]" << e.what() << std::endl; + std::cerr << "\033[31m" + << "SlamWrapper [RoboticArmSerialPort]" << e.what() + << "\033[0m" << std::endl; } // pub @@ -190,7 +192,9 @@ } config_file.close(); - std::cout << "SlamWrapper init success, has " << _armCommands.size() << " arm commands " << std::endl; + std::cout << "\033[32m" + << "SlamWrapper init success, has " << _armCommands.size() << " arm commands " + << "\033[0m" << std::endl; } //casic start @@ -398,9 +402,7 @@ end_ = seq->data().find(";", begin_); str_ = seq->data().substr(begin_ + 9, end_ - begin_ - 9); if (const int feedback_ = atoi(str_.c_str()); feedback_ == 0 || feedback_ == -1) { - std::cout << "\033[1;31m" - << "Command execution failed with index = " << index_ << "." - << "\033[0m"; + std::cout << "Command execution failed with index = " << index_ << "." << std::endl; } std::cout << notice_ << std::endl; } else if (index_ == 10001) { @@ -419,22 +421,22 @@ pause_navigation(); //倒计时结束后恢复巡检 std::thread countdown_thread([this, command] { - try { - std::cout << "发送机械臂转动指令,倒计时开始,请等待10秒" << std::endl; - _serialPortPtr->write(command); - std::this_thread::sleep_for(std::chrono::seconds(5)); + std::cout << "\033[37m" + "发送机械臂转动指令,倒计时开始,请等待10秒" + << "\033[0m" << std::endl; + _serialPortPtr->write(command); + std::this_thread::sleep_for(std::chrono::seconds(5)); - //延迟5~10秒更新节点 - TcpService::getInstance().update_node(_current_node); + //延迟5~10秒更新节点 + TcpService::getInstance().update_node(_current_node); - std::this_thread::sleep_for(std::chrono::seconds(5)); + std::this_thread::sleep_for(std::chrono::seconds(5)); - recover_navigation(); - std::cout << "倒计时结束,恢复机械臂初始位置,恢复巡检"; - _serialPortPtr->write(default_command); - } catch (const std::exception &e) { - std::cerr << "线程执行过程中发生异常: " << e.what() << std::endl; - } + recover_navigation(); + std::cout << "\033[37m" + "倒计时结束,恢复机械臂初始位置,恢复巡检" + << "\033[0m" << std::endl; + _serialPortPtr->write(default_command); }); countdown_thread.join(); } diff --git a/src/tcp_client.cpp b/src/tcp_client.cpp index c712f81..b4f0676 100644 --- a/src/tcp_client.cpp +++ b/src/tcp_client.cpp @@ -169,8 +169,11 @@ continue; } - std::cout << "TcpClient::connected to " << server_ip << ":" << server_port - << ", waiting for command ..." << std::endl; + std::cout << "\033[32m" + << "TcpClient connected to " + << server_ip << ":" << server_port + << ", waiting for command ..." + << "\033[0m" << std::endl; //连接成功后把自己的编号发送给Tcp Service const std::string dog_code = std::string("devCode:") + Config::DOG_CODE; @@ -183,11 +186,15 @@ receive_thread.join(); } catch (const std::exception &e) { if (attempt == _max_retries - 1) { - std::cout << "TcpClient::connect failed: " << e.what() << std::endl; + std::cout << "\033[31m" + << "TcpClient connect " << e.what() + << "\033[0m" << std::endl; throw; } - std::cout << "TcpClient::connect failed! " - << "Attempting to connect (" << attempt + 1 << "/" << _max_retries << ")..." << std::endl; + std::cout << "\033[33m" + << "TcpClient connect failed! " + << "Attempting to connect (" << attempt + 1 << "/" << _max_retries << ")..." + << "\033[0m" << std::endl; std::this_thread::sleep_for(std::chrono::milliseconds(_retry_interval)); } } diff --git a/src/tcp_service.cpp b/src/tcp_service.cpp index 6b30262..f9c7d97 100644 --- a/src/tcp_service.cpp +++ b/src/tcp_service.cpp @@ -80,7 +80,9 @@ throw std::runtime_error("failed to listen on socket"); } - std::cout << "TcpService started" << " and listening on port " << port << std::endl; + std::cout << "\033[32m" + << "TcpService start success and listening on " << port + << "\033[0m" << std::endl; // 接受连接 while (true) { @@ -92,11 +94,10 @@ continue; } - std::cout << "Accepted connection from " - << inet_ntoa(client_addr.sin_addr) - << ":" - << ntohs(client_addr.sin_port) - << std::endl; + std::cout << "\033[32m" + << "Accepted connection from " + << inet_ntoa(client_addr.sin_addr) << ":" << ntohs(client_addr.sin_port) + << "\033[0m" << std::endl; // 处理连接 _client_sockets.push_back(client_socket_fd); @@ -107,17 +108,15 @@ client_thread.join(); } } catch (const std::exception &e) { - std::cerr << "[TcpService] start " << e.what() << std::endl; + std::cerr << "\033[31m" + << "TcpService start " << e.what() + << "\033[0m" << std::endl; } } void TcpService::update_node(const int node) { std::stringstream ss; - ss << node << "," - << gas_value << "," - << "116.296975" << "," - << "39.990889" - << std::endl; + ss << node << "," << gas_value << "," << "116.296975" << "," << "39.990889"; const std::string message = ss.str(); std::cout << "TcpService update node: " << message << std::endl; for (const auto &client_socket: _client_sockets) { @@ -127,11 +126,7 @@ void TcpService::send_gas_warning(const int value) { std::stringstream ss; - ss << "2" << "," - << value << "," - << "116.296975" << "," - << "39.990889" - << std::endl; + ss << "2" << "," << value << "," << "116.296975" << "," << "39.990889"; const std::string message = ss.str(); std::cout << "TcpService send gas warning: " << message << std::endl; for (const auto &client_socket: _client_sockets) {