EEROS  1.0.0.0
API for the EEROS Real-Time Robotics Framework
MedianFilter.hpp
Go to the documentation of this file.
1 #ifndef ORG_EEROS_CONTROL_MEDIANFILTER_HPP_
2 #define ORG_EEROS_CONTROL_MEDIANFILTER_HPP_
3 
5 #include <algorithm>
6 #include <type_traits>
7 #include <cmath>
8 
9 
10 namespace eeros {
11  namespace control {
12 
37  template <size_t N, typename Tval = double>
38  class MedianFilter : public Block1i1o<Tval> {
39 
40  public:
45  zeroInitCurrentValues<Tval>();
46  }
47 
48 
64  virtual void run() {
65  for(size_t i = 0; i < N-1; i++) {
66  currentValues[i] = currentValues[i+1];
67  }
68 
69  currentValues[N-1] = this->in.getSignal().getValue();
70 
71  if(enabled) {
72  Tval temp[N]{};
73 
74  std::copy(std::begin(currentValues), std::end(currentValues), std::begin(temp));
75  std::sort(std::begin(temp), std::end(temp));
76 
79 
80  } else {
81  this->out.getSignal().setValue(this->in.getSignal().getValue());
82  }
83 
85  }
86 
87 
96  virtual void enable() {
97  enabled = true;
98  }
99 
100 
108  virtual void disable() {
109  enabled = false;
110  }
111 
112 
113  /*
114  * Friend operator overload to give the operator overload outside
115  * the class access to the private fields.
116  */
117  template <size_t No, typename ValT>
118  friend std::ostream& operator<<(std::ostream& os, MedianFilter<No,ValT>& filter);
119 
120 
121  protected:
122  Tval currentValues[N]{};
124  bool enabled{true};
125  constexpr static int medianIndex{static_cast<int>(floor(N/2))};
126 
127 
128  private:
129  template <typename S>
130  typename std::enable_if<std::is_arithmetic<S>::value>::type zeroInitCurrentValues() {
131  // is zeroed when initialized by default.
132  }
133 
134 
135  template <typename S>
136  typename std::enable_if<!std::is_arithmetic<S>::value>::type zeroInitCurrentValues() {
137  for(size_t i = 0; i < N; i++) {
138  currentValues[i].zero();
139  }
140  }
141  };
142 
143 
149  template <size_t N, typename Tval>
150  std::ostream& operator<<(std::ostream& os, MedianFilter<N,Tval>& filter) {
151  os << "Block MedianFilter: '" << filter.getName() << "' is enabled=";
152  os << filter.enabled << ", ";
153 
154  os << "current median=" << filter.currentMedianValue << ", ";
155 
156  os << "medianIndex=" << filter.medianIndex << ", ";
157 
158  os << "current values:[" << filter.currentValues[0];
159  for(size_t i = 1; i < N; i++){
160  os << "," << filter.currentValues[i];
161  }
162  os << "]";
163  }
164  };
165 };
166 
167 #endif /* ORG_EEROS_CONTROL_MEDIANFILTER_HPP_ */
Tval currentMedianValue
Definition: MedianFilter.hpp:123
virtual Signal< T > & getSignal()
Definition: Output.hpp:16
virtual void enable()
Definition: MedianFilter.hpp:96
virtual T getValue() const
Definition: Signal.hpp:49
Input< Tval > in
Definition: Block1i1o.hpp:27
Definition: Config.hpp:14
Output< Tval > out
Definition: Block1i1o.hpp:28
virtual void setValue(T newValue)
Definition: Signal.hpp:53
virtual void setTimestamp(timestamp_t newTimestamp)
Definition: Signal.hpp:66
Definition: MedianFilter.hpp:38
virtual void disable()
Definition: MedianFilter.hpp:108
static constexpr int medianIndex
Definition: MedianFilter.hpp:125
Definition: Block1i1o.hpp:12
virtual Signal< T > & getSignal()
Definition: Input.hpp:38
Tval currentValues[N]
Definition: MedianFilter.hpp:122
virtual timestamp_t getTimestamp() const
Definition: Signal.hpp:62
bool enabled
Definition: MedianFilter.hpp:124
MedianFilter()
Definition: MedianFilter.hpp:44
virtual void run()
Definition: MedianFilter.hpp:64