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 pid_control.hpp 00034 * 00035 * \author MAV'RIC Team 00036 * \author Felix Schill 00037 * 00038 * \brief PID controller 00039 * 00040 ******************************************************************************/ 00041 00042 00043 #ifndef PID_CONTROL_HPP_ 00044 #define PID_CONTROL_HPP_ 00045 00046 #include <cstdint> 00047 #include <cstdbool> 00048 #include <math.h> 00049 00050 00054 typedef struct 00055 { 00056 float gain; 00057 float accumulator; 00058 float clip_pre; 00059 float clip; 00060 } integrator_t; 00061 00062 00066 typedef struct 00067 { 00068 float gain; 00069 float previous; 00070 float clip; 00071 } differentiator_t; 00072 00073 00077 typedef struct 00078 { 00079 float p_gain; 00080 float clip_min; 00081 float clip_max; 00082 integrator_t integrator; 00083 differentiator_t differentiator; 00084 float soft_zone_width; 00085 } pid_controller_conf_t; 00086 00087 00091 typedef struct 00092 { 00093 float p_gain; 00094 float clip_min; 00095 float clip_max; 00096 integrator_t integrator; 00097 differentiator_t differentiator; 00098 float output; 00099 float error; 00100 float last_update_s; 00101 float dt_s; 00102 float soft_zone_width; 00103 bool is_saturated; 00104 } pid_controller_t; 00105 00106 00115 bool pid_controller_init(pid_controller_t* controller, const pid_controller_conf_t* config); 00116 00117 00127 bool pid_controller_apply_config(pid_controller_t* controller, const pid_controller_conf_t* config); 00128 00129 00135 void pid_controller_init_pass_through(pid_controller_t* controller); 00136 00137 00143 void pid_controller_reset_integrator(pid_controller_t* controller); 00144 00145 00154 float pid_controller_update(pid_controller_t* controller, float error); 00155 00156 00166 float pid_controller_update_dt(pid_controller_t* controller, float error, float dt); 00167 00168 00169 #endif /* PID_CONTROL_HPP_ */