ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
1

Why give me this error? AttributeError: 'int' object has no attribute 'a'

asked 2018-04-20 19:25:12 -0500

AlexLF gravatar image

updated 2018-04-22 04:13:20 -0500

gvdhoorn gravatar image

Hi, I need create a node, this node must be Subsriptor and Publicator. But, I have this error.

  Traceback (most recent call last):
  File "/home/user/catkin_ws/src/prueba/src/listener_3.py", line 71, in <module>
    listener()
  File "/home/user/catkin_ws/src/prueba/src/listener_3.py", line 63, in listener
    m= data.a
AttributeError: 'int' object has no attribute 'a'

My code is this:

import time
import rospy

from prueba.msg import Num
from prueba.msg import Num2
from prueba.msg import Num5
from prueba.msg import Num6
from std_msgs.msg import String

global a,b, callback, callback2, data, data1
a=0
b=0
c=0
data1=0
data=0
def callback(data):
    global a,b
    a = 5 + data.num
    print a
    print ""

    return a

def callback2(data1):
    global a,b
    b = 8 + data1.num2  
    print b
    return b

def listener():
    global a, b,c

    rospy.init_node('listener3',anonymous = True)
    rospy.Subscriber('topic_3',Num,callback)
    rospy.Subscriber('topic_4',Num2,callback2)
    pub5 = rospy.Publisher('topic_5',Num5,queue_size=10)
    pub6 = rospy.Publisher('topic_6',Num6,queue_size=10)
    m= data.a
    n= data1.b
    pub5.publish(m)
    pub6.publish(b)
    #rate.sleep()
    rospy.spin()

if __name__ == '__main__':
    listener()

I modified package.xml and CMakeList.txt, as the tutorial of "msg" explain.

I hope, any answer. Thank You


Edit: Thanks, for your answer.

Effectively, my error is in python. I need to use the value of the variable "a and b" of the callback and callback2 function, in the listener function. But first, I got an error: "global name" xx "is not defined", so I declared them at the beginning of the program. But it does not work. I need those variables to publish them through a topic to another node.

In this node, I need receive and send variables. I don´t know, if this lines are good.

rospy.init_node('listener3',anonymous = True)
rospy.Subscriber('topic_3',Num,callback)
rospy.Subscriber('topic_4',Num2,callback2)
pub5 = rospy.Publisher('topic_5',Num5,queue_size=10)
pub6 = rospy.Publisher('topic_6',Num6,queue_size=10)
m= data.a
n= data1.b
pub5.publish(m)
pub6.publish(n)

m and n values are the variables that I need call of the callback function.

I would appreciate if you helped me with that.

edit retag flag offensive close merge delete

Comments

Please do not post answers unless you are answering your own question. For everything else, edit your original post (using the edit link/button) or post comments.

gvdhoorn gravatar image gvdhoorn  ( 2018-04-22 04:14:02 -0500 )edit

1 Answer

Sort by » oldest newest most voted
3

answered 2018-04-21 00:48:58 -0500

gvdhoorn gravatar image

updated 2018-04-21 00:50:52 -0500

This is really a Python issue, not a ROS one, but:

global a,b, callback, callback2, data, data1
[..]
data=0
[..]
def listener():
    [..]
    m= data.a
    [..]

The variable data is just an integer, not an object, or some other composite structure. You cannot index into it, nor use the dot operator to select contained fields.

I believe you are expecting the data argument to callback(..) (which may or may not have a field called a, depending on the message IDL) to be available to you in listener(), but that is not how this works.


I'm also rather puzzled by the the global a,b,... line at the top of your program. Those variables are already in the global scope, so what is it you're trying to do?

If I may suggest, it might be good to try and find some good Python books or online tutorials. With just a little Python experience I think you'll find that things (such as rospy) start to make a lot more sense.

edit flag offensive delete link more

Comments

Thanks, for your answer.

Effectively, my error is in python. I need to use the value of the variable "a and b" of the callback and callback2 function, in the listener function. But first, I got an error: "global name" xx "is not defined", so I declared them at the beginning of the program.

AlexLF gravatar image AlexLF  ( 2018-04-21 23:16:16 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2018-04-20 16:00:11 -0500

Seen: 40,216 times

Last updated: Apr 22 '18