gazebo_ros_paths_plugin.cpp
Go to the documentation of this file.
00001 /*
00002  *  Gazebo - Outdoor Multi-Robot Simulator
00003  *  Copyright (C) 2003
00004  *     Nate Koenig & Andrew Howard
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  *
00020  */
00021 
00022 /* Desc: External interfaces for Gazebo
00023  * Author: John Hsu adapted original gazebo main.cc by Nate Koenig
00024  * Date: 25 Apr 2010
00025  * SVN: $Id: main.cc 8598 2010-03-22 21:59:24Z hsujohnhsu $
00026  */
00027 
00028 #include <ros/ros.h>
00029 #include <ros/package.h>
00030 #include <gazebo/gazebo_ros_api_plugin.h>
00031 
00032 #include <map>
00033 
00034 namespace gazebo
00035 {
00036 
00037   typedef std::vector<std::string> V_string;
00038   typedef std::map<std::string, std::string> M_string;
00039 
00040   class GazeboRosPathsPlugin : public SystemPlugin
00041   {
00042     public:
00043     GazeboRosPathsPlugin()
00044     {
00045       this->LoadPaths();
00046     }
00047 
00048     ~GazeboRosPathsPlugin()
00049     {
00050     };
00051 
00052     void Init()
00053     {
00054     }
00055 
00056     void Load(int argc, char** argv)
00057     {
00058     }
00059 
00060     /* copying ros::package::command() and ros::package::getPlugins() in here due to */
00061     /* a bug with compiler flag -Bsymbolic-functions                                 */
00062     /* see ticket https://code.ros.org/trac/ros/ticket/2977                          */
00063     /* this flag needs to be removed from new catkin debbuild before we can resume   */
00064     /* usage or ros::package::getPlugins() library from roslib (libroslib.so).       */
00065     void rosPackageCommandDebug(const std::string& cmd, V_string& output)
00066     {
00067       std::string out_string = ros::package::command(cmd);
00068       V_string full_list;
00069       boost::split(full_list, out_string, boost::is_any_of("\r\n"));
00070 
00071       // strip empties
00072       V_string::iterator it = full_list.begin();
00073       V_string::iterator end = full_list.end();
00074       for (; it != end; ++it)
00075       {
00076         if (!it->empty())
00077         {
00078           output.push_back(*it);
00079         }
00080       }
00081     }
00082     /* copying ros::package::command() and ros::package::getPlugins() in here due to */
00083     /* a bug with compiler flag -Bsymbolic-functions                                 */
00084     /* see ticket https://code.ros.org/trac/ros/ticket/2977                          */
00085     /* this flag needs to be removed from new catkin debbuild before we can resume   */
00086     /* usage or ros::package::getPlugins() library from roslib (libroslib.so).       */
00087     void rosPackageGetPluginsDebug(const std::string& package, const std::string& attribute, V_string& plugins)
00088     {
00089       M_string plugins_map;
00090       rosPackageGetPluginsDebug(package, attribute, plugins_map);
00091       M_string::iterator it = plugins_map.begin();
00092       M_string::iterator end = plugins_map.end();
00093       for (; it != end; ++it)
00094       {
00095         plugins.push_back(it->second);
00096       }
00097     }
00098     /* copying ros::package::command() and ros::package::getPlugins() in here due to */
00099     /* a bug with compiler flag -Bsymbolic-functions                                 */
00100     /* see ticket https://code.ros.org/trac/ros/ticket/2977                          */
00101     /* this flag needs to be removed from new catkin debbuild before we can resume   */
00102     /* usage or ros::package::getPlugins() library from roslib (libroslib.so).       */
00103     void rosPackageGetPluginsDebug(const std::string& package, const std::string& attribute, M_string& plugins)
00104     {
00105       V_string lines;
00106       rosPackageCommandDebug("plugins --attrib=" + attribute + " " + package, lines);
00107 
00108       V_string::iterator it = lines.begin();
00109       V_string::iterator end = lines.end();
00110       for (; it != end; ++it)
00111       {
00112         V_string tokens;
00113         boost::split(tokens, *it, boost::is_any_of(" "));
00114 
00115         if (tokens.size() >= 2)
00116         {
00117           std::string package = tokens[0];
00118           std::string rest = boost::join(V_string(tokens.begin() + 1, tokens.end()), " ");
00119           plugins[package] = rest;
00120         }
00121       }
00122     }
00123     void LoadPaths()
00124     {
00125 
00126       // setting Gazebo Path/Resources Configurations
00127       //   GAZEBO_RESOURCE_PATH, GAZEBO_PLUGIN_PATH and OGRE_RESOURCE_PATH for
00128       //   GazeboConfig::gazeboPaths, GazeboConfig::pluginPaths and GazeboConfig::ogrePaths
00129       // by adding paths to GazeboConfig based on ros::package
00130       // optional: setting environment variables according to ROS
00131       //           e.g. setenv("OGRE_RESOURCE_PATH",ogre_package_path.c_str(),1);
00132       // set ogre library paths by searching for package named ogre
00133       gazebo::common::SystemPaths::Instance()->ClearOgrePaths();
00134       gazebo::common::SystemPaths::Instance()->ogrePathsFromEnv = false;
00135       std::string ogre_package_name("ogre"); // @todo: un hardcode this???
00136       std::string ogre_package_path = ros::package::getPath(ogre_package_name)+std::string("/ogre/lib/OGRE");
00137       if (ogre_package_path.empty())
00138         ROS_DEBUG("Package[%s] does not exist, assuming user must have set OGRE_RESOURCE_PATH already.",ogre_package_name.c_str());
00139       else
00140       {
00141         ROS_DEBUG("ogre path %s",ogre_package_path.c_str());
00142         gazebo::common::SystemPaths::Instance()->AddOgrePaths(ogre_package_path);
00143       }
00144 
00145       // set gazebo media paths by adding all packages that exports "gazebo_media_path" for gazebo
00146       gazebo::common::SystemPaths::Instance()->ClearGazeboPaths();
00147       gazebo::common::SystemPaths::Instance()->gazeboPathsFromEnv = false;
00148       std::vector<std::string> gazebo_media_paths;
00149       rosPackageGetPluginsDebug("gazebo","gazebo_media_path",gazebo_media_paths);
00150       for (std::vector<std::string>::iterator iter=gazebo_media_paths.begin(); iter != gazebo_media_paths.end(); iter++)
00151       {
00152         ROS_DEBUG("med path %s",iter->c_str());
00153         gazebo::common::SystemPaths::Instance()->AddGazeboPaths(iter->c_str());
00154       }
00155 
00156       // set gazebo plugins paths by adding all packages that exports "plugin" for gazebo
00157       gazebo::common::SystemPaths::Instance()->ClearPluginPaths();
00158       gazebo::common::SystemPaths::Instance()->pluginPathsFromEnv = false;
00159       std::vector<std::string> plugin_paths;
00160       rosPackageGetPluginsDebug("gazebo","plugin_path",plugin_paths);
00161       for (std::vector<std::string>::iterator iter=plugin_paths.begin(); iter != plugin_paths.end(); iter++)
00162       {
00163         ROS_DEBUG("plugin path %s",(*iter).c_str());
00164         gazebo::common::SystemPaths::Instance()->AddPluginPaths(iter->c_str());
00165       }
00166 
00167       // set .gazeborc path to something else, so we don't pick up default ~/.gazeborc
00168       std::string gazeborc = ros::package::getPath("gazebo")+"/.do_not_use_gazeborc";
00169       setenv("GAZEBORC",gazeborc.c_str(),1);
00170 
00171     }
00172     private:
00173 
00174   };
00175 
00176 // Register this plugin with the simulator
00177 GZ_REGISTER_SYSTEM_PLUGIN(GazeboRosPathsPlugin)
00178 
00179 }
00180 


gazebo
Author(s): Nate Koenig, Andrew Howard, with contributions from many others. See web page for a full credits llist.
autogenerated on Sun Jan 5 2014 11:34:52