Ask Your Question

How to recieve information from a subscriber withouth executing a callback

asked 2018-01-16 16:43:53 -0500

JollyRogers gravatar image

updated 2018-01-16 16:59:05 -0500

jayess gravatar image

I have the following code:

#!/usr/bin/env python
import rospy
from std_msgs.msg import  Float64
from random import randint
from time import sleep
from std_msgs.msg import Int64

def counter(msg):


pub = rospy.Publisher('your_topic', Float64, queue_size=10)
while not rospy.is_shutdown():
        rnd = 1.0
        print "the number is " + str(rnd)
        rnd = 2.0
        print "the number is " + str(rnd)
        sub = rospy.Subscriber('I_know', Int64, counter, queue_size=100)


I am using this code to receive some signals from Arduino. The signals are sended to the topic "I_know". Although the code is running properly, I wonder if there is a way to get the signals from the topic without executing the callback? Just assigning the values from the topic to a variable from the main program.

edit retag flag offensive close merge delete


Why do you want to do this?

jayess gravatar image jayess  ( 2018-01-16 17:01:57 -0500 )edit

I am trying to make a LED blink and to count the number of times that it does. If the number of blinks reaches a determine value, I want the blinking to stop.

Also, I am wondering if it is possible.

JollyRogers gravatar image JollyRogers  ( 2018-01-16 20:11:48 -0500 )edit

2 Answers

Sort by » oldest newest most voted

answered 2018-01-16 17:01:24 -0500

jayess gravatar image

Not that I'm aware of.

By the way, you really shouldn't be creating a new subscriber during each iteration of your loop. You should do it before the loop.

edit flag offensive delete link more


Thank you, I will correct it.

JollyRogers gravatar image JollyRogers  ( 2018-01-16 20:00:49 -0500 )edit

You should also use rospy's sleep() function vs the one from the time module. If you simulate you'll encounter issues with how you're currently sleeping

jayess gravatar image jayess  ( 2018-01-16 20:04:07 -0500 )edit

@JollyRogers sorry, I accidentally deleted your comment (I'm on a phone). You'd want to actually use a Rate object's sleep method.

jayess gravatar image jayess  ( 2018-01-16 22:40:10 -0500 )edit

answered 2018-03-05 04:33:37 -0500

updated 2018-03-05 04:39:27 -0500

The function you're looking for is wait_for_message() :

However, what wait_for_message() does internally is that it creates a subscriber, waits for a single message, then unsubscribes from the topic again. You should definitely not use it in a loop, like in your example, for two reasons:

  1. Subscribing and unsubscribing from a topic at a high rate puts unnecessary burden on the rosmaster and on all publishers to that topic. It also makes it probable that your subscription doesn't show up in introspection tools (rostopic, rqt_graph). This is just not how ROS is meant to be used.
  2. You will loose messages on the topic that are sent between the point where your node unsubscribes from and then re-subscribes to the topic.

You should only ever use wait_for_message() if you really only care about a single message from a topic once in a while, not to clean up your code.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2018-01-16 16:43:53 -0500

Seen: 98 times

Last updated: Mar 05 '18