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.
This commit is contained in:
parent
08d9a393be
commit
13f01feb18
@ -80,18 +80,13 @@ bool BSplineSmoother::smooth(
|
||||
|
||||
JointTrajectory samples;
|
||||
samples.reserve(static_cast<std::size_t>(count) + 1);
|
||||
const auto as_vector = [](const Eigen::VectorXd& value) {
|
||||
return std::vector<double>(value.data(), value.data() + value.size());
|
||||
};
|
||||
for (std::size_t i = 0; i <= static_cast<std::size_t>(count); ++i) {
|
||||
if (limits.cancellation_requested && limits.cancellation_requested()) return false;
|
||||
JointTrajectoryPoint point;
|
||||
point.time_s = i == static_cast<std::size_t>(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]) ||
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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<double>& position, std::vector<double>& velocity,
|
||||
std::vector<double>& acceleration, std::vector<double>& jerk) const;
|
||||
std::vector<double> knotTimes() const;
|
||||
|
||||
// Conservative absolute bounds over the whole trajectory, orders 0..3.
|
||||
@ -45,11 +49,21 @@ public:
|
||||
const std::vector<double>& knots() const { return knots_[0]; }
|
||||
|
||||
private:
|
||||
struct PolynomialSegment {
|
||||
double start{0.0};
|
||||
double inverse_width{0.0};
|
||||
double inverse_duration{0.0};
|
||||
std::vector<std::array<double, 6>> coefficients;
|
||||
};
|
||||
|
||||
QuinticBSpline() = default;
|
||||
void buildPolynomialSegments(const std::function<bool()>& cancelled);
|
||||
const PolynomialSegment& segmentAt(double u) const;
|
||||
Eigen::VectorXd evaluate(double t, int order) const;
|
||||
|
||||
std::array<Eigen::MatrixXd, 4> controls_;
|
||||
std::array<std::vector<double>, 4> knots_;
|
||||
std::vector<PolynomialSegment> segments_;
|
||||
double duration_s_{0.0};
|
||||
};
|
||||
|
||||
|
||||
@ -49,6 +49,23 @@ double smoothstepControl(const std::vector<double>& knots, std::size_t index)
|
||||
return std::clamp(mirror ? 1.0 - progress : progress, 0.0, 1.0);
|
||||
}
|
||||
|
||||
std::array<double, 4> evaluatePolynomial(
|
||||
const std::array<double, 6>& 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> QuinticBSpline::create(
|
||||
@ -189,10 +206,67 @@ std::shared_ptr<QuinticBSpline> QuinticBSpline::create(
|
||||
throw std::runtime_error("B-spline duration is not representable");
|
||||
}
|
||||
trajectory->duration_s_ = static_cast<double>(duration);
|
||||
trajectory->buildPolynomialSegments(cancelled);
|
||||
checkCancelled(cancelled);
|
||||
return trajectory;
|
||||
}
|
||||
|
||||
void QuinticBSpline::buildPolynomialSegments(const std::function<bool()>& cancelled)
|
||||
{
|
||||
const auto& controls = controls_[0];
|
||||
const auto& knots = knots_[0];
|
||||
segments_.reserve(static_cast<std::size_t>(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<std::size_t>(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<std::array<long double, 6>, 6> work{};
|
||||
for (int i = 0; i <= kDegree; ++i) {
|
||||
work[i][0] = static_cast<long double>(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<long double>(knots[span + i - level + 1]) - knots[left];
|
||||
const long double a = (static_cast<long double>(segment.start) - knots[left]) /
|
||||
denominator;
|
||||
const long double b = static_cast<long double>(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<double>(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::size_t>(
|
||||
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<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));
|
||||
}
|
||||
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<double>& position, std::vector<double>& velocity,
|
||||
std::vector<double>& acceleration, std::vector<double>& jerk) const
|
||||
{
|
||||
if (!std::isfinite(t)) {
|
||||
throw std::invalid_argument("B-spline evaluation time must be finite");
|
||||
}
|
||||
const auto dof = static_cast<std::size_t>(controls_[0].rows());
|
||||
const std::array<std::vector<double>*, 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); }
|
||||
|
||||
Loading…
Reference in New Issue
Block a user