From 13f01feb1851e86be9ed2ffa9a6cf59c3d96b8f0 Mon Sep 17 00:00:00 2001 From: lgv Date: Wed, 28 Oct 2026 09:22:17 +0800 Subject: [PATCH] perf(trajectory): cache quintic B-spline segments for sampling Precompute local polynomial coefficients and evaluate position, velocity, acceleration, and jerk together into trajectory buffers. Preserve control points, trajectory timing, derivative bounds, and clamped endpoints. Configure the TCHL arm smoother explicitly with a 1 ms sample period and 0.05 rad control-point spacing. Validation: 9 existing spline tests and 8 headless MuJoCo MoveJ/MoveP cases passed. A 9391-point offline Debug benchmark improved from 180.67 ms to 8.13 ms. --- .../smoother/bspline/src/bspline_smoother.cpp | 9 +- cmvr-es/config/devices/arm/tchl_arm.pb.txt | 8 +- .../curves/bspline/include/quintic_bspline.h | 14 ++ .../curves/bspline/src/quintic_bspline.cpp | 129 ++++++++++++++++-- 4 files changed, 139 insertions(+), 21 deletions(-) diff --git a/cmvr-es/algorithms/smoother/bspline/src/bspline_smoother.cpp b/cmvr-es/algorithms/smoother/bspline/src/bspline_smoother.cpp index 8b6833a1..0aa0791b 100644 --- a/cmvr-es/algorithms/smoother/bspline/src/bspline_smoother.cpp +++ b/cmvr-es/algorithms/smoother/bspline/src/bspline_smoother.cpp @@ -80,18 +80,13 @@ bool BSplineSmoother::smooth( JointTrajectory samples; samples.reserve(static_cast(count) + 1); - const auto as_vector = [](const Eigen::VectorXd& value) { - return std::vector(value.data(), value.data() + value.size()); - }; for (std::size_t i = 0; i <= static_cast(count); ++i) { if (limits.cancellation_requested && limits.cancellation_requested()) return false; JointTrajectoryPoint point; point.time_s = i == static_cast(count) ? duration : std::min(i * sample_period_s, duration); - point.position = as_vector(spline->q(point.time_s)); - point.velocity = as_vector(spline->qd(point.time_s)); - point.acceleration = as_vector(spline->qdd(point.time_s)); - point.jerk = as_vector(spline->qddd(point.time_s)); + spline->evaluate(point.time_s, point.position, point.velocity, + point.acceleration, point.jerk); for (std::size_t j = 0; j < dof; ++j) { if (!std::isfinite(point.position[j]) || !std::isfinite(point.velocity[j]) || !std::isfinite(point.acceleration[j]) || !std::isfinite(point.jerk[j]) || diff --git a/cmvr-es/config/devices/arm/tchl_arm.pb.txt b/cmvr-es/config/devices/arm/tchl_arm.pb.txt index ea1adf93..434b028b 100644 --- a/cmvr-es/config/devices/arm/tchl_arm.pb.txt +++ b/cmvr-es/config/devices/arm/tchl_arm.pb.txt @@ -17,7 +17,13 @@ arm { } motion { - # 未指定平滑器时,使用五次 B 样条,采样周期 1 ms。 + joint_trajectory_smoother { + sample_period_s: 0.001 + quintic_bspline { + control_point_spacing_rad: 0.05 + } + } + move_j { settle_timeout_s: 2.0 settle_position_tolerance_rad: 0.002 diff --git a/cmvr-es/curves/bspline/include/quintic_bspline.h b/cmvr-es/curves/bspline/include/quintic_bspline.h index 08ae3d8c..165aeea6 100644 --- a/cmvr-es/curves/bspline/include/quintic_bspline.h +++ b/cmvr-es/curves/bspline/include/quintic_bspline.h @@ -36,6 +36,10 @@ public: Eigen::VectorXd qd(double t) const; Eigen::VectorXd qdd(double t) const; Eigen::VectorXd qddd(double t) const; + // Evaluate all derivatives with one interval lookup, directly into the + // caller's distinct output vectors. Existing capacity is reused. + void evaluate(double t, std::vector& position, std::vector& velocity, + std::vector& acceleration, std::vector& jerk) const; std::vector knotTimes() const; // Conservative absolute bounds over the whole trajectory, orders 0..3. @@ -45,11 +49,21 @@ public: const std::vector& knots() const { return knots_[0]; } private: + struct PolynomialSegment { + double start{0.0}; + double inverse_width{0.0}; + double inverse_duration{0.0}; + std::vector> coefficients; + }; + QuinticBSpline() = default; + void buildPolynomialSegments(const std::function& cancelled); + const PolynomialSegment& segmentAt(double u) const; Eigen::VectorXd evaluate(double t, int order) const; std::array controls_; std::array, 4> knots_; + std::vector segments_; double duration_s_{0.0}; }; diff --git a/cmvr-es/curves/bspline/src/quintic_bspline.cpp b/cmvr-es/curves/bspline/src/quintic_bspline.cpp index c92d86b8..c1445625 100644 --- a/cmvr-es/curves/bspline/src/quintic_bspline.cpp +++ b/cmvr-es/curves/bspline/src/quintic_bspline.cpp @@ -49,6 +49,23 @@ double smoothstepControl(const std::vector& knots, std::size_t index) return std::clamp(mirror ? 1.0 - progress : progress, 0.0, 1.0); } +std::array evaluatePolynomial( + const std::array& coefficients, double s, double inverse_duration) +{ + double q = coefficients.back(), v = 0.0, a = 0.0, j = 0.0; + for (int power = kDegree - 1; power >= 0; --power) { + j = j * s + 3.0 * a; + a = a * s + 2.0 * v; + v = v * s + q; + q = q * s + coefficients[power]; + } + // A stationary curve keeps zero derivatives even when a subnormal duration + // makes its reciprocal overflow. + return {q, v == 0.0 ? 0.0 : v * inverse_duration, + a == 0.0 ? 0.0 : a * inverse_duration * inverse_duration, + j == 0.0 ? 0.0 : j * inverse_duration * inverse_duration * inverse_duration}; +} + } // namespace std::shared_ptr QuinticBSpline::create( @@ -189,10 +206,67 @@ std::shared_ptr QuinticBSpline::create( throw std::runtime_error("B-spline duration is not representable"); } trajectory->duration_s_ = static_cast(duration); + trajectory->buildPolynomialSegments(cancelled); checkCancelled(cancelled); return trajectory; } +void QuinticBSpline::buildPolynomialSegments(const std::function& cancelled) +{ + const auto& controls = controls_[0]; + const auto& knots = knots_[0]; + segments_.reserve(static_cast(controls.cols() - kDegree)); + for (Eigen::Index span = kDegree; span < controls.cols(); ++span) { + checkCancelled(cancelled); + PolynomialSegment segment; + segment.start = knots[span]; + const double width = knots[span + 1] - knots[span]; + segment.inverse_width = 1.0 / width; + segment.inverse_duration = segment.inverse_width / duration_s_; + segment.coefficients.resize(static_cast(controls.rows())); + for (Eigen::Index axis = 0; axis < controls.rows(); ++axis) { + // Expand de Boor once using alpha = a + b*s on this interval. + // Subtract a local origin to avoid losing small motions at large q. + const long double origin = controls(axis, span - kDegree); + std::array, 6> work{}; + for (int i = 0; i <= kDegree; ++i) { + work[i][0] = static_cast(controls(axis, span - kDegree + i)) - origin; + } + for (int level = 1; level <= kDegree; ++level) { + for (int i = kDegree; i >= level; --i) { + const auto left = span - kDegree + i; + const long double denominator = + static_cast(knots[span + i - level + 1]) - knots[left]; + const long double a = (static_cast(segment.start) - knots[left]) / + denominator; + const long double b = static_cast(width) / denominator; + for (int power = level; power >= 1; --power) { + work[i][power] = (1.0L - a) * work[i - 1][power] + a * work[i][power] + + b * (work[i][power - 1] - work[i - 1][power - 1]); + } + work[i][0] = (1.0L - a) * work[i - 1][0] + a * work[i][0]; + } + } + work[kDegree][0] += origin; + for (int power = 0; power <= kDegree; ++power) { + const double coefficient = static_cast(work[kDegree][power]); + if (!std::isfinite(coefficient)) { + throw std::runtime_error("B-spline polynomial coefficients are not finite"); + } + segment.coefficients[axis][power] = coefficient; + } + } + segments_.push_back(std::move(segment)); + } +} + +const QuinticBSpline::PolynomialSegment& QuinticBSpline::segmentAt(double u) const +{ + const auto span = static_cast( + std::upper_bound(knots_[0].begin(), knots_[0].end(), u) - knots_[0].begin() - 1); + return segments_[span - kDegree]; +} + Eigen::RowVector2d QuinticBSpline::timeInterval() const { return {0.0, duration_s_}; @@ -204,8 +278,6 @@ Eigen::VectorXd QuinticBSpline::evaluate(double t, int order) const 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) { @@ -213,18 +285,14 @@ Eigen::VectorXd QuinticBSpline::evaluate(double t, int order) const } else if (u >= 1.0) { result = controls.col(controls.cols() - 1); } else { - const auto span = static_cast( - 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)); - } + const auto& segment = segmentAt(u); + const double s = (u - segment.start) * segment.inverse_width; + result.resize(controls.rows()); + for (Eigen::Index axis = 0; axis < result.size(); ++axis) { + result[axis] = evaluatePolynomial(segment.coefficients[axis], s, + segment.inverse_duration)[order]; } - result = work.col(degree); + return result; } for (int derivative = 0; derivative < order; ++derivative) { result /= duration_s_; @@ -232,6 +300,41 @@ Eigen::VectorXd QuinticBSpline::evaluate(double t, int order) const return result; } +void QuinticBSpline::evaluate( + double t, std::vector& position, std::vector& velocity, + std::vector& acceleration, std::vector& jerk) const +{ + if (!std::isfinite(t)) { + throw std::invalid_argument("B-spline evaluation time must be finite"); + } + const auto dof = static_cast(controls_[0].rows()); + const std::array*, 4> outputs{&position, &velocity, &acceleration, &jerk}; + for (auto* output : outputs) output->resize(dof); + const double u = std::clamp(t, 0.0, duration_s_) / duration_s_; + if (u <= 0.0 || u >= 1.0) { + // Preserve exact clamped endpoint values, including zero velocity/acceleration. + for (int order = 0; order <= 3; ++order) { + const auto& controls = controls_[order]; + const auto column = u <= 0.0 ? 0 : controls.cols() - 1; + for (std::size_t axis = 0; axis < dof; ++axis) { + double value = controls(axis, column); + for (int derivative = 0; derivative < order; ++derivative) value /= duration_s_; + (*outputs[order])[axis] = value; + } + } + return; + } + const auto& segment = segmentAt(u); + const double s = (u - segment.start) * segment.inverse_width; + for (std::size_t axis = 0; axis < dof; ++axis) { + const auto values = evaluatePolynomial(segment.coefficients[axis], s, segment.inverse_duration); + position[axis] = values[0]; + velocity[axis] = values[1]; + acceleration[axis] = values[2]; + jerk[axis] = values[3]; + } +} + 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); }