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

Access rosparam from node handle passed by reference

asked 2018-11-30 09:08:20 -0500

mcamurri gravatar image

updated 2018-11-30 09:13:44 -0500

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.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2018-11-30 09:36:46 -0500

mcamurri gravatar image

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

edit flag offensive delete link more

Question Tools

Stats

Asked: 2018-11-30 09:08:20 -0500

Seen: 700 times

Last updated: Nov 30 '18