gazebo_ros_utils.cpp
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 
36 
37 #include <sdf/sdf.hh>
38 #include <tf/transform_listener.h>
39 
40 using namespace gazebo;
41 
42 const char* GazeboRos::info() const {
43  return info_text.c_str();
44 }
46  return rosnode_;
47 }
49  return rosnode_;
50 }
51 
52 std::string GazeboRos ::resolveTF(const std::string &name) {
53  return tf::resolve(tf_prefix_, name);
54 }
56 
57  ROS_INFO_NAMED("utils", "Starting plugin %s", info());
58 
59  std::string debugLevel;
60  getParameter<std::string>(debugLevel, "rosDebugLevel", "na");
61  if(boost::iequals(debugLevel, std::string("Debug"))) {
64  }
65  } else if(boost::iequals(debugLevel, std::string("Info"))) {
68  }
69  } else if(boost::iequals(debugLevel, std::string("Warn"))) {
72  }
73  } else if(boost::iequals(debugLevel, std::string("Error"))) {
76  }
77  } else if(boost::iequals(debugLevel, std::string("Fatal"))) {
80  }
81  }
82  if (sdf_->HasElement("rosDebugLevel")) {
83  ROS_INFO_NAMED("utils", "%s: <rosDebugLevel> = %s", info(), debugLevel.c_str());
84  }
85 
86 
88  ROS_INFO_NAMED("utils", "%s: <tf_prefix> = %s", info(), tf_prefix_.c_str());
89 }
90 
91 
92 void GazeboRos ::getParameterBoolean(bool &_value, const char *_tag_name, const bool &_default) {
93  _value = _default;
94  if (!sdf_->HasElement(_tag_name)) {
95  ROS_WARN_NAMED("utils", "%s: missing <%s> default is %s",
96  info(), _tag_name, (_default?"true":"false"));
97  } else {
98  getParameterBoolean(_value, _tag_name);
99  }
100 
101 }
102 void GazeboRos ::getParameterBoolean(bool &_value, const char *_tag_name) {
103 
104  if (sdf_->HasElement(_tag_name)) {
105  std::string value = sdf_->GetElement(_tag_name)->Get<std::string>();
106  if(boost::iequals(value, std::string("true")) || boost::iequals(value, std::string("1")))
107  {
108  _value = true;
109  }
110  else if(boost::iequals(value, std::string("false")) || boost::iequals(value, std::string("0")))
111  {
112  _value = false;
113  }
114  else
115  {
116  ROS_WARN_NAMED("utils", "%s: <%s> must be either true or false but is '%s'",
117  info(), _tag_name, value.c_str());
118  }
119  }
120  ROS_DEBUG_NAMED("utils", "%s: <%s> = %s",
121  info(), _tag_name, (_value?"true":"false"));
122 
123 }
124 
125 physics::JointPtr GazeboRos::getJoint(physics::ModelPtr &_parent, const char *_tag_name, const std::string &_joint_default_name) {
126  std::string joint_name;
127  getParameter<std::string>(joint_name, _tag_name, _joint_default_name);
128  physics::JointPtr joint = _parent->GetJoint(joint_name);
129  if (!joint)
130  {
131  char error[200];
132  snprintf(error, 200,
133  "%s: couldn't get wheel hinge joint named %s",
134  info() , joint_name.c_str());
135  gzthrow(error);
136  }
137  return joint;
138 }
139 
141  if (!ros::isInitialized())
142  {
143  ROS_FATAL_STREAM(info() << "A ROS node for Gazebo has not been initialized, unable to load plugin. "
144  << "Load the Gazebo system plugin 'libgazebo_ros_api_plugin.so' in the gazebo_ros package)");
145  return;
146  }
147 }
gazebo::GazeboRos::info_text
std::string info_text
prefix for the ros tf plublisher if not set it uses the namespace_
Definition: gazebo_ros_utils.h:117
ros::console::levels::Error
Error
gazebo::GazeboRos::isInitialized
void isInitialized()
Definition: gazebo_ros_utils.cpp:140
boost::shared_ptr< ros::NodeHandle >
gazebo
ROS_FATAL_STREAM
#define ROS_FATAL_STREAM(args)
gazebo::GazeboRos::node
boost::shared_ptr< ros::NodeHandle > & node()
Definition: gazebo_ros_utils.cpp:45
tf::resolve
std::string resolve(const std::string &prefix, const std::string &frame_name)
ros::console::set_logger_level
ROSCONSOLE_DECL bool set_logger_level(const std::string &name, levels::Level level)
gazebo::GazeboRos::readCommonParameter
void readCommonParameter()
info text for log messages to identify the node
Definition: gazebo_ros_utils.cpp:55
gazebo::GazeboRos::info
const char * info() const
Definition: gazebo_ros_utils.cpp:42
gazebo::GazeboRos::getJoint
physics::JointPtr getJoint(physics::ModelPtr &_parent, const char *_tag_name, const std::string &_joint_default_name)
Definition: gazebo_ros_utils.cpp:125
ros::console::levels::Debug
Debug
ROS_INFO_NAMED
#define ROS_INFO_NAMED(name,...)
ROS_DEBUG_NAMED
#define ROS_DEBUG_NAMED(name,...)
tf::getPrefixParam
std::string getPrefixParam(ros::NodeHandle &nh)
ros::isInitialized
ROSCPP_DECL bool isInitialized()
gazebo::GazeboRos::resolveTF
std::string resolveTF(const std::string &_name)
Definition: gazebo_ros_utils.cpp:52
ros::console::levels::Info
Info
transform_listener.h
ros::console::levels::Fatal
Fatal
ros::console::notifyLoggerLevelsChanged
ROSCONSOLE_DECL void notifyLoggerLevelsChanged()
gazebo::GazeboRos::sdf_
sdf::ElementPtr sdf_
Definition: gazebo_ros_utils.h:112
ROS_WARN_NAMED
#define ROS_WARN_NAMED(name,...)
ros::console::levels::Warn
Warn
gazebo::GazeboRos::rosnode_
boost::shared_ptr< ros::NodeHandle > rosnode_
name of the launched node
Definition: gazebo_ros_utils.h:115
gazebo::GazeboRos::getParameterBoolean
void getParameterBoolean(bool &_value, const char *_tag_name, const bool &_default)
Definition: gazebo_ros_utils.cpp:92
gazebo_ros_utils.h
ROSCONSOLE_DEFAULT_NAME
#define ROSCONSOLE_DEFAULT_NAME
gazebo::GazeboRos::tf_prefix_
std::string tf_prefix_
rosnode
Definition: gazebo_ros_utils.h:116


gazebo_plugins
Author(s): John Hsu
autogenerated on Thu Nov 23 2023 03:50:28