Skip to content

Instantly share code, notes, and snippets.

@AlexanderFabisch
Created December 17, 2015 17:15
Show Gist options
  • Save AlexanderFabisch/1451a788b6079ff13582 to your computer and use it in GitHub Desktop.
Save AlexanderFabisch/1451a788b6079ff13582 to your computer and use it in GitHub Desktop.
cmake_minimum_required(VERSION 2.8.3)
project(kdl_parser_test)
find_package(urdfdom_headers REQUIRED)
include_directories(include ${urdfdom_headers_INCLUDE_DIRS})
find_package(urdfdom REQUIRED)
include_directories(include ${urdfdom_INCLUDE_DIRS})
link_directories(${urdfdom_LIBRARY_DIRS})
find_package(console_bridge REQUIRED)
include_directories(include ${console_bridge_INCLUDE_DIRS})
link_directories(${console_bridge_LIBRARY_DIRS})
find_package(orocos_kdl REQUIRED)
include_directories(include ${orocos_kdl_INCLUDE_DIRS})
link_directories(${orocos_kdl_LIBRARY_DIRS})
find_package(kdl_parser REQUIRED)
include_directories(include ${kdl_parser_INCLUDE_DIRS})
link_directories(${kdl_parser_LIBRARY_DIRS})
find_library(KDL_LIBRARY REQUIRED NAMES orocos-kdl HINTS ${orocos_kdl_LIBRARY_DIRS})
add_executable(kdl_parser_test main.cpp)
target_link_libraries(kdl_parser_test
${orocos_kdl_LIBRARIES} ${urdfdom_LIBRARIES} ${kdl_parser_LIBRARIES})
/*********************************************************************
* Software License Agreement (BSD License)
*
* Copyright (c) 2008, Willow Garage, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of the Willow Garage nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*********************************************************************/
/* Author: Wim Meeussen */
#include <kdl_parser/kdl_parser.hpp>
#include <urdf_parser/urdf_parser.h>
#include <kdl/chainfksolverpos_recursive.hpp>
#include <kdl/frames_io.hpp>
#include <urdf_model/model.h>
#include <iostream>
using namespace KDL;
using namespace std;
using namespace urdf;
void printLink(const SegmentMap::const_iterator& link, const std::string& prefix)
{
cout << prefix << "- Segment " << GetTreeElementSegment(link->second).getName() << " has "
<< GetTreeElementChildren(link->second).size() << " children" << endl;
for (unsigned int i=0; i < GetTreeElementChildren(link->second).size(); i++)
printLink(GetTreeElementChildren(link->second)[i], prefix + " ");
}
int main(int argc, char** argv)
{
if (argc < 2){
std::cerr << "Expect xml file to parse" << std::endl;
return -1;
}
boost::shared_ptr<urdf::ModelInterface> robot_model = urdf::parseURDFFile(argv[1]);
Tree my_tree;
if (!kdl_parser::treeFromUrdfModel(robot_model, my_tree))
{cerr << "Could not extract kdl tree" << endl; return false;}
// walk through tree
cout << " ======================================" << endl;
cout << " Tree has " << my_tree.getNrOfSegments() << " link(s) and a root link" << endl;
cout << " ======================================" << endl;
SegmentMap::const_iterator root = my_tree.getRootSegment();
printLink(root, "");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment