Different publishing times for certain array sizes

asked 2017-01-29 14:21:22 -0600

VitorGuizilini gravatar image

updated 2017-02-03 10:13:32 -0600

Hi,

I am trying to publish arrays using ROS, and it works fine, however there seems to be a large difference in publishing times for certain array sizes. For example, if I am publishing a 19200-dimensional array it takes around 0.001 seconds for the subscriber to receive the data, but if I decrease this array to a 6400-dimensional one it goes to around 0.03 seconds. This jump from 0.001 to 0.03 happens with apparently random array sizes, and I cannot guarantee that my application will use an array size that is magically faster.

I am using Ubuntu 16.04 and ROS Kinetic, if that makes any different. This behavior shows up with communicating C++ to C++, Python to Python and C++ to Python. Something is definitely wrong, but I cannot pinpoint the reason, so please help me. Here is a Minimal Working Example for both ends:

Server (receives Actions and returns Observations)

import rospy
from rospy_tutorials.msg import Floats
import numpy as np

class RosServer():

    def __init__( self ):

        self.cnt = 0

        rospy.init_node( 'Server' , anonymous = True )
        rospy.Subscriber( '/action' , Floats , self.callback_action )
        self.pub_observation = rospy.Publisher( '/observation' , Floats , queue_size = 1 )
        self.t1 = rospy.Time().now()

        while True:
            rospy.sleep( 1 )

    def callback_action( self , msg ):

        observation = Floats()
        observation.data = np.ones( ( 64000 ) )

        self.cnt += 1
        print( 'SERVER RECEIVED' , self.cnt )
        self.pub_observation.publish( observation )

        t2 = rospy.Time().now()
        print( 1.0 / ( t2 - self.t1 ).to_sec() )
        self.t1 = t2;

if __name__ == '__main__':

    server = RosServer()

Client (receives Observations and returns Actions)

import rospy
from rospy_tutorials.msg import Floats

class RosClient():

    def __init__( self ):

        self.cnt = 0

        rospy.init_node( 'Client' , anonymous = True )
        rospy.Subscriber( '/observation' , Floats , self.callback_observation )
        self.pub_action = rospy.Publisher( '/action' , Floats , queue_size = 1 )

        rospy.sleep ( 1 )
        self.pub_action.publish( [ 0 , 1 , 0 ] )

        while True:
            rospy.sleep( 1 )

    def callback_observation( self , msg ):

        action = Floats()
        action.data = [ 1 , 2 , 3 ]

        self.cnt += 1
        print( 'CLIENT RECEIVED' , self.cnt )
        self.pub_action.publish( action )

if __name__ == '__main__':

    server = RosClient()

If I set the numpy array size to 64000 (as it is), this example runs at about 150 Hz, and if I switch it to 6400 (only change) it goes down to 25 Hz. Any ideas as to why?

UPDATE:

I pinpointed two values in which there is this transition:

  • 128 (High Frequency) to 129 (Low Frequency)
  • 16368 (Low Frequency) to 16369 (High Frequency)

Maybe there are other transitions, but I didn't check... Anyway, what makes these numbers special?

Thanks.

edit retag flag offensive close merge delete

Comments

1

I think in these cases, an MWE would go a long way in helping people help you.

gvdhoorn gravatar image gvdhoorn  ( 2017-01-31 14:36:50 -0600 )edit

Thanks for the tip, I added some code that reproduces this issue, hopefully that will be enough.

VitorGuizilini gravatar image VitorGuizilini  ( 2017-02-02 20:41:11 -0600 )edit