Ask Your Question

ROS: Rviz - publish point - subscriber - get x,y coordinates

asked 2020-12-10 07:25:17 -0500

Varun gravatar image

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 =
    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)

if __name__ == '__main__':

In terminal

varun-flox@varunflox:~/Desktop$ ./ 
[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 
  seq: 31
    secs: 1607606021
    nsecs: 774185596
  frame_id: "map"
  x: -4.99073648453
  y: -4.93864154816
  z: -0.00288152694702
  seq: 32
    secs: 1607606216
    nsecs: 838675541
  frame_id: "map"
  x: -4.98981332779
  y: -4.96337604523
  z: -0.0113334655762

Thanks in advaNCE

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2020-12-10 07:42:43 -0500

MaximN74 gravatar image


I mostly use a C++, but I think in Python you can write (if your def callback is a subscriber):

def callback(msg):

point = PointStamped()
point.header.stamp =
point.header.frame_id = "/map"
point.point.x = msg.point.x         // access to point data structure and its x, y, z components
point.point.y = msg.point.y
point.point.z = msg.point.z
**rospy.loginfo("coordinates:x=%f y=%f" %(point.point.x, point.point.y))**
edit flag offensive delete link more


Thank you it worked

Varun gravatar image Varun  ( 2020-12-10 07:54:06 -0500 )edit

NOW THAT I CAN GET THE POINTS IN THAT TOPIC can you help me in using those coordinates to make the robot move to /cmd_vel please Ideally to replace the manual coordinates in path_list with the x,y data from the /clicked_point for realtime waypoint setting @MaximN74

i have posted the question already here

Varun gravatar image Varun  ( 2020-12-10 07:57:23 -0500 )edit

@Varun, please mark the answer as correct by clicking the checkmark next to it. Thanks

mgruhler gravatar image mgruhler  ( 2020-12-10 10:05:06 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2020-12-10 07:25:17 -0500

Seen: 409 times

Last updated: Dec 10 '20