gazebo_ros_baro.cpp
Go to the documentation of this file.
00001 //=================================================================================================
00002 // Copyright (c) 2012, Johannes Meyer, TU Darmstadt
00003 // All rights reserved.
00004 
00005 // Redistribution and use in source and binary forms, with or without
00006 // modification, are permitted provided that the following conditions are met:
00007 //     * Redistributions of source code must retain the above copyright
00008 //       notice, this list of conditions and the following disclaimer.
00009 //     * Redistributions in binary form must reproduce the above copyright
00010 //       notice, this list of conditions and the following disclaimer in the
00011 //       documentation and/or other materials provided with the distribution.
00012 //     * Neither the name of the Flight Systems and Automatic Control group,
00013 //       TU Darmstadt, nor the names of its contributors may be used to
00014 //       endorse or promote products derived from this software without
00015 //       specific prior written permission.
00016 
00017 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00018 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00019 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00020 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
00021 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00022 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00023 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00024 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00026 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027 //=================================================================================================
00028 
00029 #include <hector_quadrotor_gazebo_plugins/gazebo_ros_baro.h>
00030 #include <gazebo/common/Events.hh>
00031 #include <gazebo/physics/physics.hh>
00032 
00033 static const double DEFAULT_ELEVATION = 0.0;
00034 static const double DEFAULT_QNH       = 1013.25;
00035 
00036 namespace gazebo {
00037 
00038 GazeboRosBaro::GazeboRosBaro()
00039 {
00040 }
00041 
00043 // Destructor
00044 GazeboRosBaro::~GazeboRosBaro()
00045 {
00046   updateTimer.Disconnect(updateConnection);
00047   node_handle_->shutdown();
00048   delete node_handle_;
00049 }
00050 
00052 // Load the controller
00053 void GazeboRosBaro::Load(physics::ModelPtr _model, sdf::ElementPtr _sdf)
00054 {
00055   world = _model->GetWorld();
00056   link = _model->GetLink();
00057   link_name_ = link->GetName();
00058 
00059   if (_sdf->HasElement("bodyName"))
00060   {
00061     link_name_ = _sdf->GetElement("bodyName")->GetValueString();
00062     link = _model->GetLink(link_name_);
00063   }
00064 
00065   if (!link)
00066   {
00067     ROS_FATAL("gazebo_ros_baro plugin error: bodyName: %s does not exist\n", link_name_.c_str());
00068     return;
00069   }
00070 
00071   // default parameters
00072   namespace_.clear();
00073   frame_id_ = link->GetName();
00074   height_topic_ = "pressure_height";
00075   altimeter_topic_ = "altimeter";
00076   elevation_ = DEFAULT_ELEVATION;
00077   qnh_ = DEFAULT_QNH;
00078 
00079   // load parameters
00080   if (_sdf->HasElement("robotNamespace"))     namespace_ = _sdf->GetElement("robotNamespace")->GetValueString();
00081   if (_sdf->HasElement("frameId"))            frame_id_ = _sdf->GetElement("frameId")->GetValueString();
00082   if (_sdf->HasElement("topicName"))          height_topic_ = _sdf->GetElement("topicName")->GetValueString();
00083   if (_sdf->HasElement("altimeterTopicName")) altimeter_topic_ = _sdf->GetElement("altimeterTopicName")->GetValueString();
00084   if (_sdf->HasElement("elevation"))          elevation_ = _sdf->GetElement("elevation")->GetValueDouble();
00085   if (_sdf->HasElement("qnh"))                qnh_ = _sdf->GetElement("qnh")->GetValueDouble();
00086 
00087   // load sensor model
00088   sensor_model_.Load(_sdf);
00089 
00090   height_.header.frame_id = frame_id_;
00091 
00092   // start ros node
00093   if (!ros::isInitialized())
00094   {
00095     int argc = 0;
00096     char **argv = NULL;
00097     ros::init(argc,argv,"gazebo",ros::init_options::NoSigintHandler|ros::init_options::AnonymousName);
00098   }
00099   node_handle_ = new ros::NodeHandle(namespace_);
00100 
00101   // advertise height
00102   if (!height_topic_.empty()) {
00103 #ifdef USE_MAV_MSGS
00104     height_publisher_ = node_handle_->advertise<mav_msgs::Height>(height_topic_, 10);
00105 #else
00106     height_publisher_ = node_handle_->advertise<geometry_msgs::PointStamped>(height_topic_, 10);
00107 #endif
00108   }
00109 
00110   // advertise altimeter
00111   if (!altimeter_topic_.empty()) {
00112     altimeter_publisher_ = node_handle_->advertise<hector_uav_msgs::Altimeter>(altimeter_topic_, 10);
00113   }
00114 
00115   Reset();
00116 
00117   // connect Update function
00118   updateTimer.Load(world, _sdf);
00119   updateConnection = updateTimer.Connect(boost::bind(&GazeboRosBaro::Update, this));
00120 }
00121 
00122 void GazeboRosBaro::Reset()
00123 {
00124   updateTimer.Reset();
00125   sensor_model_.reset();
00126 }
00127 
00129 // Update the controller
00130 void GazeboRosBaro::Update()
00131 {
00132   common::Time sim_time = world->GetSimTime();
00133   double dt = updateTimer.getTimeSinceLastUpdate().Double();
00134 
00135   math::Pose pose = link->GetWorldPose();
00136   double height = sensor_model_(pose.pos.z, dt);
00137 
00138   if (height_publisher_) {
00139 #ifdef USE_MAV_MSGS
00140     double previous_height = height_.height;
00141     height_.header.stamp = ros::Time(sim_time.sec, sim_time.nsec);
00142     height_.height = height;
00143     height_.height_variance = sensor_model_.gaussian_noise*sensor_model_.gaussian_noise + sensor_model_.drift*sensor_model_.drift;
00144     height_.climb = dt > 0.0 ? (height_.height - previous_height) / dt : 0.0;
00145     height_.climb_variance = sensor_model_.gaussian_noise*sensor_model_.gaussian_noise;
00146 #else
00147     height_.header.stamp = ros::Time(sim_time.sec, sim_time.nsec);
00148     height_.point.z = height;
00149 #endif
00150 
00151     height_publisher_.publish(height_);
00152   }
00153 
00154   if (altimeter_publisher_) {
00155     altimeter_.header = height_.header;
00156     altimeter_.altitude = height + elevation_;
00157     altimeter_.pressure = pow((1.0 - altimeter_.altitude / 44330.0), 5.263157) * qnh_;
00158     altimeter_.qnh = qnh_;
00159     altimeter_publisher_.publish(altimeter_);
00160   }
00161 }
00162 
00163 // Register this plugin with the simulator
00164 GZ_REGISTER_MODEL_PLUGIN(GazeboRosBaro)
00165 
00166 } // namespace gazebo


hector_quadrotor_gazebo_plugins
Author(s): Johannes Meyer
autogenerated on Mon Oct 6 2014 00:30:24