Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Call multiple subscriber periodically at the same time

Hello. I would like to write a program that calls multiple subscribers periodically at the same time. Therefore, at the beginning, I wrote the program as shown in program1, but only rewritten main() like program2. In the program of program1, ros::spin() called three subscribers separately. So I decided to call them periodically using while. However, even in program2, they were called differently. Moreover, in program1, the publisher written in depthCb which was being executed also is not executed. How can I write the program to call multiple subscribers periodically at the same time? Thank you.

program1

#include <ros/ros.h>
...

void messageCallback(const sensor_msgs::JointState::ConstPtr& msg3)
{
   ...
}

void imageCb(const sensor_msgs::ImageConstPtr& msg2)
{
   ...
}

void depthCb( const sensor_msgs::ImageConstPtr& msg1)
{
   ...
   ros::Publisher pub = n.advertise<PointCloud> ("points2", 1);
   PointCloud::Ptr cloud (new PointCloud);
   cloud->header.frame_id = "/base";
   pcl_conversions::toPCL(ros::Time::now(), cloud->header.stamp);
   ...
   cloud->points.push_back (cloud->points[0]);
   pcl_conversions::toPCL(ros::Time::now(), cloud->header.stamp);
   pub.publish (cloud);
}

int main( int argc, char* argv[])
{
   ros::init( argc, argv, "depth_viewer" );
   ros::NodeHandle n;

   ros::Subscriber joint = n.subscribe("/hp3j/joint_states", 1000, messageCallback);
   ros::Subscriber color = n.subscribe("/pico_flexx_link_ir/image_raw", 1000, imageCb);
   ros::Subscriber depth = n.subscribe("/pico_flexx_link/depth_registered/image_raw", 3, &depthCb);

   ros::spin();
   return 0;
}

program2

int main(argc, char* argv[])
{
   ros::init( argc, argv, "depth_viewer" );
   ros::NodeHandle n;

   ros::Subscriber joint = n.subscribe("/hp3j/joint_states", 1000, messageCallback);
   ros::Subscriber color = n.subscribe("/pico_flexx_link_ir/image_raw", 1000, imageCb);
   ros::Subscriber depth = n.subscribe("/pico_flexx_link/depth_registered/image_raw", 3, &depthCb);

   ros::Rate loop_rate(0.1);
   while (ros::ok()) {
      ros::spinOnce();
      loop_rate.sleep();
   }

   return 0;
}