gazebo_ros_utils.h
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2013, Markus Bader <markus.bader@tuwien.ac.at>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of the copyright holder nor the names of its
18  * contributors may be used to endorse or promote products derived from
19  * this software without specific prior written permission.
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  *********************************************************************/
33 
34 #ifndef GAZEBO_ROS_UTILS_H
35 #define GAZEBO_ROS_UTILS_H
36 #include <map>
37 #include <boost/algorithm/string.hpp>
38 
39 #include <gazebo/common/common.hh>
40 #include <gazebo/physics/physics.hh>
41 #include <gazebo/sensors/Sensor.hh>
42 #include <gazebo/gazebo_config.h>
43 #include <ros/ros.h>
44 
45 #ifndef GAZEBO_SENSORS_USING_DYNAMIC_POINTER_CAST
46 # if GAZEBO_MAJOR_VERSION >= 7
47 #define GAZEBO_SENSORS_USING_DYNAMIC_POINTER_CAST using std::dynamic_pointer_cast
48 # else
49 #define GAZEBO_SENSORS_USING_DYNAMIC_POINTER_CAST using boost::dynamic_pointer_cast
50 # endif
51 #endif
52 
53 namespace gazebo
54 {
55 
61 inline std::string GetModelName ( const sensors::SensorPtr &parent )
62 {
63  std::string modelName;
64  std::vector<std::string> values;
65  std::string scopedName = parent->ScopedName();
66  boost::replace_all ( scopedName, "::", "," );
67  boost::split ( values, scopedName, boost::is_any_of ( "," ) );
68  if ( values.size() < 2 ) {
69  modelName = "";
70  } else {
71  modelName = values[1];
72  }
73  return modelName;
74 }
75 
83 inline std::string GetRobotNamespace ( const sensors::SensorPtr &parent, const sdf::ElementPtr &sdf, const char *pInfo = NULL )
84 {
85  std::string name_space;
86  std::stringstream ss;
87  if ( sdf->HasElement ( "robotNamespace" ) ) {
88  name_space = sdf->Get<std::string> ( "robotNamespace" );
89  if ( name_space.empty() ) {
90  ss << "The 'robotNamespace' param was empty";
91  name_space = GetModelName ( parent );
92  } else {
93  ss << "Using the 'robotNamespace' param: '" << name_space << "'";
94  }
95  } else {
96  ss << "The 'robotNamespace' param did not exit";
97  }
98  if ( pInfo != NULL ) {
99  ROS_INFO_NAMED("utils", "%s Plugin: %s" , pInfo, ss.str().c_str() );
100  }
101  return name_space;
102 }
103 
110 {
111 private:
112  sdf::ElementPtr sdf_;
113  std::string plugin_;
114  std::string namespace_;
116  std::string tf_prefix_;
117  std::string info_text;
118 
121  void readCommonParameter ();
122 public:
129  GazeboRos ( physics::ModelPtr &_parent, sdf::ElementPtr _sdf, const std::string &_plugin )
130  : sdf_ ( _sdf ), plugin_ ( _plugin ) {
131  namespace_ = _parent->GetName ();
132  if ( !sdf_->HasElement ( "robotNamespace" ) ) {
133  ROS_INFO_NAMED("utils", "%s missing <robotNamespace>, defaults is %s", plugin_.c_str(), namespace_.c_str() );
134  } else {
135  namespace_ = sdf_->GetElement ( "robotNamespace" )->Get<std::string>();
136  if ( namespace_.empty() ) {
137  namespace_ = _parent->GetName();
138  }
139  }
140  if ( !namespace_.empty() )
141  this->namespace_ += "/";
142  rosnode_ = boost::shared_ptr<ros::NodeHandle> ( new ros::NodeHandle ( namespace_ ) );
143  info_text = plugin_ + "(ns = " + namespace_ + ")";
145  }
152  GazeboRos ( sensors::SensorPtr _parent, sdf::ElementPtr _sdf, const std::string &_plugin )
153  : sdf_ ( _sdf ), plugin_ ( _plugin ) {
154 
155  std::stringstream ss;
156  if ( sdf_->HasElement ( "robotNamespace" ) ) {
157  namespace_ = sdf_->Get<std::string> ( "robotNamespace" );
158  if ( namespace_.empty() ) {
159  ss << "the 'robotNamespace' param was empty";
160  namespace_ = GetModelName ( _parent );
161  } else {
162  ss << "Using the 'robotNamespace' param: '" << namespace_ << "'";
163  }
164  } else {
165  ss << "the 'robotNamespace' param did not exit";
166  }
167  rosnode_ = boost::shared_ptr<ros::NodeHandle> ( new ros::NodeHandle ( namespace_ ) );
168  info_text = plugin_ + "(ns = " + namespace_ + ")";
169  ROS_INFO_NAMED("utils", "%s: %s" , info_text.c_str(), ss.str().c_str() );
171  }
172 
177  const char* info() const;
193  std::string resolveTF ( const std::string &_name );
194 
202  void getParameterBoolean ( bool &_value, const char *_tag_name, const bool &_default );
209  void getParameterBoolean ( bool &_value, const char *_tag_name );
217  physics::JointPtr getJoint ( physics::ModelPtr &_parent, const char *_tag_name, const std::string &_joint_default_name );
222  void isInitialized();
223 
224 
232  template <class T>
233  void getParameter ( T &_value, const char *_tag_name, const T &_default ) {
234  _value = _default;
235  if ( !sdf_->HasElement ( _tag_name ) ) {
236  ROS_WARN_NAMED("utils", "%s: missing <%s> default is %s", info(), _tag_name, boost::lexical_cast<std::string> ( _default ).c_str() );
237  } else {
238  this->getParameter<T> ( _value, _tag_name );
239  }
240  }
248  template <class T>
249  void getParameter ( T &_value, const char *_tag_name ) {
250  if ( sdf_->HasElement ( _tag_name ) ) {
251  _value = sdf_->GetElement ( _tag_name )->Get<T>();
252  }
253  ROS_DEBUG_NAMED("utils", "%s: <%s> = %s", info(), _tag_name, boost::lexical_cast<std::string> ( _value ).c_str() );
254 
255  }
256 
264  template <class T>
265  void getParameter ( T &_value, const char *_tag_name, const std::map<std::string, T> &_options, const T &_default ) {
266  _value = _default;
267  if ( !sdf_->HasElement ( _tag_name ) ) {
268  ROS_WARN_NAMED("utils", "%s: missing <%s> default is %s", info(), _tag_name, boost::lexical_cast<std::string> ( _default ).c_str() );
269  } else {
270  this->getParameter<T> ( _value, _tag_name, _options );
271  }
272  }
280  template <class T>
281  void getParameter ( T &_value, const char *_tag_name, const std::map<std::string, T> &_options ) {
282  typename std::map<std::string, T >::const_iterator it;
283  if ( sdf_->HasElement ( _tag_name ) ) {
284  std::string value = sdf_->GetElement ( _tag_name )->Get<std::string>();
285  it = _options.find ( value );
286  if ( it == _options.end() ) {
287  ROS_WARN_NAMED("utils", "%s: <%s> no matching key to %s", info(), _tag_name, value.c_str() );
288  } else {
289  _value = it->second;
290  }
291  }
292  ROS_DEBUG_NAMED("utils", "%s: <%s> = %s := %s", info(), _tag_name, ( it == _options.end() ?"default":it->first.c_str() ), boost::lexical_cast<std::string> ( _value ).c_str() );
293  }
294 };
295 
297 }
298 #endif
#define ROS_INFO_NAMED(name,...)
GazeboRos(physics::ModelPtr &_parent, sdf::ElementPtr _sdf, const std::string &_plugin)
std::string GetModelName(const sensors::SensorPtr &parent)
#define ROS_WARN_NAMED(name,...)
boost::shared_ptr< ros::NodeHandle > & node()
std::vector< double > values
boost::shared_ptr< ros::NodeHandle > rosnode_
name of the launched node
sdf::ElementPtr sdf_
physics::JointPtr getJoint(physics::ModelPtr &_parent, const char *_tag_name, const std::string &_joint_default_name)
std::string resolveTF(const std::string &_name)
void getParameter(T &_value, const char *_tag_name, const T &_default)
#define ROS_DEBUG_NAMED(name,...)
void getParameter(T &_value, const char *_tag_name, const std::map< std::string, T > &_options)
std::string tf_prefix_
rosnode
GazeboRos(sensors::SensorPtr _parent, sdf::ElementPtr _sdf, const std::string &_plugin)
void getParameter(T &_value, const char *_tag_name)
std::string info_text
prefix for the ros tf plublisher if not set it uses the namespace_
void getParameterBoolean(bool &_value, const char *_tag_name, const bool &_default)
std::string plugin_
sdf to read
std::string namespace_
name of the plugin class
const char * info() const
void getParameter(T &_value, const char *_tag_name, const std::map< std::string, T > &_options, const T &_default)
std::string GetRobotNamespace(const sensors::SensorPtr &parent, const sdf::ElementPtr &sdf, const char *pInfo=NULL)
Reads the name space tag of a sensor plugin.
boost::shared_ptr< GazeboRos > GazeboRosPtr
void readCommonParameter()
info text for log messages to identify the node


gazebo_plugins
Author(s): John Hsu
autogenerated on Tue Mar 26 2019 02:31:27