Slow publisher with callback

asked 2022-03-28 07:50:56 -0600

Ariel gravatar image

Hi,

I am publishing images with the usb_cam package on a PC and subscribing to them with an arm-based system, using the following code:

#include "ros/ros.h"
#include "sensor_msgs/Image.h"

class SubscribeAndPublish
{
public:
  SubscribeAndPublish()
  {
    //Topic to publish
    pub_ = n_.advertise<sensor_msgs::Image>("/filteredHW", 1);

    //Topic to subscribe
    sub_ = n_.subscribe("/usb_cam/image_raw", 1, &SubscribeAndPublish::callback, this);
  }

  void callback(const sensor_msgs::Image& input)
  {
    sensor_msgs::Image output;

    output.header = input.header;
    output.encoding = input.encoding;
    output.height = input.height;
    output.width = input.width;
    output.step = input.step;
    output.data = input.data;

    pub_.publish(output);

  }

private:
  ros::NodeHandle n_;
  ros::Publisher pub_;
  ros::Subscriber sub_;
};//End of class SubscribeAndPublish

int main(int argc, char **argv)
{
  //Initiate ROS
  ros::init(argc, argv, "subscribe_and_publish_grayScale");

  //Create an object of class SubscribeAndPublish that will take care of everything
  SubscribeAndPublish SAPObject;

  ros::spin();

  return 0;
}

I am getting a new image on /usb_cam/image_raw at 30 Hz. However, the one that is being publish on the callback (/filteredHW) ends up at 20 Hz just by subscribing and re-publishing it.

Is there a better way to do this, to have the same rate? Note that no processing has been done so far, which would slow things down ever further.

Thank you.

edit retag flag offensive close merge delete

Comments

just by subscribing and re-publishing it

Aren't you doing more than that? input is copied over to output which is going to take some time, especially output.data = input.data. What if you directly publish input?

abhishek47 gravatar image abhishek47  ( 2022-03-29 10:54:53 -0600 )edit

Publishing directly is reduces a bit the frequency but not that much. The processor is running at 666MHz, so copying a 640x480 RGB image should take roughly 1.38 ms. I am only copying with output.data=input=data because when I add the processing the frequency decreases even more. That's why I left only copying for this minimal example. Is there any other, better way to do the copying?

Ariel gravatar image Ariel  ( 2022-03-30 06:29:53 -0600 )edit