全局名称 'distance' 未定义
global name 'distance' is not defined
我是 python 和 ros 的新手,目前遇到了一些小问题。我确定这是我的一个简单错误,但我想不通。
我已经搜索并发现了类似的情况,但我不确定如何在我的代码中实现其他答案的解决方案。
请忽略我的代码中与问题没有直接关系的任何 'crappiness'。我只需要这个项目尽快运行。
#!/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
我该如何解决这个问题?感谢您的帮助。
在您的函数中,您将全局变量 distance
重新定义为局部变量
在python中,global
没有将变量标记为全局; global
进入函数内部,允许函数修改变量。
例如:
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():
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
我是 python 和 ros 的新手,目前遇到了一些小问题。我确定这是我的一个简单错误,但我想不通。
我已经搜索并发现了类似的情况,但我不确定如何在我的代码中实现其他答案的解决方案。
请忽略我的代码中与问题没有直接关系的任何 'crappiness'。我只需要这个项目尽快运行。
#!/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
我该如何解决这个问题?感谢您的帮助。
在您的函数中,您将全局变量 distance
重新定义为局部变量
在python中,global
没有将变量标记为全局; global
进入函数内部,允许函数修改变量。
例如:
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():
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