## For instruction on writing tutorials ## http://www.ros.org/wiki/WritingTutorials #################################### ##FILL ME IN #################################### ## for a custom note with links: ## note = This tutorial assumes you [[urdf/Tutorials/Create your own urdf file|created your own urdf file]] or use one of the existing PR2 urdf files. ## for the canned note of "This tutorial assumes that you have completed the previous tutorials:" just add the links ## note.0= ## descriptive title for the tutorial ## title = Parse a urdf file ## multi-line description to be displayed in search ## description = This tutorial teaches you how to use the urdf parser ## the next tutorial description (optional) ## next = Now that you know how to parse a urdf file, you can ## links to next tutorial (optional) ## next.0.link=[[kdl_parser/Tutorials/Start using the KDL parser|start using the KDL parser]], or ## next.1.link=[[robot_state_publisher/Tutorials/Using the robot state publisher on your own robot|use the robot state publisher on your own robot]] ## what level user is this tutorial for ## level= BeginnerCategory ## keywords = #################################### <> <> <> == Reading a URDF file == This tutorial starts off where the [[urdf/Tutorials/Create your own urdf file|previous one]] ended. You should still have your '''my_robot.urdf''' file with a description of the robot shown below. {{attachment:link.png||height="468px",width="473px"}} Let's first create a package with a dependency on the urdf parser in our sandbox: {{{{#!wiki buildsystem rosbuild {{{ $ roscd $ cd sandbox $ roscreate-pkg testbot_description urdf $ roscd testbot_description $ rosmake }}} }}}} {{{{#!wiki buildsystem catkin {{{ $ 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: {{{ /MYROBOT_description package.xml CMakeLists.txt /urdf /meshes /materials /cad }}} 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''': {{{#!cplusplus block=parser #include #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"); return 0; } }}} 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. {{{{#!wiki buildsystem rosbuild 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 }}} }}}} {{{{#!wiki buildsystem catkin 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 $ ./parser 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 [[http://www.ros.org/doc/api/urdf/html/|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 [[https://github.com/ros-visualization/rviz/|RViz]], in the file `src/rviz/robot/robot.cpp`. Note: code is in https://github.com/ros/urdfdom_headers repository, reference it for Code API. ## AUTOGENERATED DO NOT DELETE ## TutorialCategory ## FILL IN THE STACK TUTORIAL CATEGORY HERE ## LearningCategory