-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpath_sub.py
50 lines (41 loc) · 1.55 KB
/
path_sub.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
46
47
48
49
50
import paho.mqtt.client as mqtt
import rclpy # Import the ROS client library for Python
from rclpy.node import Node # Enables the use of rclpy's Node class
from geometry_msgs.msg import PoseStamped
from nav_msgs.msg import Path
import time
#check if disconect ---> connect again
def on_disconnect(client, userdata, rc):
print("disconnecting reason " +str(rc))
client.connect("broker.hivemq.com", 1883, 60)
# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
#this is the function that gives feedback of the plan that ros published and print it on terminal and publish it to MQTT on topic called (cic/plan)
def path_feedback(data):
#print ("Plan")
res = ""
#print ("Before: ", len(data.poses))
step = 20
for i in range(0,len(data.poses),step):
point = "{:0.2f},{:0.2f}_".format(data.poses[i].pose.position.x, data.poses[i].pose.position.y)
res += point
print ("After: ", len(res.split('_')))
client.publish("cic/plan", res)
print (time.time())
client = mqtt.Client()
def main(args=None):
global client, x
# Initialize the rclpy library
#client = mqtt.Client()
client.on_connect = on_connect
client.on_disconnect = on_disconnect
# Create the node
rclpy.init(args=args)
x = Node('values')
global_plan_sub = x.create_subscription(Path, "plan" , path_feedback, 10)
client.connect("broker.hivemq.com", 1883, 60)
rclpy.spin(x)
#client.loop_forever()
if __name__ == '__main__':
main()