moving_average.h
Go to the documentation of this file.
1 /*
2  * Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 
18 #ifndef COB_TWIST_CONTROLLER_UTILS_MOVING_AVERAGE_H
19 #define COB_TWIST_CONTROLLER_UTILS_MOVING_AVERAGE_H
20 
21 #include <stdint.h>
22 #include <ros/ros.h>
23 #include <deque>
24 
25 template
26 <typename T>
28 {
29  public:
30  explicit MovingAverageBase()
31  {}
32 
33  virtual void reset() = 0;
34  virtual void addElement(T element) = 0;
35  virtual bool calcMovingAverage(T& average) const = 0;
36 };
37 
38 template
39 <typename T>
41 {
42  public:
43  explicit MovingAverageSimple(uint16_t size)
44  : MovingAverageBase<T>(),
45  size_(size)
46  {
47  weighting_.assign(size_, 1.0);
48  }
49 
50  virtual void reset()
51  {
52  s_.clear();
53  }
54 
55  virtual void addElement(T element)
56  {
57  if (s_.size() < size_)
58  {
59  s_.push_front(element);
60  }
61  else
62  {
63  s_.pop_back();
64  s_.push_front(element);
65  }
66  }
67 
68  virtual bool calcMovingAverage(T& average) const
69  {
70  if (!s_.empty())
71  {
72  T sum;
73  T diff;
74  for (uint16_t i = 0; i < s_.size(); ++i)
75  {
76  sum += s_[i] * weighting_[i];
77  diff += weighting_[i];
78  }
79  average = sum / diff;
80  return true;
81  }
82  else
83  {
84  // no element available
85  return false;
86  }
87  }
88 
89  protected:
90  uint16_t size_;
91  std::deque<T> s_;
92  std::deque<double> weighting_;
93 };
94 
95 template
96 <typename T>
98 {
99  public:
100  explicit MovingAverageWeighted(uint16_t size)
101  : MovingAverageSimple<T>(size)
102  {
103  this->weighting_.clear();
104  for (uint16_t i = 0; i < this->size_; i++)
105  {
106  this->weighting_.push_front(triangle(i));
107  }
108  }
109 
110  private:
111  double triangle(uint16_t n)
112  {
113  if (n == 0)
114  {
115  return 0.0;
116  }
117  else
118  {
119  return static_cast<double>(n)*(static_cast<double>(n)+1.0)/2.0;
120  }
121  }
122 };
123 
124 template
125 <typename T>
127 {
128  public:
129  explicit MovingAverageExponential(double factor)
130  : MovingAverageBase<T>(),
131  factor_(factor)
132  {
133  empty_ = true;
134  }
135 
136  virtual void reset()
137  {
138  average_ = T();
139  empty_ = true;
140  }
141 
142  void addElement(T element)
143  {
144  if (empty_)
145  {
146  average_ = element;
147  empty_ = false;
148  }
149  else
150  {
151  average_ = factor_ * element + (1.0 - factor_) * average_;
152  }
153  }
154 
155  bool calcMovingAverage(T& average) const
156  {
157  if (!empty_)
158  {
159  average = average_;
160  return true;
161  }
162  else
163  {
164  // no element available
165  return false;
166  }
167  }
168 
169  private:
170  bool empty_;
171  double factor_;
173 };
174 
175 
180 
181 #endif // COB_TWIST_CONTROLLER_UTILS_MOVING_AVERAGE_H
MovingAverageExponential< double > MovingAvgExponential_double_t
MovingAverageWeighted< double > MovingAvgWeighted_double_t
virtual void addElement(T element)=0
virtual void reset()=0
IMETHOD Vector diff(const Vector &p_w_a, const Vector &p_w_b, double dt=1)
MovingAverageWeighted(uint16_t size)
MovingAverageBase< double > MovingAvgBase_double_t
MovingAverageExponential(double factor)
virtual bool calcMovingAverage(T &average) const =0
virtual void reset()
bool calcMovingAverage(T &average) const
MovingAverageSimple(uint16_t size)
void addElement(T element)
std::deque< double > weighting_
double triangle(uint16_t n)
virtual bool calcMovingAverage(T &average) const
MovingAverageSimple< double > MovingAvgSimple_double_t
virtual void addElement(T element)
std::deque< T > s_


cob_twist_controller
Author(s): Felix Messmer , Marco Bezzon , Christoph Mark , Francisco Moreno
autogenerated on Thu Apr 8 2021 02:40:00