How can I subscribers multiple topics to get data at the same time (python)?
Hi, Am new to ROS and I need a little help. There are 4 topics (robot0/sonar1, robot0/sonar2, ...) of same type (sensor_msgs/Range). I need to process these 4 values together.
#!/usr/bin/env python
import rospy
from sensor_msgs.msg import Range
def callback0(data):
rospy.loginfo(rospy.get_caller_id() + "I heard %s", data.range)
sonar0 = int(data.range)
def callback1(data):
rospy.loginfo(rospy.get_caller_id() + "I heard %s", data.range)
sonar1 = data.range
def callback2(data):
rospy.loginfo(rospy.get_caller_id() + "I heard %s", data.range)
sonar2 = data.range
def callback3(data):
rospy.loginfo(rospy.get_caller_id() + "I heard %s", data.range)
sonar3 = data.range
def listener():
# In ROS, nodes are uniquely named. If two nodes with the same
# node are launched, the previous one is kicked off. The
# anonymous=True flag means that rospy will choose a unique
# name for our 'listener' node so that multiple listeners can
# run simultaneously.
rospy.init_node('listener', anonymous=True)
rospy.Subscriber("robot0/sonar_0", Range, callback0)
rospy.Subscriber("robot0/sonar_1", Range, callback1)
rospy.Subscriber("robot0/sonar_2", Range, callback2)
rospy.Subscriber("robot0/sonar_3", Range, callback3)
rospy.loginfo(rospy.get_caller_id() + "I heard %s %s %s %s", sonar0,sonar1,sonar2,sonar3)
# spin() simply keeps python from exiting until this node is stopped
rospy.spin()
if __name__ == '__main__':
sonar0=0
sonar1=0
sonar2=0
sonar3=0
listener()
The line
rospy.loginfo(rospy.get_caller_id() + "I heard %s %s %s %s", sonar0,sonar1,sonar2,sonar3)
is not even executing. But I was able to see the result of individual callback. What is the best way to do this? I need an another function that has to process these 4 values.
Update:
Just like you said I am only getting the
rospy.loginfo(rospy.get_caller_id() + "I heard %s %s %s %s", sonar0,sonar1,sonar2,sonar3)
line executed once. What is the best way to do this? I want to store all the values in an array and process once all the four values are received. Then repeat this process again.
do you get anything displayed - one set of values (probably all zeros) then nothing else, or nothing at all ?
yes. one set of values... All zero