perf(arm): cache B-spline sampling and silence frequent status logs

This commit is contained in:
linbo 2026-10-28 16:41:13 +08:00
parent c571b476a1
commit f89d5b383f
6 changed files with 144 additions and 26 deletions

View File

@ -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]) ||

View File

@ -18,7 +18,13 @@ arm {
}
motion {
# 与 lgv_dev_tc_motor 一致:默认五次 B 样条平滑,采样周期 1 ms。
joint_trajectory_smoother {
sample_period_s: 0.001
quintic_bspline {
control_point_spacing_rad: 0.1
}
}
move_j {
settle_timeout_s: 2.0
settle_position_tolerance_rad: 0.002

View File

@ -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};
};

View File

@ -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); }

View File

@ -575,9 +575,9 @@ grpc::Status gRPCArmServiceImpl::GetArmInfo(grpc::ServerContext*,
response->set_detected_from_sdk(model.detected_from_sdk);
response->set_driver_type(arm->typeName());
fillFeedback(response->mutable_header(), true);
CMVR_LOG(DEBUG) << "[gRPCArmServiceImpl] (GetArmInfo): success, id=" << device_id
<< ", manufacturer=" << model.manufacturer
<< ", model=" << model.name << ", dof=" << model.dof;
// CMVR_LOG(DEBUG) << "[gRPCArmServiceImpl] (GetArmInfo): success, id=" << device_id
// << ", manufacturer=" << model.manufacturer
// << ", model=" << model.name << ", dof=" << model.dof;
return grpc::Status::OK;
} catch (const std::exception& e) {
fillFeedback(response->mutable_header(), false, e.what());

View File

@ -141,8 +141,8 @@ grpc::Status gRPCSystemServiceImpl::GetSystemStatus(grpc::ServerContext* context
}
response->mutable_header()->set_success(true);
setCurrentTimestamp(response->mutable_header()->mutable_timestamp());
CMVR_LOG(DEBUG) << "[gRPCSystemServiceImpl] (GetSystemStatus): success, devices="
<< response->device_list_size();
// CMVR_LOG(DEBUG) << "[gRPCSystemServiceImpl] (GetSystemStatus): success, devices="
// << response->device_list_size();
return grpc::Status::OK;
}
catch (const std::exception &e) {