ros2_control - galactic
Loading...
Searching...
No Matches
rolling_mean_accumulator.hpp
1// Copyright 2020 PAL Robotics S.L.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15/*
16 * Author: Víctor López
17 */
18
19#ifndef DIFF_DRIVE_CONTROLLER__ROLLING_MEAN_ACCUMULATOR_HPP_
20#define DIFF_DRIVE_CONTROLLER__ROLLING_MEAN_ACCUMULATOR_HPP_
21
22#include <cassert>
23#include <cstdio>
24#include <vector>
25
26namespace diff_drive_controller
27{
34template <typename T>
36{
37public:
38 explicit RollingMeanAccumulator(size_t rolling_window_size)
39 : buffer_(rolling_window_size, 0.0), next_insert_(0), sum_(0.0), buffer_filled_(false)
40 {
41 }
42
43 void accumulate(T val)
44 {
45 sum_ -= buffer_[next_insert_];
46 sum_ += val;
47 buffer_[next_insert_] = val;
48 next_insert_++;
49 buffer_filled_ |= next_insert_ >= buffer_.size();
50 next_insert_ = next_insert_ % buffer_.size();
51 }
52
53 T getRollingMean() const
54 {
55 size_t valid_data_count = buffer_filled_ * buffer_.size() + !buffer_filled_ * next_insert_;
56 assert(valid_data_count > 0);
57 return sum_ / valid_data_count;
58 }
59
60private:
61 std::vector<T> buffer_;
62 size_t next_insert_;
63 T sum_;
64 bool buffer_filled_;
65};
66} // namespace diff_drive_controller
67#endif // DIFF_DRIVE_CONTROLLER__ROLLING_MEAN_ACCUMULATOR_HPP_
Simplification of boost::accumulators::accumulator_set<double, bacc::stats<bacc::tag::rolling_mean>> ...
Definition rolling_mean_accumulator.hpp:36