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 (
"," ) );
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 )
132 if ( !
sdf_->HasElement (
"robotNamespace" ) ) {
135 namespace_ =
sdf_->GetElement (
"robotNamespace" )->Get<std::string>();
141 this->namespace_ +=
"/";
152 GazeboRos ( sensors::SensorPtr _parent, sdf::ElementPtr _sdf,
const std::string &_plugin )
155 std::stringstream ss;
156 if (
sdf_->HasElement (
"robotNamespace" ) ) {
159 ss <<
"the 'robotNamespace' param was empty";
162 ss <<
"Using the 'robotNamespace' param: '" <<
namespace_ <<
"'";
165 ss <<
"the 'robotNamespace' param did not exit";
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() );