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_model/quadrotor_aerodynamics.h>
00030 #include <hector_quadrotor_model/helpers.h>
00031 
00032 #include <ros/node_handle.h>
00033 #include <boost/array.hpp>
00034 
00035 extern "C" {
00036   #include "quadrotorDrag/quadrotorDrag.h"
00037   #include "quadrotorDrag/quadrotorDrag_initialize.h"
00038 }
00039 
00040 #include <boost/array.hpp>
00041 
00042 namespace hector_quadrotor_model {
00043 
00044 // extern void quadrotorDrag(const real_T uin[6], const DragParameters parameter, real_T dt, real_T y[6]);
00045 struct QuadrotorAerodynamics::DragModel {
00046   DragParameters parameters_;
00047   boost::array<real_T,6> u;
00048   boost::array<real_T,6> y;
00049 };
00050 
00051 QuadrotorAerodynamics::QuadrotorAerodynamics()
00052 {
00053   // initialize drag model
00054   quadrotorDrag_initialize();
00055   drag_model_ = new DragModel;
00056 }
00057 
00058 QuadrotorAerodynamics::~QuadrotorAerodynamics()
00059 {
00060   delete drag_model_;
00061 }
00062 
00063 inline void QuadrotorAerodynamics::f(const real_T uin[10], real_T dt, real_T y[14]) const
00064 {
00065   ::quadrotorDrag(uin, drag_model_->parameters_, dt, y);
00066 }
00067 
00068 void QuadrotorAerodynamics::configure(const std::string &ns)
00069 {
00070   ros::NodeHandle param(ns);
00071 
00072   // get model parameters
00073   param.getParam("C_wxy", drag_model_->parameters_.C_wxy);
00074   param.getParam("C_wz",  drag_model_->parameters_.C_wz);
00075   param.getParam("C_mxy", drag_model_->parameters_.C_mxy);
00076   param.getParam("C_mz",  drag_model_->parameters_.C_mz);
00077 
00078 #ifndef NDEBUG
00079   std::cout << "Loaded the following quadrotor drag model parameters from namespace " << param.getNamespace() << ":" << std::endl;
00080   std::cout << "C_wxy = " << drag_model_->parameters_.C_wxy << std::endl;
00081   std::cout << "C_wz = "  << drag_model_->parameters_.C_wz << std::endl;
00082   std::cout << "C_mxy = " << drag_model_->parameters_.C_mxy << std::endl;
00083   std::cout << "C_mz = "  << drag_model_->parameters_.C_mz << std::endl;
00084 #endif
00085 
00086   reset();
00087 }
00088 
00089 void QuadrotorAerodynamics::reset()
00090 {
00091   boost::mutex::scoped_lock lock(mutex_);
00092   drag_model_->u.assign(0.0);
00093   drag_model_->y.assign(0.0);
00094 
00095   twist_ = geometry_msgs::Twist();
00096   wind_ = geometry_msgs::Vector3();
00097   wrench_ = geometry_msgs::Wrench();
00098 }
00099 
00100 void QuadrotorAerodynamics::setTwist(const geometry_msgs::Twist& twist)
00101 {
00102   boost::mutex::scoped_lock lock(mutex_);
00103   twist_ = twist;
00104 }
00105 
00106 void QuadrotorAerodynamics::setWind(const geometry_msgs::Vector3& wind)
00107 {
00108   boost::mutex::scoped_lock lock(mutex_);
00109   wind_ = wind;
00110 }
00111 
00112 void QuadrotorAerodynamics::update(double dt)
00113 {
00114   if (dt <= 0.0) return;
00115   boost::mutex::scoped_lock lock(mutex_);
00116 
00117   // fill input vector u for drag model
00118   drag_model_->u[0] =  (twist_.linear.x - wind_.x);
00119   drag_model_->u[1] = -(twist_.linear.y - wind_.y);
00120   drag_model_->u[2] = -(twist_.linear.z - wind_.z);
00121   drag_model_->u[3] =  twist_.angular.x;
00122   drag_model_->u[4] = -twist_.angular.y;
00123   drag_model_->u[5] = -twist_.angular.z;
00124 
00125 //  std::cout << "u = [ ";
00126 //  for(std::size_t i = 0; i < drag_model_->u.size(); ++i)
00127 //    std::cout << drag_model_->u[i] << " ";
00128 //  std::cout << "]" << std::endl;
00129 
00130   checknan(drag_model_->u, "drag model input");
00131 
00132   // update drag model
00133   f(drag_model_->u.data(), dt, drag_model_->y.data());
00134 
00135   checknan(drag_model_->y, "drag model output");
00136 
00137   //  std::cout << "y = [ ";
00138   //  for(std::size_t i = 0; i < propulsion_model_->y.size(); ++i)
00139   //    std::cout << propulsion_model_->y[i] << " ";
00140   //  std::cout << "]" << std::endl;
00141 
00142   wrench_.force.x  =  drag_model_->y[0];
00143   wrench_.force.y  = -drag_model_->y[1];
00144   wrench_.force.z  = -drag_model_->y[2];
00145   wrench_.torque.x =  drag_model_->y[3];
00146   wrench_.torque.x = -drag_model_->y[4];
00147   wrench_.torque.x = -drag_model_->y[5];
00148 }
00149 
00150 } // namespace hector_quadrotor_model


hector_quadrotor_model
Author(s): Johannes Meyer and Alexander Sendobry
autogenerated on Mon Oct 6 2014 00:29:53