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.
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.