sr_math_utils.hpp
Go to the documentation of this file.
1 /*
2 * Copyright 2011 Shadow Robot Company Ltd.
3 *
4 * This program is free software: you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the Free
6 * Software Foundation version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16 
28 #ifndef _SR_MATH_UTILS_HPP_
29 #define _SR_MATH_UTILS_HPP_
30 
31 #include <boost/random/mersenne_twister.hpp>
32 #include <boost/random/uniform_int.hpp>
33 #include <boost/random/uniform_real.hpp>
34 #include <boost/thread/detail/singleton.hpp>
35 
36 #include <utility>
37 
38 #include <ros/ros.h>
39 
40 namespace sr_math_utils
41 {
42 static const double pi = 3.14159265;
43 
51 static inline double to_rad(double degrees)
52 {
53  return degrees * 0.017453292519943295;
54 }
55 
63 static inline double to_degrees(double rad)
64 {
65  return rad * 57.295779513082323;
66 }
67 
68 static inline int ipow(int base, int exp)
69 {
70  int result = 1;
71  while (exp)
72  {
73  if (exp & 1)
74  {
75  result *= base;
76  }
77  exp >>= 1;
78  base *= base;
79  }
80 
81  return result;
82 }
83 
84 static inline bool is_bit_mask_index_true(int64_t bit_mask, int index)
85 {
86  if (bit_mask & (((int64_t) 1) << index))
87  {
88  return true;
89  }
90  else
91  {
92  return false;
93  }
94 }
95 
96 static inline bool is_bit_mask_index_false(int64_t bit_mask, int index)
97 {
98  return !(is_bit_mask_index_true(bit_mask, index));
99 }
100 
112 static inline uint64_t counter_with_overflow(uint64_t full_value, uint16_t new_value)
113 {
114  uint16_t last_value = full_value & 0xFFFF; // Split the full value into the lower part
115  full_value &= (uint64_t) 0xFFFFFFFFFFFF0000LL; // and the overflow part
116 
117  if (new_value < last_value)
118  { // if we overflowed
119  full_value += (uint64_t) 0x0000000000010000LL;
120  } // then count the overflow
121 
122  full_value |= (uint64_t) new_value; // replace the bottom 16 bits with their new value
123 
124  return full_value;
125 }
126 
140 static inline double linear_interpolate_(double x,
141  double x0, double y0,
142  double x1, double y1)
143 {
144  // y1 - y0
145  double y = y1 - y0;
146  // (y1 - y0) / (x1 - x0)
147  y /= (x1 - x0);
148  // (x-x0)*((y1-y0)/(x1-x0))
149  y *= (x - x0);
150  // y0 + (x-x0)*((y1-y0)/(x1-x0))
151  y += y0;
152 
153  return y;
154 }
155 
156 namespace filters
157 {
158 
160 {
161 public:
162  explicit LowPassFilter(double tau = 0.05)
163  : is_first(true), dt(0.0),
164  timestamp_1(0.0), q_prev(0.0),
165  tau(tau), value_derivative(0.0, 0.0)
166  {
167  };
168 
179  std::pair<double, double> compute(double q, double timestamp)
180  {
181  if (is_first)
182  {
183  q_prev = q;
184  // initializing dt to 1ms
185  dt = 0.001;
186 
187  is_first = false;
188  }
189  else
190  {
191  dt = timestamp - timestamp_1;
192  }
193 
194  double alpha = 0.0;
195  if (tau > 0.0 && dt > 0.0)
196  alpha = exp(-dt / tau);
197 
198  // filtering the input
199  value_derivative.first = alpha * value_derivative.first + (1 - alpha) * q;
200  // filtering the derivative
201  value_derivative.second = alpha * value_derivative.second + (1 - alpha) / dt * (value_derivative.first - q_prev);
202 
203  q_prev = value_derivative.first;
204  timestamp_1 = timestamp;
205 
206  return value_derivative;
207  };
208 
209 private:
210  bool is_first;
212 
213  std::pair<double, double> value_derivative;
214 };
215 
222 {
223 public:
224  explicit AlphaBetaFilter(double alpha = 0.85, double beta = 0.05)
225  : a(alpha), b(beta),
226  xk_1(0.0), vk_1(0.0), xk(0.0), vk(0.0), rk(0.0),
227  dt(0.0), timestamp_1(0.0)
228  {
229  };
230 
241  std::pair<double, double> compute(double xm, double timestamp)
242  {
243  dt = timestamp - timestamp_1;
244 
245  xk = xk_1 + (vk_1 * dt);
246  vk = vk_1;
247 
248  rk = xm - xk;
249 
250  xk += a * rk;
251  vk += (b * rk) / dt;
252 
253  value_derivative.first = xk;
254  value_derivative.second = vk;
255 
256  xk_1 = xk;
257  vk_1 = vk;
258  timestamp_1 = timestamp;
259 
260  return value_derivative;
261  };
262 
263 protected:
264  double a, b;
265  double xk_1, vk_1, xk, vk, rk;
266  double dt, timestamp_1;
267 
268  std::pair<double, double> value_derivative;
269 };
270 } // namespace filters
271 
279 static inline int sign(double x)
280 {
281  return x < 0.0 ? -1 : 1;
282 }
284 // FILTERS //
286 
287 
289 // RANDOM //
291 
296 class Random_
297 {
298 public:
300  dist(boost::uniform_real<>(0.0, 1.0))
301  {
302  };
303 
311  template<typename T>
312  T generate(T min = static_cast<T> (0), T max = static_cast<T> (1))
313  {
314  return static_cast<T> (min + dist(gen) * (max - min));
315  }
316 
317 protected:
318  boost::mt19937 gen;
319  boost::uniform_real<> dist;
320 };
321 
335 typedef boost::detail::thread::singleton<class Random_> Random;
336 } // namespace sr_math_utils
337 
338 /* For the emacs weenies in the crowd.
339 Local Variables:
340  c-basic-offset: 2
341 End:
342  */
343 
344 #endif
static int sign(double x)
static uint64_t counter_with_overflow(uint64_t full_value, uint16_t new_value)
static double to_degrees(double rad)
T generate(T min=static_cast< T >(0), T max=static_cast< T >(1))
std::pair< double, double > value_derivative
double y
static int ipow(int base, int exp)
boost::uniform_real dist
static double to_rad(double degrees)
std::pair< double, double > compute(double xm, double timestamp)
static const double pi
std::pair< double, double > value_derivative
double min(double a, double b)
static double linear_interpolate_(double x, double x0, double y0, double x1, double y1)
INLINE Rall1d< T, V, S > exp(const Rall1d< T, V, S > &arg)
boost::detail::thread::singleton< class Random_ > Random
AlphaBetaFilter(double alpha=0.85, double beta=0.05)
static bool is_bit_mask_index_true(int64_t bit_mask, int index)
list a
static bool is_bit_mask_index_false(int64_t bit_mask, int index)
std::pair< double, double > compute(double q, double timestamp)
double max(double a, double b)


sr_utilities
Author(s): Ugo Cupcic
autogenerated on Mon Feb 28 2022 23:52:19