Integrate OMPL RRTConnect and quintic B-spline smoothing into moveP, reuse the B-spline trajectory generator for moveJ, and remove the TOPPRA paths and dependencies. Separate joint and Cartesian planners, smoothers, and the standalone curve math library. Move speedL execution into SpeedLExecutor, tighten velocity QP feasibility, and add adaptive speed reduction, braking recovery, feedback-derived TCP velocity, and grouped cyclic velocity submission. Improve MuJoCo control cycle synchronization and camera streaming. Centralize kinematics and collision geometry in RobotModel, expose box obstacles through CollisionChecker, and extract SelfCollisionMonitor for protective stops and recovery. Add cancellable moveP pre-approach and configurable failure recovery to the touch workflow, with updated configs, documentation, and regression coverage. Validation: recorded -j8 build and 133 passing tests on 2026-10-08, including headless MuJoCo checks for eight target poses and seven moveJ returns. Static scene configuration removal passed 27 follow-up tests and six arm configuration parses. Staged diff and shell syntax checks pass; no new hardware tests were run for this commit.
265 lines
11 KiB
C++
265 lines
11 KiB
C++
#include "curves/bspline/include/quintic_bspline.h"
|
|
|
|
#include <algorithm>
|
|
#include <cmath>
|
|
#include <limits>
|
|
#include <stdexcept>
|
|
|
|
namespace cmvr {
|
|
namespace {
|
|
|
|
constexpr int kDegree = 5;
|
|
constexpr std::size_t kMaxControlPoints = 10000;
|
|
constexpr double kSmoothstepMaximumSlope = 1.875;
|
|
constexpr double kDurationMargin = 1.001;
|
|
|
|
void checkCancelled(const std::function<bool()>& cancelled)
|
|
{
|
|
if (cancelled && cancelled()) {
|
|
throw std::runtime_error("B-spline planning cancelled");
|
|
}
|
|
}
|
|
|
|
void validateLimits(const std::vector<double>& limits, std::size_t dof)
|
|
{
|
|
if (limits.size() != dof ||
|
|
!std::all_of(limits.begin(), limits.end(), [](double value) {
|
|
return std::isfinite(value) && value > 0.0;
|
|
})) {
|
|
throw std::invalid_argument("B-spline limits must be positive, finite and match the DOF");
|
|
}
|
|
}
|
|
|
|
// The degree-five blossom of 10u^3-15u^4+6u^5 is e3-3e4+6e5,
|
|
// where ek is the elementary symmetric polynomial of the five arguments.
|
|
// Thus a straight reference path reproduces one global minimum-jerk quintic,
|
|
// rather than acquiring speed ripples from a uniform spatial control polygon.
|
|
double smoothstepControl(const std::vector<double>& knots, std::size_t index)
|
|
{
|
|
const bool mirror = knots[index + 3] > 0.5;
|
|
std::array<double, 6> elementary{};
|
|
elementary[0] = 1.0;
|
|
for (int argument = 1; argument <= kDegree; ++argument) {
|
|
const double value = mirror ? 1.0 - knots[index + argument] : knots[index + argument];
|
|
for (int order = argument; order >= 1; --order) {
|
|
elementary[order] += value * elementary[order - 1];
|
|
}
|
|
}
|
|
const double progress = elementary[3] - 3.0 * elementary[4] + 6.0 * elementary[5];
|
|
return std::clamp(mirror ? 1.0 - progress : progress, 0.0, 1.0);
|
|
}
|
|
|
|
} // namespace
|
|
|
|
std::shared_ptr<QuinticBSpline> QuinticBSpline::create(
|
|
const std::vector<std::vector<double>>& waypoints,
|
|
const std::vector<double>& velocity_limits,
|
|
const std::vector<double>& acceleration_limits,
|
|
const std::vector<double>& jerk_limits,
|
|
double minimum_duration_s,
|
|
const std::function<bool()>& cancelled,
|
|
double control_point_spacing_rad)
|
|
{
|
|
if (waypoints.empty() || waypoints.size() > kMaxControlPoints ||
|
|
waypoints.front().empty() || !std::isfinite(minimum_duration_s) ||
|
|
minimum_duration_s <= 0.0) {
|
|
throw std::invalid_argument("B-spline requires a nonempty path and positive finite duration");
|
|
}
|
|
if (!std::isfinite(control_point_spacing_rad) || control_point_spacing_rad <= 0.0) {
|
|
throw std::invalid_argument("B-spline control-point spacing must be positive and finite");
|
|
}
|
|
const auto dof = waypoints.front().size();
|
|
if (dof > static_cast<std::size_t>(std::numeric_limits<Eigen::Index>::max())) {
|
|
throw std::invalid_argument("B-spline DOF is too large");
|
|
}
|
|
validateLimits(velocity_limits, dof);
|
|
validateLimits(acceleration_limits, dof);
|
|
validateLimits(jerk_limits, dof);
|
|
checkCancelled(cancelled);
|
|
|
|
std::vector<Eigen::VectorXd> vertices;
|
|
std::vector<double> chord_lengths;
|
|
vertices.reserve(waypoints.size());
|
|
chord_lengths.reserve(waypoints.size());
|
|
double length = 0.0;
|
|
for (const auto& waypoint : waypoints) {
|
|
checkCancelled(cancelled);
|
|
if (waypoint.size() != dof ||
|
|
!std::all_of(waypoint.begin(), waypoint.end(), [](double value) {
|
|
return std::isfinite(value);
|
|
})) {
|
|
throw std::invalid_argument("B-spline waypoints must be finite and have matching DOF");
|
|
}
|
|
Eigen::VectorXd vertex = Eigen::Map<const Eigen::VectorXd>(
|
|
waypoint.data(), static_cast<Eigen::Index>(dof));
|
|
if (!vertices.empty()) {
|
|
const Eigen::VectorXd delta = vertex - vertices.back();
|
|
const double distance = delta.stableNorm();
|
|
if (!std::isfinite(distance)) {
|
|
throw std::invalid_argument("B-spline path length is not finite");
|
|
}
|
|
if (distance == 0.0) {
|
|
continue;
|
|
}
|
|
length += distance;
|
|
if (!std::isfinite(length)) {
|
|
throw std::invalid_argument("B-spline path length is not finite");
|
|
}
|
|
}
|
|
vertices.push_back(std::move(vertex));
|
|
chord_lengths.push_back(length);
|
|
}
|
|
|
|
// Three equal controls at each clamped endpoint impose q'=q''=0 without
|
|
// stopping at any of the interior reference points.
|
|
// Check the floating-point count before conversion, including tiny spacing.
|
|
const long double interval_count = std::ceil(
|
|
static_cast<long double>(length) * kSmoothstepMaximumSlope / control_point_spacing_rad);
|
|
if (!std::isfinite(interval_count) || interval_count > kMaxControlPoints - kDegree) {
|
|
throw std::invalid_argument("B-spline path exceeds the control-point limit");
|
|
}
|
|
const auto intervals = std::max<std::size_t>(1, static_cast<std::size_t>(interval_count));
|
|
const auto control_count = intervals + kDegree;
|
|
auto trajectory = std::shared_ptr<QuinticBSpline>(new QuinticBSpline);
|
|
auto& knots = trajectory->knots_[0];
|
|
knots.assign(control_count + kDegree + 1, 0.0);
|
|
for (std::size_t i = kDegree + 1; i < control_count; ++i) {
|
|
knots[i] = static_cast<double>(i - kDegree) / intervals;
|
|
}
|
|
std::fill(knots.begin() + static_cast<std::ptrdiff_t>(control_count), knots.end(), 1.0);
|
|
|
|
auto& controls = trajectory->controls_[0];
|
|
controls.resize(static_cast<Eigen::Index>(dof), static_cast<Eigen::Index>(control_count));
|
|
for (int i = 0; i < 3; ++i) {
|
|
controls.col(i) = vertices.front();
|
|
controls.col(static_cast<Eigen::Index>(control_count) - 1 - i) = vertices.back();
|
|
}
|
|
std::size_t segment = 1;
|
|
for (std::size_t i = 3; i + 3 < control_count; ++i) {
|
|
checkCancelled(cancelled);
|
|
const double distance = length * smoothstepControl(knots, i);
|
|
while (segment + 1 < vertices.size() && chord_lengths[segment] < distance) {
|
|
++segment;
|
|
}
|
|
const double alpha = std::clamp(
|
|
(distance - chord_lengths[segment - 1]) /
|
|
(chord_lengths[segment] - chord_lengths[segment - 1]),
|
|
0.0, 1.0);
|
|
controls.col(static_cast<Eigen::Index>(i)) =
|
|
vertices[segment - 1] + alpha * (vertices[segment] - vertices[segment - 1]);
|
|
}
|
|
|
|
// Differentiating a B-spline gives another B-spline: fewer controls, lower
|
|
// degree, and the first/last knot removed at each derivative order.
|
|
for (int order = 1; order <= 3; ++order) {
|
|
const auto& previous_controls = trajectory->controls_[order - 1];
|
|
const auto& previous_knots = trajectory->knots_[order - 1];
|
|
const int previous_degree = kDegree - order + 1;
|
|
auto& derivative_controls = trajectory->controls_[order];
|
|
derivative_controls.resize(previous_controls.rows(), previous_controls.cols() - 1);
|
|
trajectory->knots_[order].assign(previous_knots.begin() + 1, previous_knots.end() - 1);
|
|
for (Eigen::Index i = 0; i < derivative_controls.cols(); ++i) {
|
|
checkCancelled(cancelled);
|
|
const double span = previous_knots[i + previous_degree + 1] - previous_knots[i + 1];
|
|
derivative_controls.col(i) =
|
|
(previous_controls.col(i + 1) - previous_controls.col(i)) * (previous_degree / span);
|
|
}
|
|
if (!derivative_controls.allFinite()) {
|
|
throw std::runtime_error("B-spline derivatives are not finite");
|
|
}
|
|
}
|
|
|
|
// The derivative curve lies in the convex hull of its derivative controls,
|
|
// so these bounds hold continuously, including between controller samples.
|
|
long double required_duration = 0.0L;
|
|
for (int order = 1; order <= 3; ++order) {
|
|
const auto& limits = order == 1 ? velocity_limits :
|
|
order == 2 ? acceleration_limits : jerk_limits;
|
|
const Eigen::VectorXd bounds = trajectory->controls_[order].cwiseAbs().rowwise().maxCoeff();
|
|
for (Eigen::Index joint = 0; joint < bounds.size(); ++joint) {
|
|
const long double ratio = static_cast<long double>(bounds[joint]) / limits[joint];
|
|
const long double duration = order == 1 ? ratio :
|
|
order == 2 ? std::sqrt(ratio) : std::cbrt(ratio);
|
|
required_duration = std::max(required_duration, duration);
|
|
}
|
|
}
|
|
const long double duration = std::max(
|
|
static_cast<long double>(minimum_duration_s), required_duration * kDurationMargin);
|
|
if (!std::isfinite(duration) || duration > std::numeric_limits<double>::max()) {
|
|
throw std::runtime_error("B-spline duration is not representable");
|
|
}
|
|
trajectory->duration_s_ = static_cast<double>(duration);
|
|
checkCancelled(cancelled);
|
|
return trajectory;
|
|
}
|
|
|
|
Eigen::RowVector2d QuinticBSpline::timeInterval() const
|
|
{
|
|
return {0.0, duration_s_};
|
|
}
|
|
|
|
Eigen::VectorXd QuinticBSpline::evaluate(double t, int order) const
|
|
{
|
|
if (!std::isfinite(t)) {
|
|
throw std::invalid_argument("B-spline evaluation time must be finite");
|
|
}
|
|
const auto& controls = controls_[order];
|
|
const auto& knots = knots_[order];
|
|
const int degree = kDegree - order;
|
|
const double u = std::clamp(t, 0.0, duration_s_) / duration_s_;
|
|
Eigen::VectorXd result;
|
|
if (u <= 0.0) {
|
|
result = controls.col(0);
|
|
} else if (u >= 1.0) {
|
|
result = controls.col(controls.cols() - 1);
|
|
} else {
|
|
const auto span = static_cast<Eigen::Index>(
|
|
std::upper_bound(knots.begin(), knots.end(), u) - knots.begin() - 1);
|
|
Eigen::MatrixXd work = controls.middleCols(span - degree, degree + 1);
|
|
for (int level = 1; level <= degree; ++level) {
|
|
for (int j = degree; j >= level; --j) {
|
|
const auto left = span - degree + j;
|
|
const double alpha = (u - knots[left]) /
|
|
(knots[span + j - level + 1] - knots[left]);
|
|
work.col(j) = work.col(j - 1) + alpha * (work.col(j) - work.col(j - 1));
|
|
}
|
|
}
|
|
result = work.col(degree);
|
|
}
|
|
for (int derivative = 0; derivative < order; ++derivative) {
|
|
result /= duration_s_;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
Eigen::VectorXd QuinticBSpline::q(double t) const { return evaluate(t, 0); }
|
|
Eigen::VectorXd QuinticBSpline::qd(double t) const { return evaluate(t, 1); }
|
|
Eigen::VectorXd QuinticBSpline::qdd(double t) const { return evaluate(t, 2); }
|
|
Eigen::VectorXd QuinticBSpline::qddd(double t) const { return evaluate(t, 3); }
|
|
|
|
std::vector<double> QuinticBSpline::knotTimes() const
|
|
{
|
|
std::vector<double> times;
|
|
for (double knot : knots_[0]) {
|
|
const double time = knot * duration_s_;
|
|
if (times.empty() || time != times.back()) {
|
|
times.push_back(time);
|
|
}
|
|
}
|
|
return times;
|
|
}
|
|
|
|
Eigen::VectorXd QuinticBSpline::derivativeBounds(int order) const
|
|
{
|
|
if (order < 0 || order > 3) {
|
|
throw std::invalid_argument("B-spline derivative order must be between zero and three");
|
|
}
|
|
Eigen::VectorXd bounds = controls_[order].cwiseAbs().rowwise().maxCoeff();
|
|
for (int derivative = 0; derivative < order; ++derivative) {
|
|
bounds /= duration_s_;
|
|
}
|
|
return bounds;
|
|
}
|
|
|
|
} // namespace cmvr
|