SimpleFOClibrary 2.4.0
Loading...
Searching...
No Matches
lowpass_filter.cpp
Go to the documentation of this file.
1#include "lowpass_filter.h"
2
3LowPassFilter::LowPassFilter(float time_constant, float sampling_time)
4 : Tf(time_constant)
5 , Ts(sampling_time)
6 , y_prev(0.0f)
7{
9}
10
11
13{
14 // initalise the elapsed time with the fixed sampling tims Ts
15 float dt = Ts;
16 // if Ts is not set, use adaptive sampling time
17 // calculate the ellapsed time dt
18 if(!_isset(dt)){
19 unsigned long timestamp = _micros();
20 dt = (timestamp - timestamp_prev)*1e-6f;
21
22 if (dt < 0.0f ) dt = 1e-3f;
23 else if(dt > 0.3f) {
24 y_prev = x;
26 return x;
27 }
29 }
30
31 // calculate the first order filer
32 float alpha = Tf/(Tf + dt);
33 float y = alpha*y_prev + (1.0f - alpha)*x;
34
35 // save the variables for the future steps
36 y_prev = y;
37 return y;
38}
float operator()(float x)
float y_prev
filtered value in previous execution step
float Ts
Fixed sampling time (optional default NOT_SET)
float Tf
Low pass filter time constant.
LowPassFilter(float Tf, float Ts=NOT_SET)
unsigned long timestamp_prev
Last execution timestamp.
float x
Definition foc_utils.cpp:54
#define _isset(a)
Definition foc_utils.h:13
unsigned long _micros()