00001 #include <cstdlib> // for std::abs on integers 00002 #include "robodyn_utilities/RateLimiter.h" 00003 00004 RateLimiter::RateLimiter(): 00005 rateLimit(1.0), 00006 complete(0) 00007 { 00008 } 00009 00010 RateLimiter::~RateLimiter() 00011 { 00012 } 00013 00014 void RateLimiter::setRateLimit(double lim) 00015 { 00016 rateLimit = lim; 00017 } 00018 00019 double RateLimiter::getLimitedValue(double current, double previous) 00020 { 00021 double diff = current - previous; 00022 double output = current; 00023 00024 if (diff < -rateLimit) 00025 { 00026 output = previous - rateLimit; 00027 } 00028 00029 if (diff > rateLimit) 00030 { 00031 output = previous + rateLimit; 00032 } 00033 00034 setCompletionCondition(diff, rateLimit); 00035 00036 return output; 00037 } 00038 00039 int RateLimiter::getLimitedValue(int current, int previous) 00040 { 00041 int diff = current - previous; 00042 00043 int lim = (int)rateLimit; 00044 00045 if (lim < 1) 00046 { 00047 lim = 1; 00048 } 00049 00050 int output = current; 00051 00052 if (diff < -lim) 00053 { 00054 output = previous - lim; 00055 } 00056 00057 if (diff > lim) 00058 { 00059 output = previous + lim; 00060 } 00061 00062 setCompletionCondition(diff, lim); 00063 00064 return output; 00065 } 00066 00067 int RateLimiter::getCompletionCondition() 00068 { 00069 return complete; 00070 } 00071 00072 void RateLimiter::setCompletionCondition(double diff, double lim) 00073 { 00074 if (std::abs(diff) > lim) 00075 { 00076 complete = 0; // false 00077 } 00078 else // diff < lim 00079 { 00080 complete = 1; 00081 } 00082 } 00083 00084 void RateLimiter::setCompletionCondition(int diff, int lim) 00085 { 00086 if (std::abs(diff) > lim) 00087 { 00088 complete = 0; // false 00089 } 00090 else // diff < lim 00091 { 00092 complete = 1; // true 00093 } 00094 }