Libbarrett
1.2.4
|
00001 /* 00002 Copyright 2012 Barrett Technology <support@barrett.com> 00003 00004 This file is part of libbarrett. 00005 00006 This version of libbarrett is free software: you can redistribute it 00007 and/or modify it under the terms of the GNU General Public License as 00008 published by the Free Software Foundation, either version 3 of the 00009 License, or (at your option) any later version. 00010 00011 This version of libbarrett is distributed in the hope that it will be 00012 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 GNU General Public License for more details. 00015 00016 You should have received a copy of the GNU General Public License along 00017 with this version of libbarrett. If not, see 00018 <http://www.gnu.org/licenses/>. 00019 00020 Further, non-binding information about licensing is available at: 00021 <http://wiki.barrett.com/libbarrett/wiki/LicenseNotes> 00022 */ 00023 00024 /* 00025 * rate_limiter-inl.h 00026 * 00027 * Created on: Apr 12, 2012 00028 * Author: dc 00029 */ 00030 00031 #include <cassert> 00032 00033 #include <barrett/math/utils.h> 00034 #include <barrett/systems/rate_limiter.h> 00035 00036 00037 namespace barrett { 00038 namespace systems { 00039 00040 00041 template<typename T, typename MathTraits> 00042 void RateLimiter<T,MathTraits>::setLimit(const T& newLimit) 00043 { 00044 // Limit must be non-negative. Zero is a special value meaning "don't limit". 00045 assert(newLimit == math::abs(newLimit)); 00046 limit = newLimit; 00047 maxDelta = T_s * limit; 00048 } 00049 00050 template<typename T, typename MathTraits> 00051 void RateLimiter<T,MathTraits>::setCurVal(const T& newPos) 00052 { 00053 data = newPos; 00054 } 00055 00056 template<typename T, typename MathTraits> 00057 void RateLimiter<T,MathTraits>::operate() 00058 { 00059 const T& x = this->input.getValue(); 00060 00061 if (limit == MT::zero()) { 00062 data = x; 00063 } else { 00064 delta = MT::sub(x, data); 00065 data += MT::mult(math::sign(delta), math::min(math::abs(delta), maxDelta)); 00066 } 00067 00068 this->outputValue->setData(&data); 00069 } 00070 00071 // TODO(dc): anyway to remove the code duplication with PIDController? 00072 template<typename T, typename MathTraits> 00073 void RateLimiter<T,MathTraits>::getSamplePeriodFromEM() 00074 { 00075 if (this->hasExecutionManager()) { 00076 assert(this->getExecutionManager()->getPeriod() > 0.0); 00077 T_s = this->getExecutionManager()->getPeriod(); 00078 } else { 00079 T_s = 0.0; 00080 } 00081 setLimit(limit); // Update maxDelta 00082 } 00083 00084 00085 } 00086 }