0
$\begingroup$

Rosanswers logo

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.


Originally posted by Ananthakrishnan on ROS Answers with karma: 63 on 2015-10-18

Post score: 5


Original comments

Comment by nickw on 2015-10-18:
do you get anything displayed - one set of values (probably all zeros) then nothing else, or nothing at all ?

Comment by Ananthakrishnan on 2015-10-18:
yes. one set of values... All zero

$\endgroup$

2 Answers 2

0
$\begingroup$

Rosanswers logo

You need to use message_filters.TimeSynchronizer for this. Please look at the example code in the documentation: http://wiki.ros.org/message_filters#Example_.28Python.29-1

Please note that there is also the ApproximateTime synchronization, which is often what you want if the sensor time stamps are not exactly in sync.


Originally posted by Dimitri Schachmann with karma: 789 on 2015-10-18

This answer was ACCEPTED on the original site

Post score: 10


Original comments

Comment by Ananthakrishnan on 2015-10-18:
Thanks... That worked. I was able to use ApproximateTime synchronization. That solved my issue. But I am not able to use message_filter with cmd_vel bcz it doesn't have any header. Is there any workaround 4 tat? Now am chckng t value of cmd_vel inside the call back function using wait_for_message

Comment by Dimitri Schachmann on 2015-10-18:
a solution would be to make the data source publish messages with a header.

Comment by Dimitri Schachmann on 2015-10-18:
a workaround could be to use http://wiki.ros.org/topic_tools/transform to subscribe to your sensor data and output a new message type with a header. Than your node subscibes to that. You probably have to define that new message type yourself, unless it already exists.

Comment by Ananthakrishnan on 2015-10-19:
I think its easy to use wait_for_message inside the callback function rather than going for republishing the topic. It solved my issue. But thanks for the reply...

$\endgroup$
0
$\begingroup$

Rosanswers logo

I just ran your code, don't have a bunch of nodes publishing to the topics, but I get one line of 4 zeros as I would expect.

The line you say never runs does run, but onlyruns once at the beginning, on the next line it reaches ros.spin(). It stays at that point. The only activity you will then see are from the callbacks being called when messages come in on a topic, so you would see individual values being printed out as they come in on each of the topics.


Originally posted by nickw with karma: 1504 on 2015-10-18

This answer was NOT ACCEPTED on the original site

Post score: 2

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.