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


urg_stamped
Author(s): Atsushi Watanabe
autogenerated on Tue May 11 2021 02:14:05