34 #ifndef GAZEBO_ROS_UTILS_H 35 #define GAZEBO_ROS_UTILS_H 37 #include <boost/algorithm/string.hpp> 39 #include <gazebo/common/common.hh> 40 #include <gazebo/physics/physics.hh> 41 #include <gazebo/sensors/Sensor.hh> 42 #include <gazebo/gazebo_config.h> 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 49 #define GAZEBO_SENSORS_USING_DYNAMIC_POINTER_CAST using boost::dynamic_pointer_cast 61 inline std::string
GetModelName (
const sensors::SensorPtr &parent )
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 ) {
71 modelName = values[1];
83 inline std::string
GetRobotNamespace (
const sensors::SensorPtr &parent,
const sdf::ElementPtr &sdf,
const char *pInfo = NULL )
85 std::string name_space;
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";
93 ss <<
"Using the 'robotNamespace' param: '" << name_space <<
"'";
96 ss <<
"The 'robotNamespace' param did not exit";
98 if ( pInfo != NULL ) {
99 ROS_INFO_NAMED(
"utils",
"%s Plugin: %s" , pInfo, ss.str().c_str() );
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() );
135 namespace_ = sdf_->GetElement (
"robotNamespace" )->Get<std::string>();
136 if ( namespace_.empty() ) {
137 namespace_ = _parent->GetName();
140 if ( !namespace_.empty() )
141 this->namespace_ +=
"/";
143 info_text = plugin_ +
"(ns = " + namespace_ +
")";
152 GazeboRos ( sensors::SensorPtr _parent, sdf::ElementPtr _sdf,
const std::string &_plugin )
153 : sdf_ ( _sdf ), plugin_ ( _plugin ) {
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";
162 ss <<
"Using the 'robotNamespace' param: '" << namespace_ <<
"'";
165 ss <<
"the 'robotNamespace' param did not exit";
168 info_text = plugin_ +
"(ns = " + namespace_ +
")";
169 ROS_INFO_NAMED(
"utils",
"%s: %s" , info_text.c_str(), ss.str().c_str() );
177 const char*
info()
const;
193 std::string
resolveTF (
const std::string &_name );
217 physics::JointPtr
getJoint ( physics::ModelPtr &_parent,
const char *_tag_name,
const std::string &_joint_default_name );
233 void getParameter ( T &_value,
const char *_tag_name,
const T &_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() );
238 this->getParameter<T> ( _value, _tag_name );
250 if ( sdf_->HasElement ( _tag_name ) ) {
251 _value = sdf_->GetElement ( _tag_name )->Get<T>();
253 ROS_DEBUG_NAMED(
"utils",
"%s: <%s> = %s",
info(), _tag_name, boost::lexical_cast<std::string> ( _value ).c_str() );
265 void getParameter ( T &_value,
const char *_tag_name,
const std::map<std::string, T> &_options,
const T &_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() );
270 this->getParameter<T> ( _value, _tag_name, _options );
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() );
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() );
#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
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
const char * info() const
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
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