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

How to execute subscriber callback on some condition?

asked 2017-07-17 15:06:30 -0600

sosoup gravatar image

Hi all,

I am working on a small project in Python and ROS Indigo. To implement a small functionality, I have created a node in which the node has a subscriber (queue size 10) and corresponding callback method. The callback method makes a call to a web-api in turn.

The web-api looses network connection to the server for a brief period and therefore the data is not pushed to the web-api. My understanding is ROS makes a queue for executing callback methods. Because then, I would hold the callback execution as long as there is no established connection to the web-api and execute callback once the connection is back. If the subscribed data would get accumulated, I wont loose them when eventually the callback would get execute.

Please suggest how to achieve this. Thanks

class Message():
    def __init__(self):

        # Create a subscriber for our custom message.
        rospy.Subscriber("message_topic", String, self.callback, queue_size=10)

        # Create the client
        self.wc = WebClient(self.user_name)

    def callback(self, data):
        try:
            # condition to check connection here does not help, still loose messages if no connection
            # if self.wc.connect():
            self.wc.api_call('send_msg', text=data.data)
        except Exception as e:
            print "Problem in connection : ",e
            rospy.sleep(2.0)
            self.make_connection()

# Main function.
if __name__ == '__main__':
    # Initialize the node and name it.
    rospy.init_node('web_message')
    try:
        msg = Message()
     except rospy.ROSInterruptException: pass
edit retag flag offensive close merge delete

Comments

Part of the problem may be that callback() is only executed when data is received. So, if you're not connected, you're not receiving data, and therefore callback() is not executed.

jayess gravatar image jayess  ( 2017-07-17 17:44:16 -0600 )edit

Hi, thanks fo the response. Receiving subscribed data is not dependent on the network.

sosoup gravatar image sosoup  ( 2017-07-18 00:11:27 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted
2

answered 2017-07-18 12:50:41 -0600

lucasw gravatar image

updated 2017-07-18 12:51:08 -0600

Right now your queue_size is 10 meaning you will buffer 10 messages and no more while the sleep wait for the wifi connection is going on. You could increase that size greatly to handle so many seconds of outage.

Or you could buffer the incoming messages yourself (in a data structure you create in the python node) which would give you the ability to take other actions in the case of long outages- e.g. delete every other old message, only keep interesting ones, generate a warning to the user through another channel, or buffer without limit until you get a MemoryError exception and perhaps can recover or crash from it.

edit flag offensive delete link more

Comments

Thank you for your response. This is exactly what I did. Things looks good now. Thank you so much for your answer anyway.

sosoup gravatar image sosoup  ( 2017-07-19 01:14:35 -0600 )edit

Question Tools

2 followers

Stats

Asked: 2017-07-17 14:31:00 -0600

Seen: 1,666 times

Last updated: Jul 18 '17