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

@jdlangs is correct that shared_from_this() is the correct way to get a shared pointer to the node.

Besides the compilation issue, your code looks troublesome. rclcpp::spin() is a blocking function, so this->loop() will only be called once. You could try using rclcpp::spin_some() instead, but I suggest that you call rclcpp::spin() from outside your node, for example in a main function like this:

int main(int argc, char ** argv)
{
  rclcpp::init(argc, argv);

  auto odom_node = std::make_shared<OdomNode>();
  rclcpp::spin(odom_node);

  rclcpp::shutdown();
  return 0;
}

And perhaps start a timer for your update method, for example in the constructor:

OdomNode::OdomNode(rclcpp::Duration & loop_rate)
{
  timer_ = this->create_wall_timer(loop_rate.to_chrono(), std::bind(&OdomNode::loop, this));
}

This is the pattern you'll find in many of the examples and demos. This helps make your node more flexible. For example, it leaves open the possibility to compose your node with others into a single process, or easily change the type of executor or callback group.

@jdlangs is correct that shared_from_this() is the correct way to get a shared pointer to the node.

Besides the compilation issue, your code looks troublesome. rclcpp::spin() is a blocking function, so this->loop() will only be called once. You could try using rclcpp::spin_some() instead, but I suggest that you call rclcpp::spin() from outside your node, for example in a main function like this:

int main(int argc, char ** argv)
{
  rclcpp::init(argc, argv);

  auto odom_node = std::make_shared<OdomNode>();
  rclcpp::spin(odom_node);

  rclcpp::shutdown();
  return 0;
}

And perhaps start a timer for to replace your update method, for example in the constructor:

OdomNode::OdomNode(rclcpp::Duration & loop_rate)
{
  timer_ = this->create_wall_timer(loop_rate.to_chrono(), std::bind(&OdomNode::loop, this));
}

This is the pattern you'll find in many of the examples and demos. This helps make your node more flexible. For example, it leaves open the possibility to compose your node with others into a single process, or easily change the type of executor or callback group.