plcopen 0.22.0
载入中...
搜索中...
未找到
verify.h
浏览该文件的文档.
1#pragma once
2
3// Kinematics plugin conformance harness (approved matrix, decisions #2/#8):
4// round-trip consistency (inverse(forward(q), seed=q) == q), seed-branch
5// stability along continuous joint paths, and singularity-margin sanity,
6// driven by a deterministic LCG over caller-supplied joint ranges. The
7// contract is asserted here, not trusted: every reference implementation and
8// every third-party plugin runs the same harness.
9
10#include <cmath>
11#include <cstddef>
12#include <cstdint>
13#include <cstring>
14
15#include "kin/differential.h"
16#include "kin/kinematics.h"
17
18namespace plcopen::core::kin
19{
20
22{
23 double minimum = -1.0;
24 double maximum = 1.0;
25};
26
28{
29 int round_trips = 0;
30 int branch_steps = 0;
32};
33
34namespace detail
35{
36
37struct Lcg
38{
39 std::uint32_t state = 0x4B1D5EEDu;
40
41 double range(double minimum, double maximum)
42 {
43 state = state * 1664525u + 1013904223u;
44 const double unit = static_cast<double>(state >> 8) / 16777216.0;
45 return minimum + unit * (maximum - minimum);
46 }
47};
48
49} // namespace detail
50
51// Returns ok when the plugin honors the contract over `iterations` random
52// joint states; the first violation aborts with the failing error code.
54 const VerifyRange *joint_ranges,
55 int iterations,
56 double tolerance,
57 VerifyReport &report)
58{
59 const std::size_t joints = plugin.joint_count();
60 if(joints < 1 || joints > 8 || plugin.cartesian_count() < 2 ||
61 plugin.cartesian_count() > 3) {
63 }
64
65 detail::Lcg rng{};
66 double q[8] = {};
67 double solved[8] = {};
68
69 // Round trip: inverse(forward(q), seed=q) must reproduce q.
70 for(int i = 0; i < iterations; ++i) {
71 for(std::size_t j = 0; j < joints; ++j) {
72 q[j] = rng.range(joint_ranges[j].minimum, joint_ranges[j].maximum);
73 }
74 geom::Vec3 cartesian{};
75 rt::ErrorCode result = plugin.forward(q, joints, cartesian);
76 if(result != rt::ErrorCode::ok) {
77 return result;
78 }
79 result = plugin.inverse(cartesian, q, joints, solved);
80 if(result != rt::ErrorCode::ok) {
81 return result;
82 }
83 for(std::size_t j = 0; j < joints; ++j) {
84 const double error = std::fabs(solved[j] - q[j]);
85 if(error > report.worst_round_trip_error) {
86 report.worst_round_trip_error = error;
87 }
88 if(error > tolerance) {
90 }
91 }
92 ++report.round_trips;
93 }
94
95 // Branch stability: along a continuous joint-space walk, solving each
96 // pose with the previous solution as the seed must not jump branches
97 // (bounded joint steps).
98 for(std::size_t j = 0; j < joints; ++j) {
99 q[j] = 0.5 * (joint_ranges[j].minimum + joint_ranges[j].maximum);
100 }
101 double previous[8] = {};
102 for(std::size_t j = 0; j < joints; ++j) {
103 previous[j] = q[j];
104 }
105 const int walk = iterations < 512 ? iterations : 512;
106 for(int i = 0; i < walk; ++i) {
107 for(std::size_t j = 0; j < joints; ++j) {
108 const double span = joint_ranges[j].maximum - joint_ranges[j].minimum;
109 double next = q[j] + rng.range(-0.01, 0.01) * span;
110 if(next < joint_ranges[j].minimum) {
111 next = joint_ranges[j].minimum;
112 } else if(next > joint_ranges[j].maximum) {
113 next = joint_ranges[j].maximum;
114 }
115 q[j] = next;
116 }
117 geom::Vec3 cartesian{};
118 if(plugin.forward(q, joints, cartesian) != rt::ErrorCode::ok) {
120 }
121 if(plugin.inverse(cartesian, previous, joints, solved) != rt::ErrorCode::ok) {
123 }
124 for(std::size_t j = 0; j < joints; ++j) {
125 const double span = joint_ranges[j].maximum - joint_ranges[j].minimum;
126 if(std::fabs(solved[j] - previous[j]) > 0.25 * span) {
127 return rt::ErrorCode::precondition_failed; // branch jump
128 }
129 previous[j] = solved[j];
130 }
131 ++report.branch_steps;
132 }
133
134 return rt::ErrorCode::ok;
135}
136
144
145// Compliance-layer oracle (D01, test/verification only — never called in
146// production): validates an analytical TranslationPathDifferential against an
147// independent 6th-order FK-only Implicit-Function-Theorem central finite-
148// difference oracle (with offset-stabilized differences and scale-adaptive
149// step selection) and checks bitwise determinism via object representation.
151 const Kinematics &plugin,
152 const TranslationPathDifferential &differential,
153 const VerifyRange *joint_ranges,
154 int iterations,
155 double qs_tolerance,
156 double qss_tolerance,
158 double min_singularity_margin = 0.15)
159{
160 report = DifferentialVerifyReport{};
161 const std::size_t joints = plugin.joint_count();
162 if(joint_ranges == nullptr || joints < 2 || joints > 3 ||
163 plugin.cartesian_count() != joints || differential.joint_count() != joints ||
164 iterations <= 0 || !std::isfinite(qs_tolerance) || qs_tolerance <= 0.0 ||
165 !std::isfinite(qss_tolerance) || qss_tolerance <= 0.0) {
167 }
168
169 detail::Lcg rng{};
170 rng.state = 0x7B2B5EEDu;
171 constexpr double kPi = 3.14159265358979323846;
172 constexpr double kEps = 2.2204460492503131e-16;
173 // Independent Implicit-Function-Theorem (FK-only) 6th-order FD oracle:
174 // NEVER calls `differential.line_derivatives()` at shifted points.
175 // Because F(q(s)) = p0 + s*d identically on any smooth branch:
176 // J(q) * q_s = d
177 // J(q) * q_ss = -D^2 F(q)[q_s, q_s]
178 // Offset-stabilized symmetric differences and scale-adaptive step doubling
179 // (accepted only while doubled-step shift stays inside estimated roundoff)
180 // prevent false rejections on small-scale / large-offset affine plugins
181 // without sacrificing 6th-order accuracy on curved mechanisms.
182 constexpr double kStepJInit = 1.0e-3;
183 constexpr double kStepHInit = 5.0e-3;
184 double q[3] = {};
185 double q_check[3] = {};
186 double qs[3] = {};
187 double qss[3] = {};
188 double qs_repeat[3] = {};
189 double qss_repeat[3] = {};
190
191 auto same_double_bits = [](double a, double b) {
192 static_assert(sizeof(std::uint64_t) == sizeof(double),
193 "IEEE-754 binary64 size expected");
194 std::uint64_t bits_a = 0;
195 std::uint64_t bits_b = 0;
196 std::memcpy(&bits_a, &a, sizeof(double));
197 std::memcpy(&bits_b, &b, sizeof(double));
198 return bits_a == bits_b;
199 };
200
201 auto vec3_comp = [](const geom::Vec3 &v, std::size_t idx) -> double {
202 return idx == 0 ? v.x : (idx == 1 ? v.y : v.z);
203 };
204
205 auto solve_small_linear = [&](const double A_in[3][3], const double b_in[3],
206 double x_out[3]) -> bool {
207 double A[3][3] = {};
208 double b[3] = {};
209 for(std::size_t r = 0; r < joints; ++r) {
210 b[r] = b_in[r];
211 for(std::size_t c = 0; c < joints; ++c) {
212 A[r][c] = A_in[r][c];
213 }
214 }
215 for(std::size_t col = 0; col < joints; ++col) {
216 std::size_t piv = col;
217 double best = std::fabs(A[col][col]);
218 for(std::size_t r = col + 1; r < joints; ++r) {
219 const double v = std::fabs(A[r][col]);
220 if(v > best) {
221 best = v;
222 piv = r;
223 }
224 }
225 if(!std::isfinite(best) || best <= 1.0e-12) {
226 return false;
227 }
228 if(piv != col) {
229 for(std::size_t c = col; c < joints; ++c) {
230 const double tmp = A[col][c];
231 A[col][c] = A[piv][c];
232 A[piv][c] = tmp;
233 }
234 const double tb = b[col];
235 b[col] = b[piv];
236 b[piv] = tb;
237 }
238 for(std::size_t r = col + 1; r < joints; ++r) {
239 const double f = A[r][col] / A[col][col];
240 for(std::size_t c = col + 1; c < joints; ++c) {
241 A[r][c] -= f * A[col][c];
242 }
243 b[r] -= f * b[col];
244 }
245 }
246 for(std::size_t k = joints; k > 0; --k) {
247 const std::size_t r = k - 1;
248 double sum = b[r];
249 for(std::size_t c = r + 1; c < joints; ++c) {
250 sum -= A[r][c] * x_out[c];
251 }
252 x_out[r] = sum / A[r][r];
253 if(!std::isfinite(x_out[r])) {
254 return false;
255 }
256 }
257 return true;
258 };
259
260 const int max_attempts = iterations * 8;
261 for(int attempt = 0; attempt < max_attempts && report.verified_samples < iterations; ++attempt) {
262 for(std::size_t j = 0; j < joints; ++j) {
263 q[j] = rng.range(joint_ranges[j].minimum, joint_ranges[j].maximum);
264 }
265 if(plugin.singularity_margin(q, joints) < min_singularity_margin) {
266 ++report.skipped_boundary;
267 continue;
268 }
269 geom::Vec3 p0{};
270 if(plugin.forward(q, joints, p0) != rt::ErrorCode::ok ||
271 plugin.inverse(p0, q, joints, q_check) != rt::ErrorCode::ok) {
272 ++report.skipped_boundary;
273 continue;
274 }
275 bool rt_ok = true;
276 for(std::size_t j = 0; j < joints; ++j) {
277 if(std::fabs(q_check[j] - q[j]) > 1.0e-8) {
278 rt_ok = false;
279 break;
280 }
281 }
282 if(!rt_ok) {
283 ++report.skipped_boundary;
284 continue;
285 }
286
287 // Random unit direction (2D planar or 3D unit sphere).
288 const double azimuth = rng.range(0.0, 2.0 * kPi);
289 geom::Vec3 dir{};
290 if(joints == 2) {
291 dir = geom::Vec3{std::cos(azimuth), std::sin(azimuth), 0.0};
292 } else {
293 const double dz = rng.range(-1.0, 1.0);
294 const double radial = std::sqrt(1.0 - dz * dz);
295 dir = geom::Vec3{radial * std::cos(azimuth), radial * std::sin(azimuth), dz};
296 }
297
298 // 1. Independent FK Jacobian J(q) via offset-stabilized 6th-order central FD
299 // with scale-adaptive step doubling when roundoff dominates.
300 double J[3][3] = {};
301 bool fk_ok = true;
302 constexpr double kOff[6] = {-3.0, -2.0, -1.0, 1.0, 2.0, 3.0};
303 auto eval_j_col = [&](std::size_t col, double h, double col_out[3],
304 double &f_mag_out) -> bool {
305 geom::Vec3 fp[6]{};
306 f_mag_out = 0.0;
307 for(int s = 0; s < 6; ++s) {
308 double q_shift[3] = {q[0], q[1], q[2]};
309 q_shift[col] += kOff[s] * h;
310 if(plugin.forward(q_shift, joints, fp[s]) != rt::ErrorCode::ok) {
311 return false;
312 }
313 for(std::size_t row = 0; row < joints; ++row) {
314 const double mag = std::fabs(vec3_comp(fp[s], row));
315 if(mag > f_mag_out) f_mag_out = mag;
316 }
317 }
318 const double inv_denom = 1.0 / (60.0 * h);
319 for(std::size_t row = 0; row < joints; ++row) {
320 const double d1 = vec3_comp(fp[3], row) - vec3_comp(fp[2], row);
321 const double d2 = vec3_comp(fp[4], row) - vec3_comp(fp[1], row);
322 const double d3 = vec3_comp(fp[5], row) - vec3_comp(fp[0], row);
323 col_out[row] = (45.0 * d1 - 9.0 * d2 + d3) * inv_denom;
324 }
325 return true;
326 };
327 for(std::size_t col = 0; col < joints && fk_ok; ++col) {
328 double h_j = kStepJInit;
329 double col_cur[3] = {};
330 double f_mag = 0.0;
331 if(!eval_j_col(col, h_j, col_cur, f_mag)) {
332 fk_ok = false;
333 break;
334 }
335 for(int grow = 0; grow < 20; ++grow) {
336 double col_norm = 0.0;
337 for(std::size_t r = 0; r < joints; ++r) {
338 const double av = std::fabs(col_cur[r]);
339 if(av > col_norm) col_norm = av;
340 }
341 const double rnd_cur = 16.0 * kEps * (f_mag + 1.0e-300) / h_j;
342 if(rnd_cur <= 1.0e-12 * (col_norm > 1.0e-12 ? col_norm : 1.0e-12) ||
343 h_j >= 1.0e3) {
344 break;
345 }
346 double col_next[3] = {};
347 double f_mag_next = 0.0;
348 const double h_next = h_j * 2.0;
349 if(!eval_j_col(col, h_next, col_next, f_mag_next)) {
350 break;
351 }
352 const double rnd_next = 16.0 * kEps * (f_mag_next + 1.0e-300) / h_next;
353 double diff_inf = 0.0;
354 for(std::size_t r = 0; r < joints; ++r) {
355 const double d = std::fabs(col_next[r] - col_cur[r]);
356 if(d > diff_inf) diff_inf = d;
357 }
358 if(diff_inf <= 4.0 * (rnd_cur + rnd_next)) {
359 h_j = h_next;
360 f_mag = f_mag_next;
361 for(std::size_t r = 0; r < joints; ++r) col_cur[r] = col_next[r];
362 } else {
363 break;
364 }
365 }
366 for(std::size_t row = 0; row < joints; ++row) {
367 J[row][col] = col_cur[row];
368 }
369 }
370 if(!fk_ok) {
371 ++report.skipped_boundary;
372 continue;
373 }
374
375 // 2. Solve J * fd_qs = dir independently of `differential` and estimate ||J^{-1}||_inf.
376 const double rhs_qs[3] = {dir.x, dir.y, dir.z};
377 double fd_qs[3] = {};
378 if(!solve_small_linear(J, rhs_qs, fd_qs)) {
379 ++report.skipped_boundary;
380 continue;
381 }
382 double inv_j_norm = 1.0;
383 for(std::size_t k = 0; k < joints; ++k) {
384 double ek[3] = {0.0, 0.0, 0.0};
385 ek[k] = 1.0;
386 double col_inv[3] = {};
387 if(solve_small_linear(J, ek, col_inv)) {
388 for(std::size_t r = 0; r < joints; ++r) {
389 const double av = std::fabs(col_inv[r]);
390 if(av > inv_j_norm) inv_j_norm = av;
391 }
392 }
393 }
394
395 // 3. Compute directional 2nd derivative H = D^2 F(q)[fd_qs, fd_qs] via
396 // offset-stabilized 6th-order central 2nd FD of F(q + tau * v_hat)
397 // with scale-adaptive step doubling, then solve J * fd_qss = -H.
398 double norm_qs = 0.0;
399 for(std::size_t j = 0; j < joints; ++j) {
400 norm_qs += fd_qs[j] * fd_qs[j];
401 }
402 norm_qs = std::sqrt(norm_qs);
403 double fd_qss[3] = {};
404 if(norm_qs > 1.0e-15) {
405 double v_hat[3] = {};
406 for(std::size_t j = 0; j < joints; ++j) {
407 v_hat[j] = fd_qs[j] / norm_qs;
408 }
409 auto eval_d2f_hat = [&](double h, double d2_out[3], double &f_mag_out) -> bool {
410 geom::Vec3 fh[6]{};
411 f_mag_out = 0.0;
412 for(std::size_t row = 0; row < joints; ++row) {
413 const double m0 = std::fabs(vec3_comp(p0, row));
414 if(m0 > f_mag_out) f_mag_out = m0;
415 }
416 for(int s = 0; s < 6; ++s) {
417 double q_shift[3] = {q[0], q[1], q[2]};
418 for(std::size_t j = 0; j < joints; ++j) {
419 q_shift[j] += kOff[s] * h * v_hat[j];
420 }
421 if(plugin.forward(q_shift, joints, fh[s]) != rt::ErrorCode::ok) {
422 return false;
423 }
424 for(std::size_t row = 0; row < joints; ++row) {
425 const double mag = std::fabs(vec3_comp(fh[s], row));
426 if(mag > f_mag_out) f_mag_out = mag;
427 }
428 }
429 const double inv_h2 = 1.0 / (180.0 * h * h);
430 for(std::size_t row = 0; row < joints; ++row) {
431 const double c0 = vec3_comp(p0, row);
432 // Paired offset-stabilized differences around p0 prevent offset cancellation.
433 const double s1 = (vec3_comp(fh[3], row) - c0) + (vec3_comp(fh[2], row) - c0);
434 const double s2 = (vec3_comp(fh[4], row) - c0) + (vec3_comp(fh[1], row) - c0);
435 const double s3 = (vec3_comp(fh[5], row) - c0) + (vec3_comp(fh[0], row) - c0);
436 d2_out[row] = (270.0 * s1 - 27.0 * s2 + 2.0 * s3) * inv_h2;
437 }
438 return true;
439 };
440
441 double h_h = kStepHInit;
442 double d2_cur[3] = {};
443 double f_mag_h = 0.0;
444 if(!eval_d2f_hat(h_h, d2_cur, f_mag_h)) {
445 ++report.skipped_boundary;
446 continue;
447 }
448 const double qs_sq = norm_qs * norm_qs;
449 for(int grow = 0; grow < 24; ++grow) {
450 const double rnd_d2_cur = 32.0 * kEps * (f_mag_h + 1.0e-300) / (h_h * h_h);
451 const double sigma_qss = rnd_d2_cur * qs_sq * inv_j_norm;
452 if(sigma_qss <= 0.02 * qss_tolerance || h_h >= 1.0e4) {
453 break;
454 }
455 double d2_next[3] = {};
456 double f_mag_next = 0.0;
457 const double h_next = h_h * 2.0;
458 if(!eval_d2f_hat(h_next, d2_next, f_mag_next)) {
459 break;
460 }
461 const double rnd_d2_next = 32.0 * kEps * (f_mag_next + 1.0e-300) / (h_next * h_next);
462 double diff_inf = 0.0;
463 for(std::size_t r = 0; r < joints; ++r) {
464 const double d = std::fabs(d2_next[r] - d2_cur[r]);
465 if(d > diff_inf) diff_inf = d;
466 }
467 if(diff_inf <= 4.0 * (rnd_d2_cur + rnd_d2_next)) {
468 h_h = h_next;
469 f_mag_h = f_mag_next;
470 for(std::size_t r = 0; r < joints; ++r) d2_cur[r] = d2_next[r];
471 } else {
472 break;
473 }
474 }
475 double rhs_qss[3] = {};
476 for(std::size_t row = 0; row < joints; ++row) {
477 rhs_qss[row] = -(d2_cur[row] * qs_sq);
478 }
479 if(!solve_small_linear(J, rhs_qss, fd_qss)) {
480 ++report.skipped_boundary;
481 continue;
482 }
483 }
484
485 const rt::ErrorCode rc1 = differential.line_derivatives(p0, dir, q, joints, qs, qss);
486 const rt::ErrorCode rc2 =
487 differential.line_derivatives(p0, dir, q, joints, qs_repeat, qss_repeat);
488 if(rc1 != rt::ErrorCode::ok || rc2 != rt::ErrorCode::ok) {
489 return rc1 != rt::ErrorCode::ok ? rc1 : rc2;
490 }
491 for(std::size_t j = 0; j < joints; ++j) {
492 if(!same_double_bits(qs[j], qs_repeat[j]) ||
493 !same_double_bits(qss[j], qss_repeat[j])) {
494 return rt::ErrorCode::precondition_failed; // non-deterministic (bitwise)
495 }
496 const double err_qs = std::fabs(qs[j] - fd_qs[j]);
497 const double err_qss = std::fabs(qss[j] - fd_qss[j]);
498 if(err_qs > report.worst_qs_error) {
499 report.worst_qs_error = err_qs;
500 }
501 if(err_qss > report.worst_qss_error) {
502 report.worst_qss_error = err_qss;
503 }
504 if(!std::isfinite(err_qs) || err_qs > qs_tolerance ||
505 !std::isfinite(err_qss) || err_qss > qss_tolerance) {
507 }
508 }
509 geom::Vec3 fv1{};
510 geom::Vec3 fv2{};
511 const rt::ErrorCode fv_rc1 = differential.forward_velocity(q, qs, joints, fv1);
512 if(fv_rc1 != rt::ErrorCode::unsupported) {
513 const rt::ErrorCode fv_rc2 = differential.forward_velocity(q, qs, joints, fv2);
514 if(fv_rc1 != rt::ErrorCode::ok || fv_rc2 != rt::ErrorCode::ok) {
515 return fv_rc1 != rt::ErrorCode::ok ? fv_rc1 : fv_rc2;
516 }
517 if(!same_double_bits(fv1.x, fv2.x) ||
518 !same_double_bits(fv1.y, fv2.y) ||
519 !same_double_bits(fv1.z, fv2.z)) {
521 }
522 const double fv_err = detail::norm3_safe(
523 fv1.x - dir.x, fv1.y - dir.y, fv1.z - dir.z);
524 if(!std::isfinite(fv_err) || fv_err > qs_tolerance) {
526 }
527 }
528 ++report.verified_samples;
529 }
530
531 if(report.verified_samples < iterations) {
533 }
534 return rt::ErrorCode::ok;
535}
536
537} // namespace plcopen::core::kin
virtual rt::ErrorCode inverse(geom::Vec3 cartesian, const double *seed_joints, std::size_t joint_count, double *joints_out) const =0
virtual std::size_t cartesian_count() const =0
virtual double singularity_margin(const double *joints, std::size_t joint_count) const =0
virtual std::size_t joint_count() const =0
virtual rt::ErrorCode forward(const double *joints, std::size_t joint_count, geom::Vec3 &cartesian) const =0
virtual std::size_t joint_count() const =0
virtual rt::ErrorCode line_derivatives(geom::Vec3 plugin_point, geom::Vec3 unit_direction, const double *base_joints, std::size_t joint_count, double *q_s_out, double *q_ss_out) const =0
virtual rt::ErrorCode forward_velocity(const double *joints, const double *joint_velocities, std::size_t joint_count, geom::Vec3 &linear_velocity_out) const
double norm3_safe(double x, double y, double z)
rt::ErrorCode verify_translation_path_differential(const Kinematics &plugin, const TranslationPathDifferential &differential, const VerifyRange *joint_ranges, int iterations, double qs_tolerance, double qss_tolerance, DifferentialVerifyReport &report, double min_singularity_margin=0.15)
rt::ErrorCode verify_kinematics(const Kinematics &plugin, const VerifyRange *joint_ranges, int iterations, double tolerance, VerifyReport &report)
double range(double minimum, double maximum)