plcopen 0.22.0
载入中...
搜索中...
未找到
sync.h
浏览该文件的文档.
1#pragma once
2
3#include <cmath>
4#include <cstddef>
5
6#include "geom/geometry.h"
7#include "rt/error.h"
8#include "rt/static_vector.h"
9
10namespace plcopen::core::exec
11{
12
13struct GearMap
14{
15 double ratio = 1.0;
16 double offset = 0.0;
17};
18
19inline double sample_gear(double master_position, GearMap map)
20{
21 return master_position * map.ratio + map.offset;
22}
23
25{
26 double master = 0.0;
27 double slave = 0.0;
28};
29
30// Cam v2 addendum (approved 2026-07-06, KB-046): classical rest-to-rest
31// motion-law generation, offline/submit domain. cam_law_value evaluates the
32// unit-domain law (x in [0,1] maps to s in [0,1]); the modified-sine
33// constants come from the exact piecewise antiderivatives at run time, not
34// copied from handbooks. generate_cam_law discretizes into a caller-owned
35// point array consumable by the existing linear/spline reconstruction
36// (strictly increasing master by construction).
43
44inline double cam_law_value(CamLaw law, double x)
45{
46 constexpr double Pi = 3.14159265358979323846;
47 x = x < 0.0 ? 0.0 : (x > 1.0 ? 1.0 : x);
48 if(law == CamLaw::cycloidal) {
49 return x - std::sin(2.0 * Pi * x) / (2.0 * Pi);
50 }
51 if(law == CamLaw::poly345) {
52 return x * x * x * (10.0 + x * (-15.0 + 6.0 * x));
53 }
54 // Modified sine: acceleration sin(w1*x) on [0,1/8] and [7/8,1],
55 // cos(w2*(x-1/8)) between; velocity/position from the exact
56 // antiderivatives with continuity constants, normalized by the total.
57 const double w1 = 4.0 * Pi;
58 const double w2 = 4.0 * Pi / 3.0;
59 const double v18 = 1.0 / w1;
60 const double s18 = (0.125 - 1.0 / w1) / w1;
61 const double s78 = s18 + v18 * 0.75 + 2.0 / (w2 * w2);
62 const double total = s78 + v18 * 0.125 - 1.0 / (w1 * w1);
63 double s = 0.0;
64 if(x < 0.125) {
65 s = (x - std::sin(w1 * x) / w1) / w1;
66 } else if(x <= 0.875) {
67 const double u = x - 0.125;
68 s = s18 + v18 * u + (1.0 - std::cos(w2 * u)) / (w2 * w2);
69 } else {
70 s = s78 + v18 * (x - 0.875) - (std::sin(w1 * x) + 1.0) / (w1 * w1);
71 }
72 return s / total;
73}
74
76 double master_span,
77 double rise,
78 CamPoint *points,
79 std::size_t point_count)
80{
81 if(points == nullptr || point_count < 8 || point_count > 64 ||
82 !std::isfinite(master_span) || master_span <= 0.0 || !std::isfinite(rise)) {
84 }
85 for(std::size_t i = 0; i < point_count; ++i) {
86 const double x =
87 static_cast<double>(i) / static_cast<double>(point_count - 1);
88 points[i].master = master_span * x;
89 points[i].slave = rise * cam_law_value(law, x);
90 }
91 return rt::ErrorCode::ok;
92}
93
94// Non-owning cam table handle: L5/L6 consumers keep the point storage alive.
96{
97 const CamPoint *points = nullptr;
98 std::size_t size = 0;
99 bool periodic = false;
100
101 bool valid() const
102 {
103 if(points == nullptr || size < 2) {
104 return false;
105 }
106 for(std::size_t i = 0; i < size; ++i) {
107 if(!std::isfinite(points[i].master) || !std::isfinite(points[i].slave)) {
108 return false;
109 }
110 if(i > 0 && points[i].master <= points[i - 1].master) {
111 return false;
112 }
113 }
114 return true;
115 }
116
117 // Full table validation is an engage-time concern (valid()); the cycle-path
118 // sample only guards the cheap emptiness/finiteness preconditions.
119 rt::Result<double> sample(double master_position) const
120 {
121 if(points == nullptr || size < 2 || !std::isfinite(master_position)) {
123 }
124 double position = master_position;
125 if(periodic) {
126 const double begin = points[0].master;
127 const double span = points[size - 1].master - begin;
128 position = std::fmod(position - begin, span);
129 if(position < 0.0) {
130 position += span;
131 }
132 position += begin;
133 }
134 if(position <= points[0].master) {
135 return rt::Result<double>::success(points[0].slave);
136 }
137 for(std::size_t i = 1; i < size; ++i) {
138 if(position <= points[i].master) {
139 const CamPoint before = points[i - 1];
140 const CamPoint after = points[i];
141 const double ratio = (position - before.master) / (after.master - before.master);
142 return rt::Result<double>::success(before.slave +
143 (after.slave - before.slave) * ratio);
144 }
145 }
146 return rt::Result<double>::success(points[size - 1].slave);
147 }
148};
149
150template <std::size_t Capacity> class CamTable
151{
152public:
154 {
155 if(points_.size() > 0 && point.master <= points_[points_.size() - 1].master) {
157 }
158 return points_.push_back(point);
159 }
160
161 std::size_t size() const
162 {
163 return points_.size();
164 }
165
167 {
168 periodic_ = periodic;
169 }
170
171 bool periodic() const
172 {
173 return periodic_;
174 }
175
177 {
178 return CamTableView{points_.data(), points_.size(), periodic_};
179 }
180
181 rt::Result<double> sample(double master_position) const
182 {
183 return view().sample(master_position);
184 }
185
186private:
187 static_assert(Capacity >= 2, "CamTable capacity must be at least two");
189 bool periodic_ = false;
190};
191
192// Cam run-layer interpolation (approved cam matrix, decision #2): linear is
193// the byte-identical C0 compatibility mode; spline is the C2 cubic
194// reconstruction below.
196{
199};
200
201// C2 cubic-spline reconstruction over a cam table (approved cam matrix,
202// decisions #1/#3/#4): natural boundary for aperiodic tables, fully periodic
203// boundary (position/velocity/acceleration continuous across the wrap) for
204// periodic ones. build() solves the tridiagonal moment system once in the
205// planning domain; sample() locates the interval and evaluates one cubic, so
206// the cycle-path cost stays in the C0 order of magnitude. Fixed storage, no
207// allocation.
209{
210public:
211 static constexpr std::size_t MaxPoints = 64;
212
214 {
215 size_ = 0;
216 if(!table.valid() || table.size > MaxPoints) {
218 }
219 if(table.periodic &&
220 std::fabs(table.points[0].slave - table.points[table.size - 1].slave) > 1e-12) {
221 // A periodic spline over a mismatched wrap has no definition.
223 }
224 for(std::size_t i = 0; i < table.size; ++i) {
225 points_[i] = table.points[i];
226 }
227 size_ = table.size;
228 periodic_ = table.periodic;
229
230 const std::size_t n = size_;
231 double h[MaxPoints] = {};
232 for(std::size_t i = 0; i + 1 < n; ++i) {
233 h[i] = points_[i + 1].master - points_[i].master;
234 }
235
236 if(!periodic_) {
237 // Natural boundary: m[0] = m[n-1] = 0, Thomas elimination.
238 double diagonal[MaxPoints] = {};
239 double upper[MaxPoints] = {};
240 double rhs[MaxPoints] = {};
241 m2_[0] = 0.0;
242 m2_[n - 1] = 0.0;
243 if(n == 2) {
244 return rt::ErrorCode::ok;
245 }
246 for(std::size_t i = 1; i + 1 < n; ++i) {
247 diagonal[i] = (h[i - 1] + h[i]) / 3.0;
248 upper[i] = h[i] / 6.0;
249 rhs[i] = (points_[i + 1].slave - points_[i].slave) / h[i] -
250 (points_[i].slave - points_[i - 1].slave) / h[i - 1];
251 }
252 for(std::size_t i = 2; i + 1 < n; ++i) {
253 const double factor = (h[i - 1] / 6.0) / diagonal[i - 1];
254 diagonal[i] -= factor * upper[i - 1];
255 rhs[i] -= factor * rhs[i - 1];
256 }
257 for(std::size_t i = n - 2; i >= 1; --i) {
258 const double above = i + 2 < n ? upper[i] * m2_[i + 1] : 0.0;
259 m2_[i] = (rhs[i] - above) / diagonal[i];
260 if(i == 1) {
261 break;
262 }
263 }
264 return rt::ErrorCode::ok;
265 }
266
267 // Periodic boundary: unknowns m[0..n-2] on a cyclic tridiagonal
268 // equations (m[n-1] = m[0]); Sherman-Morrison over the Thomas solve
269 // (needs >= 3 unknowns; the 1- and 2-unknown wraps solve directly).
270 const std::size_t cyclic = n - 1;
271 if(cyclic < 2) {
272 m2_[0] = 0.0;
273 m2_[n - 1] = 0.0;
274 return rt::ErrorCode::ok;
275 }
276 if(cyclic == 2) {
277 const double h0 = h[0];
278 const double h1 = h[1];
279 const double d0 = (points_[1].slave - points_[0].slave) / h0 -
280 (points_[0].slave - points_[1].slave) / h1;
281 const double d1 = (points_[2].slave - points_[1].slave) / h1 -
282 (points_[1].slave - points_[0].slave) / h0;
283 const double b0 = (h1 + h0) / 3.0;
284 const double c0 = h0 / 6.0 + h1 / 6.0; // both couplings reach the other node
285 const double determinant = b0 * b0 - c0 * c0;
286 if(determinant == 0.0) {
287 size_ = 0;
289 }
290 m2_[0] = (b0 * d0 - c0 * d1) / determinant;
291 m2_[1] = (b0 * d1 - c0 * d0) / determinant;
292 m2_[2] = m2_[0];
293 return rt::ErrorCode::ok;
294 }
295 double a[MaxPoints] = {}; // sub-diagonal
296 double b[MaxPoints] = {}; // diagonal
297 double c[MaxPoints] = {}; // super-diagonal
298 double d[MaxPoints] = {}; // right-hand side
299 for(std::size_t j = 0; j < cyclic; ++j) {
300 const std::size_t previous = j == 0 ? cyclic - 1 : j - 1;
301 const double h_prev = h[previous];
302 const double h_next = h[j];
303 const double y_prev = points_[previous].slave;
304 const double y_here = points_[j].slave;
305 const double y_next = points_[j + 1].slave;
306 a[j] = h_prev / 6.0;
307 b[j] = (h_prev + h_next) / 3.0;
308 c[j] = h_next / 6.0;
309 d[j] = (y_next - y_here) / h_next - (y_here - y_prev) / h_prev;
310 }
311
312 // Sherman-Morrison: fold the two cyclic corners into u v^T.
313 const double corner_first = a[0]; // couples m[0] to m[cyclic-1]
314 const double corner_last = c[cyclic - 1]; // couples m[cyclic-1] to m[0]
315 const double gamma = -b[0];
316 double b_mod[MaxPoints] = {};
317 for(std::size_t j = 0; j < cyclic; ++j) {
318 b_mod[j] = b[j];
319 }
320 b_mod[0] -= gamma;
321 b_mod[cyclic - 1] -= corner_first * corner_last / gamma;
322
323 double x[MaxPoints] = {};
324 double z[MaxPoints] = {};
325 double u[MaxPoints] = {};
326 u[0] = gamma;
327 u[cyclic - 1] = corner_last;
328 if(!solve_tridiagonal(a, b_mod, c, d, x, cyclic) ||
329 !solve_tridiagonal(a, b_mod, c, u, z, cyclic)) {
330 size_ = 0;
332 }
333 const double v_dot_x = x[0] + (corner_first / gamma) * x[cyclic - 1];
334 const double v_dot_z = 1.0 + z[0] + (corner_first / gamma) * z[cyclic - 1];
335 if(v_dot_z == 0.0) {
336 size_ = 0;
338 }
339 for(std::size_t j = 0; j < cyclic; ++j) {
340 m2_[j] = x[j] - z[j] * (v_dot_x / v_dot_z);
341 }
342 m2_[n - 1] = m2_[0];
343 return rt::ErrorCode::ok;
344 }
345
346 bool valid() const
347 {
348 return size_ >= 2;
349 }
350
351 rt::Result<double> sample(double master_position) const
352 {
353 return evaluate(master_position, 0);
354 }
355
356 // Analytic derivatives with respect to the master position (order 1 or
357 // 2); the acceptance tests assert C2 continuity through these.
358 rt::Result<double> sample_derivative(double master_position, int order) const
359 {
360 return evaluate(master_position, order);
361 }
362
363private:
364 static bool solve_tridiagonal(const double *a,
365 const double *b,
366 const double *c,
367 const double *d,
368 double *x,
369 std::size_t n)
370 {
371 double diagonal[MaxPoints] = {};
372 double rhs[MaxPoints] = {};
373 for(std::size_t i = 0; i < n; ++i) {
374 diagonal[i] = b[i];
375 rhs[i] = d[i];
376 }
377 for(std::size_t i = 1; i < n; ++i) {
378 if(diagonal[i - 1] == 0.0) {
379 return false;
380 }
381 const double factor = a[i] / diagonal[i - 1];
382 diagonal[i] -= factor * c[i - 1];
383 rhs[i] -= factor * rhs[i - 1];
384 }
385 if(diagonal[n - 1] == 0.0) {
386 return false;
387 }
388 x[n - 1] = rhs[n - 1] / diagonal[n - 1];
389 for(std::size_t i = n - 1; i >= 1; --i) {
390 x[i - 1] = (rhs[i - 1] - c[i - 1] * x[i]) / diagonal[i - 1];
391 if(i == 1) {
392 break;
393 }
394 }
395 return true;
396 }
397
398 rt::Result<double> evaluate(double master_position, int order) const
399 {
400 if(size_ < 2 || !std::isfinite(master_position)) {
402 }
403 double position = master_position;
404 if(periodic_) {
405 const double begin = points_[0].master;
406 const double span = points_[size_ - 1].master - begin;
407 position = std::fmod(position - begin, span);
408 if(position < 0.0) {
409 position += span;
410 }
411 position += begin;
412 } else if(position <= points_[0].master || position >= points_[size_ - 1].master) {
413 // Aperiodic boundary contract: outside the table the slave holds
414 // the endpoint value, so every master-derivative is exactly zero.
415 // At the boundary nodes themselves the sample is in-domain and the
416 // derivatives must match the one-sided interior limit — fall
417 // through to the interval evaluation for exact node hits.
418 if(order == 0) {
419 const double endpoint =
420 position <= points_[0].master ? points_[0].slave : points_[size_ - 1].slave;
421 return rt::Result<double>::success(endpoint);
422 }
423 if(position < points_[0].master || position > points_[size_ - 1].master) {
424 return rt::Result<double>::success(0.0);
425 }
426 }
427
428 std::size_t index = 1;
429 while(index < size_ - 1 && position > points_[index].master) {
430 ++index;
431 }
432 const CamPoint before = points_[index - 1];
433 const CamPoint after = points_[index];
434 const double interval = after.master - before.master;
435 const double left = after.master - position;
436 const double right = position - before.master;
437 const double m_before = m2_[index - 1];
438 const double m_after = m2_[index];
439
440 if(order == 0) {
441 const double value =
442 m_before * left * left * left / (6.0 * interval) +
443 m_after * right * right * right / (6.0 * interval) +
444 (before.slave - m_before * interval * interval / 6.0) * left / interval +
445 (after.slave - m_after * interval * interval / 6.0) * right / interval;
446 return rt::Result<double>::success(value);
447 }
448 if(order == 1) {
449 const double value =
450 -m_before * left * left / (2.0 * interval) +
451 m_after * right * right / (2.0 * interval) +
452 (after.slave - before.slave) / interval -
453 (m_after - m_before) * interval / 6.0;
454 return rt::Result<double>::success(value);
455 }
456 const double value = (m_before * left + m_after * right) / interval;
457 return rt::Result<double>::success(value);
458 }
459
460 CamPoint points_[MaxPoints] = {};
461 double m2_[MaxPoints] = {};
462 std::size_t size_ = 0;
463 bool periodic_ = false;
464};
465
467{
468 return base + overlay;
469}
470
471} // namespace plcopen::core::exec
static constexpr std::size_t MaxPoints
定义 sync.h:211
rt::Result< double > sample(double master_position) const
定义 sync.h:351
rt::ErrorCode build(const CamTableView &table)
定义 sync.h:213
rt::Result< double > sample_derivative(double master_position, int order) const
定义 sync.h:358
std::size_t size() const
定义 sync.h:161
void set_periodic(bool periodic)
定义 sync.h:166
rt::ErrorCode push(CamPoint point)
定义 sync.h:153
CamTableView view() const
定义 sync.h:176
rt::Result< double > sample(double master_position) const
定义 sync.h:181
static constexpr Result success(const T &value)
定义 error.h:24
static constexpr Result failure(ErrorCode error)
定义 error.h:29
geom::Vec3 apply_overlay(geom::Vec3 base, geom::Vec3 overlay)
定义 sync.h:466
double cam_law_value(CamLaw law, double x)
定义 sync.h:44
double sample_gear(double master_position, GearMap map)
定义 sync.h:19
rt::ErrorCode generate_cam_law(CamLaw law, double master_span, double rise, CamPoint *points, std::size_t point_count)
定义 sync.h:75
rt::Result< double > sample(double master_position) const
定义 sync.h:119