How to do a subscriber node?
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: http://docs.ros.org/api/sensor_msgs/html/msg/NavSatFix.html 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", data.data)
def infoGetter():
rospy.init_node('infoGetter', anonymous=True)
rospy.Subscriber("sensor_msgs/NavSatFix", float64, callback)
rospy.spin()
if __name__ == '__main__':
infoGetter()
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
EDIT 1:
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
rospy.spin()
if __name__ == '__main__':
infoGetter()
EDIT 2
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.initnode('infoGetter', anonymous=True) rospy.Subscriber("/novatel/fix", NavSatFix, callback) #rospy.spin() if _name__ == 'main': infoGetter()
Asked by negotiator14 on 2015-08-10 11:45:54 UTC
Answers
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
}
Mark
Asked by MarkyMark2012 on 2015-08-13 07:08:28 UTC
Comments
Have you done the tutorials? There is an example about how to write a python subscriber.
Asked by Javier V. Gómez on 2015-08-10 12:51:05 UTC
Looks okay on a quick glance. So you are good now?
Asked by mgruhler on 2015-08-11 01:11:44 UTC
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 withrostopic list
.Asked by Chrissi on 2015-08-11 04:36:00 UTC