| Note: This tutorial assumes you created your own urdf file or use one of the existing PR2 urdf files.. |
Parse a urdf file
Description: This tutorial teaches you how to use the urdf parserTutorial 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
Contents
Reading a URDF file
This tutorial starts off where the previous one ended. You should still have your my_urdf.xml file with a description of the robot shown below.
Let's first create a package with a dependency on the urdf parser:
$ roscd sandbox $ roscreate-pkg learning_urdf urdf $ roscd learning_urdf $ rosmake
Now first copy your my_urdf.xml file to the package we just created:
$ cp /path/to/.../my_urdf.xml .
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"
3
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];
11
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 11-12:
11 urdf::Model model;
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_urdf.xml
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.






