Python 类型错误:'numpy.float64' 对象不可调用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37232872/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 19:05:51  来源:igfitidea点击:

TypeError: 'numpy.float64' object is not callable?

pythonnumpycallros

提问by Ronnie Kisor

I can't figure out why I'm getting this error. Any help would be appreciated.

我不明白为什么我会收到这个错误。任何帮助,将不胜感激。

This code is for basic autonomous navigation of a small all terrain vehicle. For some reason it's getting caught in the heading and bearing calculation functions. I've tried putting either one first in the run function, and it does the same thing.

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

I'm fairly inexperienced with python, so it's likely something simple that I am overlooking.

我对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

Here's the whole output:

这是整个输出:

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

Thanks

谢谢

回答by Roberto

You can't use the same variable name for a function and a float (in the same namespace). And you both defined a bearfunction and a bearvariable pointing to a float. You need to change one of the two names.

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

回答by hpaulj

Wrong, so wrong!!! :)

错了,错了!!!:)

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