-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathros2kafka.py
35 lines (29 loc) · 1.09 KB
/
ros2kafka.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
import rospy
from nav_msgs.msg import Odometry
import json
from datetime import datetime
from kafka import KafkaProducer
count = 0
def callback(msg):
global count
messages={
"id":count,
"posex":float("{0:.5f}".format(msg.pose.pose.position.x)),
"posey":float("{0:.5f}".format(msg.pose.pose.position.y)),
"posez":float("{0:.5f}".format(msg.pose.pose.position.z)),
"orientx":float("{0:.5f}".format(msg.pose.pose.orientation.x)),
"orienty":float("{0:.5f}".format(msg.pose.pose.orientation.y)),
"orientz":float("{0:.5f}".format(msg.pose.pose.orientation.z)),
"orientw":float("{0:.5f}".format(msg.pose.pose.orientation.w))
}
print(f"Producing message {datetime.now()} Message :\n {str(messages)}")
producer.send("rosmsgs",messages)
count+=1
producer = KafkaProducer(
bootstrap_servers=["172.18.0.4:9092"],
value_serializer=lambda message: json.dumps(message).encode('utf-8')
)
if __name__=="__main__":
rospy.init_node('odomSubscriber', anonymous=True)
rospy.Subscriber('odom',Odometry,callback)
rospy.spin()