Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

While @bvbdort's answer will probably work, there is a convenience method that essentially does all that for you in a single line: ros::topic:waitForMessage(..).

It also supports specifying timeouts, which NodeHandle to use and some other niceties.

While @bvbdort's answer will probably work, there is a convenience method that essentially does all that for you in a single line: ros::topic:waitForMessage(..).

It also supports specifying timeouts, which NodeHandle to use and some other niceties.


Edit:

#include <ros/ros.h>
#include <sensor_msgs/NavSatFix.h>
#include <geometry_msgs/PointStamped.h>

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

  sensor_msgs::NavSatFix navsatfix_msg = ros::topic::waitForMessage("/POSITIONS");

  geometry_msgs::PointStamped point;
  point.point.x = navsatfix_msg->latitude;
  point.point.y = navsatfix_msg->longitude;
  point.point.z = navsatfix_msg->altitude;

  // do something with 'point'
  ...
}

While @bvbdort's answer will probably work, there is a convenience method that essentially does all that for you in a single line: ros::topic:waitForMessage(..).

It also supports specifying timeouts, which NodeHandle to use and some other niceties.


Edit:

#include <ros/ros.h>
#include <sensor_msgs/NavSatFix.h>
#include <geometry_msgs/PointStamped.h>

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

  sensor_msgs::NavSatFix sensor_msgs::NavSatFixConstPtr navsatfix_msg = ros::topic::waitForMessage("/POSITIONS");

  geometry_msgs::PointStamped point;
  point.point.x = navsatfix_msg->latitude;
  point.point.y = navsatfix_msg->longitude;
  point.point.z = navsatfix_msg->altitude;

  // do something with 'point'
  ...
}