0
$\begingroup$

Rosanswers logo

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.


Originally posted by mcamurri on ROS Answers with karma: 102 on 2018-11-30

Post score: 0

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

The getParam calls were surrounded by ROS_ASSERT (not shown in the simplified example) so they were never called. Closing.


Originally posted by mcamurri with karma: 102 on 2018-11-30

This answer was ACCEPTED on the original site

Post score: 0

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.