timestamp_moving_average.h
Go to the documentation of this file.
1 /*
2  * Copyright 2018 The urg_stamped Authors
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 #ifndef TIMESTAMP_MOVING_AVERAGE_H
18 #define TIMESTAMP_MOVING_AVERAGE_H
19 
20 #include <ros/ros.h>
21 
22 #include <cmath>
23 #include <vector>
24 
26 {
27 protected:
28  size_t window_size_;
30  std::vector<ros::Time> buffer_;
31  size_t pos_;
32 
33 public:
35  const size_t window_size,
36  const ros::Duration &interval)
37  : window_size_(window_size)
38  , interval_(interval)
39  , pos_(0)
40  {
41  buffer_.resize(window_size);
42  }
43  void setInterval(const ros::Duration &interval)
44  {
45  interval_ = interval;
46  }
47  ros::Time update(const ros::Time &stamp)
48  {
49  buffer_[pos_ % window_size_] = stamp;
50  pos_++;
51  if (pos_ < window_size_)
52  return stamp;
53 
54  ros::Duration sum(0);
55  for (const ros::Time &b : buffer_)
56  {
57  sum += ros::Duration(remainder((b - stamp).toSec(), interval_.toSec()));
58  }
59  return stamp + sum * (1.0 / window_size_);
60  }
61  void reset()
62  {
63  pos_ = 0;
64  }
65 };
66 
67 #endif // TIMESTAMP_MOVING_AVERAGE_H
ros::Time update(const ros::Time &stamp)
std::vector< ros::Time > buffer_
void setInterval(const ros::Duration &interval)
TimestampMovingAverage(const size_t window_size, const ros::Duration &interval)


urg_stamped
Author(s): Atsushi Watanabe
autogenerated on Thu Jun 6 2019 19:55:59