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

Publisher and Subscriber nodes don't seem to be communicating [closed]

asked 2022-02-08 16:01:59 -0500

distro gravatar image

updated 2022-02-08 16:13:42 -0500

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.

edit retag flag offensive reopen merge delete

Closed for the following reason duplicate question by distro
close date 2022-02-08 22:32:14.527637

Comments

Could you also post your publisher here?

Akhil Kurup gravatar image Akhil Kurup  ( 2022-02-08 16:06:12 -0500 )edit

@Akhil Kurup If you would please take a look, I updated the question here

distro gravatar image distro  ( 2022-02-08 22:31:25 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2022-02-08 16:28:09 -0500

miura gravatar image

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
edit flag offensive delete link more

Comments

@miura did not work, im also not sure how im calling hello() before callback

distro gravatar image distro  ( 2022-02-08 17:08:17 -0500 )edit

Thank you for trying it. I am sorry that I could not help you.

miura gravatar image miura  ( 2022-02-09 17:40:30 -0500 )edit
1

@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

distro gravatar image distro  ( 2022-02-09 20:05:42 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2022-02-08 16:01:59 -0500

Seen: 59 times

Last updated: Feb 08 '22