我是python和ros的新手,我现在有一个小问题。我确信这对我来说是一个简单的错误,但我无法弄明白。
我搜索并发现了类似的情况,但我不确定如何实现我的代码中其他答案的解决方案。
请忽略我的代码中没有直接导致问题的任何“困惑”。我只需要这个项目尽快运行。
#!/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
我该如何解决这个问题?我感谢任何帮助。
答案 0 :(得分:0)
在您的函数中,您将全局变量distance
重新定义为本地变量
答案 1 :(得分:0)
在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