SimpleFOClibrary  2.1
lowpass_filter.cpp
Go to the documentation of this file.
1 #include "lowpass_filter.h"
2 
3 LowPassFilter::LowPassFilter(float time_constant)
4  : Tf(time_constant)
5  , y_prev(0.0f)
6 {
8 }
9 
10 
12 {
13  unsigned long timestamp = _micros();
14  float dt = (timestamp - timestamp_prev)*1e-6f;
15 
16  if (dt < 0.0f || dt > 0.5f)
17  dt = 1e-3f;
18 
19  float alpha = Tf/(Tf + dt);
20  float y = alpha*y_prev + (1.0f - alpha)*x;
21 
22  y_prev = y;
23  timestamp_prev = timestamp;
24  return y;
25 }
LowPassFilter::y_prev
float y_prev
filtered value in previous execution step
Definition: lowpass_filter.h:25
lowpass_filter.h
_micros
unsigned long _micros()
Definition: time_utils.cpp:21
LowPassFilter::timestamp_prev
unsigned long timestamp_prev
Last execution timestamp.
Definition: lowpass_filter.h:24
LowPassFilter::operator()
float operator()(float x)
Definition: lowpass_filter.cpp:11
LowPassFilter::Tf
float Tf
Low pass filter time constant.
Definition: lowpass_filter.h:21
LowPassFilter::LowPassFilter
LowPassFilter(float Tf)
Definition: lowpass_filter.cpp:3