global name 'distance' is not defined : <p>I'm new to python and ros, and I'm having a little issue at the moment. I'm sure it's a simple error on my part, but I can't figure it out.</p>

<p>I've searched and found similar situations, but I'm not sure how to implement the solutions given to other answers on my code.</p>

<p>Please ignore any 'crappiness' in my code that does not directly contribute to the issue.  I just need this project to run as quickly as possible.</p>

<pre><code>#!/usr/bin/env python

import rospy
from geometry_msgs.msg import Twist, Vector3Stamped
from math import radians
from sensor_msgs.msg import NavSatFix
import geometry_msgs.msg
import time
import numpy

global distance
global pub
global dest_lat
global dest_long
global move_cmd
global turn_cmd
global bearing
global heading
global initial_bearing
global cur_lat
global prev_lat
global cur_long
global prev_long

bearing = 0

################################################################################
lat_dest = 30.210406
#                                                   DESTINATION COORDINATES
long_dest = -92.022914
################################################################################

move_cmd = Twist()
turn_cmd = Twist()

def nav_dist(navsat):
    R = 6373000        # Radius of the earth in m
    cur_lat = navsat.latitude
    cur_long = navsat.longitude
    dLat = cur_lat - lat_dest
    dLon = cur_long - long_dest
    x = dLon * numpy.cos((lat_dest+cur_lat)/2)
    distance = numpy.sqrt(x**2 + dLat**2) * R
    return distance


def bearing():
    if (bearing == 0):
        bearing = initial_bearing
        return bearing
    else:
        bearing = calculate_bearing(cur_lat, prev_lat, cur_long, prev_long)
        return bearing


def calculate_bearing(lat1, lat2, long1, long2):
    dLon = long2 - long1
    y = np.sin(dLon) * np.cos(lat2)
    x = np.cos(lat1)*np.sin(lat2) - np.sin(lat1)*np.cos(lat2)*np.cos(dLon)
    bearing = (np.rad2deg(np.arctan2(y, x)) + 360) % 360
    return bearing

def calculate_nav_angle(lat1, lat_dest, long1, long_dest):
    dLon = long_dest - long1
    y = np.sin(dLon) * np.cos(lat_dest)
    x = np.cos(lat1)*np.sin(lat_dest) - np.sin(lat1)*np.cos(lat_dest)*np.cos(dLon)
    heading = (np.rad2deg(np.arctan2(y, x)) + 360) % 360
    return heading

def navigate(distance, nav_angle):
    turn_cmd.angular.z = radians(bearing - heading)
    move_cmd.linear.x = distance
    pub.publish(turn_cmd)
    time.sleep(.01)
    pub.publish(move_cmd)
    time.sleep(.001)

    prev_long = cur_long
    prev_lat = cur_lat

###############################################################################################

#################################    callbacks and run    #################################

###############################################################################################

def call_nav(navsat):
    rospy.loginfo("Latitude: %s", navsat.latitude)      #Print GPS co-or to terminal
    rospy.loginfo("Longitude: %s", navsat.longitude)
    nav_dist(navsat)
    time.sleep(.001)


def call_bear(bearing):
    rospy.loginfo("Bearing: %s", bearing.vector)            #Print mag reading for bearing
    x = -bearing.vector.y
    y = bearing.vector.z
    initial_bearing = numpy.arctan2(y, x)
    return initial_bearing

def run():
    pub = rospy.Publisher('/husky_velocity_controller/cmd_vel', Twist)
    rospy.Subscriber("/imu_um6/mag", Vector3Stamped, call_bear)
    rospy.Subscriber("/gps/fix", NavSatFix, call_nav)
    rospy.init_node('navigate_that_husky')

    rospy.spin()

if __name__ == '__main__':
    run()
    navigate(distance, nav_angle)
</code></pre>

<p>and here is the error</p>

<pre><code>Traceback (most recent call last):
  File "NewTest.py", line 111, in &lt;module&gt;
    navigate(distance, nav_angle)
NameError: global name 'distance' is not defined
</code></pre>

<p>how might I fix this?  I appreciate any help.</p>
