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

Retrieve robot name

asked 2017-10-03 11:34:58 -0500

muttidrhummer gravatar image

Is there a way to retrieve robot name from the urdf in c++? Or any other way to get it? Thanks

edit retag flag offensive close merge delete


any reason you need the name? There's got to be an easier way, but you could read the robot description from the parameter server then parse the xml string. If you know you're going to need the name why not load the name into the parameter server in your launch file?

andymcevoy gravatar image andymcevoy  ( 2017-10-05 23:53:12 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2017-10-16 11:41:48 -0500

Hi @muttidrhummer!

I have created a video that answer exactly this question.

Summarizing, this can be accomplished by loading a URDF file, parsing it and calling the getName() method of the C++ Urdf model.

To see how to load and parse a URDF file, you can follow the Parse a urdf file tutorial.

After you create your parser.cpp, before the return 0; you should add

ROS_INFO("The robot name is: %s", model.getName().c_str());

to print the name of the robot on the URDF file, as we can see on the code below:

#include <urdf/model.h>
#include "ros/ros.h"

int main(int argc, char** argv){
  ros::init(argc, argv, "my_parser");
  if (argc != 2){
    ROS_ERROR("Need a urdf file as argument");
    return -1;
  std::string urdf_file = argv[1];

  urdf::Model model;
  if (!model.initFile(urdf_file)){
    ROS_ERROR("Failed to parse urdf file");
    return -1;
  ROS_INFO("Successfully parsed urdf file");
  ROS_INFO("The robot name is: %s", model.getName().c_str());
  return 0;

After compile the code and generate the executable called parser following the Parse a urdf file tutorial, you can call it with the path of the URDF file as argument.

Something like: rosrun your_package_name parser /path/to/your/file.urdf

If you still have any doubt, just check the video.

edit flag offensive delete link more


Thanks, works like a charm.

muttidrhummer gravatar image muttidrhummer  ( 2017-10-18 04:07:50 -0500 )edit


You're Welcome.

Ruben Alves gravatar image Ruben Alves  ( 2017-10-18 17:20:26 -0500 )edit

Question Tools



Asked: 2017-10-03 11:34:58 -0500

Seen: 8,502 times

Last updated: Oct 16 '17