test_filter.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016-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 #include <cmath>
31 #include <cstddef>
32 
34 
35 #include <gtest/gtest.h>
36 
37 TEST(Filter, LPFCharacteristic)
38 {
39  for (int time_const = 20; time_const < 100; time_const += 20)
40  {
42  ASSERT_LT(std::abs(lpf.get()), 1e-6);
43 
44  // Input step function
45  float ret = 0;
46  for (int i = 0; i < time_const; ++i)
47  {
48  ret = lpf.in(1.0);
49  }
50  // Check value at 1 time unit
51  ASSERT_TRUE(ret == lpf.get());
52  ASSERT_LT(std::abs(ret - (1.0 - expf(-1.0))), 1e-2);
53 
54  for (int i = time_const; i < time_const * 100; ++i)
55  {
56  ret = lpf.in(1.0);
57  }
58  // Check value at inf time
59  ASSERT_TRUE(ret == lpf.get());
60  ASSERT_LT(std::abs(ret - 1.0), 1e-2);
61 
62  // Check set
63  lpf.set(1.0);
64  ASSERT_LT(std::abs(lpf.get() - 1.0), 1e-2);
65  lpf.in(1.0);
66  ASSERT_LT(std::abs(lpf.get() - 1.0), 1e-2);
67  }
68 }
69 
70 TEST(Filter, HPFCharacteristic)
71 {
72  for (int time_const = 20; time_const < 100; time_const += 20)
73  {
76 
77  // Input step function
78  for (int i = 0; i < time_const * 10; ++i)
79  {
80  float ret_h, ret_l;
81  ret_l = lpf.in(1.0);
82  ret_h = hpf.in(1.0);
83 
84  // Check complementarity against LPF
85  ASSERT_LT(std::abs(ret_l + ret_h - 1.0), 1e-2);
86  }
87  }
88 }
89 
90 TEST(Filter, AugleLPF)
91 {
92  for (float zero = 0.0; zero < M_PI * 2 * 4; zero += M_PI * 2)
93  {
94  // Check 0.5 rad to 2pi - 0.5 rad transition
95  const float start1 = zero + 0.5;
96  const float end1 = zero + M_PI * 2.0 - 0.5;
97 
100  ASSERT_LT(std::abs(lpf.get() - start1), 1e-6);
101  ASSERT_LT(std::abs(lpf_angle.get() - start1), 1e-6);
102 
103  for (int i = 0; i < 100; ++i)
104  {
105  lpf.in(end1);
106  lpf_angle.in(end1);
107  ASSERT_GT(lpf.get(), start1);
108  ASSERT_LT(lpf_angle.get(), start1);
109  }
110  ASSERT_LT(std::abs(lpf.get() - end1), 1e-2);
111  ASSERT_LT(std::abs(lpf_angle.get() - (zero - 0.5)), 1e-2);
112 
113  // Check 2pi - 0.5 to 0.5 rad transition
114  const float start2 = zero - 0.5;
115  const float end2 = zero - M_PI * 2.0 + 0.5;
116 
117  lpf.set(start2);
118  lpf_angle.set(start2);
119  ASSERT_LT(std::abs(lpf.get() - start2), 1e-6);
120  ASSERT_LT(std::abs(lpf_angle.get() - start2), 1e-6);
121 
122  for (int i = 0; i < 100; ++i)
123  {
124  lpf.in(end2);
125  lpf_angle.in(end2);
126  ASSERT_LT(lpf.get(), start2);
127  ASSERT_GT(lpf_angle.get(), start2);
128  }
129  ASSERT_LT(std::abs(lpf.get() - end2), 1e-2);
130  ASSERT_LT(std::abs(lpf_angle.get() - (zero + 0.5)), 1e-2);
131  }
132 }
133 
134 int main(int argc, char** argv)
135 {
136  testing::InitGoogleTest(&argc, argv);
137 
138  return RUN_ALL_TESTS();
139 }
void set(const float &out0)
Definition: filter.h:80
TEST(Filter, LPFCharacteristic)
Definition: test_filter.cpp:37
int main(int argc, char **argv)
float in(const float &i)
Definition: filter.h:85


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