MAV'RIC
/home/travis/build/lis-epfl/MAVRIC_Library/control/rate_controller.hpp
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 rate_controller.hpp
00034  *
00035  * \author MAV'RIC Team
00036  * \author Julien Lecoeur
00037  * \author Basil Huber
00038  *
00039  * \brief A controller for rate control
00040  *
00041  * TODO: update this text
00042  * \details It takes a command in attitude (roll/pitch/yaw or quaternion) as
00043  * input, and computes a torque command on roll pitch and yaw.
00044  * The inner PID loop controls the angular speed around the 3 axis. This inner
00045  * loop is fed by the outer PID loop which controls the attitude.
00046  * The error of the outer loop is computed using quaternion arithmetic, and thus
00047  * avoids gimbal locks as long as the attitude error is smaller than 90 degrees
00048  * on the pitch axis.
00049  *
00050  ******************************************************************************/
00051 
00052 
00053 #ifndef RATE_CONTROLLER_HPP_
00054 #define RATE_CONTROLLER_HPP_
00055 
00056 #include "control/controller.hpp"
00057 #include "control/pid_controller.hpp"
00058 #include "util/constants.hpp"
00059 #include "sensing/ahrs.hpp"
00060 
00061 
00062 class Rate_controller : public Controller<rate_command_t, torque_command_t>
00063 {
00064 public:
00065 
00069     struct conf_t
00070     {
00071         pid_controller_conf_t pid_config[3];   
00072     };
00073 
00074 
00080     static inline conf_t default_config();
00081 
00082 
00086     struct args_t
00087     {
00088         const AHRS&         ahrs;                  
00089         rate_command_t&       rate_command;          
00090         torque_command_t&     torque_command;        
00091     };
00092 
00093 
00100     Rate_controller(const args_t& args, const conf_t& config = default_config());
00101 
00102 
00108     bool update(void);
00109 
00110 
00118     bool set_command(const rate_command_t& command);
00119 
00120 
00128     bool get_command(rate_command_t& command) const;
00129 
00130 
00138     bool get_output(torque_command_t& command) const;
00139 
00140 
00146     pid_controller_t& get_pid_X(void);
00147     pid_controller_t& get_pid_Y(void);
00148     pid_controller_t& get_pid_Z(void);
00149 
00150 private:
00151     const AHRS&         ahrs_;                  
00152     rate_command_t&       rate_command_;          
00153     torque_command_t&     torque_command_;        
00154 
00155     pid_controller_t      pid_[3];                
00156     float                 dt_s_;                  
00157     float                 last_update_s_;         
00158 };
00159 
00160 Rate_controller::conf_t Rate_controller::default_config()
00161 {
00162     conf_t conf = {};
00163 
00164     // -----------------------------------------------------------------
00165     // ------ ROLL RATE PID --------------------------------------------
00166     // -----------------------------------------------------------------
00167     conf.pid_config[ROLL]                           = {};
00168     conf.pid_config[ROLL].p_gain                    = 0.07f;
00169     conf.pid_config[ROLL].clip_min                  = -0.9f;
00170     conf.pid_config[ROLL].clip_max                  = 0.9f;
00171     conf.pid_config[ROLL].integrator                = {};
00172     conf.pid_config[ROLL].integrator.gain           = 0.125f;
00173     conf.pid_config[ROLL].integrator.clip_pre       = 6.0f;
00174     conf.pid_config[ROLL].integrator.clip           = 0.3f;
00175     conf.pid_config[ROLL].differentiator            = {};
00176     conf.pid_config[ROLL].differentiator.gain       = 0.008f;
00177     conf.pid_config[ROLL].differentiator.clip       = 0.14f;
00178     conf.pid_config[ROLL].soft_zone_width           = 0.0f;
00179     // -----------------------------------------------------------------
00180     // ------ PITCH RATE PID -------------------------------------------
00181     // -----------------------------------------------------------------
00182     conf.pid_config[PITCH]                          = {};
00183     conf.pid_config[PITCH].p_gain                   = 0.07f;
00184     conf.pid_config[PITCH].clip_min                 = -0.9f;
00185     conf.pid_config[PITCH].clip_max                 = 0.9f;
00186     conf.pid_config[PITCH].integrator               = {};
00187     conf.pid_config[PITCH].integrator.gain          = 0.125f,
00188     conf.pid_config[PITCH].integrator.clip_pre      = 6.0f;
00189     conf.pid_config[PITCH].integrator.clip          = 0.3f;
00190     conf.pid_config[PITCH].differentiator           = {};
00191     conf.pid_config[PITCH].differentiator.gain      = 0.008f;
00192     conf.pid_config[PITCH].differentiator.clip      = 0.14f;
00193     conf.pid_config[PITCH].soft_zone_width          = 0.0f;
00194     // -----------------------------------------------------------------
00195     // ------ YAW RATE PID ---------------------------------------------
00196     // -----------------------------------------------------------------
00197     conf.pid_config[YAW]                            = {};
00198     conf.pid_config[YAW].p_gain                     = 0.3f;
00199     conf.pid_config[YAW].clip_min                   = -0.3f;
00200     conf.pid_config[YAW].clip_max                   = 0.3f;
00201     conf.pid_config[YAW].integrator                 = {};
00202     conf.pid_config[YAW].integrator.gain            = 0.075f;
00203     conf.pid_config[YAW].integrator.clip_pre        = 1.0f;
00204     conf.pid_config[YAW].integrator.clip            = 0.045f;
00205     conf.pid_config[YAW].differentiator             = {};
00206     conf.pid_config[YAW].differentiator.gain        = 0.0f;
00207     conf.pid_config[YAW].differentiator.clip        = 0.0f;
00208     conf.pid_config[YAW].soft_zone_width            = 0.0;
00209 
00210     return conf;
00211 };
00212 
00213 #endif /* RATE_CONTROLLER_HPP_ */
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines