ROS学习笔记37(对URDF文件进行语法分析)

1 读取一个URDF文件

这里首先创建一个功能包,如下所示:

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

 切换到/urdf文件夹,然后创建如下文件:

​mkdir urdf
cd urdf

然后我们标准的文件夹结构应该如下:

/MYROBOT_description
  package.xml
  CMakeLists.txt
  /urdf
  /meshes
  /materials
  /cad

ROS学习笔记37(对URDF文件进行语法分析)_第1张图片

  把刚刚的my_robot.urdf粘贴到刚刚的文件中。

同时,在src下创建一个parser.cpp文件:

#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.

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})

构建您的功能包,并且构建他。 

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

输出应该如下:

ROS学习笔记37(对URDF文件进行语法分析)_第2张图片

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. 

你可能感兴趣的:(ROS)