Hello,
So currently I have two GPS modules and they each have their own nodes. The 2 nodes publish their GPS information onto their own topics. So 2 nodes and 2 topics. I'm able to subscribe to both nodes but I'm having trouble using the variables in the callback function to calculate distance.
I would get this error: NameError: global name 'gps_lat' is not defined
Here's my code below
#!/usr/bin/env python
import rospy
from std_msgs.msg import String
from nmea_msgs.msg import Sentence
from sensor_msgs.msg import NavSatFix
import math
def callback(data):
gps_lat = round(data.latitude, 6)
gps_lon = round(data.longitude, 6)
def callback1(data):
gps1_lat = round(data.latitude, 6)
gps1_lon = round(data.longitude, 6)
def listener():
rospy.init_node('gps_monitor', anonymous=True)
rospy.Subscriber("/fix", NavSatFix, callback)
rospy.Subscriber("/fix1", NavSatFix, callback1)
rospy.spin()
delta_lat = (gps_lat*(108000) - gps1_lat*(108000))
delta_lon = (gps_lon*(108000) - gps1_lon*(108000))
hyp_m = (delta_lat**2 + delta_lon**2)**0.5
hyp_ft = (hyp_m*3.2800839)
rospy.loginfo("Distance is %s in ft.", hyp_ft)
if __name__ == '__main__':
listener()
Originally posted by SupermanPrime01 on ROS Answers with karma: 27 on 2017-04-26
Post score: 0