Reading data from Topic
I am using a GPS plugin on my model. I know it works as I have tested by using
rostopic echo gps_out
and it continually outputs data in the NavSatFix form.
I am trying to write a script that will ultimately give my robot velocity commands through reading a target and current GPS location.
The python script I'm currently writing is to read in the data and then assign the longitude and latitude to global variables which can be displayed to confirm they are correct. This is the python script currently.
#!/usr/bin/env python
import rospy
from sensor_msgs.msg import NavSatFix
import tf
global longitude
global latitude
latitude='Not re-assigned'
longitude='Not re-assigned'
def callback(data):
latitude = data.latitude
longitude = data.longitude
rospy.init_node('topic_reader')
rospy.Subscriber('gps_out', NavSatFix, callback)
print("Longitude:", longitude)
print("Latitude:", latitude)
As can be read from the code, the only output is
Longitude: Not re-assigned
Latitude: Not re-assigned
What am I doing wrong?
From my understanding I am setting longitude and latitude variables to be global, and assigning them the initial conditions. Then assigned the nodes name, then subscribing to the topic and running callback which should assign the values to the variables which will allow them to be printed.