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

As far as I am aware, ros 2 doesn't have a waitForMessage function like in ros1, that being said one can write a sort of workaround like

// waiting for first gps message to set the origin
RCLCPP_INFO(this->get_logger(), "Waiting for first gps message");
rclcpp::WaitSet wait_set;
wait_set.add_subscription(sub_);
auto ret = wait_set.wait(std::chrono::seconds(100));
if (ret.kind() == rclcpp::WaitResultKind::Ready) {
  sensor_msgs::msg::NavSatFix msg;
  rclcpp::MessageInfo info;
  auto ret_take = sub_->take(msg, info);
  if (ret_take) {
    RCLCPP_INFO(this->get_logger(), "heard gps, initializing offsets");
    /* do stuff */
  } else {
    RCLCPP_ERROR(this->get_logger(), "no message recieved from gps");
  }
} else {
  RCLCPP_ERROR(this->get_logger(), "couldn't wait for gps message");
  return;
}

Specifically this one I wrote to be particular to a gps message, but what it does in the /* do stuff */ section is up to you.

Note: I wrote/use this in a ros2 foxy pkg.

As far as I am aware, ros 2 doesn't have a waitForMessage function like in ros1, that being said one can write a sort of workaround like

// waiting for first gps message to set the origin
RCLCPP_INFO(this->get_logger(), "Waiting for first gps message");
rclcpp::WaitSet wait_set;
wait_set.add_subscription(sub_);
auto ret = wait_set.wait(std::chrono::seconds(100));
if (ret.kind() == rclcpp::WaitResultKind::Ready) {
  sensor_msgs::msg::NavSatFix msg;
msg; /* CHANGE AS RELEVANT */
  rclcpp::MessageInfo info;
  auto ret_take = sub_->take(msg, info);
  if (ret_take) {
    RCLCPP_INFO(this->get_logger(), "heard gps, initializing offsets");
    /* do stuff */
  } else {
    RCLCPP_ERROR(this->get_logger(), "no message recieved from gps");
  }
} else {
  RCLCPP_ERROR(this->get_logger(), "couldn't wait for gps message");
  return;
}

Where sub_ is some previously defined subscription member variable of the node class. Specifically this one code I wrote to be particular to a gps message, but what it does in the /* do stuff */ section is up to you.

Note: I wrote/use this in a ros2 foxy pkg.

As far as I am aware, ros 2 doesn't have a waitForMessage function like in ros1, that being said one can write a sort of workaround like

// waiting for first gps message to set the origin
RCLCPP_INFO(this->get_logger(), "Waiting for first gps message");
rclcpp::WaitSet wait_set;
wait_set.add_subscription(sub_);
auto ret = wait_set.wait(std::chrono::seconds(100));
if (ret.kind() == rclcpp::WaitResultKind::Ready) {
  sensor_msgs::msg::NavSatFix msg; /* CHANGE AS RELEVANT */
// change as relevant
  rclcpp::MessageInfo info;
  auto ret_take = sub_->take(msg, info);
  if (ret_take) {
    RCLCPP_INFO(this->get_logger(), "heard gps, initializing offsets");
    /* do stuff */
  } else {
    RCLCPP_ERROR(this->get_logger(), "no message recieved from gps");
  }
} else {
  RCLCPP_ERROR(this->get_logger(), "couldn't wait for gps message");
  return;
}

Where sub_ is some previously defined subscription member variable of the node class. Specifically this code I wrote to be particular to a gps message, but what it does in the /* do stuff */ section is up to you.

Note: I wrote/use this in a ros2 foxy pkg.

As far as I am aware, ros 2 doesn't have a waitForMessage function like in ros1, that being said one can write a sort of workaround like

// waiting for first gps message to set the origin
RCLCPP_INFO(this->get_logger(), "Waiting for first gps message");
rclcpp::WaitSet wait_set;
wait_set.add_subscription(sub_);
auto ret = wait_set.wait(std::chrono::seconds(100));
if (ret.kind() == rclcpp::WaitResultKind::Ready) {
  sensor_msgs::msg::NavSatFix msg; // change as relevant
  rclcpp::MessageInfo info;
  auto ret_take = sub_->take(msg, info);
  if (ret_take) {
    RCLCPP_INFO(this->get_logger(), "heard gps, initializing offsets");
    /* do stuff */
  } else {
    RCLCPP_ERROR(this->get_logger(), "no message recieved from gps");
  }
} else {
  RCLCPP_ERROR(this->get_logger(), "couldn't wait for gps message");
  return;
}

Where sub_ is some previously defined subscription member variable of the node class. I figure you can wait for the messages in the class constructor before starting the timer loop.

Specifically this code I wrote to be particular to a gps message, but what it does in the /* do stuff */ section is up to you.

Note: I wrote/use wrote/used this in a ros2 foxy pkg.