diff --git a/src/constant_config.hpp b/src/constant_config.hpp new file mode 100644 index 0000000..ff5171e --- /dev/null +++ b/src/constant_config.hpp @@ -0,0 +1,27 @@ +// +// Created by pengx on 2025/4/10. +// + +#ifndef CONSTANT_CONFIG_HPP +#define CONSTANT_CONFIG_HPP + +namespace ConstantConfig { + constexpr int TCP_REMOTE_PORT = 30029; + + constexpr const char *SERVICE_ADDRESS = "192.168.123.125"; + constexpr int TCP_LOCALE_PORT = 8888; + + + constexpr const char *GAS_PORT = "/dev/ttyUSB0"; + constexpr int BAUD_RATE = 115200; + + constexpr const char *API_RECEIVE_DATA = "/device/receiveData"; + constexpr const char *HTTP_PORT = "30028"; + constexpr const char *DOG_CODE = "B42D4000OCAD3N82"; + + constexpr const char *SLAM_NET_PORT = "eth0"; + + constexpr const char *ROBOTIC_ARM_PORT = "/dev/ttyACM0"; +} + +#endif //CONSTANT_CONFIG_HPP diff --git a/src/constant_config.hpp b/src/constant_config.hpp new file mode 100644 index 0000000..ff5171e --- /dev/null +++ b/src/constant_config.hpp @@ -0,0 +1,27 @@ +// +// Created by pengx on 2025/4/10. +// + +#ifndef CONSTANT_CONFIG_HPP +#define CONSTANT_CONFIG_HPP + +namespace ConstantConfig { + constexpr int TCP_REMOTE_PORT = 30029; + + constexpr const char *SERVICE_ADDRESS = "192.168.123.125"; + constexpr int TCP_LOCALE_PORT = 8888; + + + constexpr const char *GAS_PORT = "/dev/ttyUSB0"; + constexpr int BAUD_RATE = 115200; + + constexpr const char *API_RECEIVE_DATA = "/device/receiveData"; + constexpr const char *HTTP_PORT = "30028"; + constexpr const char *DOG_CODE = "B42D4000OCAD3N82"; + + constexpr const char *SLAM_NET_PORT = "eth0"; + + constexpr const char *ROBOTIC_ARM_PORT = "/dev/ttyACM0"; +} + +#endif //CONSTANT_CONFIG_HPP diff --git a/src/message_handler.cpp b/src/message_handler.cpp index 3c38cdc..ec3d7d8 100644 --- a/src/message_handler.cpp +++ b/src/message_handler.cpp @@ -5,12 +5,13 @@ #include "tcp_service.hpp" #include "tcp_client.hpp" #include "methane_serial_port.hpp" +#include "constant_config.hpp" int main() { //启动TCP服务端 std::thread service_thread([] { boost::asio::io_service io; - TcpService::getInstance().start(8888); + TcpService::getInstance().start(ConstantConfig::TCP_LOCALE_PORT); io.run(); }); @@ -18,8 +19,7 @@ std::thread client_thread([] { boost::asio::io_service io; TcpClient client; - // client.connect("111.198.10.15", 11647); - client.connect("192.168.123.18", 9001); + client.connect(ConstantConfig::SERVICE_ADDRESS, ConstantConfig::TCP_REMOTE_PORT); io.run(); }); @@ -28,7 +28,7 @@ try { boost::asio::io_service io; //打开串口 - MethaneSerialPort sp(io, "/dev/ttyUSB0", 115200); //甲烷串口 + MethaneSerialPort sp(io, ConstantConfig::GAS_PORT, ConstantConfig::BAUD_RATE); //甲烷串口 sp.read_from_port(); io.run(); } catch (std::exception &e) { diff --git a/src/constant_config.hpp b/src/constant_config.hpp new file mode 100644 index 0000000..ff5171e --- /dev/null +++ b/src/constant_config.hpp @@ -0,0 +1,27 @@ +// +// Created by pengx on 2025/4/10. +// + +#ifndef CONSTANT_CONFIG_HPP +#define CONSTANT_CONFIG_HPP + +namespace ConstantConfig { + constexpr int TCP_REMOTE_PORT = 30029; + + constexpr const char *SERVICE_ADDRESS = "192.168.123.125"; + constexpr int TCP_LOCALE_PORT = 8888; + + + constexpr const char *GAS_PORT = "/dev/ttyUSB0"; + constexpr int BAUD_RATE = 115200; + + constexpr const char *API_RECEIVE_DATA = "/device/receiveData"; + constexpr const char *HTTP_PORT = "30028"; + constexpr const char *DOG_CODE = "B42D4000OCAD3N82"; + + constexpr const char *SLAM_NET_PORT = "eth0"; + + constexpr const char *ROBOTIC_ARM_PORT = "/dev/ttyACM0"; +} + +#endif //CONSTANT_CONFIG_HPP diff --git a/src/message_handler.cpp b/src/message_handler.cpp index 3c38cdc..ec3d7d8 100644 --- a/src/message_handler.cpp +++ b/src/message_handler.cpp @@ -5,12 +5,13 @@ #include "tcp_service.hpp" #include "tcp_client.hpp" #include "methane_serial_port.hpp" +#include "constant_config.hpp" int main() { //启动TCP服务端 std::thread service_thread([] { boost::asio::io_service io; - TcpService::getInstance().start(8888); + TcpService::getInstance().start(ConstantConfig::TCP_LOCALE_PORT); io.run(); }); @@ -18,8 +19,7 @@ std::thread client_thread([] { boost::asio::io_service io; TcpClient client; - // client.connect("111.198.10.15", 11647); - client.connect("192.168.123.18", 9001); + client.connect(ConstantConfig::SERVICE_ADDRESS, ConstantConfig::TCP_REMOTE_PORT); io.run(); }); @@ -28,7 +28,7 @@ try { boost::asio::io_service io; //打开串口 - MethaneSerialPort sp(io, "/dev/ttyUSB0", 115200); //甲烷串口 + MethaneSerialPort sp(io, ConstantConfig::GAS_PORT, ConstantConfig::BAUD_RATE); //甲烷串口 sp.read_from_port(); io.run(); } catch (std::exception &e) { diff --git a/src/methane_serial_port.cpp b/src/methane_serial_port.cpp index 30d986e..f7e2b80 100644 --- a/src/methane_serial_port.cpp +++ b/src/methane_serial_port.cpp @@ -3,6 +3,9 @@ // #include "methane_serial_port.hpp" +#include "tcp_service.hpp" +#include "constant_config.hpp" + #include #include #include @@ -10,7 +13,6 @@ #include #include #include -#include "tcp_service.hpp" namespace beast = boost::beast; namespace http = beast::http; @@ -18,25 +20,26 @@ using tcp = net::ip::tcp; void upload_to_server(const int value) { - //TODO 根据实际情况修改为服务器TCP客户端HTTP和端口 - const std::string server_address = "192.168.123.125"; - const std::string server_port = "30028"; - const std::string dog_code = "B42D4000OCAD3N82"; net::io_context ioc; tcp::resolver resolver{ioc}; - auto const results = resolver.resolve(server_address, server_port); + auto const results = resolver.resolve(ConstantConfig::SERVICE_ADDRESS, ConstantConfig::HTTP_PORT); tcp::socket socket{ioc}; try { net::connect(socket, results.begin(), results.end()); // 构建HTTP请求 - http::request req{http::verb::post, "/device/receiveData", 11}; - req.set(http::field::host, server_address); + http::request req{http::verb::post, ConstantConfig::API_RECEIVE_DATA, 11}; + req.set(http::field::host, ConstantConfig::SERVICE_ADDRESS); req.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING); req.set(http::field::content_type, "application/json"); req.set(http::field::connection, "keep-alive"); //{"type":6,"devcode":"1212","data":{"gas":"20"}} - const std::string body = R"({"type":6,"devcode":")" + dog_code + R"(","data":{"gas":")" + std::to_string(value) + - "\"}}"; + std::ostringstream oss; + oss << R"({"type":6,"devcode":")" + << ConstantConfig::DOG_CODE + << R"(","data":{"gas":")" + << value + << R"("}})"; + std::string body = oss.str(); req.body() = body; req.prepare_payload(); diff --git a/src/constant_config.hpp b/src/constant_config.hpp new file mode 100644 index 0000000..ff5171e --- /dev/null +++ b/src/constant_config.hpp @@ -0,0 +1,27 @@ +// +// Created by pengx on 2025/4/10. +// + +#ifndef CONSTANT_CONFIG_HPP +#define CONSTANT_CONFIG_HPP + +namespace ConstantConfig { + constexpr int TCP_REMOTE_PORT = 30029; + + constexpr const char *SERVICE_ADDRESS = "192.168.123.125"; + constexpr int TCP_LOCALE_PORT = 8888; + + + constexpr const char *GAS_PORT = "/dev/ttyUSB0"; + constexpr int BAUD_RATE = 115200; + + constexpr const char *API_RECEIVE_DATA = "/device/receiveData"; + constexpr const char *HTTP_PORT = "30028"; + constexpr const char *DOG_CODE = "B42D4000OCAD3N82"; + + constexpr const char *SLAM_NET_PORT = "eth0"; + + constexpr const char *ROBOTIC_ARM_PORT = "/dev/ttyACM0"; +} + +#endif //CONSTANT_CONFIG_HPP diff --git a/src/message_handler.cpp b/src/message_handler.cpp index 3c38cdc..ec3d7d8 100644 --- a/src/message_handler.cpp +++ b/src/message_handler.cpp @@ -5,12 +5,13 @@ #include "tcp_service.hpp" #include "tcp_client.hpp" #include "methane_serial_port.hpp" +#include "constant_config.hpp" int main() { //启动TCP服务端 std::thread service_thread([] { boost::asio::io_service io; - TcpService::getInstance().start(8888); + TcpService::getInstance().start(ConstantConfig::TCP_LOCALE_PORT); io.run(); }); @@ -18,8 +19,7 @@ std::thread client_thread([] { boost::asio::io_service io; TcpClient client; - // client.connect("111.198.10.15", 11647); - client.connect("192.168.123.18", 9001); + client.connect(ConstantConfig::SERVICE_ADDRESS, ConstantConfig::TCP_REMOTE_PORT); io.run(); }); @@ -28,7 +28,7 @@ try { boost::asio::io_service io; //打开串口 - MethaneSerialPort sp(io, "/dev/ttyUSB0", 115200); //甲烷串口 + MethaneSerialPort sp(io, ConstantConfig::GAS_PORT, ConstantConfig::BAUD_RATE); //甲烷串口 sp.read_from_port(); io.run(); } catch (std::exception &e) { diff --git a/src/methane_serial_port.cpp b/src/methane_serial_port.cpp index 30d986e..f7e2b80 100644 --- a/src/methane_serial_port.cpp +++ b/src/methane_serial_port.cpp @@ -3,6 +3,9 @@ // #include "methane_serial_port.hpp" +#include "tcp_service.hpp" +#include "constant_config.hpp" + #include #include #include @@ -10,7 +13,6 @@ #include #include #include -#include "tcp_service.hpp" namespace beast = boost::beast; namespace http = beast::http; @@ -18,25 +20,26 @@ using tcp = net::ip::tcp; void upload_to_server(const int value) { - //TODO 根据实际情况修改为服务器TCP客户端HTTP和端口 - const std::string server_address = "192.168.123.125"; - const std::string server_port = "30028"; - const std::string dog_code = "B42D4000OCAD3N82"; net::io_context ioc; tcp::resolver resolver{ioc}; - auto const results = resolver.resolve(server_address, server_port); + auto const results = resolver.resolve(ConstantConfig::SERVICE_ADDRESS, ConstantConfig::HTTP_PORT); tcp::socket socket{ioc}; try { net::connect(socket, results.begin(), results.end()); // 构建HTTP请求 - http::request req{http::verb::post, "/device/receiveData", 11}; - req.set(http::field::host, server_address); + http::request req{http::verb::post, ConstantConfig::API_RECEIVE_DATA, 11}; + req.set(http::field::host, ConstantConfig::SERVICE_ADDRESS); req.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING); req.set(http::field::content_type, "application/json"); req.set(http::field::connection, "keep-alive"); //{"type":6,"devcode":"1212","data":{"gas":"20"}} - const std::string body = R"({"type":6,"devcode":")" + dog_code + R"(","data":{"gas":")" + std::to_string(value) + - "\"}}"; + std::ostringstream oss; + oss << R"({"type":6,"devcode":")" + << ConstantConfig::DOG_CODE + << R"(","data":{"gas":")" + << value + << R"("}})"; + std::string body = oss.str(); req.body() = body; req.prepare_payload(); diff --git a/src/slam_wrapper.cpp b/src/slam_wrapper.cpp index a751e0c..cb0bd6b 100644 --- a/src/slam_wrapper.cpp +++ b/src/slam_wrapper.cpp @@ -4,16 +4,18 @@ #include "slam_wrapper.hpp" #include "tcp_service.hpp" +#include "constant_config.hpp" + #include #include SlamWrapper::SlamWrapper() { - ChannelFactory::Instance()->Init(0, "eth0"); + ChannelFactory::Instance()->Init(0, ConstantConfig::SLAM_NET_PORT); std::thread serial_port_thread([this] { //初始化机械臂串口 boost::asio::io_service io; - serial_port_ = new RoboticArmSerialPort(io, "/dev/ttyACM0", 115200); + serial_port_ = new RoboticArmSerialPort(io, ConstantConfig::ROBOTIC_ARM_PORT, ConstantConfig::BAUD_RATE); io.run(); }); serial_port_thread.detach(); @@ -114,34 +116,34 @@ str_ = seq->data().substr(begin_ + 7, end_ - begin_ - 7); const int arrive_ = atoi(str_.c_str()); std::cout << "I arrived node " << arrive_ << ". " << notice_ << std::endl; - TcpService::getInstance().update_node(arrive_); - if (arrive_ == 2) { - //发送暂停指令 - pause_navigation(); + //发送暂停指令 + pause_navigation(); + //倒计时结束后恢复巡检 + std::thread countdown_thread([this,&arrive_] { + try { + //朝右下方 + std::cout << "发送机械臂转动指令,倒计时开始,请等待10秒" << std::endl; + serial_port_->write({ + 0xFE, 0xFE, 0x0F, 0x3C, + 0x0B, 0x71, 0x08, 0x25, 0x06, 0xFB, + 0x08, 0x2E, 0x0A, 0xCB, 0x08, 0x28, + 0x14, 0xFA + }); + std::this_thread::sleep_for(std::chrono::seconds(5)); - //TODO 控制机械臂转动到预设的角度 - //朝右下方 - std::cout << "发送机械臂转动指令,倒计时开始,请等待10秒" << std::endl; - serial_port_->write({ - 0xFE, 0xFE, 0x0F, 0x3C, - 0x0B, 0x71, 0x08, 0x25, 0x06, 0xFB, - 0x08, 0x2E, 0x0A, 0xCB, 0x08, 0x28, - 0x14, 0xFA - }); + //延迟5~10秒更新节点 + TcpService::getInstance().update_node(arrive_); - //倒计时结束后恢复巡检 - std::thread countdown_thread([this] { - try { - std::this_thread::sleep_for(std::chrono::seconds(10)); - recover_navigation(); - std::cout << "倒计时结束,恢复机械臂初始位置,恢复巡检"; - serial_port_->write(default_command); - } catch (const std::exception &e) { - std::cerr << "线程执行过程中发生异常: " << e.what() << std::endl; - } - }); - countdown_thread.join(); - } + std::this_thread::sleep_for(std::chrono::seconds(5)); + + recover_navigation(); + std::cout << "倒计时结束,恢复机械臂初始位置,恢复巡检"; + serial_port_->write(default_command); + } catch (const std::exception &e) { + std::cerr << "线程执行过程中发生异常: " << e.what() << std::endl; + } + }); + countdown_thread.join(); } } diff --git a/src/constant_config.hpp b/src/constant_config.hpp new file mode 100644 index 0000000..ff5171e --- /dev/null +++ b/src/constant_config.hpp @@ -0,0 +1,27 @@ +// +// Created by pengx on 2025/4/10. +// + +#ifndef CONSTANT_CONFIG_HPP +#define CONSTANT_CONFIG_HPP + +namespace ConstantConfig { + constexpr int TCP_REMOTE_PORT = 30029; + + constexpr const char *SERVICE_ADDRESS = "192.168.123.125"; + constexpr int TCP_LOCALE_PORT = 8888; + + + constexpr const char *GAS_PORT = "/dev/ttyUSB0"; + constexpr int BAUD_RATE = 115200; + + constexpr const char *API_RECEIVE_DATA = "/device/receiveData"; + constexpr const char *HTTP_PORT = "30028"; + constexpr const char *DOG_CODE = "B42D4000OCAD3N82"; + + constexpr const char *SLAM_NET_PORT = "eth0"; + + constexpr const char *ROBOTIC_ARM_PORT = "/dev/ttyACM0"; +} + +#endif //CONSTANT_CONFIG_HPP diff --git a/src/message_handler.cpp b/src/message_handler.cpp index 3c38cdc..ec3d7d8 100644 --- a/src/message_handler.cpp +++ b/src/message_handler.cpp @@ -5,12 +5,13 @@ #include "tcp_service.hpp" #include "tcp_client.hpp" #include "methane_serial_port.hpp" +#include "constant_config.hpp" int main() { //启动TCP服务端 std::thread service_thread([] { boost::asio::io_service io; - TcpService::getInstance().start(8888); + TcpService::getInstance().start(ConstantConfig::TCP_LOCALE_PORT); io.run(); }); @@ -18,8 +19,7 @@ std::thread client_thread([] { boost::asio::io_service io; TcpClient client; - // client.connect("111.198.10.15", 11647); - client.connect("192.168.123.18", 9001); + client.connect(ConstantConfig::SERVICE_ADDRESS, ConstantConfig::TCP_REMOTE_PORT); io.run(); }); @@ -28,7 +28,7 @@ try { boost::asio::io_service io; //打开串口 - MethaneSerialPort sp(io, "/dev/ttyUSB0", 115200); //甲烷串口 + MethaneSerialPort sp(io, ConstantConfig::GAS_PORT, ConstantConfig::BAUD_RATE); //甲烷串口 sp.read_from_port(); io.run(); } catch (std::exception &e) { diff --git a/src/methane_serial_port.cpp b/src/methane_serial_port.cpp index 30d986e..f7e2b80 100644 --- a/src/methane_serial_port.cpp +++ b/src/methane_serial_port.cpp @@ -3,6 +3,9 @@ // #include "methane_serial_port.hpp" +#include "tcp_service.hpp" +#include "constant_config.hpp" + #include #include #include @@ -10,7 +13,6 @@ #include #include #include -#include "tcp_service.hpp" namespace beast = boost::beast; namespace http = beast::http; @@ -18,25 +20,26 @@ using tcp = net::ip::tcp; void upload_to_server(const int value) { - //TODO 根据实际情况修改为服务器TCP客户端HTTP和端口 - const std::string server_address = "192.168.123.125"; - const std::string server_port = "30028"; - const std::string dog_code = "B42D4000OCAD3N82"; net::io_context ioc; tcp::resolver resolver{ioc}; - auto const results = resolver.resolve(server_address, server_port); + auto const results = resolver.resolve(ConstantConfig::SERVICE_ADDRESS, ConstantConfig::HTTP_PORT); tcp::socket socket{ioc}; try { net::connect(socket, results.begin(), results.end()); // 构建HTTP请求 - http::request req{http::verb::post, "/device/receiveData", 11}; - req.set(http::field::host, server_address); + http::request req{http::verb::post, ConstantConfig::API_RECEIVE_DATA, 11}; + req.set(http::field::host, ConstantConfig::SERVICE_ADDRESS); req.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING); req.set(http::field::content_type, "application/json"); req.set(http::field::connection, "keep-alive"); //{"type":6,"devcode":"1212","data":{"gas":"20"}} - const std::string body = R"({"type":6,"devcode":")" + dog_code + R"(","data":{"gas":")" + std::to_string(value) + - "\"}}"; + std::ostringstream oss; + oss << R"({"type":6,"devcode":")" + << ConstantConfig::DOG_CODE + << R"(","data":{"gas":")" + << value + << R"("}})"; + std::string body = oss.str(); req.body() = body; req.prepare_payload(); diff --git a/src/slam_wrapper.cpp b/src/slam_wrapper.cpp index a751e0c..cb0bd6b 100644 --- a/src/slam_wrapper.cpp +++ b/src/slam_wrapper.cpp @@ -4,16 +4,18 @@ #include "slam_wrapper.hpp" #include "tcp_service.hpp" +#include "constant_config.hpp" + #include #include SlamWrapper::SlamWrapper() { - ChannelFactory::Instance()->Init(0, "eth0"); + ChannelFactory::Instance()->Init(0, ConstantConfig::SLAM_NET_PORT); std::thread serial_port_thread([this] { //初始化机械臂串口 boost::asio::io_service io; - serial_port_ = new RoboticArmSerialPort(io, "/dev/ttyACM0", 115200); + serial_port_ = new RoboticArmSerialPort(io, ConstantConfig::ROBOTIC_ARM_PORT, ConstantConfig::BAUD_RATE); io.run(); }); serial_port_thread.detach(); @@ -114,34 +116,34 @@ str_ = seq->data().substr(begin_ + 7, end_ - begin_ - 7); const int arrive_ = atoi(str_.c_str()); std::cout << "I arrived node " << arrive_ << ". " << notice_ << std::endl; - TcpService::getInstance().update_node(arrive_); - if (arrive_ == 2) { - //发送暂停指令 - pause_navigation(); + //发送暂停指令 + pause_navigation(); + //倒计时结束后恢复巡检 + std::thread countdown_thread([this,&arrive_] { + try { + //朝右下方 + std::cout << "发送机械臂转动指令,倒计时开始,请等待10秒" << std::endl; + serial_port_->write({ + 0xFE, 0xFE, 0x0F, 0x3C, + 0x0B, 0x71, 0x08, 0x25, 0x06, 0xFB, + 0x08, 0x2E, 0x0A, 0xCB, 0x08, 0x28, + 0x14, 0xFA + }); + std::this_thread::sleep_for(std::chrono::seconds(5)); - //TODO 控制机械臂转动到预设的角度 - //朝右下方 - std::cout << "发送机械臂转动指令,倒计时开始,请等待10秒" << std::endl; - serial_port_->write({ - 0xFE, 0xFE, 0x0F, 0x3C, - 0x0B, 0x71, 0x08, 0x25, 0x06, 0xFB, - 0x08, 0x2E, 0x0A, 0xCB, 0x08, 0x28, - 0x14, 0xFA - }); + //延迟5~10秒更新节点 + TcpService::getInstance().update_node(arrive_); - //倒计时结束后恢复巡检 - std::thread countdown_thread([this] { - try { - std::this_thread::sleep_for(std::chrono::seconds(10)); - recover_navigation(); - std::cout << "倒计时结束,恢复机械臂初始位置,恢复巡检"; - serial_port_->write(default_command); - } catch (const std::exception &e) { - std::cerr << "线程执行过程中发生异常: " << e.what() << std::endl; - } - }); - countdown_thread.join(); - } + std::this_thread::sleep_for(std::chrono::seconds(5)); + + recover_navigation(); + std::cout << "倒计时结束,恢复机械臂初始位置,恢复巡检"; + serial_port_->write(default_command); + } catch (const std::exception &e) { + std::cerr << "线程执行过程中发生异常: " << e.what() << std::endl; + } + }); + countdown_thread.join(); } } diff --git a/src/tcp_client.cpp b/src/tcp_client.cpp index 91c6720..e819793 100644 --- a/src/tcp_client.cpp +++ b/src/tcp_client.cpp @@ -5,6 +5,7 @@ #include "tcp_client.hpp" #include "tcp_service.hpp" #include "slam_wrapper.hpp" +#include "constant_config.hpp" #include #include @@ -146,7 +147,7 @@ << ", waiting for command ..." << std::endl; //连接成功后把自己的编号发送给Tcp Service - const std::string dog_code = "B42D4000OCAD3N82"; + const std::string dog_code = std::string("devCode:") + ConstantConfig::DOG_CODE; send(_client_socket, dog_code.c_str(), dog_code.size(), 0); // 启动接收数据的线程