LowPass.h
Go to the documentation of this file.
1 /*
2  * Copyright 2015-2019 CNRS-UM LIRMM, CNRS-AIST JRL
3  *
4  * This file is inspired by Stephane's Caron implementation as part of
5  * lipm_walking_controller <https://github.com/stephane-caron/lipm_walking_controller>
6  */
7 
8 #pragma once
9 
10 #include <mc_rtc/logging.h>
11 #include <algorithm>
12 
13 namespace mc_filter
14 {
15 
21 template<typename T>
22 struct LowPass
23 {
31  LowPass(double dt, double period = 0) : cutoffPeriod_(period), dt_(dt) { reset(T::Zero()); }
32 
34  double cutoffPeriod() const { return cutoffPeriod_; }
35 
43  void cutoffPeriod(double period)
44  {
45  if(period < 2 * dt_)
46  {
47  mc_rtc::log::warning("Time constant must be at least twice the timestep (Nyquist–Shannon sampling theorem)");
48  period = 2 * dt_;
49  }
50  cutoffPeriod_ = period;
51  }
52 
58  void reset(const T & value) { eval_ = value; }
59 
65  void update(const T & newValue)
66  {
67  double x = (cutoffPeriod_ <= dt_) ? 1. : dt_ / cutoffPeriod_;
68  eval_ = x * newValue + (1. - x) * eval_;
69  }
70 
74  const T & eval() const { return eval_; }
75 
79  double dt() const { return dt_; }
80 
88  void dt(double dt)
89  {
90  dt_ = dt;
91  cutoffPeriod(cutoffPeriod_);
92  }
93 
94 private:
95  T eval_;
96  double cutoffPeriod_ = 0.;
97 
98 protected:
99  double dt_ = 0.005; // [s]
100 };
101 
102 } // namespace mc_filter
mc_filter::LowPass::dt
double dt() const
Definition: LowPass.h:79
mc_filter::LowPass::dt
void dt(double dt)
Definition: LowPass.h:88
mc_filter
Definition: ExponentialMovingAverage.h:14
mc_filter::LowPass
Definition: LowPass.h:22
mc_filter::LowPass::cutoffPeriod
double cutoffPeriod() const
Definition: LowPass.h:34
mc_filter::LowPass::update
void update(const T &newValue)
Definition: LowPass.h:65
mc_rtc::log::warning
void warning(Args &&... args)
Definition: logging.h:69
mc_filter::LowPass::dt_
double dt_
Definition: LowPass.h:99
mc_filter::LowPass::LowPass
LowPass(double dt, double period=0)
Definition: LowPass.h:31
logging.h
mc_filter::LowPass::eval
const T & eval() const
Definition: LowPass.h:74
mc_filter::LowPass::reset
void reset(const T &value)
Definition: LowPass.h:58
mc_filter::LowPass::cutoffPeriod
void cutoffPeriod(double period)
Definition: LowPass.h:43