#includeint main(int argc, char** argv) { ros::init(argc, argv, "my_node", ros::init_options::AnonymousName); ros::NodeHandle nh; std::string node_namespace = nh.getNamespace(); ROS_INFO_STREAM("Node Namespace: " << node_namespace); return 0; }
#includeThis example also creates a ROS node with name "my_node", but it explicitly specifies a parent namespace of "/my_parent_node". The getNamespace function is used to retrieve the full namespace of the node ("/my_parent_node/my_node") and it is then printed to the console using ROS_INFO_STREAM. This code should also be saved in a .cpp file and then compiled as a ROS package using the Catkin build system. Package Library: ROSint main(int argc, char** argv) { ros::init(argc, argv, "my_node"); ros::NodeHandle nh("/my_parent_node/my_node"); std::string node_namespace = nh.getNamespace(); ROS_INFO_STREAM("Node Namespace: " << node_namespace); return 0; }