376 lines
14 KiB
C++
376 lines
14 KiB
C++
#include <algorithm>
|
|
#include <cmath>
|
|
#include <iostream>
|
|
#include <limits>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#include <data_convert/data_convert.h>
|
|
|
|
#include "dock_planner/dock_calc.h"
|
|
#include "dock_planner/dock_planner_config.h"
|
|
|
|
namespace
|
|
{
|
|
|
|
constexpr double kPi = 3.14159265358979323846;
|
|
int failures = 0;
|
|
|
|
void check(bool condition, const char* expression, int line)
|
|
{
|
|
if (!condition)
|
|
{
|
|
std::cerr << "FAIL line " << line << ": " << expression << '\n';
|
|
++failures;
|
|
}
|
|
}
|
|
|
|
#define CHECK(expression) check((expression), #expression, __LINE__)
|
|
|
|
robot_geometry_msgs::Point point(double x, double y)
|
|
{
|
|
robot_geometry_msgs::Point result;
|
|
result.x = x;
|
|
result.y = y;
|
|
result.z = 0.0;
|
|
return result;
|
|
}
|
|
|
|
robot_geometry_msgs::PoseStamped pose(double x, double y, double yaw)
|
|
{
|
|
robot_geometry_msgs::PoseStamped result;
|
|
result.pose.position.x = x;
|
|
result.pose.position.y = y;
|
|
result.pose.orientation = data_convert::createQuaternionMsgFromYaw(yaw);
|
|
return result;
|
|
}
|
|
|
|
bool near(double first, double second, double tolerance = 1e-8)
|
|
{
|
|
return std::fabs(first - second) <= tolerance;
|
|
}
|
|
|
|
double normalizeAngle(double angle)
|
|
{
|
|
return std::remainder(angle, 2.0 * kPi);
|
|
}
|
|
|
|
double longitudinal(const robot_geometry_msgs::Pose& value,
|
|
const robot_geometry_msgs::Pose& goal)
|
|
{
|
|
const double yaw = data_convert::getYaw(goal.orientation);
|
|
const double dx = value.position.x - goal.position.x;
|
|
const double dy = value.position.y - goal.position.y;
|
|
return dx * std::cos(yaw) + dy * std::sin(yaw);
|
|
}
|
|
|
|
double lateral(const robot_geometry_msgs::Pose& value,
|
|
const robot_geometry_msgs::Pose& goal)
|
|
{
|
|
const double yaw = data_convert::getYaw(goal.orientation);
|
|
const double dx = value.position.x - goal.position.x;
|
|
const double dy = value.position.y - goal.position.y;
|
|
return -dx * std::sin(yaw) + dy * std::cos(yaw);
|
|
}
|
|
|
|
bool edgeIsForward(const robot_geometry_msgs::Pose& first,
|
|
const robot_geometry_msgs::Pose& second)
|
|
{
|
|
const double motion_yaw = std::atan2(second.position.y - first.position.y,
|
|
second.position.x - first.position.x);
|
|
const double body_yaw = data_convert::getYaw(first.orientation);
|
|
return std::cos(normalizeAngle(motion_yaw - body_yaw)) > 0.0;
|
|
}
|
|
|
|
double threePointCurvature(const robot_geometry_msgs::Pose& first,
|
|
const robot_geometry_msgs::Pose& middle,
|
|
const robot_geometry_msgs::Pose& last)
|
|
{
|
|
const double ab = std::hypot(middle.position.x - first.position.x,
|
|
middle.position.y - first.position.y);
|
|
const double bc = std::hypot(last.position.x - middle.position.x,
|
|
last.position.y - middle.position.y);
|
|
const double ac = std::hypot(last.position.x - first.position.x,
|
|
last.position.y - first.position.y);
|
|
if (ab <= 1e-12 || bc <= 1e-12 || ac <= 1e-12)
|
|
{
|
|
return 0.0;
|
|
}
|
|
const double twice_area = std::fabs(
|
|
(middle.position.x - first.position.x) * (last.position.y - first.position.y) -
|
|
(middle.position.y - first.position.y) * (last.position.x - first.position.x));
|
|
return 2.0 * twice_area / (ab * bc * ac);
|
|
}
|
|
|
|
std::size_t checkForwardThenReverse(
|
|
const std::vector<robot_geometry_msgs::Pose>& path,
|
|
const robot_geometry_msgs::Pose& goal,
|
|
double maximum_curvature)
|
|
{
|
|
CHECK(path.size() > 3);
|
|
std::size_t cusp_index = path.size();
|
|
int direction_changes = 0;
|
|
bool previous_forward = edgeIsForward(path[0], path[1]);
|
|
CHECK(previous_forward);
|
|
|
|
for (std::size_t index = 1; index + 1 < path.size(); ++index)
|
|
{
|
|
const bool current_forward = edgeIsForward(path[index], path[index + 1]);
|
|
if (current_forward != previous_forward)
|
|
{
|
|
++direction_changes;
|
|
cusp_index = index;
|
|
}
|
|
previous_forward = current_forward;
|
|
}
|
|
|
|
CHECK(direction_changes == 1);
|
|
CHECK(cusp_index > 0);
|
|
CHECK(cusp_index + 1 < path.size());
|
|
|
|
for (std::size_t index = 0; index < cusp_index; ++index)
|
|
{
|
|
CHECK(edgeIsForward(path[index], path[index + 1]));
|
|
}
|
|
for (std::size_t index = cusp_index; index + 1 < path.size(); ++index)
|
|
{
|
|
CHECK(!edgeIsForward(path[index], path[index + 1]));
|
|
CHECK(std::fabs(lateral(path[index], goal)) < 1e-7);
|
|
CHECK(std::fabs(normalizeAngle(
|
|
data_convert::getYaw(path[index].orientation) -
|
|
data_convert::getYaw(goal.orientation))) < 1e-7);
|
|
}
|
|
|
|
for (std::size_t index = 1; index < cusp_index; ++index)
|
|
{
|
|
CHECK(threePointCurvature(path[index - 1], path[index], path[index + 1]) <=
|
|
maximum_curvature + 1e-5);
|
|
}
|
|
return cusp_index;
|
|
}
|
|
|
|
std::vector<robot_geometry_msgs::Pose> generatePath(
|
|
dock_planner::DockCalc& calculator,
|
|
const robot_geometry_msgs::PoseStamped& start,
|
|
const robot_geometry_msgs::PoseStamped& goal,
|
|
const std::vector<robot_geometry_msgs::Point>& robot_footprint,
|
|
const std::vector<robot_geometry_msgs::Point>& dock_footprint)
|
|
{
|
|
std::vector<robot_geometry_msgs::Pose> path;
|
|
CHECK(calculator.makePlanMoveToDock(start, goal, robot_footprint,
|
|
dock_footprint, path));
|
|
CHECK(path.size() > 3);
|
|
CHECK(near(path.front().position.x, start.pose.position.x));
|
|
CHECK(near(path.front().position.y, start.pose.position.y));
|
|
CHECK(near(path.back().position.x, goal.pose.position.x));
|
|
CHECK(near(path.back().position.y, goal.pose.position.y));
|
|
|
|
for (std::size_t index = 0; index < path.size(); ++index)
|
|
{
|
|
const auto& current = path[index];
|
|
CHECK(std::isfinite(current.position.x));
|
|
CHECK(std::isfinite(current.position.y));
|
|
CHECK(std::isfinite(current.orientation.z));
|
|
CHECK(std::isfinite(current.orientation.w));
|
|
if (index > 0)
|
|
{
|
|
CHECK(std::hypot(current.position.x - path[index - 1].position.x,
|
|
current.position.y - path[index - 1].position.y) > 1e-9);
|
|
}
|
|
}
|
|
return path;
|
|
}
|
|
|
|
dock_planner::DockPlannerConfig apmConfig()
|
|
{
|
|
dock_planner::DockPlannerConfig config;
|
|
config.wheel_base = 1.3268;
|
|
config.max_steering_angle = 0.50;
|
|
config.path_point_spacing = 0.05;
|
|
return config;
|
|
}
|
|
|
|
void testConfig()
|
|
{
|
|
dock_planner::DockPlannerConfig config;
|
|
std::string error;
|
|
CHECK(config.validate(error));
|
|
CHECK(error.empty());
|
|
CHECK(std::isnan(config.minimumTurningRadius()));
|
|
|
|
config.wheel_base = 1.3268;
|
|
CHECK(near(config.minimumTurningRadius(),
|
|
config.wheel_base / std::tan(config.max_steering_angle)));
|
|
|
|
config.max_steering_angle = 0.0;
|
|
CHECK(!config.validate(error));
|
|
config = dock_planner::DockPlannerConfig{};
|
|
config.front_axle_frame = config.rear_axle_frame;
|
|
CHECK(!config.validate(error));
|
|
config = dock_planner::DockPlannerConfig{};
|
|
config.maximum_forward_extension = config.minimum_forward_distance - 0.01;
|
|
CHECK(!config.validate(error));
|
|
config = dock_planner::DockPlannerConfig{};
|
|
config.path_point_spacing = std::numeric_limits<double>::quiet_NaN();
|
|
CHECK(!config.validate(error));
|
|
}
|
|
|
|
void testPathGeneration()
|
|
{
|
|
dock_planner::DockPlannerConfig config = apmConfig();
|
|
dock_planner::DockCalc calculator(config);
|
|
|
|
const std::vector<robot_geometry_msgs::Point> robot_footprint = {
|
|
point(1.65, -0.40), point(1.65, 0.40),
|
|
point(-0.35, 0.40), point(-0.35, -0.40)};
|
|
const std::vector<robot_geometry_msgs::Point> dock_footprint = {
|
|
point(0.50, -0.40), point(0.50, 0.40),
|
|
point(-0.50, 0.40), point(-0.50, -0.40)};
|
|
|
|
const auto goal = pose(0.0, 0.0, 0.0);
|
|
const double maximum_curvature = 1.0 / config.minimumTurningRadius();
|
|
for (const auto& start : {pose(2.8, 0.6, -1.2),
|
|
pose(2.8, 0.0, 2.0),
|
|
pose(2.8, -0.6, 1.2)})
|
|
{
|
|
const auto path = generatePath(calculator, start, goal,
|
|
robot_footprint, dock_footprint);
|
|
const std::size_t cusp = checkForwardThenReverse(path, goal.pose,
|
|
maximum_curvature);
|
|
CHECK(longitudinal(path[cusp], goal.pose) > longitudinal(start.pose, goal.pose));
|
|
CHECK(std::fabs(normalizeAngle(
|
|
data_convert::getYaw(path.front().orientation) -
|
|
data_convert::getYaw(start.pose.orientation))) > 0.1);
|
|
}
|
|
|
|
const auto rotated_goal = pose(0.0, 0.0, kPi / 2.0);
|
|
const auto rotated_path = generatePath(
|
|
calculator, pose(0.6, 2.8, 0.0), rotated_goal,
|
|
robot_footprint, dock_footprint);
|
|
checkForwardThenReverse(rotated_path, rotated_goal.pose, maximum_curvature);
|
|
|
|
// The previous projection<=1 failure is now valid: the robot first drives
|
|
// away from the dock until the curvature-safe cusp, then reverses straight.
|
|
const auto close_path = generatePath(calculator, pose(0.8, 0.2, 0.0), goal,
|
|
robot_footprint, dock_footprint);
|
|
checkForwardThenReverse(close_path, goal.pose, maximum_curvature);
|
|
|
|
std::vector<robot_geometry_msgs::Pose> path;
|
|
CHECK(!calculator.makePlanMoveToDock(pose(-2.8, 0.6, 0.0), goal,
|
|
robot_footprint, dock_footprint, path));
|
|
CHECK(path.empty());
|
|
|
|
auto invalid_start = pose(2.8, 0.6, 0.0);
|
|
invalid_start.pose.position.x = std::numeric_limits<double>::infinity();
|
|
CHECK(!calculator.makePlanMoveToDock(invalid_start, goal, robot_footprint,
|
|
dock_footprint, path));
|
|
CHECK(path.empty());
|
|
CHECK(!calculator.makePlanMoveToDock(pose(2.8, 0.6, 0.0), goal, {},
|
|
dock_footprint, path));
|
|
|
|
config.maximum_forward_extension = 0.30;
|
|
calculator.setConfig(config);
|
|
CHECK(!calculator.makePlanMoveToDock(pose(2.8, 0.6, 0.0), goal,
|
|
robot_footprint, dock_footprint, path));
|
|
CHECK(path.empty());
|
|
}
|
|
|
|
bool pathsEqual(const std::vector<robot_geometry_msgs::Pose>& first,
|
|
const std::vector<robot_geometry_msgs::Pose>& second)
|
|
{
|
|
if (first.size() != second.size())
|
|
{
|
|
return false;
|
|
}
|
|
for (std::size_t index = 0; index < first.size(); ++index)
|
|
{
|
|
if (!near(first[index].position.x, second[index].position.x) ||
|
|
!near(first[index].position.y, second[index].position.y) ||
|
|
!near(first[index].orientation.z, second[index].orientation.z) ||
|
|
!near(first[index].orientation.w, second[index].orientation.w))
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
void testAsymmetricFootprint()
|
|
{
|
|
dock_planner::DockPlannerConfig config;
|
|
config.wheel_base = 1.0;
|
|
config.max_steering_angle = 1.0;
|
|
dock_planner::DockCalc calculator(config);
|
|
|
|
const std::vector<robot_geometry_msgs::Point> stacker_footprint = {
|
|
point(0.8, -0.5), point(0.8, 0.5), point(-1.2, 0.5), point(-1.2, -0.5)};
|
|
const std::vector<robot_geometry_msgs::Point> rear_matched_footprint = {
|
|
point(1.2, -0.5), point(1.2, 0.5), point(-1.2, 0.5), point(-1.2, -0.5)};
|
|
const std::vector<robot_geometry_msgs::Point> smaller_rear_footprint = {
|
|
point(0.8, -0.5), point(0.8, 0.5), point(-0.8, 0.5), point(-0.8, -0.5)};
|
|
const std::vector<robot_geometry_msgs::Point> dock_footprint = {
|
|
point(0.5, -0.4), point(0.5, 0.4), point(-0.5, 0.4), point(-0.5, -0.4)};
|
|
|
|
const auto start = pose(0.5, 0.5, 0.0);
|
|
const auto goal = pose(0.0, 0.0, 0.0);
|
|
std::vector<robot_geometry_msgs::Pose> asymmetric_path;
|
|
std::vector<robot_geometry_msgs::Pose> reference_path;
|
|
CHECK(calculator.makePlanMoveToDock(start, goal, stacker_footprint,
|
|
dock_footprint, asymmetric_path));
|
|
CHECK(calculator.makePlanMoveToDock(start, goal, rear_matched_footprint,
|
|
dock_footprint, reference_path));
|
|
CHECK(pathsEqual(asymmetric_path, reference_path));
|
|
CHECK(calculator.makePlanMoveToDock(start, goal, smaller_rear_footprint,
|
|
dock_footprint, reference_path));
|
|
CHECK(!pathsEqual(asymmetric_path, reference_path));
|
|
}
|
|
|
|
void testDockAnchor()
|
|
{
|
|
dock_planner::DockPlannerConfig config;
|
|
config.wheel_base = 1.0;
|
|
config.max_steering_angle = 1.0;
|
|
const std::vector<robot_geometry_msgs::Point> robot_footprint = {
|
|
point(0.50, -0.35), point(0.50, 0.35),
|
|
point(-0.50, 0.35), point(-0.50, -0.35)};
|
|
const std::vector<robot_geometry_msgs::Point> dock_footprint = {
|
|
point(0.9, -0.8), point(0.9, 0.8), point(-0.9, 0.8), point(-0.9, -0.8)};
|
|
const auto start = pose(0.5, 0.4, 0.0);
|
|
const auto goal = pose(0.0, 0.0, 0.0);
|
|
|
|
config.dock_anchor = dock_planner::DockAnchor::kFace;
|
|
dock_planner::DockCalc face_calculator(config);
|
|
std::vector<robot_geometry_msgs::Pose> face_path;
|
|
CHECK(face_calculator.makePlanMoveToDock(start, goal, robot_footprint,
|
|
dock_footprint, face_path));
|
|
|
|
config.dock_anchor = dock_planner::DockAnchor::kCenter;
|
|
dock_planner::DockCalc center_calculator(config);
|
|
std::vector<robot_geometry_msgs::Pose> no_dock_path;
|
|
CHECK(center_calculator.makePlanMoveToDock(start, goal, robot_footprint,
|
|
{}, no_dock_path));
|
|
CHECK(pathsEqual(face_path, no_dock_path));
|
|
|
|
std::vector<robot_geometry_msgs::Pose> center_path;
|
|
CHECK(center_calculator.makePlanMoveToDock(start, goal, robot_footprint,
|
|
dock_footprint, center_path));
|
|
CHECK(!pathsEqual(face_path, center_path));
|
|
}
|
|
|
|
} // namespace
|
|
|
|
int main()
|
|
{
|
|
testConfig();
|
|
testPathGeneration();
|
|
testAsymmetricFootprint();
|
|
testDockAnchor();
|
|
if (failures != 0)
|
|
{
|
|
std::cerr << failures << " DockPlanner test assertion(s) failed.\n";
|
|
return 1;
|
|
}
|
|
std::cout << "DockPlanner tests passed.\n";
|
|
return 0;
|
|
}
|