【问题标题】:TypeError: 'numpy.float64' object is not callable?TypeError:“numpy.float64”对象不可调用?
【发布时间】:2021-11-26 03:26:40
【问题描述】:

我无法弄清楚为什么会出现此错误。任何帮助将不胜感激。

此代码用于小型全地形车的基本自主导航。由于某种原因,它陷入了航向和方位计算功能。我已经尝试将其中一个放在 run 函数中,它做同样的事情。

我对 python 相当缺乏经验,所以我可能忽略了一些简单的东西。

#!/usr/bin/env python

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

lat = 0.0
lon = 0.0
x = 0.0
y = 0.0
z = 0.0
head = 0.0
bear = 0.0


###########################################################
destLat = 30.210406
#                                   Destination
destLon = -92.022914
############################################################


class sub():

    def __init__(self):
        rospy.init_node('Test1', anonymous=False)
        rospy.Subscriber("/imu_um6/mag", Vector3Stamped, self.call_1)
        rospy.Subscriber("/gps/fix", NavSatFix, self.call_2)

    def call_1(self, mag):
        global x
        global y
        global z
        x = mag.vector.x
        y= mag.vector.y
        z= mag.vector.z
    
    def call_2(self, gps):
        global lat
        global lon

        lat = gps.latitude
        lon = gps.longitude

def head(lat, lon):
    global head
    # Define heading here
    head = numpy.rad2deg(numpy.arctan2(z, y)) + 90
    print(head)


def bear(y,z):
    global bear
    # Define bearing Here
    dLon = destLon - lon
    vert = numpy.sin(dLon) * numpy.cos(destLat)
    horiz = numpy.cos(lat)*numpy.sin(destLat) - numpy.sin(lat)*numpy.cos(destLat)*numpy.cos(dLon)
    bear = (numpy.rad2deg(numpy.arctan2(vert, horiz)) + 360) % 360
    print(bear)


def nav(head, bear, destLat, destLon):
    # Do Navigation Here
    pub = rospy.Publisher('/husky_velocity_controller/cmd_vel', Twist, queue_size=10)
    move_cmd = Twist()
    turn_cmd = Twist()

    move_cmd.linear.x = 2
    turn_cmd.angular.z = radians(45)

    turn_angle = head - bear
#   Prettify the angle 
    if (turn_angle > 180):
        turn_angle -= 360
    elif (turn_angle < -180):
        turn_angle += 360
    else:
        turn_angle = turn_angle

    if (abs(lat-destLat)<.0005 and abs(lon-destLon)<.0005):
        pub.publish(Twist())
        r.sleep()
    else:
        if (abs(turn_angle) < 8):
            pub.publish(move_cmd)
            rospy.spin()
        else:
            pub.publish(turn_cmd)
            r = rospy.Rate(5);
            r.sleep()
    


def shutdown(self):
    rospy.loginfo("Stop Husky")
    cmd_vel.publish(Twist())
    rospy.sleep(1)


def run():
    sub()
    bear(y,z)
    head(lat,lon)
    nav(head, bear, destLat, destLon)
    print('here')



if __name__ == '__main__':
    try:
        while not rospy.is_shutdown():
            run()
    except rospy.ROSInterruptException:
        rospy.loginfo("stopped")
        pass

这是整个输出:

163.11651134
90.0
here
Traceback (most recent call last):
  File "classTest.py", line 117, in <module>
    run()
  File "classTest.py", line 107, in run
    bear(y,z)
TypeError: 'numpy.float64' object is not callable

【问题讨论】:

  • 注意run 工作一次;这是第二个遇到不同bear(和head)的电话。我会尝试摆脱这种globals 方法来保存状态。将状态保存为类对象的属性会更好。但我不确定rosby 调用会如何发挥作用。

标签: python numpy


【解决方案1】:

您不能对函数和浮点数使用相同的变量名(在同一个命名空间中)。你们都定义了一个bear 函数和一个指向浮点数的bear 变量。您需要更改两个名称之一。

【讨论】:

    【解决方案2】:

    错了,大错特错!!! :)

    def bear(y,z):
        global bear
        ....
    

    【讨论】:

    • 虽然我完全同意这一点,但我不知道这个答案是否清楚地回答了 OP 的问题。
    • 本身可能不是原因。如果没有global,您可能会收到此错误。但是这些行的并列指向了使用标识符bear 的一些有趣的事情。也许程序员习惯于将变量和函数保存在不同的命名空间中的语言。过度使用global也是一个常见的菜鸟错误,
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-22
    • 2020-09-11
    • 2020-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多