ROS: Rviz - publish point - subscriber - get x,y coordinates
Hello Everyone
I am trying to get x,y coordinates from rviz and get it subscribed to a topic So when i do publish point and click on any free space in rviz i get its x,y,z coordinates in the topic /clicked_point I want to subscribe to these points and print it on the terminal. The code i have is a simple subscriber to the topic /clicked_point.
But when i run the code i get x,y as 0.0 and not the desired coordinates that i click.
Can someone help me please in looking at my code. I suspect it has to do something with the point stamped attributes.
Please let me know
Full code
#!/usr/bin/env python
import rospy
from geometry_msgs.msg import PointStamped
import tf
import random
import numpy as np
global x,y,z
x = 0.0
y = 0.0
z = 0.0
def callback(msg):
point = PointStamped()
point.header.stamp = rospy.Time.now()
point.header.frame_id = "/map"
point.point.x = x
point.point.y = y
point.point.z = z
rospy.loginfo("coordinates:x=%f y=%f" %(x,y))
def listener():
rospy.init_node('goal_publisher', anonymous=True)
rospy.point_pub = rospy.Subscriber('/clicked_point', PointStamped, callback)
rospy.spin()
if __name__ == '__main__':
listener()
In terminal
varun-flox@varunflox:~/Desktop$ ./ros_localisation_subscriber.py
[INFO] [1607606216.839750]: coordinates:x=0.000000 y=0.000000
[INFO] [1607606221.911950]: coordinates:x=0.000000 y=0.000000
In another terminal with rostopic echo /clicked_point
varun-flox@varunflox:~$ rostopic echo /clicked_point
header:
seq: 31
stamp:
secs: 1607606021
nsecs: 774185596
frame_id: "map"
point:
x: -4.99073648453
y: -4.93864154816
z: -0.00288152694702
---
header:
seq: 32
stamp:
secs: 1607606216
nsecs: 838675541
frame_id: "map"
point:
x: -4.98981332779
y: -4.96337604523
z: -0.0113334655762
---
Thanks in advaNCE