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

How can I access NodeHandle w/ Nodelets?

asked 2018-07-17 11:02:26 -0500

jpde.lopes gravatar image

updated 2018-07-17 19:32:11 -0500

Hi,

I have the following Nodelet onInit:

void OD::onInit()
{
    ros::NodeHandle& private_nh = getPrivateNodeHandle();
    ros::NodeHandle& nh = getNodeHandle();
    NODELET_DEBUG("Initialized the Nodelet (OD)");
    odom_sub        = nh.subscribe("/base_odometry/odom", 10, &OD::odomCallback, this);
}

Where I have declared a nodeHandle and a privateNodeHandle. If I understood correctly, a good rule of thumb is to use the nodeHandle with Topics and the Private one with Parameters.

Well, on the odomCallback, I want check if nh.ok() or if private_nh.ok(), as well as private_nh.param<float>("uav_sphere_scale", scale, 1);, but none of this work and give the error:

error: ‘nh’ was not declared in this scope
(...)
error: ‘private_nh’ was not declared in this scope
private_nh.param<float>("uav_sphere_scale", scale, 1);

On my class, OD, the onInit was declared as public and the odomCallback as protected.

My question is, how can I access, either the NodeHandle, or the PrivateNodeHandle on my callback function?

EDIT: I already tried to declare the NodeHandlers a-priori:

namespace nodelet_obstacle_detector

{ class OD : public nodelet::Nodelet { public: virtual void onInit(); ros::NodeHandle& private_nh; ros::NodeHandle& nh;

But it shows the following error when I compile:

/home/(...)/nodelet_obstacle_detector.h:49:11: error: uninitialized reference member in ‘class nodelet_obstacle_detector::OD’
 class OD : public nodelet::Nodelet
       ^
/home/(...)/nodelet_obstacle_detector.h:54:30: note: ‘ros::NodeHandle& nodelet_obstacle_detector::OD::nh’ should be initialized
         ros::NodeHandle& nh;

Thank you in advance.

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
2

answered 2018-07-17 16:54:18 -0500

kev-the-dev gravatar image
ros::NodeHandle& private_nh = getPrivateNodeHandle();
ros::NodeHandle& nh = getNodeHandle();

There are just local variables, that only exist within onInit(). You need to declare them in your class declaration and set them in this function. Something like this:

class OD
{
  ....
  ros::NodeHandle private_nh;
  ros::NodeHandle nh;
  void onInit()
  {
      ...
      private_nh = getPrivateNodeHandle();
      nh = getNodeHandle();
  }
}
edit flag offensive delete link more

Comments

Hi, I had tried that before, but it when I try to compile it shows the following error:

 /home/(...): note: ‘ros::NodeHandle& nodelet_obstacle_detector::OD::nh’ should be initialized ros::NodeHanndle& nh;
jpde.lopes gravatar image jpde.lopes  ( 2018-07-17 17:09:04 -0500 )edit

So, I think that somehow I can't declare the variables w/out initializing it. I now included this attempt on my question.

jpde.lopes gravatar image jpde.lopes  ( 2018-07-17 17:10:20 -0500 )edit
1

Yeah that's how C++ works, all variables must be initialized in the constructor. So you can either make it not be a reference (no & in the declaration), or just use getPrivateNodeHandle() each time you want to use it. (e.g getPrivateNodeHandle().param<float>("uav_sphere_scale", scale, 1);

kev-the-dev gravatar image kev-the-dev  ( 2018-07-17 17:20:52 -0500 )edit

Understood. Thank you very much.

jpde.lopes gravatar image jpde.lopes  ( 2018-07-17 17:23:18 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2018-07-17 11:02:26 -0500

Seen: 987 times

Last updated: Jul 17 '18