-
Notifications
You must be signed in to change notification settings - Fork 1
/
ZmqRosBridgeV1.py
45 lines (35 loc) · 1.12 KB
/
ZmqRosBridgeV1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import zmq
import rospy
from std_msgs.msg import String
import struct
context = zmq.Context()
poller = zmq.Poller()
# 订阅者套接字
sub_socket = context.socket(zmq.SUB)
sub_socket.connect(f"tcp://localhost:5555")
sub_socket.setsockopt_string(zmq.SUBSCRIBE, '')
poller.register(sub_socket, zmq.POLLIN)
# 发布者套接字
pub_socket = context.socket(zmq.PUB)
pub_socket.bind("tcp://*:5559")
def callback(data):
pub_socket.send_string(data.data)
# ROS订阅者和发布者
ros_sub = rospy.Subscriber("ros_topicc", String, callback)
ros_pub = rospy.Publisher("ros_topic", String, queue_size=10)
def main_loop():
while not rospy.is_shutdown():
try:
socks = dict(poller.poll())
except KeyboardInterrupt:
break
# 监听订阅者
if sub_socket in socks and socks[sub_socket] == zmq.POLLIN:
message = sub_socket.recv()
rospy.loginfo(f"Received message: {message}")
data = struct.unpack('i', message)
ros_pub.publish(str(data[0]))
context.term()
if __name__ == '__main__':
rospy.init_node('zmq_ros_node')
main_loop()