MAV'RIC
|
00001 /******************************************************************************* 00002 * Copyright (c) 2009-2016, MAV'RIC Development Team 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 * 00008 * 1. Redistributions of source code must retain the above copyright notice, 00009 * this list of conditions and the following disclaimer. 00010 * 00011 * 2. Redistributions in binary form must reproduce the above copyright notice, 00012 * this list of conditions and the following disclaimer in the documentation 00013 * and/or other materials provided with the distribution. 00014 * 00015 * 3. Neither the name of the copyright holder nor the names of its contributors 00016 * may be used to endorse or promote products derived from this software without 00017 * specific prior written permission. 00018 * 00019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00020 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00021 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00022 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 00023 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00024 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00025 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00026 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00027 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00028 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00029 * POSSIBILITY OF SUCH DAMAGE. 00030 ******************************************************************************/ 00031 00032 /******************************************************************************* 00033 * \file ahrs_madgwick.hpp 00034 * 00035 * \author MAV'RIC Team 00036 * \author SOH Madgwick 00037 * \author Julien Lecoeur 00038 * \author Simon Pyroth 00039 * 00040 * \brief Implementation of Madgwick's AHRS algorithms. 00041 * 00042 * See: http://www.x-io.co.uk/node/8#open_source_ahrs_and_imu_algorithms 00043 * 00044 * Date Author Notes 00045 * 29/09/2011 SOH Madgwick Initial release 00046 * 02/10/2011 SOH Madgwick Optimised for reduced CPU load 00047 * 19/02/2012 SOH Madgwick Magnetometer measurement is normalised 00048 * 04/02/2014 Julien Lecoeur Adapt to MAVRIC 00049 * 00050 ******************************************************************************/ 00051 00052 00058 #ifndef AHRS_MADGWICK_HPP_ 00059 #define AHRS_MADGWICK_HPP_ 00060 00061 #include "sensing/ahrs.hpp" 00062 #include "sensing/imu.hpp" 00063 #include "drivers/airspeed_analog.hpp" 00064 00065 00066 class AHRS_madgwick: public AHRS 00067 { 00068 public: 00072 struct conf_t 00073 { 00074 float beta; // 2 * proportional gain (Kp) 00075 float zeta; // Gyro drift bias gain 00076 bool acceleration_correction; // Enable the correction of the parasitic accelerations ? 00077 float correction_speed; // Airspeed from which the correction should start 00078 }; 00079 00080 00084 static inline conf_t default_config() 00085 { 00086 conf_t conf = {}; 00087 00088 conf.beta = 0.06f; 00089 conf.zeta = 0.0f; 00090 conf.acceleration_correction = false; 00091 conf.correction_speed = 0.0f; 00092 00093 return conf; 00094 }; 00095 00096 00106 AHRS_madgwick(const Imu& imu, const Airspeed_analog& airspeed, const conf_t& config = default_config()); 00107 00108 00114 bool update(void); 00115 00116 00122 float last_update_s(void) const; 00123 00124 00132 bool is_healthy(void) const; 00133 00134 00140 quat_t attitude(void) const; 00141 00142 00148 std::array<float,3> angular_speed(void) const; 00149 00150 00156 std::array<float,3> linear_acceleration(void) const; 00157 00158 protected: 00159 const Imu& imu_; 00160 const Airspeed_analog& airspeed_; 00161 00162 quat_t attitude_; 00163 std::array<float,3> angular_speed_; 00164 std::array<float,3> linear_acc_; 00165 float last_update_s_; 00166 00167 float beta_; 00168 float zeta_; 00169 uint32_t acceleration_correction_; 00170 float correction_speed_; 00171 00172 private: 00173 // Global variables used in the Madgwick algorithm. 00174 // Quaternions used for the Madgwick algorithm. They are in the MADGWICK FRAME, not in the MAVRIC FRAME ! 00175 float q0_, q1_, q2_, q3_; 00176 00177 // Estimated gyro biases. Warning: they are also expressed in MADGWICK FRAME ! Transformation has to be made to express them in MAVRIC FRAME ! 00178 float w_bx_, w_by_, w_bz_; 00179 00196 void madgwick_algo(float gx, float gy, float gz, float ax, float ay, float az, float mx, float my, float mz, float beta, float zeta, float dt); 00197 }; 00198 00199 #endif /* AHRS_MADGWICK_HPP_ */