test_filter.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2016-2017, the neonavigation authors
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Neither the name of the copyright holder nor the names of its
00014  *       contributors may be used to endorse or promote products derived from
00015  *       this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00027  * POSSIBILITY OF SUCH DAMAGE.
00028  */
00029 
00030 #include <cstddef>
00031 
00032 #include <gtest/gtest.h>
00033 
00034 #include <trajectory_tracker/filter.h>
00035 
00036 TEST(Filter, LPFCharacteristic)
00037 {
00038   for (int time_const = 20; time_const < 100; time_const += 20)
00039   {
00040     trajectory_tracker::Filter lpf(trajectory_tracker::Filter::FILTER_LPF, time_const, 0.0);
00041     ASSERT_LT(fabs(lpf.get()), 1e-6);
00042 
00043     // Input step function
00044     float ret = 0;
00045     for (int i = 0; i < time_const; ++i)
00046     {
00047       ret = lpf.in(1.0);
00048     }
00049     // Check value at 1 time unit
00050     ASSERT_TRUE(ret == lpf.get());
00051     ASSERT_LT(fabs(ret - (1.0 - expf(-1.0))), 1e-2);
00052 
00053     for (int i = time_const; i < time_const * 100; ++i)
00054     {
00055       ret = lpf.in(1.0);
00056     }
00057     // Check value at inf time
00058     ASSERT_TRUE(ret == lpf.get());
00059     ASSERT_LT(fabs(ret - 1.0), 1e-2);
00060 
00061     // Check set
00062     lpf.set(1.0);
00063     ASSERT_LT(fabs(lpf.get() - 1.0), 1e-2);
00064     lpf.in(1.0);
00065     ASSERT_LT(fabs(lpf.get() - 1.0), 1e-2);
00066   }
00067 }
00068 
00069 TEST(Filter, HPFCharacteristic)
00070 {
00071   for (int time_const = 20; time_const < 100; time_const += 20)
00072   {
00073     trajectory_tracker::Filter lpf(trajectory_tracker::Filter::FILTER_LPF, time_const, 0.0);
00074     trajectory_tracker::Filter hpf(trajectory_tracker::Filter::FILTER_HPF, time_const, 0.0);
00075 
00076     // Input step function
00077     for (int i = 0; i < time_const * 10; ++i)
00078     {
00079       float ret_h, ret_l;
00080       ret_l = lpf.in(1.0);
00081       ret_h = hpf.in(1.0);
00082 
00083       // Check complementarity against LPF
00084       ASSERT_LT(fabs(ret_l + ret_h - 1.0), 1e-2);
00085     }
00086   }
00087 }
00088 
00089 TEST(Filter, AugleLPF)
00090 {
00091   for (float zero = 0.0; zero < M_PI * 2 * 4; zero += M_PI * 2)
00092   {
00093     // Check 0.5 rad to 2pi - 0.5 rad transition
00094     const float start1 = zero + 0.5;
00095     const float end1 = zero + M_PI * 2.0 - 0.5;
00096 
00097     trajectory_tracker::Filter lpf(trajectory_tracker::Filter::FILTER_LPF, 10, start1);
00098     trajectory_tracker::Filter lpf_angle(trajectory_tracker::Filter::FILTER_LPF, 10, start1, true);
00099     ASSERT_LT(fabs(lpf.get() - start1), 1e-6);
00100     ASSERT_LT(fabs(lpf_angle.get() - start1), 1e-6);
00101 
00102     for (int i = 0; i < 100; ++i)
00103     {
00104       lpf.in(end1);
00105       lpf_angle.in(end1);
00106       ASSERT_GT(lpf.get(), start1);
00107       ASSERT_LT(lpf_angle.get(), start1);
00108     }
00109     ASSERT_LT(fabs(lpf.get() - end1), 1e-2);
00110     ASSERT_LT(fabs(lpf_angle.get() - (zero - 0.5)), 1e-2);
00111 
00112     // Check 2pi - 0.5 to 0.5 rad transition
00113     const float start2 = zero - 0.5;
00114     const float end2 = zero - M_PI * 2.0 + 0.5;
00115 
00116     lpf.set(start2);
00117     lpf_angle.set(start2);
00118     ASSERT_LT(fabs(lpf.get() - start2), 1e-6);
00119     ASSERT_LT(fabs(lpf_angle.get() - start2), 1e-6);
00120 
00121     for (int i = 0; i < 100; ++i)
00122     {
00123       lpf.in(end2);
00124       lpf_angle.in(end2);
00125       ASSERT_LT(lpf.get(), start2);
00126       ASSERT_GT(lpf_angle.get(), start2);
00127     }
00128     ASSERT_LT(fabs(lpf.get() - end2), 1e-2);
00129     ASSERT_LT(fabs(lpf_angle.get() - (zero + 0.5)), 1e-2);
00130   }
00131 }
00132 
00133 int main(int argc, char** argv)
00134 {
00135   testing::InitGoogleTest(&argc, argv);
00136 
00137   return RUN_ALL_TESTS();
00138 }


trajectory_tracker
Author(s): Atsushi Watanabe
autogenerated on Sat Jun 22 2019 20:07:25