Publisher and Subscriber nodes don't seem to be communicating
I made a publisher node that publishes Float64MultiArray
message type to a topic zones
. The script that contains this node constantly changes the .data portion of the message type. So for example, when it runs and I rostopic echo /zones
, the .data portion of the message type looks like this [1.234,0.345]
(the floating point numbers constantly change). Int he terminal. So the publisher node obviously works. I'm testing to make a subscriber node to this topic zone
.As seen here below
#!/usr/bin/python3
import rospy
from std_msgs.msg import Float64MultiArray
from rospy.numpy_msg import numpy_msg
import numpy as np
v=Float64MultiArray()
def callback(msg):
global v
v=msg
rospy.init_node("array_listener")
rospy.Subscriber("zones",Float64MultiArray, callback)
def hello():
global v
print(v.data[0])
hello()
when I run this script, v.data[0]
is empty and gives and as a result the script gives back an index error on the terminal. v.data[0] is not supposed to be empty, it should have the first element in .data
. I dont know why the subscriber does not seem to get information from the topic. I really need help on this.
Asked by distro on 2022-02-08 17:01:59 UTC
Answers
I think you are calling hello before callback.
Try the following.
#!/usr/bin/python3
import rospy
from std_msgs.msg import Float64MultiArray
from rospy.numpy_msg import numpy_msg
import numpy as np
v=Float64MultiArray()
def callback(msg):
global v
v=msg
print(v.data[0]) # added
rospy.init_node("array_listener")
rospy.Subscriber("zones",Float64MultiArray, callback)
def hello():
global v
print(v.data[0])
# hello() # to comment
rospy.spin() # added
Asked by miura on 2022-02-08 17:28:09 UTC
Comments
@miura did not work, im also not sure how im calling hello() before callback
Asked by distro on 2022-02-08 18:08:17 UTC
Thank you for trying it. I am sorry that I could not help you.
Asked by miura on 2022-02-09 18:40:30 UTC
@miura Its alright! I found the correct way thanks to someone else, check the answer above! Please don't apologize for trying to help me
Asked by distro on 2022-02-09 21:05:42 UTC
Comments
Could you also post your publisher here?
Asked by Akhil Kurup on 2022-02-08 17:06:12 UTC
@Akhil Kurup If you would please take a look, I updated the question here
Asked by distro on 2022-02-08 23:31:25 UTC