00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
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 }
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
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
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 }