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

Revision history [back]

click to hide/show revision 1
initial version

For more complicated cases, here's what I use:

sensor_msgs::PointCloud2ConstPtr pcl;
auto sub = nh.subscribe<sensor_msgs::PointCloud2>("scan", 10, [&](const sensor_msgs::PointCloud2ConstPtr& msg){pcl=msg;});

// publish

#define WAIT_FOR_MESSAGE(msg) \
while (ros::ok() && msg == nullptr) \
{ \
  ros::spinOnce(); \
  ros::WallDuration(0.01).sleep(); \
}

WAIT_FOR_MESSAGE(pcl)

For more complicated cases, here's what I use:

sensor_msgs::PointCloud2ConstPtr pcl;
auto sub = nh.subscribe<sensor_msgs::PointCloud2>("scan", 10, [&](const sensor_msgs::PointCloud2ConstPtr& msg){pcl=msg;});

// publish

#define WAIT_FOR_MESSAGE(msg) \
while (ros::ok() && msg == nullptr) \
{ \
  ros::spinOnce(); \
  ros::WallDuration(0.01).sleep(); \
}

WAIT_FOR_MESSAGE(pcl)

You can of course limit the maximum waiting time.

For more complicated cases, here's what I use:

sensor_msgs::PointCloud2ConstPtr pcl;
auto sub = nh.subscribe<sensor_msgs::PointCloud2>("scan", 10, [&](const sensor_msgs::PointCloud2ConstPtr& msg){pcl=msg;});

// publish

#define WAIT_FOR_MESSAGE(msg) \
{ size_t i = 0;\
  while (ros::ok() && msg == nullptr) nullptr && i < 100) \
 { \
   ros::spinOnce(); \
   ros::WallDuration(0.01).sleep(); \
    ++i;\
  } \
  if (i == 100) ADD_FAILURE(); \
}

WAIT_FOR_MESSAGE(pcl)

You can of course limit the maximum waiting time.