filter.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014-2017, the neonavigation authors
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the copyright holder nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #ifndef TRAJECTORY_TRACKER_FILTER_H
31 #define TRAJECTORY_TRACKER_FILTER_H
32 
33 #include <cassert>
34 #include <cmath>
35 
36 namespace trajectory_tracker
37 {
38 class Filter
39 {
40 public:
41  enum Type
42  {
45  };
46 
47 protected:
49  float time_const_;
50  float x_;
51  float out_;
52  float k_[4];
53  bool angle_;
54 
55 public:
56  Filter(const enum Type type, const float tc, const float out0, const bool angle = false)
57  {
58  angle_ = angle;
59  time_const_ = tc;
60  type_ = type;
61  switch (type_)
62  {
63  case FILTER_LPF:
64  k_[3] = -1 / (1.0 + 2 * time_const_);
65  k_[2] = -k_[3];
66  k_[1] = (1.0 - 2 * time_const_) * k_[3];
67  k_[0] = -k_[1] - 1.0;
68  x_ = (1 - k_[2]) * out0 / k_[3];
69  break;
70  case FILTER_HPF:
71  k_[3] = -1 / (1.0 + 2 * time_const_);
72  k_[2] = -k_[3] * 2 * time_const_;
73  k_[1] = (1.0 - 2 * time_const_) * k_[3];
74  k_[0] = 2 * time_const_ * (-k_[1] + 1.0);
75  x_ = (1 - k_[2]) * out0 / k_[3];
76  break;
77  }
78  out_ = out0;
79  }
80  void set(const float& out0)
81  {
82  x_ = (1 - k_[2]) * out0 / k_[3];
83  out_ = out0;
84  }
85  float in(const float& i)
86  {
87  float in = i;
88  assert(std::isfinite(in));
89 
90  if (angle_)
91  {
92  in = out_ + remainder(in - out_, M_PI * 2.0);
93  }
94  x_ = k_[0] * in + k_[1] * x_;
95  out_ = k_[2] * in + k_[3] * x_;
96 
97  assert(std::isfinite(out_));
98  return out_;
99  }
100  float get()
101  {
102  return out_;
103  }
104 };
105 } // namespace trajectory_tracker
106 
107 #endif // TRAJECTORY_TRACKER_FILTER_H
TFSIMD_FORCE_INLINE tfScalar angle(const Quaternion &q1, const Quaternion &q2)
float in(const float &i)
Definition: filter.h:85
Filter(const enum Type type, const float tc, const float out0, const bool angle=false)
Definition: filter.h:56


trajectory_tracker
Author(s): Atsushi Watanabe
autogenerated on Wed May 12 2021 02:20:40