test_parser.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002 * Software License Agreement (BSD License)
00003 *
00004 *  Copyright (c) 2011, Willow Garage, Inc.
00005 *  All rights reserved.
00006 *
00007 *  Redistribution and use in source and binary forms, with or without
00008 *  modification, are permitted provided that the following conditions
00009 *  are met:
00010 *
00011 *   * Redistributions of source code must retain the above copyright
00012 *     notice, this list of conditions and the following disclaimer.
00013 *   * Redistributions in binary form must reproduce the above
00014 *     copyright notice, this list of conditions and the following
00015 *     disclaimer in the documentation and/or other materials provided
00016 *     with the distribution.
00017 *   * Neither the name of the Willow Garage nor the names of its
00018 *     contributors may be used to endorse or promote products derived
00019 *     from this software without specific prior written permission.
00020 *
00021 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032 *  POSSIBILITY OF SUCH DAMAGE.
00033 *********************************************************************/
00034 
00035 /* Author: Ioan Sucan */
00036 
00037 #include <srdfdom/model.h>
00038 #include <urdf_parser/urdf_parser.h>
00039 #include <fstream>
00040 #include <stdexcept>
00041 #include <boost/lexical_cast.hpp>
00042 
00043 #define EXPECT_TRUE(arg) if (!(arg)) throw std::runtime_error("Assertion failed at line " + boost::lexical_cast<std::string>(__LINE__))
00044 
00045 #ifndef TEST_RESOURCE_LOCATION
00046 #  define TEST_RESOURCE_LOCATION "."
00047 #endif
00048 
00049 boost::shared_ptr<urdf::ModelInterface> loadURDF(const std::string& filename)
00050 {
00051   // get the entire file
00052   std::string xml_string;
00053   std::fstream xml_file(filename.c_str(), std::fstream::in);
00054   if (xml_file.is_open())
00055   {
00056     while (xml_file.good())
00057     {
00058       std::string line;
00059       std::getline( xml_file, line);
00060       xml_string += (line + "\n");
00061     }
00062     xml_file.close();
00063     return urdf::parseURDF(xml_string);
00064   }
00065   else
00066   {
00067     throw std::runtime_error("Could not open file " + filename + " for parsing.");
00068     return boost::shared_ptr<urdf::ModelInterface>();
00069   }
00070 }
00071 
00072 void testSimple(void)
00073 {
00074   srdf::Model s;
00075   boost::shared_ptr<urdf::ModelInterface> u = loadURDF(std::string(TEST_RESOURCE_LOCATION) + "/pr2_desc.urdf");
00076   EXPECT_TRUE(u != NULL);
00077   
00078   EXPECT_TRUE(s.initFile(*u, std::string(TEST_RESOURCE_LOCATION) + "/pr2_desc.1.srdf"));
00079   EXPECT_TRUE(s.getVirtualJoints().size() == 0);
00080   EXPECT_TRUE(s.getGroups().size() == 0);
00081   EXPECT_TRUE(s.getGroupStates().size() == 0);
00082   EXPECT_TRUE(s.getDisabledCollisionPairs().empty());
00083   EXPECT_TRUE(s.getEndEffectors().size() == 0);
00084   
00085   EXPECT_TRUE(s.initFile(*u, std::string(TEST_RESOURCE_LOCATION) + "/pr2_desc.2.srdf"));
00086   EXPECT_TRUE(s.getVirtualJoints().size() == 1);
00087   EXPECT_TRUE(s.getGroups().size() == 1);
00088   EXPECT_TRUE(s.getGroupStates().size() == 0);
00089   EXPECT_TRUE(s.getDisabledCollisionPairs().empty());
00090   EXPECT_TRUE(s.getEndEffectors().size() == 0);
00091   
00092   EXPECT_TRUE(s.initFile(*u, std::string(TEST_RESOURCE_LOCATION) + "/pr2_desc.1.srdf"));
00093   EXPECT_TRUE(s.getVirtualJoints().size() == 0);
00094   EXPECT_TRUE(s.getGroups().size() == 0);
00095   EXPECT_TRUE(s.getGroupStates().size() == 0);
00096   EXPECT_TRUE(s.getDisabledCollisionPairs().empty());
00097   EXPECT_TRUE(s.getEndEffectors().size() == 0);
00098 }
00099 
00100 void testComplex(void)
00101 {
00102   srdf::Model s;
00103   boost::shared_ptr<urdf::ModelInterface> u = loadURDF(std::string(TEST_RESOURCE_LOCATION) + "/pr2_desc.urdf");
00104   EXPECT_TRUE(u != NULL);
00105 
00106   EXPECT_TRUE(s.initFile(*u, std::string(TEST_RESOURCE_LOCATION) + "/pr2_desc.3.srdf"));
00107   EXPECT_TRUE(s.getVirtualJoints().size() == 1);
00108   EXPECT_TRUE(s.getGroups().size() == 7);
00109   EXPECT_TRUE(s.getGroupStates().size() == 2);
00110   EXPECT_TRUE(s.getDisabledCollisionPairs().size() == 2);
00111   EXPECT_TRUE(s.getDisabledCollisionPairs()[0].reason_ == "adjacent");
00112   EXPECT_TRUE(s.getEndEffectors().size() == 2);
00113   
00114   EXPECT_TRUE(s.getVirtualJoints()[0].name_ == "world_joint");
00115   EXPECT_TRUE(s.getVirtualJoints()[0].type_ == "planar");
00116   for (std::size_t i = 0 ; i < s.getGroups().size() ; ++i)
00117   {
00118     if (s.getGroups()[i].name_ == "left_arm" || s.getGroups()[i].name_ == "right_arm")
00119       EXPECT_TRUE(s.getGroups()[i].chains_.size() == 1);
00120     if (s.getGroups()[i].name_ == "arms")
00121       EXPECT_TRUE(s.getGroups()[i].subgroups_.size() == 2);
00122     if (s.getGroups()[i].name_ == "base")
00123       EXPECT_TRUE(s.getGroups()[i].joints_.size() == 1);
00124     if (s.getGroups()[i].name_ == "l_end_effector" || s.getGroups()[i].name_ == "r_end_effector")
00125     {
00126       EXPECT_TRUE(s.getGroups()[i].links_.size() == 1);
00127       EXPECT_TRUE(s.getGroups()[i].joints_.size() == 9);
00128     }
00129     if (s.getGroups()[i].name_ == "whole_body")
00130     {
00131       EXPECT_TRUE(s.getGroups()[i].joints_.size() == 1);
00132       EXPECT_TRUE(s.getGroups()[i].subgroups_.size() == 2);
00133     }
00134   }
00135   int index = 0;
00136   if (s.getGroupStates()[0].group_ != "arms")
00137     index = 1;
00138   
00139   EXPECT_TRUE(s.getGroupStates()[index].group_ == "arms");
00140   EXPECT_TRUE(s.getGroupStates()[index].name_ == "tuck_arms");
00141   EXPECT_TRUE(s.getGroupStates()[1-index].group_ == "base");
00142   EXPECT_TRUE(s.getGroupStates()[1-index].name_ == "home");
00143   
00144   const std::vector<double> &v = s.getGroupStates()[index].joint_values_.find("l_shoulder_pan_joint")->second;
00145   EXPECT_TRUE(v.size() == 1u);
00146   EXPECT_TRUE(v[0] == 0.2);
00147   const std::vector<double> &w = s.getGroupStates()[1-index].joint_values_.find("world_joint")->second;
00148   EXPECT_TRUE(w.size() == 3u);
00149   EXPECT_TRUE(w[0] == 0.4);
00150   EXPECT_TRUE(w[1] == 0);
00151   EXPECT_TRUE(w[2] == -1);
00152   
00153   index = (s.getEndEffectors()[0].name_[0] == 'r') ? 0 : 1;
00154   EXPECT_TRUE(s.getEndEffectors()[index].name_ == "r_end_effector");
00155   EXPECT_TRUE(s.getEndEffectors()[index].component_group_ == "r_end_effector");
00156   EXPECT_TRUE(s.getEndEffectors()[index].parent_link_ == "r_wrist_roll_link");
00157 }
00158 
00159 int main(int argc, char **argv)
00160 {
00161   testSimple();
00162   testComplex();
00163   return 0;
00164 }


srdfdom
Author(s): Ioan Sucan
autogenerated on Mon Oct 6 2014 07:45:58