Note: This tutorial assumes you created your own urdf file or use one of the existing PR2 urdf files..
(!) Please ask about problems and questions regarding this tutorial on Don't forget to include in your question the link to this page, the versions of your OS & ROS, and also add appropriate tags.

Parse a urdf file

Description: This tutorial teaches you how to use the urdf parser

Tutorial Level: BEGINNER

Next Tutorial: Now that you know how to parse a urdf file, you can start using the KDL parser, or use the robot state publisher on your own robot

Reading a URDF file

This tutorial starts off where the previous one ended. You should still have your my_robot.urdf file with a description of the robot shown below.


Let's first create a package with a dependency on the urdf parser in our sandbox:

  $ roscd
  $ cd sandbox
  $ roscreate-pkg testbot_description urdf
  $ roscd testbot_description
  $ rosmake

  $ cd ~/catkin_ws/src
  $ catkin_create_pkg testbot_description urdf
  $ cd testbot_description

Now create a /urdf folder to store the urdf file we just created:

mkdir urdf
cd urdf

This follows the convention of always storing your robot's URDF file in a ROS package named MYROBOT_description and within a subfolder named /urdf. Other standard subfolders of your robot's description package include /meshes, /media and /cad, like so:


Next, copy your my_robot.urdf file to the package and folder we just created:

  $ cp /path/to/.../testbot_description/urdf/my_robot.urdf .

Create a folder src/ and fire up your editor to create a file called src/parser.cpp:

   1 #include <urdf/model.h>
   2 #include "ros/ros.h"
   4 int main(int argc, char** argv){
   5   ros::init(argc, argv, "my_parser");
   6   if (argc != 2){
   7     ROS_ERROR("Need a urdf file as argument");
   8     return -1;
   9   }
  10   std::string urdf_file = argv[1];
  12   urdf::Model model;
  13   if (!model.initFile(urdf_file)){
  14     ROS_ERROR("Failed to parse urdf file");
  15     return -1;
  16   }
  17   ROS_INFO("Successfully parsed urdf file");
  18   return 0;
  19 }

The real action happens in lines 12-13. Here we create a parser object, and initialize it from a file by providing the filename. The initFile method returns true if the URDF file was parsed successfully.

Now let's try to run this code. First add the following line to your CMakeList.txt file:

  rosbuild_add_executable(parser src/parser.cpp)

build your package, and run it.

$ make
$ ./bin/parser my_robot.urdf

Now let's try to run this code. First add the following lines to your CMakeList.txt file:

 add_executable(parser src/parser.cpp)
 target_link_libraries(parser ${catkin_LIBRARIES})

build your package, and run it.

$ cd ~/catkin_ws   
$ catkin_make
$ .<path>/parser <path>my_robot.urdf
# Example: ./devel/lib/testbot_description/parser ./src/testbot_description/urdf/my_robot.urdf

The output should look something like this:

  [ INFO] 1254520129.560927000: Successfully parsed urdf file

Now take a look at the code API to see how to start using the URDF model you just created. A good example of the URDF model class in action is Robot::load() in RViz, in the file src/rviz/robot/robot.cpp.

Note: code is in repository, reference it for Code API.

Wiki: urdf/Tutorials/Parse a urdf file (last edited 2017-09-23 18:23:10 by JanKoniarik)