1. 问题描述
如下图所示,数据的传输通过TCP-IP通信,客户端接收到消息以后用rostopic 发布出去,rosinfo打印消息,原本一行出现一个数据(在服务器这端是这样),但是图中一行出现了两个甚至多个,RR,RP
2. 解决方法:
产生这个问题的原因是接收端(客户端)接收了以后,rostopic发布出去的频率不够,导致消息阻塞,发布在了同一行。也就是rate = rospy.Rate(50) # 10hz
这一行代码,决定了ros发布的频率,原先设置的是10HZ,但是服务器发过来的数据大于10HZ,所以就阻塞了,有些数据发在了同一行
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import roslib
import rospy
#import tf
#import math
#import numpy as np
#from tf import transformations
from std_msgs.msg import Float32
from std_msgs.msg import String
import socket
#import json
#import time
# import sys
# def get_tf():
# rospy.init_node('talker', anonymous=True)
# pub = rospy.Publisher('gesture', String, queue_size=10)
# s = socket.socket()
# IP = "192.168.103.13"
# port = 12345
# # address = ('192.168.103.13', 12345)
# # readdress = ('192.168.103.26', 12345)
# # host = socket.gethostname()
# # port = 60000
#
#
#
# # s.connect(address)
#
#
# s.connect((IP, port))
# hand_msg = s.recv(1024)
# print(hand_msg)
# print (s.recv(1024).decode())
# gesture_flag = s.recv(1024).decode()
# rospy.loginfo(gesture_flag)
# print(gesture_flag)
# pub.publish(gesture_flag)
# s.close()
if __name__ == '__main__':
s = socket.socket()
IP = "192.168.103.13"
port = 12345
s.connect((IP, port))
#gesture_flag = s.recv(1024).decode()
rospy.init_node('talker', anonymous=True)
#pub = rospy.Publisher('gesture', String, queue_size=10)
#gesture_flag = s.recv(1024).decode()
#rospy.init_node('talker', anonymous=True)
rate = rospy.Rate(50) # 10hz
while not rospy.is_shutdown():
gesture_flag = s.recv(1024).decode()
#pub = rospy.Publisher('gesture', String, queue_size=10)
pub = rospy.Publisher('gesture', String)
# hello_str = "hello world %s" % rospy.get_time()
#rospy.loginfo(gesture_flag)
print (gesture_flag)
pub.publish(gesture_flag)
rate.sleep()
#rospy.spin()