From 4b329a36e03ca4872fc78a9da517dd49af0045c1 Mon Sep 17 00:00:00 2001 From: hehe Date: Mon, 5 Jan 2026 12:45:58 +0800 Subject: [PATCH] add meta_key client --- meta_dev/launch/meta_dev.launch.py | 4 +- meta_dev/src/main.cpp | 254 ++++++++++++++++++----------- 2 files changed, 161 insertions(+), 97 deletions(-) diff --git a/meta_dev/launch/meta_dev.launch.py b/meta_dev/launch/meta_dev.launch.py index f375b9e..74095d6 100644 --- a/meta_dev/launch/meta_dev.launch.py +++ b/meta_dev/launch/meta_dev.launch.py @@ -2,5 +2,5 @@ from launch import LaunchDescription from launch_ros.actions import Node def generate_launch_description(): return LaunchDescription([ - Node(package="meta_dev",executable="meta_dev_node",name="meta_dev_node",output="screen",parameters=[]) - ]) \ No newline at end of file + Node(package="meta_dev",executable="meta_dev_node",name="meta_dev_node",output="screen",parameters=[{"meta_ip":"192.168.10.195"}]) + ]) diff --git a/meta_dev/src/main.cpp b/meta_dev/src/main.cpp index 8d40c21..1c3ba7d 100644 --- a/meta_dev/src/main.cpp +++ b/meta_dev/src/main.cpp @@ -1,6 +1,7 @@ #include"rclcpp/rclcpp.hpp" #include"sys/socket.h" #include "netinet/in.h" +#include #include "unistd.h" #include "interfaces/msg/meta_key.hpp" #include @@ -29,104 +30,117 @@ class UdpServerNode:public rclcpp::Node int32_t wr; }; HandData hand_data; + std::string meta_ip="192.168.10.73"; + const int meta_port=6666; + const int robot_port = 9999; + const int udp_buf_len=256; + unsigned char udp_buf[256]; public: UdpServerNode():Node("meta_dev_node"){ pub_meta=create_publisher("/meta_key",10); init_crc8(); - udp_thread=std::thread([this](){ - struct sockaddr_in server_addr,client_addr; - socklen_t client_len = sizeof(client_addr); - const int PORT = 9999; - const int udp_buf_len=256; - unsigned char udp_buf[256]; - int sockfd; - if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { - printf("UDP socket creation failed"); - return; - } - int flags = fcntl(sockfd, F_GETFL, 0); - fcntl(sockfd, F_SETFL, flags | SOCK_NONBLOCK); - // 配置服务器地址 - memset(&server_addr, 0, sizeof(server_addr)); - server_addr.sin_family = AF_INET; - server_addr.sin_addr.s_addr = INADDR_ANY; - server_addr.sin_port = htons(PORT); - - // 绑定端口 - if (bind(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) { - printf("Bind failed on port %d", PORT); - close(sockfd); - return; - } - - std::cout<<"UDP listener bound to port:"<< PORT< 0) { - std::cout<<"udp recv:"<pub_meta->publish(metaKey); - } else if (ret < 0) { - if (errno == EAGAIN || errno == EWOULDBLOCK ) { - std::this_thread::sleep_for(std::chrono::milliseconds(10)); - continue; - } - std::cout<<"recvfrom error:"< 0) { + /* + std::cout<<"udp recv:"<pub_meta->publish(metaKey); + } else if (ret < 0) { + if (errno == EAGAIN || errno == EWOULDBLOCK ) { + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + continue; + } + std::cout<<"recvfrom error:"< 0) { + printf("Server replied: %02x,%02x,%02x\n", udp_buf[0], udp_buf[1], udp_buf[2]); + return 1; + } else { + if(type==1) + printf("meta not response login fail \n"); + else if(type==0) + printf("meta not response logout fail \n"); + return 0; + } + + } }; int main(int argc,char* argv[]){ rclcpp::init(argc,argv); auto node =std::make_shared(); + node->init(); rclcpp::spin(node); + node->login_meta(0); rclcpp::shutdown(); return 0; -} \ No newline at end of file +}