robotoc
robotoc - efficient ROBOT Optimal Control solvers
Loading...
Searching...
No Matches
moving_window_filter.hpp
Go to the documentation of this file.
1#ifndef ROBOTOC_MOVING_WINDOW_FILTER_HPP_
2#define ROBOTOC_MOVING_WINDOW_FILTER_HPP_
3
4#include <stdexcept>
5#include <iostream>
6#include <cassert>
7
9
10namespace robotoc {
11
16template <int dim>
18public:
19 using Vector = Eigen::Matrix<double, dim, 1>;
20
27 MovingWindowFilter(const double time_length,
28 const double min_sampling_period=0.0)
29 : time_length_(time_length),
30 min_sampling_period_(min_sampling_period),
31 last_sampling_time_(0.0),
32 time_(),
33 data_(),
34 average_(Vector::Zero()) {
35 if (time_length <= 0.0) {
36 throw std::out_of_range("[MovingWindowFilter] invalid argument: 'time_length' must be positive!");
37 }
38 if (min_sampling_period < 0) {
39 throw std::out_of_range("[MovingWindowFilter] invalid argument: 'min_sampling_period' must be non-negative!");
40 }
41 }
42
47 : time_length_(0),
48 min_sampling_period_(0.0),
49 last_sampling_time_(0.0),
50 time_(),
51 data_(),
52 average_(Vector::Zero()) {
53 }
54
59
64
69
74
78 MovingWindowFilter& operator=(MovingWindowFilter&&) noexcept = default;
79
86 void setParameters(const double time_length,
87 const double min_sampling_period=0.0) {
88 if (time_length <= 0.0) {
89 throw std::out_of_range("[MovingWindowFilter] invalid argument: 'time_length' must be positive!");
90 }
91 if (min_sampling_period < 0) {
92 throw std::out_of_range("[MovingWindowFilter] invalid argument: 'min_sampling_period' must be non-negative!");
93 }
94 time_length_ = time_length;
95 min_sampling_period_ = min_sampling_period;
96 }
97
101 void clear() {
102 last_sampling_time_ = 0.0;
103 time_.clear();
104 data_.clear();
105 average_.setZero();
106 }
107
113 void push_back(const double t, const Vector& data) {
114 if (time_.empty()) {
115 time_.push_back(t);
116 data_.push_back(data);
117 average_ = data;
118 last_sampling_time_ = t;
119 }
120 else {
121 if (t - last_sampling_time_ >= min_sampling_period_) {
122 time_.push_back(t);
123 data_.push_back(data);
124 while (t - time_.front() > time_length_) {
125 time_.pop_front();
126 data_.pop_front();
127 }
128 average_.setZero();
129 for (const auto& e : data_) {
130 average_.noalias() += e;
131 }
132 average_.array() /= data_.size();
133 last_sampling_time_ = t;
134 }
135 }
136 }
137
141 int size() const {
142 return data_.size();
143 }
144
148 const Vector& average() const {
149 return average_;
150 }
151
152 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
153
154private:
155 double time_length_, min_sampling_period_, last_sampling_time_;
156 std::deque<double> time_;
158 Vector average_;
159
160};
161
162} // namespace robotoc
163
164#endif // ROBOTOC_MOVING_WINDOW_FILTER_HPP_
Moving window filter for foot step planning.
Definition: moving_window_filter.hpp:17
int size() const
Gets the current data size.
Definition: moving_window_filter.hpp:141
const Vector & average() const
Gets the average.
Definition: moving_window_filter.hpp:148
MovingWindowFilter(const double time_length, const double min_sampling_period=0.0)
Constructs the filter.
Definition: moving_window_filter.hpp:27
void push_back(const double t, const Vector &data)
Push back a data.
Definition: moving_window_filter.hpp:113
MovingWindowFilter(MovingWindowFilter &&) noexcept=default
Default move constructor.
MovingWindowFilter()
Default constructor.
Definition: moving_window_filter.hpp:46
MovingWindowFilter(const MovingWindowFilter &)=default
Default copy constructor.
void clear()
Clear the filter.
Definition: moving_window_filter.hpp:101
Eigen::Matrix< double, dim, 1 > Vector
Definition: moving_window_filter.hpp:19
MovingWindowFilter & operator=(const MovingWindowFilter &)=default
Default copy assign operator.
~MovingWindowFilter()
Destructor.
Definition: moving_window_filter.hpp:58
void setParameters(const double time_length, const double min_sampling_period=0.0)
Set parameters of the filter.
Definition: moving_window_filter.hpp:86
Definition: constraint_component_base.hpp:17
std::deque< T, Eigen::aligned_allocator< T > > aligned_deque
std deque with Eigen::aligned_allocator.
Definition: aligned_deque.hpp:14