<span id="mktg5"></span>

<i id="mktg5"><meter id="mktg5"></meter></i>

        <label id="mktg5"><meter id="mktg5"></meter></label>
        最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
        問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
        當前位置: 首頁 - 科技 - 知識百科 - 正文

        ROS多個master消息互通

        來源:懂視網 責編:小采 時間:2020-11-27 14:28:04
        文檔

        ROS多個master消息互通

        ROS多個master消息互通:需求有時候我們需要有幾個不同的master, 他們之間要交換topic的內容,這時候就不能使用ros自帶的設置同一個master的方法.我們的處理方法是,構造一個client和一個server,他們運行在不同的master下面, client在master1下訂閱topic1,然后通過t
        推薦度:
        導讀ROS多個master消息互通:需求有時候我們需要有幾個不同的master, 他們之間要交換topic的內容,這時候就不能使用ros自帶的設置同一個master的方法.我們的處理方法是,構造一個client和一個server,他們運行在不同的master下面, client在master1下訂閱topic1,然后通過t

        需求

        有時候我們需要有幾個不同的master, 他們之間要交換topic的內容,這時候就不能使用ros自帶的設置同一個master的方法.

        我們的處理方法是,構造一個client和一個server,他們運行在不同的master下面, client在master1下訂閱topic1,然后通過tcp協議(自己定義一個消息協議格式)發到master2下面的server,進行消息解析,再發布出master2下面的topic1,這樣我們不改變ros自帶的topic框架,就實現topic1的消息從master1到master2的傳播.

        下面我們實現的是將一個amcl_pose的topic,消息類型是PoseWithCovarianceStamped從master1傳到master2,其他的topic代碼類似

        client 的代碼

        #! /usr/bin/env python# -*- coding=utf-8 -*-import socketimport structimport rospyimport timefrom geometry_msgs.msg import PoseWithCovarianceStamped,PoseStamped#message proto# id | length | datadef send_msg(sock, msg ,id):
         # Prefix each message with a 4-byte id and length (network byte order)
         msg = struct.pack('>I',int(id)) + struct.pack('>I', len(msg)) + msg
         sock.sendall(msg)def odomCallback(msg):
         global odom_socket
        
         data = ""
        
         id = msg.header.seq print "send id: ",id
         x = msg.pose.pose.position.x
         y = msg.pose.pose.position.y #orientation
         orien_z = msg.pose.pose.orientation.z
         orien_w = msg.pose.pose.orientation.w
        
         data += str(x) + "," + str(y)+ "," + str(orien_z)+ "," + str(orien_w)
        
         send_msg(odom_socket,data,id)
        
        
        odom_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        odom_socket.connect(('127.0.0.1',8000))
        
        rospy.init_node('server_node')
        
        rospy.Subscriber('/amcl_pose',PoseWithCovarianceStamped,odomCallback)
        
        rospy.spin()

        server 的代碼

        #! /usr/bin/env python# -*- coding=utf-8 -*-import socketimport time,os,fcntlimport structimport rospyfrom geometry_msgs.msg import PoseWithCovarianceStamped,PoseStamped#message proto# id | length | datadef recv_msg(sock):
         try: # Read message length and unpack it into an integer
         raw_id = recvall(sock, 4) if not raw_id: return None
         id = struct.unpack('>I', raw_id)[0] print "receive id: ",id
         raw_msglen = recvall(sock, 4) if not raw_msglen: return None
         msglen = struct.unpack('>I', raw_msglen)[0] # Read the message data
         return recvall(sock, msglen) except Exception ,e: return Nonedef recvall(sock, n):
         # Helper function to recv n bytes or return None if EOF is hit
         data = ''
         while len(data) < n:
         packet = sock.recv(n - len(data)) if not packet: return None
         data += packet return data##把接受的數據重新打包成ros topic發出去def msg_construct(data):
        
         list = data.split(',')
        
         pose = PoseWithCovarianceStamped()
         pose.header.stamp = rospy.Time.now()
         pose.header.frame_id = "/map"
         pose.pose.pose.position.x = float(list[0])
         pose.pose.pose.position.y = float(list[1])
         pose.pose.pose.position.z = 0
         pose.pose.pose.orientation.x = 0
         pose.pose.pose.orientation.y = 0
         pose.pose.pose.orientation.z = float(list[2])
         pose.pose.pose.orientation.w = float(list[3])
         pose.pose.covariance = [0.25, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.25, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.06853891945200942] return pose#初始化socket,監聽8000端口odom_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
        odom_socket.bind(('',8000))
        odom_socket.listen(10)
        
        (client,address) = odom_socket.accept()
        
        rospy.init_node("client_node")
        odom_pub = rospy.Publisher("/amcl_pose",PoseWithCovarianceStamped,queue_size=30)
        r = rospy.Rate(1000)#設置noblock,否則會阻塞在接聽,下面while不會一直循環,只有在有數據才進行下一次循環fcntl.fcntl(client, fcntl.F_SETFL, os.O_NONBLOCK)while not rospy.is_shutdown():
         data = recv_msg(client) if data:
         odom_pub.publish(msg_construct(data))
         r.sleep()

        結論

        上面的代碼在局域網內測試過,發送圖像,激光的數據都可以保證不丟數據。

        聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

        文檔

        ROS多個master消息互通

        ROS多個master消息互通:需求有時候我們需要有幾個不同的master, 他們之間要交換topic的內容,這時候就不能使用ros自帶的設置同一個master的方法.我們的處理方法是,構造一個client和一個server,他們運行在不同的master下面, client在master1下訂閱topic1,然后通過t
        推薦度:
        標簽: 信息 互通 多個
        • 熱門焦點

        最新推薦

        猜你喜歡

        熱門推薦

        專題
        Top
        主站蜘蛛池模板: 日本黄页网址在线看免费不卡| 亚洲人成网站18禁止| 一级毛片完整版免费播放一区| 超pen个人视频国产免费观看| 麻豆狠色伊人亚洲综合网站 | 亚洲精品无码国产| 中国一级特黄的片子免费| 亚洲中文字幕丝袜制服一区| 99视频在线免费观看| 亚洲AV无码一区二区乱孑伦AS| 日韩精品无码免费专区午夜 | 特级毛片aaaa免费观看| 亚洲精品麻豆av| a毛片全部免费播放| 亚洲国产精品人久久| 1000部夫妻午夜免费| 国产亚洲玖玖玖在线观看| 日本免费一本天堂在线| 日韩久久无码免费毛片软件| 亚洲色自偷自拍另类小说| 99国产精品视频免费观看| 久久综合久久综合亚洲| 亚洲av麻豆aⅴ无码电影| 成人电影在线免费观看| 亚洲mv国产精品mv日本mv| 亚洲av区一区二区三| 你懂的在线免费观看| 亚洲一级毛片免费看| 一级毛片直播亚洲| 久久成人免费电影| 亚洲熟妇AV一区二区三区宅男| 亚洲高清视频一视频二视频三| 国产偷伦视频免费观看| 亚洲熟妇无码一区二区三区导航| 国产成人精品久久亚洲| 无码国产精品一区二区免费虚拟VR| 看一级毛片免费观看视频| 亚洲欧洲免费视频| 免费大片在线观看网站| 日韩视频在线观看免费| 久久久久亚洲精品无码网址色欲|