gazebo_quadrotor_aerodynamics.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_quadrotor_aerodynamics.h>
00030 #include <hector_quadrotor_model/helpers.h>
00031 
00032 #include <gazebo/common/Events.hh>
00033 #include <gazebo/physics/physics.hh>
00034 
00035 namespace gazebo {
00036 
00037 using namespace common;
00038 using namespace math;
00039 using namespace hector_quadrotor_model;
00040 
00041 GazeboQuadrotorAerodynamics::GazeboQuadrotorAerodynamics()
00042 {
00043 }
00044 
00045 GazeboQuadrotorAerodynamics::~GazeboQuadrotorAerodynamics()
00046 {
00047   event::Events::DisconnectWorldUpdateBegin(updateConnection);
00048   node_handle_->shutdown();
00049   callback_queue_thread_.join();
00050   delete node_handle_;
00051 }
00052 
00054 // Load the controller
00055 void GazeboQuadrotorAerodynamics::Load(physics::ModelPtr _model, sdf::ElementPtr _sdf)
00056 {
00057   world = _model->GetWorld();
00058   link = _model->GetLink();
00059 
00060   // default parameters
00061   namespace_.clear();
00062   param_namespace_ = "quadrotor_aerodynamics";
00063   wind_topic_ = "wind";
00064 
00065   // load parameters
00066   if (_sdf->HasElement("robotNamespace")) namespace_ = _sdf->GetElement("robotNamespace")->GetValueString();
00067   if (_sdf->HasElement("paramNamespace")) param_namespace_ = _sdf->GetElement("paramNamespace")->GetValueString();
00068   if (_sdf->HasElement("windTopicName"))  wind_topic_ = _sdf->GetElement("windTopicName")->GetValueString();
00069 
00070   // get model parameters
00071   model_.configure(param_namespace_);
00072 
00073   // start ros node
00074   if (!ros::isInitialized())
00075   {
00076     int argc = 0;
00077     char **argv = NULL;
00078     ros::init(argc,argv,"gazebo",ros::init_options::NoSigintHandler|ros::init_options::AnonymousName);
00079   }
00080   node_handle_ = new ros::NodeHandle(namespace_);
00081 
00082   // subscribe command
00083   if (!wind_topic_.empty())
00084   {
00085     ros::SubscribeOptions ops;
00086     ops.callback_queue = &callback_queue_;
00087     ops.initByFullCallbackType<const geometry_msgs::Vector3 &>(
00088       wind_topic_, 1,
00089       boost::bind(&QuadrotorAerodynamics::setWind, &model_, _1)
00090     );
00091     wind_subscriber_ = node_handle_->subscribe(ops);
00092   }
00093 
00094   callback_queue_thread_ = boost::thread( boost::bind( &GazeboQuadrotorAerodynamics::QueueThread,this ) );
00095 
00096   // New Mechanism for Updating every World Cycle
00097   // Listen to the update event. This event is broadcast every
00098   // simulation iteration.
00099   updateConnection = event::Events::ConnectWorldUpdateBegin(
00100       boost::bind(&GazeboQuadrotorAerodynamics::Update, this));
00101 }
00102 
00104 // Update the controller
00105 void GazeboQuadrotorAerodynamics::Update()
00106 {
00107   // Get simulator time
00108   Time current_time = world->GetSimTime();
00109   Time dt = current_time - last_time_;
00110   last_time_ = current_time;
00111   if (dt <= 0.0) return;
00112 
00113   // Get new commands/state
00114   // callback_queue_.callAvailable();
00115 
00116   // fill input vector u for drag model
00117   geometry_msgs::Twist twist;
00118   fromVector(link->GetRelativeLinearVel(), twist.linear);
00119   fromVector(link->GetRelativeAngularVel(), twist.angular);
00120   model_.setTwist(twist);
00121 
00122   // update the model
00123   model_.update(dt.Double());
00124 
00125   // get wrench from model
00126   Vector3 force, torque;
00127   toVector(model_.getWrench().force, force);
00128   toVector(model_.getWrench().torque, torque);
00129 
00130   // set force and torque in gazebo
00131   link->AddRelativeForce(force);
00132   link->AddRelativeTorque(torque - link->GetInertial()->GetCoG().Cross(force));
00133 }
00134 
00136 // Reset the controller
00137 void GazeboQuadrotorAerodynamics::Reset()
00138 {
00139   model_.reset();
00140 }
00141 
00143 // custom callback queue thread
00144 void GazeboQuadrotorAerodynamics::QueueThread()
00145 {
00146   static const double timeout = 0.01;
00147 
00148   while (node_handle_->ok())
00149   {
00150     callback_queue_.callAvailable(ros::WallDuration(timeout));
00151   }
00152 }
00153 
00154 // Register this plugin with the simulator
00155 GZ_REGISTER_MODEL_PLUGIN(GazeboQuadrotorAerodynamics)
00156 
00157 } // namespace gazebo


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