Ask Your Question

AlexLF's profile - activity

2019-01-15 20:17:43 -0500 received badge  Student (source)
2018-09-20 13:14:42 -0500 received badge  Famous Question (source)
2018-04-22 08:44:13 -0500 received badge  Notable Question (source)
2018-04-21 23:16:30 -0500 marked best answer Why give me this error? AttributeError: 'int' object has no attribute 'a'

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.

2018-04-21 23:16:30 -0500 received badge  Scholar (source)
2018-04-21 23:16:16 -0500 commented answer Why give me this error? AttributeError: 'int' object has no attribute 'a'

Thanks, for your answer. Effectively, my error is in python. I need to use the value of the variable "a and b" of the c

2018-04-21 23:14:10 -0500 received badge  Supporter (source)
2018-04-21 23:12:41 -0500 answered a question Why give me this error? AttributeError: 'int' object has no attribute 'a'

Thanks, for your answer. Effectively, my error is in python. I need to use the value of the variable "a and b" of the c

2018-04-21 05:18:04 -0500 received badge  Popular Question (source)
2018-04-21 00:44:09 -0500 asked a question Why give me this error? AttributeError: 'int' object has no attribute 'a'

Why give me this error? AttributeError: 'int' object has no attribute 'a' Hi, I need create a node, this node must be S

2018-04-21 00:44:09 -0500 asked a question Why give me error? AttributeError: 'int' object has no attribute 'a'

Why give me error? AttributeError: 'int' object has no attribute 'a' Hi, I need create a node, this node must be Subsrip

2018-04-20 19:25:12 -0500 received badge  Organizer (source)