Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Access rosparam from node handle passed by reference

Hi all, I have a ROS node which inits and constructs a NodeHandle object. The node is run from a launch file that loads a YAML file into the parameter server.

From the main I can access correctly to the rosparam I want to load:

int main(int argc, char**argv) {
    ros::init(argc, argv, "my_node");
    ros::NodeHandle nh;
    bool param;
    // this returns true
    nh.getParam("/my_param", param);
}

But if I have a class that takes this nodehandle as reference and tries to access the same parameter it does not work:

class MyClass {
public:
    MyClass(ros::NodeHandle& nh) : nh_(nh) {
        bool param;
        // this returns false            
        nh_.getParam("/my_param", param);
    }
private:
        ros::NodeHandle nh_;
};

int main(int argc, char**argv) {
    ros::init(argc, argv, "my_node");
    ros::NodeHandle nh;
    // call inside constructor returns false
    MyClass my_class(nh);
    bool param; 
    // this returns true       
    nh.getParam("/my_param", param);
}

Why is that?

The object is passed by reference so it's the same object.

How can I get the param from within the class?

Thanks.

Access rosparam from node handle passed by reference

Hi all, I have a ROS node which inits and constructs a NodeHandle object. The node is run from a launch file that loads a YAML file into the parameter server.

From the main I can access correctly to the rosparam I want to load:

int main(int argc, char**argv) {
    ros::init(argc, argv, "my_node");
    ros::NodeHandle nh;
    bool param;
    // this returns true
    nh.getParam("/my_param", param);
}

But if I have a class that takes this nodehandle as reference and tries to access the same parameter it does not work:

class MyClass {
public:
    MyClass(ros::NodeHandle& nh) : nh_(nh) {
        bool param;
        // this returns false            
        nh_.getParam("/my_param", param);
    }
private:
        ros::NodeHandle ros::NodeHandle& nh_;
};

int main(int argc, char**argv) {
    ros::init(argc, argv, "my_node");
    ros::NodeHandle nh;
    // call inside constructor returns false
    MyClass my_class(nh);
    bool param; 
    // this returns true       
    nh.getParam("/my_param", param);
}

Why is that?

The object is passed by reference so it's the same object.

How can I get the param from within the class?

Thanks.