update
This commit is contained in:
@@ -1,76 +0,0 @@
|
|||||||
#ifndef DOCK_PLANNER_UTILS_DUBINS_H_
|
|
||||||
#define DOCK_PLANNER_UTILS_DUBINS_H_
|
|
||||||
|
|
||||||
#include <array>
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
namespace dock_planner
|
|
||||||
{
|
|
||||||
namespace dubins
|
|
||||||
{
|
|
||||||
|
|
||||||
/** Planar pose in the frame the caller works in. */
|
|
||||||
struct Pose2D
|
|
||||||
{
|
|
||||||
double x = 0.0; // [m]
|
|
||||||
double y = 0.0; // [m]
|
|
||||||
double yaw = 0.0; // [rad]
|
|
||||||
};
|
|
||||||
|
|
||||||
enum class SegmentType
|
|
||||||
{
|
|
||||||
kLeft, ///< constant-radius turn, counter-clockwise
|
|
||||||
kStraight,
|
|
||||||
kRight, ///< constant-radius turn, clockwise
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Shortest forward-only path between two poses at a fixed turning radius.
|
|
||||||
*
|
|
||||||
* The three segment lengths are stored in metres, in travel order, together with
|
|
||||||
* the word that produced them. `valid` is false when no word applies, which for
|
|
||||||
* a strictly positive radius only happens if an input is non-finite.
|
|
||||||
*/
|
|
||||||
struct Path
|
|
||||||
{
|
|
||||||
bool valid = false;
|
|
||||||
std::array<SegmentType, 3> types{SegmentType::kStraight, SegmentType::kStraight,
|
|
||||||
SegmentType::kStraight};
|
|
||||||
std::array<double, 3> lengths{0.0, 0.0, 0.0}; // [m]
|
|
||||||
double radius = 0.0; // [m]
|
|
||||||
|
|
||||||
/** Total travelled distance [m]. */
|
|
||||||
double length() const;
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Solve for the shortest of the six Dubins words.
|
|
||||||
*
|
|
||||||
* Forward motion only, so the result never reverses direction. That is the
|
|
||||||
* property the docking approach relies on: the robot reaches the terminal pose
|
|
||||||
* having changed direction at most where the caller puts a cusp, never inside
|
|
||||||
* the lead-in.
|
|
||||||
*
|
|
||||||
* @param radius Turning radius [m]; must be > 0. A differential-drive robot has
|
|
||||||
* no such radius and is handled by the caller instead of by a degenerate
|
|
||||||
* radius here — passing 0 returns an invalid path on purpose.
|
|
||||||
*/
|
|
||||||
Path shortestPath(const Pose2D& start, const Pose2D& goal, double radius);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Sample a solved path at approximately @p spacing metres.
|
|
||||||
*
|
|
||||||
* Includes both endpoints. The final sample carries the goal yaw exactly rather
|
|
||||||
* than the accumulated one, so a caller can compare it against its own contract
|
|
||||||
* without absorbing the integration error of three segments.
|
|
||||||
*/
|
|
||||||
std::vector<Pose2D> sample(const Path& path, const Pose2D& start, const Pose2D& goal,
|
|
||||||
double spacing);
|
|
||||||
|
|
||||||
/** Wrap to (-pi, pi]. Exposed because callers compare yaw against a contract. */
|
|
||||||
double normalizeAngle(double angle);
|
|
||||||
|
|
||||||
} // namespace dubins
|
|
||||||
} // namespace dock_planner
|
|
||||||
|
|
||||||
#endif // DOCK_PLANNER_UTILS_DUBINS_H_
|
|
||||||
@@ -1,322 +0,0 @@
|
|||||||
#include "dock_planner/utils/dubins.h"
|
|
||||||
|
|
||||||
#include <algorithm>
|
|
||||||
#include <cmath>
|
|
||||||
#include <limits>
|
|
||||||
|
|
||||||
namespace dock_planner
|
|
||||||
{
|
|
||||||
namespace dubins
|
|
||||||
{
|
|
||||||
namespace
|
|
||||||
{
|
|
||||||
|
|
||||||
constexpr double kPi = 3.14159265358979323846;
|
|
||||||
constexpr double kTwoPi = 2.0 * kPi;
|
|
||||||
constexpr double kEpsilon = 1e-9;
|
|
||||||
|
|
||||||
/** Wrap to [0, 2*pi). The Dubins word equations are written in this range. */
|
|
||||||
double positiveAngle(double angle)
|
|
||||||
{
|
|
||||||
angle = std::fmod(angle, kTwoPi);
|
|
||||||
return angle < 0.0 ? angle + kTwoPi : angle;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** One candidate word: three normalised parameters plus the segment types. */
|
|
||||||
struct Word
|
|
||||||
{
|
|
||||||
bool valid = false;
|
|
||||||
std::array<SegmentType, 3> types{SegmentType::kStraight, SegmentType::kStraight,
|
|
||||||
SegmentType::kStraight};
|
|
||||||
double t = 0.0; // first segment, radians for a turn / normalised length for a straight
|
|
||||||
double p = 0.0; // middle segment
|
|
||||||
double q = 0.0; // last segment
|
|
||||||
|
|
||||||
double cost() const
|
|
||||||
{
|
|
||||||
return valid ? t + p + q : std::numeric_limits<double>::infinity();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The six words in the normalised frame: start at the origin heading @p alpha,
|
|
||||||
* goal at distance @p d heading @p beta, unit turning radius.
|
|
||||||
*
|
|
||||||
* Kept as one function per word rather than a table: each has its own
|
|
||||||
* feasibility test, and a shared skeleton would hide which test belongs to which.
|
|
||||||
*/
|
|
||||||
Word wordLSL(double alpha, double beta, double d)
|
|
||||||
{
|
|
||||||
const double sa = std::sin(alpha), sb = std::sin(beta);
|
|
||||||
const double ca = std::cos(alpha), cb = std::cos(beta);
|
|
||||||
const double c_ab = std::cos(alpha - beta);
|
|
||||||
|
|
||||||
const double p_sq = 2.0 + d * d - 2.0 * c_ab + 2.0 * d * (sa - sb);
|
|
||||||
Word word;
|
|
||||||
word.types = {SegmentType::kLeft, SegmentType::kStraight, SegmentType::kLeft};
|
|
||||||
if (p_sq < 0.0)
|
|
||||||
{
|
|
||||||
return word;
|
|
||||||
}
|
|
||||||
const double tmp = std::atan2(cb - ca, d + sa - sb);
|
|
||||||
word.valid = true;
|
|
||||||
word.t = positiveAngle(tmp - alpha);
|
|
||||||
word.p = std::sqrt(p_sq);
|
|
||||||
word.q = positiveAngle(beta - tmp);
|
|
||||||
return word;
|
|
||||||
}
|
|
||||||
|
|
||||||
Word wordRSR(double alpha, double beta, double d)
|
|
||||||
{
|
|
||||||
const double sa = std::sin(alpha), sb = std::sin(beta);
|
|
||||||
const double ca = std::cos(alpha), cb = std::cos(beta);
|
|
||||||
const double c_ab = std::cos(alpha - beta);
|
|
||||||
|
|
||||||
const double p_sq = 2.0 + d * d - 2.0 * c_ab + 2.0 * d * (sb - sa);
|
|
||||||
Word word;
|
|
||||||
word.types = {SegmentType::kRight, SegmentType::kStraight, SegmentType::kRight};
|
|
||||||
if (p_sq < 0.0)
|
|
||||||
{
|
|
||||||
return word;
|
|
||||||
}
|
|
||||||
const double tmp = std::atan2(ca - cb, d - sa + sb);
|
|
||||||
word.valid = true;
|
|
||||||
word.t = positiveAngle(alpha - tmp);
|
|
||||||
word.p = std::sqrt(p_sq);
|
|
||||||
word.q = positiveAngle(tmp - beta);
|
|
||||||
return word;
|
|
||||||
}
|
|
||||||
|
|
||||||
Word wordLSR(double alpha, double beta, double d)
|
|
||||||
{
|
|
||||||
const double sa = std::sin(alpha), sb = std::sin(beta);
|
|
||||||
const double ca = std::cos(alpha), cb = std::cos(beta);
|
|
||||||
const double c_ab = std::cos(alpha - beta);
|
|
||||||
|
|
||||||
const double p_sq = -2.0 + d * d + 2.0 * c_ab + 2.0 * d * (sa + sb);
|
|
||||||
Word word;
|
|
||||||
word.types = {SegmentType::kLeft, SegmentType::kStraight, SegmentType::kRight};
|
|
||||||
if (p_sq < 0.0)
|
|
||||||
{
|
|
||||||
return word;
|
|
||||||
}
|
|
||||||
word.p = std::sqrt(p_sq);
|
|
||||||
const double tmp = std::atan2(-ca - cb, d + sa + sb) - std::atan2(-2.0, word.p);
|
|
||||||
word.valid = true;
|
|
||||||
word.t = positiveAngle(tmp - alpha);
|
|
||||||
word.q = positiveAngle(tmp - positiveAngle(beta));
|
|
||||||
return word;
|
|
||||||
}
|
|
||||||
|
|
||||||
Word wordRSL(double alpha, double beta, double d)
|
|
||||||
{
|
|
||||||
const double sa = std::sin(alpha), sb = std::sin(beta);
|
|
||||||
const double ca = std::cos(alpha), cb = std::cos(beta);
|
|
||||||
const double c_ab = std::cos(alpha - beta);
|
|
||||||
|
|
||||||
const double p_sq = d * d - 2.0 + 2.0 * c_ab - 2.0 * d * (sa + sb);
|
|
||||||
Word word;
|
|
||||||
word.types = {SegmentType::kRight, SegmentType::kStraight, SegmentType::kLeft};
|
|
||||||
if (p_sq < 0.0)
|
|
||||||
{
|
|
||||||
return word;
|
|
||||||
}
|
|
||||||
word.p = std::sqrt(p_sq);
|
|
||||||
const double tmp = std::atan2(ca + cb, d - sa - sb) - std::atan2(2.0, word.p);
|
|
||||||
word.valid = true;
|
|
||||||
word.t = positiveAngle(alpha - tmp);
|
|
||||||
word.q = positiveAngle(beta - tmp);
|
|
||||||
return word;
|
|
||||||
}
|
|
||||||
|
|
||||||
Word wordRLR(double alpha, double beta, double d)
|
|
||||||
{
|
|
||||||
const double sa = std::sin(alpha), sb = std::sin(beta);
|
|
||||||
const double ca = std::cos(alpha), cb = std::cos(beta);
|
|
||||||
const double c_ab = std::cos(alpha - beta);
|
|
||||||
|
|
||||||
const double tmp = (6.0 - d * d + 2.0 * c_ab + 2.0 * d * (sa - sb)) / 8.0;
|
|
||||||
Word word;
|
|
||||||
word.types = {SegmentType::kRight, SegmentType::kLeft, SegmentType::kRight};
|
|
||||||
if (std::fabs(tmp) > 1.0)
|
|
||||||
{
|
|
||||||
return word;
|
|
||||||
}
|
|
||||||
word.valid = true;
|
|
||||||
word.p = positiveAngle(kTwoPi - std::acos(tmp));
|
|
||||||
word.t = positiveAngle(alpha - std::atan2(ca - cb, d - sa + sb) + word.p / 2.0);
|
|
||||||
word.q = positiveAngle(alpha - beta - word.t + word.p);
|
|
||||||
return word;
|
|
||||||
}
|
|
||||||
|
|
||||||
Word wordLRL(double alpha, double beta, double d)
|
|
||||||
{
|
|
||||||
const double sa = std::sin(alpha), sb = std::sin(beta);
|
|
||||||
const double ca = std::cos(alpha), cb = std::cos(beta);
|
|
||||||
const double c_ab = std::cos(alpha - beta);
|
|
||||||
|
|
||||||
const double tmp = (6.0 - d * d + 2.0 * c_ab + 2.0 * d * (sb - sa)) / 8.0;
|
|
||||||
Word word;
|
|
||||||
word.types = {SegmentType::kLeft, SegmentType::kRight, SegmentType::kLeft};
|
|
||||||
if (std::fabs(tmp) > 1.0)
|
|
||||||
{
|
|
||||||
return word;
|
|
||||||
}
|
|
||||||
word.valid = true;
|
|
||||||
word.p = positiveAngle(kTwoPi - std::acos(tmp));
|
|
||||||
word.t = positiveAngle(-alpha - std::atan2(ca - cb, d + sa - sb) + word.p / 2.0);
|
|
||||||
word.q = positiveAngle(positiveAngle(beta) - alpha - word.t + word.p);
|
|
||||||
return word;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Advance one segment. @p length is metres for a straight, radians for a turn. */
|
|
||||||
Pose2D advance(const Pose2D& from, SegmentType type, double length, double radius)
|
|
||||||
{
|
|
||||||
Pose2D to = from;
|
|
||||||
switch (type)
|
|
||||||
{
|
|
||||||
case SegmentType::kLeft:
|
|
||||||
to.x = from.x + radius * (std::sin(from.yaw + length) - std::sin(from.yaw));
|
|
||||||
to.y = from.y - radius * (std::cos(from.yaw + length) - std::cos(from.yaw));
|
|
||||||
to.yaw = from.yaw + length;
|
|
||||||
break;
|
|
||||||
case SegmentType::kRight:
|
|
||||||
to.x = from.x - radius * (std::sin(from.yaw - length) - std::sin(from.yaw));
|
|
||||||
to.y = from.y + radius * (std::cos(from.yaw - length) - std::cos(from.yaw));
|
|
||||||
to.yaw = from.yaw - length;
|
|
||||||
break;
|
|
||||||
case SegmentType::kStraight:
|
|
||||||
to.x = from.x + length * std::cos(from.yaw);
|
|
||||||
to.y = from.y + length * std::sin(from.yaw);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
return to;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Turn angle [rad] a segment consumes; 0 for a straight. */
|
|
||||||
double turnAngle(SegmentType type, double length, double radius)
|
|
||||||
{
|
|
||||||
return type == SegmentType::kStraight ? 0.0 : length / radius;
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace
|
|
||||||
|
|
||||||
double Path::length() const
|
|
||||||
{
|
|
||||||
return valid ? lengths[0] + lengths[1] + lengths[2] : 0.0;
|
|
||||||
}
|
|
||||||
|
|
||||||
double normalizeAngle(double angle)
|
|
||||||
{
|
|
||||||
angle = std::fmod(angle + kPi, kTwoPi);
|
|
||||||
if (angle <= 0.0)
|
|
||||||
{
|
|
||||||
angle += kTwoPi;
|
|
||||||
}
|
|
||||||
return angle - kPi;
|
|
||||||
}
|
|
||||||
|
|
||||||
Path shortestPath(const Pose2D& start, const Pose2D& goal, double radius)
|
|
||||||
{
|
|
||||||
Path result;
|
|
||||||
if (!std::isfinite(start.x) || !std::isfinite(start.y) || !std::isfinite(start.yaw) ||
|
|
||||||
!std::isfinite(goal.x) || !std::isfinite(goal.y) || !std::isfinite(goal.yaw) ||
|
|
||||||
!std::isfinite(radius) || radius <= kEpsilon)
|
|
||||||
{
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
const double dx = goal.x - start.x;
|
|
||||||
const double dy = goal.y - start.y;
|
|
||||||
const double distance = std::hypot(dx, dy);
|
|
||||||
const double d = distance / radius;
|
|
||||||
// Bearing is meaningless when the poses coincide; any value works because
|
|
||||||
// alpha and beta then differ only by the yaw change, which every word still
|
|
||||||
// resolves. Using the start yaw keeps alpha at zero and the words stable.
|
|
||||||
const double bearing = distance > kEpsilon ? std::atan2(dy, dx) : start.yaw;
|
|
||||||
const double alpha = positiveAngle(start.yaw - bearing);
|
|
||||||
const double beta = positiveAngle(goal.yaw - bearing);
|
|
||||||
|
|
||||||
const Word words[] = {
|
|
||||||
wordLSL(alpha, beta, d), wordRSR(alpha, beta, d), wordLSR(alpha, beta, d),
|
|
||||||
wordRSL(alpha, beta, d), wordRLR(alpha, beta, d), wordLRL(alpha, beta, d),
|
|
||||||
};
|
|
||||||
|
|
||||||
const Word* best = nullptr;
|
|
||||||
for (const Word& word : words)
|
|
||||||
{
|
|
||||||
if (word.valid && (best == nullptr || word.cost() < best->cost()))
|
|
||||||
{
|
|
||||||
best = &word;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (best == nullptr || !std::isfinite(best->cost()))
|
|
||||||
{
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
result.valid = true;
|
|
||||||
result.types = best->types;
|
|
||||||
result.radius = radius;
|
|
||||||
// Normalised parameters become metres the same way for both kinds: a turn of
|
|
||||||
// t radians at this radius covers r*t, and a straight of p normalised units
|
|
||||||
// covers r*p because the frame was scaled by the radius.
|
|
||||||
result.lengths = {best->t * radius, best->p * radius, best->q * radius};
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::vector<Pose2D> sample(const Path& path, const Pose2D& start, const Pose2D& goal,
|
|
||||||
double spacing)
|
|
||||||
{
|
|
||||||
std::vector<Pose2D> samples;
|
|
||||||
if (!path.valid || !std::isfinite(spacing) || spacing <= 0.0)
|
|
||||||
{
|
|
||||||
return samples;
|
|
||||||
}
|
|
||||||
|
|
||||||
const double total = path.length();
|
|
||||||
samples.reserve(static_cast<std::size_t>(total / spacing) + 4);
|
|
||||||
samples.push_back(start);
|
|
||||||
|
|
||||||
Pose2D cursor = start;
|
|
||||||
for (std::size_t index = 0; index < path.types.size(); ++index)
|
|
||||||
{
|
|
||||||
const SegmentType type = path.types[index];
|
|
||||||
const double segment_length = path.lengths[index];
|
|
||||||
if (!(segment_length > kEpsilon))
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
const int steps = std::max(1, static_cast<int>(std::ceil(segment_length / spacing)));
|
|
||||||
for (int step = 1; step <= steps; ++step)
|
|
||||||
{
|
|
||||||
const double travelled = segment_length * static_cast<double>(step) /
|
|
||||||
static_cast<double>(steps);
|
|
||||||
const double parameter = type == SegmentType::kStraight
|
|
||||||
? travelled
|
|
||||||
: turnAngle(type, travelled, path.radius);
|
|
||||||
samples.push_back(advance(cursor, type, parameter, path.radius));
|
|
||||||
}
|
|
||||||
const double parameter = type == SegmentType::kStraight
|
|
||||||
? segment_length
|
|
||||||
: turnAngle(type, segment_length, path.radius);
|
|
||||||
cursor = advance(cursor, type, parameter, path.radius);
|
|
||||||
}
|
|
||||||
|
|
||||||
// The caller's contract is about the goal pose, not about the integration of
|
|
||||||
// three segments. Snapping the last sample keeps a downstream equality test
|
|
||||||
// meaningful instead of chasing accumulated rounding.
|
|
||||||
if (!samples.empty())
|
|
||||||
{
|
|
||||||
samples.back() = goal;
|
|
||||||
}
|
|
||||||
for (Pose2D& pose : samples)
|
|
||||||
{
|
|
||||||
pose.yaw = normalizeAngle(pose.yaw);
|
|
||||||
}
|
|
||||||
return samples;
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace dubins
|
|
||||||
} // namespace dock_planner
|
|
||||||
Reference in New Issue
Block a user