ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

How to do a subscriber node?

asked 2015-08-10 11:45:54 -0500

negotiator14 gravatar image

updated 2015-08-12 16:09:20 -0500

I want to subscribe to the NavSatFix topic and be able to retrieve the longitude and latitude from a GPS that is publishing that infor. Here's the documentation of the message that I need to get the data from: I've been following the ROS tutorials to work with it but it's really barebones and I don't get how I'm supposed to get that info. This is what I have so far:

#!/usr/bin/env python
import rospy
from sensor_msgs import NavSatFix

def callback(data):
    rospy.loginfo(rospy.get_caller_id() + "I heard %s", 

def infoGetter():

rospy.init_node('infoGetter', anonymous=True)

rospy.Subscriber("sensor_msgs/NavSatFix", float64, callback)


if __name__ == '__main__':

Now I need to be able to retrieve the info the I need from the topic that is publishing the Longitude and Altitude. My main question is to know how to do that step


This is what I got from the tutorials:

#!/usr/bin/env python
import rospy
from sensor_msgs.msg import NavSatFix

def callback(data):
    rospy.loginfo("Longitude: %f, Altitude %f" % (data.longitude, data.latitude))
    longitude = data.longitude
    latitude = data.latitude
    #Call for the programName(longitude,latitude)

def infoGetter():

    rospy.init_node('infoGetter', anonymous=True)

    rospy.Subscriber("sensor_msgs/NavSatFix", NavSatFix, callback)#First parameter is the name of the topic


if __name__ == '__main__':


I finally was able to subscribe to my desired topic and get the values that I need for that topic. Now I want to send those values to another program to do some manipulation. What I did is that I made the function call inside the callback function but it only seems to "work" when I have rospy.spin() in my infoGetter function. However I don't want it to run infinitely, I just want it to run once, subscribe to the topic, get the info needed and send it to whatTheHell() to do only 1 calculation. Is there a way to just run the program once without having it looping forever and ever?

Here's my code for reference:

#!/usr/bin/env python import rospy from sensor_msgs.msg import NavSatFix from RetreiveFiles import whatTheHell

def callback(data): rospy.loginfo("Longitude: %f, Latitude %f" % (data.longitude, data.latitude)) #global latitude #global longitude latitude = data.latitude longitude = data.longitude whatTheHell(latitude, longitude)

def infoGetter(): rospy.init_node('infoGetter', anonymous=True) rospy.Subscriber("/novatel/fix", NavSatFix, callback) #rospy.spin() if __name__ == '__main__': infoGetter()

edit retag flag offensive close merge delete


Have you done the tutorials? There is an example about how to write a python subscriber.

Javier V. Gómez gravatar image Javier V. Gómez  ( 2015-08-10 12:51:05 -0500 )edit

Looks okay on a quick glance. So you are good now?

mgruhler gravatar image mgruhler  ( 2015-08-11 01:11:44 -0500 )edit

I have the feeling that sensor_msgs/NavSatFix is not really the topic your message is published on. In case it still doesn't work, try and find the real topic name with rostopic list.

Chrissi gravatar image Chrissi  ( 2015-08-11 04:36:00 -0500 )edit

1 Answer

Sort by » oldest newest most voted

answered 2015-08-13 07:08:28 -0500

MarkyMark2012 gravatar image

Not sure if you have finished with this? However the easiest way to have something only run once would be to excapsulate it in an if clause with a static member - something like

 static boolean haveIDoneItYet = FALSE
 if (haveIDoneItYet == FALSE) 
       // Do your thing once
       haveIDoneItYet = TRUE


edit flag offensive delete link more

Question Tools

1 follower


Asked: 2015-08-10 11:45:54 -0500

Seen: 2,121 times

Last updated: Aug 13 '15