Python 全球名称';距离';没有定义
我是python和ros的新手,现在有点问题。我敢肯定这是我的一个简单错误,但我想不出来 我搜索并发现了类似的情况,但我不确定如何在我的代码中实现其他答案的解决方案 请忽略我的代码中任何不直接导致问题的“垃圾”。我只需要这个项目尽快运行Python 全球名称';距离';没有定义,python,ros,Python,Ros,我是python和ros的新手,现在有点问题。我敢肯定这是我的一个简单错误,但我想不出来 我搜索并发现了类似的情况,但我不确定如何在我的代码中实现其他答案的解决方案 请忽略我的代码中任何不直接导致问题的“垃圾”。我只需要这个项目尽快运行 #!/usr/bin/env python import rospy from geometry_msgs.msg import Twist, Vector3Stamped from math import radians from sensor_msgs.m
#!/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)
这里是错误
Traceback (most recent call last):
File "NewTest.py", line 111, in <module>
navigate(distance, nav_angle)
NameError: global name 'distance' is not defined
回溯(最近一次呼叫最后一次):
文件“NewTest.py”,第111行,在
导航(距离、导航角度)
NameError:未定义全局名称“距离”
我该如何解决这个问题?非常感谢您的帮助。在函数中,您正在将全局变量
距离重新定义为局部变量在python中,全局变量
不标记为全局变量<代码>全局
进入函数内部,允许函数修改变量
例如:
def eggs():
x = 5 #<- local variable
print(x)
x = 10 #<- top-level variable
eggs() #<- prints 5
print(x) #<- prints 10 because the function doesn't touch the top-level variable
def eggs():
x=5#在这一行之前,你永远不会给距离赋予任何值。global
语句不会将值设置为变量。实际上,当在模块范围内执行时,就像在代码中一样,它根本不起任何作用。谢谢。刚才一位朋友向我指出了这一点。
def eggs():
global x
x = 5 #<- modify the top level variable
print(x)
x = 10 #<- top-level variable
eggs() #<- prints 5
print(x) #<- prints 5 because now the function can modify the top-level veriable