gazebo_ros_baro.cpp
Go to the documentation of this file.
1 //=================================================================================================
2 // Copyright (c) 2012-2016, Institute of Flight Systems and Automatic Control,
3 // Technische Universität Darmstadt.
4 // All rights reserved.
5 
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are met:
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of hector_quadrotor nor the names of its contributors
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
16 
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
21 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 //=================================================================================================
28 
30 #include <gazebo/common/Events.hh>
31 #include <gazebo/physics/physics.hh>
32 
33 #include <cmath>
34 
35 static const double DEFAULT_ELEVATION = 0.0;
36 static const double DEFAULT_QNH = 1013.25;
37 
38 namespace gazebo {
39 
41 {
42 }
43 
45 // Destructor
47 {
49 
51 
53  delete node_handle_;
54 }
55 
57 // Load the controller
58 void GazeboRosBaro::Load(physics::ModelPtr _model, sdf::ElementPtr _sdf)
59 {
60  world = _model->GetWorld();
61  link = _model->GetLink();
62  link_name_ = link->GetName();
63 
64  if (_sdf->HasElement("bodyName"))
65  {
66  link_name_ = _sdf->GetElement("bodyName")->Get<std::string>();
67  link = _model->GetLink(link_name_);
68  }
69 
70  if (!link)
71  {
72  ROS_FATAL("gazebo_ros_baro plugin error: bodyName: %s does not exist\n", link_name_.c_str());
73  return;
74  }
75 
76  // default parameters
77  namespace_.clear();
78  frame_id_ = link->GetName();
79  height_topic_ = "pressure_height";
80  altimeter_topic_ = "altimeter";
82  qnh_ = DEFAULT_QNH;
83 
84  // load parameters
85  if (_sdf->HasElement("robotNamespace")) namespace_ = _sdf->GetElement("robotNamespace")->Get<std::string>();
86  if (_sdf->HasElement("frameId")) frame_id_ = _sdf->GetElement("frameId")->Get<std::string>();
87  if (_sdf->HasElement("topicName")) height_topic_ = _sdf->GetElement("topicName")->Get<std::string>();
88  if (_sdf->HasElement("altimeterTopicName")) altimeter_topic_ = _sdf->GetElement("altimeterTopicName")->Get<std::string>();
89  if (_sdf->HasElement("elevation")) elevation_ = _sdf->GetElement("elevation")->Get<double>();
90  if (_sdf->HasElement("qnh")) qnh_ = _sdf->GetElement("qnh")->Get<double>();
91 
92  // load sensor model
93  sensor_model_.Load(_sdf);
94 
95  height_.header.frame_id = frame_id_;
96 
97  // Make sure the ROS node for Gazebo has already been initialized
98  if (!ros::isInitialized())
99  {
100  ROS_FATAL_STREAM("A ROS node for Gazebo has not been initialized, unable to load plugin. "
101  << "Load the Gazebo system plugin 'libgazebo_ros_api_plugin.so' in the gazebo_ros package)");
102  return;
103  }
104 
106 
107  // advertise height
108  if (!height_topic_.empty()) {
109  height_publisher_ = node_handle_->advertise<geometry_msgs::PointStamped>(height_topic_, 10);
110  }
111 
112  // advertise altimeter
113  if (!altimeter_topic_.empty()) {
114  altimeter_publisher_ = node_handle_->advertise<hector_uav_msgs::Altimeter>(altimeter_topic_, 10);
115  }
116 
117  // setup dynamic_reconfigure server
118  dynamic_reconfigure_server_.reset(new dynamic_reconfigure::Server<SensorModelConfig>(ros::NodeHandle(*node_handle_, altimeter_topic_)));
120 
121  Reset();
122 
123  // connect Update function
124  updateTimer.Load(world, _sdf);
126 }
127 
129 {
130  updateTimer.Reset();
132 }
133 
135 // Update the controller
137 {
138 #if (GAZEBO_MAJOR_VERSION >= 8)
139  common::Time sim_time = world->SimTime();
140 #else
141  common::Time sim_time = world->GetSimTime();
142 #endif
143  double dt = updateTimer.getTimeSinceLastUpdate().Double();
144 
145 #if (GAZEBO_MAJOR_VERSION >= 8)
146  ignition::math::Pose3d pose = link->WorldPose();
147  double height = sensor_model_(pose.Pos().Z(), dt);
148 #else
149  math::Pose pose = link->GetWorldPose();
150  double height = sensor_model_(pose.pos.z, dt);
151 #endif
152 
153  if (height_publisher_) {
154  height_.header.stamp = ros::Time(sim_time.sec, sim_time.nsec);
155  height_.point.z = height;
157  }
158 
159  if (altimeter_publisher_) {
160  altimeter_.header = height_.header;
161  altimeter_.altitude = height + elevation_;
162  altimeter_.pressure = std::pow((1.0 - altimeter_.altitude / 44330.0), 5.263157) * qnh_;
163  altimeter_.qnh = qnh_;
165  }
166 }
167 
168 // Register this plugin with the simulator
169 GZ_REGISTER_MODEL_PLUGIN(GazeboRosBaro)
170 
171 } // namespace gazebo
#define ROS_FATAL(...)
boost::shared_ptr< dynamic_reconfigure::Server< SensorModelConfig > > dynamic_reconfigure_server_
virtual void Load(physics::WorldPtr _world, sdf::ElementPtr _sdf, const std::string &_prefix="update")
void publish(const boost::shared_ptr< M > &message) const
static const double DEFAULT_QNH
virtual void Load(sdf::ElementPtr _sdf, const std::string &prefix=std::string())
ROSCPP_DECL bool isInitialized()
virtual void Reset()
physics::WorldPtr world
The parent World.
static const double DEFAULT_ELEVATION
virtual event::ConnectionPtr Connect(const boost::function< void()> &_subscriber, bool connectToWorldUpdateBegin=true)
geometry_msgs::PointStamped height_
virtual void Load(physics::ModelPtr _model, sdf::ElementPtr _sdf)
ros::Publisher height_publisher_
hector_uav_msgs::Altimeter altimeter_
ros::NodeHandle * node_handle_
#define ROS_FATAL_STREAM(args)
virtual void Disconnect(event::ConnectionPtr const &_c=event::ConnectionPtr())
virtual void dynamicReconfigureCallback(SensorModelConfig &config, uint32_t level)
common::Time getTimeSinceLastUpdate() const
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
physics::LinkPtr link
The link referred to by this plugin.
virtual void reset()
ros::Publisher altimeter_publisher_
event::ConnectionPtr updateConnection
std::string altimeter_topic_


hector_quadrotor_gazebo_plugins
Author(s): Johannes Meyer
autogenerated on Mon Jun 10 2019 13:36:58