test_path2d.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2018, 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 <algorithm>
00031 #include <cstddef>
00032 #include <string>
00033 
00034 #include <gtest/gtest.h>
00035 
00036 #include <trajectory_tracker/eigen_line.h>
00037 #include <trajectory_tracker/path2d.h>
00038 
00039 namespace
00040 {
00041 double getRemainedDistance(const trajectory_tracker::Path2D& path, const Eigen::Vector2d& p)
00042 {
00043   const auto nearest = path.findNearest(path.begin(), path.end(), p);
00044   const Eigen::Vector2d pos_on_line =
00045       trajectory_tracker::projection2d((nearest - 1)->pos_, nearest->pos_, p);
00046   return path.remainedDistance(path.begin(), nearest, path.end(), pos_on_line);
00047 }
00048 }  // namespace
00049 
00050 TEST(Path2D, RemainedDistance)
00051 {
00052   trajectory_tracker::Path2D path_full;
00053   for (double x = 0.0; x < 10.0 - 1e-3; x += 0.2)
00054     path_full.push_back(trajectory_tracker::Pose2D(Eigen::Vector2d(x, 0), 0, 1));
00055   path_full.push_back(trajectory_tracker::Pose2D(Eigen::Vector2d(10.0, 0), 0, 1));
00056 
00057   // Single pose path means orientation control mode. More than two poses should be line following.
00058   for (size_t l = 2; l < path_full.size(); ++l)
00059   {
00060     trajectory_tracker::Path2D path;
00061     path.resize(l);
00062     std::copy(path_full.end() - l, path_full.end(), path.begin());
00063 
00064     std::string err_msg = "failed for " + std::to_string(l) + " pose(s) path";
00065 
00066     ASSERT_NEAR(getRemainedDistance(path, Eigen::Vector2d(0, 0)), 10.0, 1e-2) << err_msg;
00067 
00068     ASSERT_NEAR(getRemainedDistance(path, Eigen::Vector2d(8.25, 0)), 1.75, 1e-2) << err_msg;
00069     ASSERT_NEAR(getRemainedDistance(path, Eigen::Vector2d(10.25, 0)), -0.25, 1e-2) << err_msg;
00070 
00071     ASSERT_NEAR(getRemainedDistance(path, Eigen::Vector2d(8.25, 0.1)), 1.75, 1e-2) << err_msg;
00072     ASSERT_NEAR(getRemainedDistance(path, Eigen::Vector2d(8.25, -0.1)), 1.75, 1e-2) << err_msg;
00073     ASSERT_NEAR(getRemainedDistance(path, Eigen::Vector2d(10.25, 0.1)), -0.25, 1e-2) << err_msg;
00074     ASSERT_NEAR(getRemainedDistance(path, Eigen::Vector2d(10.25, -0.1)), -0.25, 1e-2) << err_msg;
00075   }
00076 }
00077 
00078 TEST(Path2D, Curvature)
00079 {
00080   for (float c = 1.0; c < 4.0; c += 0.4)
00081   {
00082     trajectory_tracker::Path2D path;
00083     for (double a = 0; a < 1.57; a += 0.1)
00084       path.push_back(trajectory_tracker::Pose2D(Eigen::Vector2d(std::cos(a), std::sin(a)) * c, 0, 1));
00085 
00086     ASSERT_NEAR(path.getCurvature(path.begin(), path.end(), path[0].pos_, 10.0), 1.0 / c, 1e-2);
00087   }
00088 }
00089 
00090 TEST(Path2D, LocalGoalWithoutSwitchBack)
00091 {
00092   for (int yaw_i = -1; yaw_i <= 1; ++yaw_i)
00093   {
00094     const double yaw_diff = yaw_i * 0.1;
00095 
00096     trajectory_tracker::Path2D path;
00097     Eigen::Vector2d p(0, 0);
00098     double yaw(0);
00099     for (int i = 0; i < 10; ++i)
00100     {
00101       p -= Eigen::Vector2d(std::cos(yaw), std::sin(yaw)) * 0.1;
00102       yaw += yaw_diff;
00103       path.push_back(trajectory_tracker::Pose2D(p, yaw, 1));
00104     }
00105     ASSERT_EQ(path.findLocalGoal(path.begin(), path.end(), true), path.end());
00106     ASSERT_EQ(path.findLocalGoal(path.begin(), path.end(), false), path.end());
00107   }
00108 }
00109 
00110 TEST(Path2D, LocalGoalWithSwitchBack)
00111 {
00112   for (int yaw_i = -1; yaw_i <= 1; ++yaw_i)
00113   {
00114     const double yaw_diff = yaw_i * 0.1;
00115 
00116     trajectory_tracker::Path2D path;
00117     Eigen::Vector2d p(0, 0);
00118     double yaw(0);
00119     for (int i = 0; i < 5; ++i)
00120     {
00121       p -= Eigen::Vector2d(std::cos(yaw), std::sin(yaw)) * 0.1;
00122       yaw += yaw_diff;
00123       path.push_back(trajectory_tracker::Pose2D(p, yaw, 1));
00124     }
00125     for (int i = 0; i < 5; ++i)
00126     {
00127       p += Eigen::Vector2d(std::cos(yaw), std::sin(yaw)) * 0.1;
00128       yaw += yaw_diff;
00129       path.push_back(trajectory_tracker::Pose2D(p, yaw, 1));
00130     }
00131     const auto it_local_goal = path.findLocalGoal(path.begin(), path.end(), true);
00132     ASSERT_EQ(it_local_goal, path.begin() + 5);
00133     ASSERT_EQ(path.findLocalGoal(it_local_goal, path.end(), true), path.end());
00134 
00135     // no switch back motion under (allow_switchback == false)
00136     ASSERT_EQ(path.findLocalGoal(path.begin(), path.end(), false), path.end());
00137   }
00138 }
00139 
00140 int main(int argc, char** argv)
00141 {
00142   testing::InitGoogleTest(&argc, argv);
00143 
00144   return RUN_ALL_TESTS();
00145 }


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