plcopen 0.22.0
载入中...
搜索中...
未找到
differential.h
浏览该文件的文档.
1#pragma once
2
3// Math-Max Step 5 (Y7b2b): optional non-breaking companion interface providing
4// exact closed-form first and second joint arc-length derivatives (dq/ds,
5// d^2q/ds^2) along translational Cartesian LINE paths without modifying
6// kin::Kinematics vtables or introducing RTTI.
7// Normative spec: doc/compliance/cartesian-target-takeover-semantics.md (D01).
8//
9// RT-SAFE: zero heap allocation, no locks, no exceptions, no OS calls, no
10// wall-clock access, no floating-point time state, no finite differences.
11
12#include <cmath>
13#include <cstddef>
14
15#include "geom/geometry.h"
16#include "rt/error.h"
17
19{
20
21// Provable global interval upper bounds for (q_s, q_ss, q_sss) and joint range
22// along an entire translational line segment x(s) = plugin_point + s * unit_direction
23// for s in [0, path_length] on the configuration branch of `start_joints`.
24// Used by CartesianTakeoverPlanner to certify long tails (base_dur > MaxReplayCycles)
25// via the Peano Kernel Theorem without heuristic sampling or finite differences.
27{
28 double qs_pos[3] = {};
29 double qs_neg[3] = {};
30 double qs_abs[3] = {};
31 double qss_abs[3] = {};
32 double qsss_abs[3] = {};
33 double q_min[3] = {};
34 double q_max[3] = {};
36 bool has_joint_range = false;
37};
38
40{
41public:
42 virtual ~TranslationPathDifferential() = default;
43
44 virtual std::size_t joint_count() const = 0;
45
46 // Evaluates closed-form first and second joint derivatives with respect to
47 // Cartesian arc length s along x(s) = plugin_point + s * unit_direction at
48 // s = 0 on the exact configuration branch of `base_joints`.
49 // Does not mutate q_s_out / q_ss_out on failure.
51 geom::Vec3 unit_direction,
52 const double *base_joints,
53 std::size_t joint_count,
54 double *q_s_out,
55 double *q_ss_out) const = 0;
56
57 // Optional certified global interval bounds over the entire segment
58 // s in [0, path_length]. Implementations overriding this MUST return
59 // mathematically proven upper bounds (never finite-difference heuristics).
60 // Default returns rt::ErrorCode::unsupported, causing the planner to
61 // require full tick-by-tick replay (base_dur + 2 <= MaxReplayCycles) and
62 // reject uncertified long tails fail-closed.
64 geom::Vec3 unit_direction,
65 double path_length,
66 const double *start_joints,
67 std::size_t joint_count,
68 LineDerivativeBounds &bounds_out) const
69 {
70 (void)plugin_point;
71 (void)unit_direction;
72 (void)path_length;
73 (void)start_joints;
74 (void)joint_count;
75 (void)bounds_out;
77 }
78
79 // Optional closed-form forward velocity mapping v_tcp = J(q) * dq in the
80 // plugin's Cartesian frame (zero finite differences, immune to large-offset
81 // subtraction cancellation). Default returns `unsupported`.
82 virtual rt::ErrorCode forward_velocity(const double *joints,
83 const double *joint_velocities,
84 std::size_t joint_count,
85 geom::Vec3 &linear_velocity_out) const
86 {
87 (void)joints;
88 (void)joint_velocities;
89 (void)joint_count;
90 (void)linear_velocity_out;
92 }
93};
94
95namespace detail
96{
97
99{
100 return std::isfinite(v.x) && std::isfinite(v.y) && std::isfinite(v.z);
101}
102
103inline double norm3_safe(double x, double y, double z)
104{
105 return std::hypot(std::hypot(x, y), z);
106}
107
109{
110 if(!is_finite_vec3(d)) {
111 return false;
112 }
113 const double n = norm3_safe(d.x, d.y, d.z);
114 return std::isfinite(n) && std::fabs(n - 1.0) <= 1.0e-6;
115}
116
117} // namespace detail
118
119// Closed-form differential companion for kin::CartesianGantry:
120// x_i = scale_i * q_i + offset_i => dq_i/ds = d_i / scale_i, d^2q_i/ds^2 = 0.
122{
123public:
124 static constexpr double FkConsistencyTolerance = 1.0e-6;
125
126 CartesianGantryDifferential(std::size_t axis_count,
127 const double *scale,
128 const double *offset)
129 : count_(axis_count)
130 {
131 if(scale != nullptr && offset != nullptr && axis_count >= 2 && axis_count <= 3) {
132 bool ok = true;
133 for(std::size_t i = 0; i < axis_count; ++i) {
134 scale_[i] = scale[i];
135 offset_[i] = offset[i];
136 if(!std::isfinite(scale_[i]) || scale_[i] == 0.0 || !std::isfinite(offset_[i])) {
137 ok = false;
138 }
139 }
140 valid_ = ok;
141 }
142 }
143
144 std::size_t joint_count() const override
145 {
146 return count_;
147 }
148
150 geom::Vec3 unit_direction,
151 const double *base_joints,
152 std::size_t joint_count,
153 double *q_s_out,
154 double *q_ss_out) const override
155 {
156 if(!valid_ || joint_count != count_ || base_joints == nullptr ||
157 q_s_out == nullptr || q_ss_out == nullptr ||
158 !detail::is_finite_vec3(plugin_point) ||
159 !detail::is_unit_direction(unit_direction)) {
161 }
162 if(count_ == 2 && std::fabs(unit_direction.z) > 1.0e-6) {
164 }
165 for(std::size_t i = 0; i < count_; ++i) {
166 if(!std::isfinite(base_joints[i])) {
168 }
169 }
170
171 const double fk_x = scale_[0] * base_joints[0] + offset_[0];
172 const double fk_y = scale_[1] * base_joints[1] + offset_[1];
173 const double fk_z = count_ > 2 ? (scale_[2] * base_joints[2] + offset_[2]) : 0.0;
174 const double fk_err = detail::norm3_safe(
175 fk_x - plugin_point.x, fk_y - plugin_point.y, fk_z - plugin_point.z);
176 if(!std::isfinite(fk_err) || fk_err > FkConsistencyTolerance) {
178 }
179
180 const double dir[3] = {unit_direction.x, unit_direction.y, unit_direction.z};
181 double qs[3] = {};
182 for(std::size_t i = 0; i < count_; ++i) {
183 qs[i] = dir[i] / scale_[i];
184 if(!std::isfinite(qs[i])) {
186 }
187 }
188 for(std::size_t i = 0; i < count_; ++i) {
189 q_s_out[i] = qs[i];
190 q_ss_out[i] = 0.0;
191 }
192 return rt::ErrorCode::ok;
193 }
194
196 geom::Vec3 unit_direction,
197 double path_length,
198 const double *start_joints,
199 std::size_t joint_count,
200 LineDerivativeBounds &bounds_out) const override
201 {
202 if(!std::isfinite(path_length) || path_length <= 1.0e-12) {
204 }
205 double qs[3] = {};
206 double qss[3] = {};
208 plugin_point, unit_direction, start_joints, joint_count, qs, qss);
209 if(rc != rt::ErrorCode::ok) {
210 return rc;
211 }
214 b.has_joint_range = true;
215 for(std::size_t i = 0; i < count_; ++i) {
216 const double q0 = start_joints[i];
217 const double qL = q0 + qs[i] * path_length;
218 if(!std::isfinite(qL)) {
220 }
221 const double aq = std::fabs(qs[i]);
222 b.qs_pos[i] = qs[i] > 0.0 ? qs[i] : 0.0;
223 b.qs_neg[i] = qs[i] < 0.0 ? -qs[i] : 0.0;
224 b.qs_abs[i] = aq;
225 b.qss_abs[i] = 0.0;
226 b.qsss_abs[i] = 0.0;
227 b.q_min[i] = q0 < qL ? q0 : qL;
228 b.q_max[i] = q0 > qL ? q0 : qL;
229 }
230 bounds_out = b;
231 return rt::ErrorCode::ok;
232 }
233
234 rt::ErrorCode forward_velocity(const double *joints,
235 const double *joint_velocities,
236 std::size_t joint_count,
237 geom::Vec3 &linear_velocity_out) const override
238 {
239 if(!valid_ || joint_count != count_ || joints == nullptr ||
240 joint_velocities == nullptr) {
242 }
243 double v[3] = {0.0, 0.0, 0.0};
244 for(std::size_t i = 0; i < count_; ++i) {
245 if(!std::isfinite(joints[i]) || !std::isfinite(joint_velocities[i])) {
247 }
248 v[i] = scale_[i] * joint_velocities[i];
249 if(!std::isfinite(v[i])) {
251 }
252 }
253 linear_velocity_out = {v[0], v[1], v[2]};
254 return rt::ErrorCode::ok;
255 }
256
257private:
258 std::size_t count_ = 0;
259 double scale_[3] = {1.0, 1.0, 1.0};
260 double offset_[3] = {0.0, 0.0, 0.0};
261 bool valid_ = false;
262};
263
264// Closed-form differential companion for kin::Scara (2R or 2R+Z):
265// x = L1*cos(t1) + L2*cos(t1+t2)
266// y = L1*sin(t1) + L2*sin(t1+t2)
267// z = q2 (when with_z)
268// Exact first and second arc-length derivatives along x''(s) = 0 on the branch
269// of base_joints; fails closed with `infeasible` when |sin(t2)| <= 1e-6.
271{
272public:
273 static constexpr double FkConsistencyTolerance = 1.0e-6;
274 static constexpr double MinSinTheta2 = 1.0e-6;
275
276 ScaraDifferential(double link1, double link2, bool with_z)
277 : link1_(link1)
278 , link2_(link2)
279 , with_z_(with_z)
280 , valid_(std::isfinite(link1) && link1 > 0.0 && std::isfinite(link2) && link2 > 0.0)
281 {
282 }
283
284 std::size_t joint_count() const override
285 {
286 return with_z_ ? 3 : 2;
287 }
288
290 geom::Vec3 unit_direction,
291 const double *base_joints,
292 std::size_t joint_count,
293 double *q_s_out,
294 double *q_ss_out) const override
295 {
296 const std::size_t expected = this->joint_count();
297 if(!valid_ || joint_count != expected || base_joints == nullptr ||
298 q_s_out == nullptr || q_ss_out == nullptr ||
299 !detail::is_finite_vec3(plugin_point) ||
300 !detail::is_unit_direction(unit_direction)) {
302 }
303 if(!with_z_ && std::fabs(unit_direction.z) > 1.0e-6) {
305 }
306 for(std::size_t i = 0; i < expected; ++i) {
307 if(!std::isfinite(base_joints[i])) {
309 }
310 }
311
312 const double t1 = base_joints[0];
313 const double t2 = base_joints[1];
314 const double t12 = t1 + t2;
315 if(!std::isfinite(t12)) {
317 }
318 const double c1 = std::cos(t1);
319 const double s1 = std::sin(t1);
320 const double c2 = std::cos(t2);
321 const double s2 = std::sin(t2);
322 const double c12 = std::cos(t12);
323 const double s12 = std::sin(t12);
324
325 const double fk_x = link1_ * c1 + link2_ * c12;
326 const double fk_y = link1_ * s1 + link2_ * s12;
327 const double fk_z = with_z_ ? base_joints[2] : 0.0;
328 const double fk_err = detail::norm3_safe(
329 fk_x - plugin_point.x, fk_y - plugin_point.y, fk_z - plugin_point.z);
330 if(!std::isfinite(fk_err) || fk_err > FkConsistencyTolerance) {
332 }
333
334 if(!std::isfinite(s2) || std::fabs(s2) <= MinSinTheta2) {
336 }
337
338 const double denom1 = link1_ * s2;
339 const double denom12 = link1_ * link2_ * s2;
340 if(!std::isfinite(denom1) || denom1 == 0.0 ||
341 !std::isfinite(denom12) || denom12 == 0.0) {
343 }
344
345 const double dx = unit_direction.x;
346 const double dy = unit_direction.y;
347 const double t1_s = (c12 * dx + s12 * dy) / denom1;
348 const double t2_s =
349 (-(link1_ * c1 + link2_ * c12) * dx - (link1_ * s1 + link2_ * s12) * dy) / denom12;
350 const double w12 = t1_s + t2_s;
351 const double w1_sq = t1_s * t1_s;
352 const double w12_sq = w12 * w12;
353
354 const double t1_ss = (link1_ * c2 * w1_sq + link2_ * w12_sq) / denom1;
355 const double t2_ss =
356 -((link1_ + link2_ * c2) * link1_ * w1_sq +
357 (link2_ + link1_ * c2) * link2_ * w12_sq) /
358 denom12;
359
360 if(!std::isfinite(t1_s) || !std::isfinite(t2_s) ||
361 !std::isfinite(t1_ss) || !std::isfinite(t2_ss)) {
363 }
364
365 q_s_out[0] = t1_s;
366 q_s_out[1] = t2_s;
367 q_ss_out[0] = t1_ss;
368 q_ss_out[1] = t2_ss;
369 if(with_z_) {
370 q_s_out[2] = unit_direction.z;
371 q_ss_out[2] = 0.0;
372 }
373 return rt::ErrorCode::ok;
374 }
375
376 // Closed-form provable global interval upper bounds for (q_s, q_ss, q_sss),
377 // singularity margin min |sin(t2)|, and unwrapped joint ranges [q_min, q_max]
378 // over the entire segment s in [0, path_length].
380 geom::Vec3 unit_direction,
381 double path_length,
382 const double *start_joints,
383 std::size_t joint_count,
384 LineDerivativeBounds &bounds_out) const override
385 {
386 if(!std::isfinite(path_length) || path_length <= 1.0e-12) {
388 }
389 double qs0[3] = {};
390 double qss0[3] = {};
392 plugin_point, unit_direction, start_joints, joint_count, qs0, qss0);
393 if(rc != rt::ErrorCode::ok) {
394 return rc;
395 }
396
397 const double t1_0 = start_joints[0];
398 const double t2_0 = start_joints[1];
399 const double s2_0 = std::sin(t2_0);
400 const double sigma = s2_0 >= 0.0 ? 1.0 : -1.0;
401
402 const double x0 = plugin_point.x;
403 const double y0 = plugin_point.y;
404 const double dx = unit_direction.x;
405 const double dy = unit_direction.y;
406 const double d_xy = std::hypot(dx, dy);
407
409 b.has_joint_range = true;
410
411 if(dx == 0.0 && dy == 0.0) {
412 // Exact pure Z translation: planar joints are strictly stationary.
413 b.min_singularity_margin = std::fabs(s2_0);
414 b.q_min[0] = b.q_max[0] = t1_0;
415 b.q_min[1] = b.q_max[1] = t2_0;
416 if(with_z_) {
417 const double z0 = start_joints[2];
418 const double zL = z0 + unit_direction.z * path_length;
419 if(!std::isfinite(zL)) {
421 }
422 b.qs_pos[2] = unit_direction.z > 0.0 ? unit_direction.z : 0.0;
423 b.qs_neg[2] = unit_direction.z < 0.0 ? -unit_direction.z : 0.0;
424 b.qs_abs[2] = std::fabs(unit_direction.z);
425 b.q_min[2] = z0 < zL ? z0 : zL;
426 b.q_max[2] = z0 > zL ? z0 : zL;
427 }
428 bounds_out = b;
429 return rt::ErrorCode::ok;
430 }
431 if(!std::isfinite(d_xy) || !(d_xy > 0.0)) {
433 }
434
435 const double ux = dx / d_xy;
436 const double uy = dy / d_xy;
437 const double L_xy = d_xy * path_length;
438 const double xL = x0 + dx * path_length;
439 const double yL = y0 + dy * path_length;
440 const double r0 = std::hypot(x0, y0);
441 const double rL = std::hypot(xL, yL);
442 if(!std::isfinite(ux) || !std::isfinite(uy) || !std::isfinite(L_xy) ||
443 !std::isfinite(xL) || !std::isfinite(yL) ||
444 !std::isfinite(r0) || !std::isfinite(rL)) {
446 }
447
448 // r^2(s) is a convex parabola on [0, L]:
449 // max r is at {0, L}; min r is at {0, L, s_ca}.
450 // Computing planar closest approach in unit planar direction (ux, uy)
451 // avoids d_xy^2 underflow even for near-pure-Z long segments.
452 double r_max = r0 > rL ? r0 : rL;
453 double r_min = r0 < rL ? r0 : rL;
454 const double dot0 = x0 * dx + y0 * dy;
455 const double proj0 = x0 * ux + y0 * uy;
456 if(!std::isfinite(dot0) || !std::isfinite(proj0)) {
458 }
459 if(proj0 < 0.0 && (-proj0) < L_xy) {
460 // Cancellation-free closest-approach radius: |p0 x u_xy|.
461 const double r_ca = std::fabs(x0 * uy - y0 * ux);
462 if(!std::isfinite(r_ca)) {
464 }
465 if(r_ca < r_min) {
466 r_min = r_ca;
467 }
468 }
469
470 const double r_outer = link1_ + link2_;
471 const double r_inner = std::fabs(link1_ - link2_);
472 if(!(r_max < r_outer) || !(r_min > r_inner) || !(r_min > 1.0e-12)) {
474 }
475
476 const double two_l1_l2 = 2.0 * link1_ * link2_;
477 // Cancellation-free algebraic forms for 1 - c_max and 1 + c_min.
478 const double one_minus_cmax =
479 ((r_outer - r_max) * (r_outer + r_max)) / two_l1_l2;
480 const double one_plus_cmax =
481 ((r_max - r_inner) * (r_max + r_inner)) / two_l1_l2;
482 const double one_minus_cmin =
483 ((r_outer - r_min) * (r_outer + r_min)) / two_l1_l2;
484 const double one_plus_cmin =
485 ((r_min - r_inner) * (r_min + r_inner)) / two_l1_l2;
486 if(!(one_minus_cmax > 0.0) || !(one_plus_cmax > 0.0) ||
487 !(one_minus_cmin > 0.0) || !(one_plus_cmin > 0.0)) {
489 }
490
491 const double sin_at_rmax = std::sqrt(one_minus_cmax * one_plus_cmax);
492 const double sin_at_rmin = std::sqrt(one_minus_cmin * one_plus_cmin);
493 // Concavity of sin on [0, pi] guarantees global minimum occurs at {r_min, r_max}.
494 const double s_min_raw = sin_at_rmax < sin_at_rmin ? sin_at_rmax : sin_at_rmin;
495 const double s_min = s_min_raw * (1.0 - 1.0e-12);
496 if(!std::isfinite(s_min) || s_min <= MinSinTheta2) {
498 }
499
500 const double c_max = 1.0 - one_minus_cmax;
501 const double c_min = one_plus_cmin - 1.0;
502 const double abs_cmax = std::fabs(c_max);
503 const double abs_cmin = std::fabs(c_min);
504 const double C_max = abs_cmax > abs_cmin ? abs_cmax : abs_cmin;
505
506 // Exact analytical global upper bounds over all s in [0, path_length]:
507 // |t1_s| <= d_xy / (L1 * S_min)
508 // |t1_s+t2_s| <= d_xy / (L2 * S_min)
509 // t2_s(s) = -(x(s)*dx + y(s)*dy) / (L1 * L2 * sin(t2(s)))
510 // t1_ss = (L1*cos(t2)*t1_s^2 + L2*(t1_s+t2_s)^2) / (L1*sin(t2))
511 // (t1+t2)_ss = -(L1*t1_s^2 + L2*cos(t2)*(t1_s+t2_s)^2) / (L2*sin(t2))
512 // t2_ss = -(d_xy^2 + L1*L2*cos(t2)*t2_s^2) / (L1*L2*sin(t2))
513 // t1_sss = t1_s^3 + (3*L1*cos(t2)*t1_s*t1_ss + 3*L2*w12*w12_ss) / (L1*sin(t2))
514 // t2_sss = t2_s^3 - 3*(cos(t2)/sin(t2))*t2_s*t2_ss
515 constexpr double kRoundPad = 1.0 + 1.0e-12;
516 const double u0 = -dot0;
517 const double uL = -(xL * dx + yL * dy);
518 const double p_max = std::fabs(u0) > std::fabs(uL) ? std::fabs(u0) : std::fabs(uL);
519
520 const double q1_1 = (d_xy / (link1_ * s_min)) * kRoundPad;
521 const double q12_1 = (d_xy / (link2_ * s_min)) * kRoundPad;
522 const double denom12_min = link1_ * link2_ * s_min;
523 const double q2_1 = (p_max / denom12_min) * kRoundPad;
524
525 const double sig_u0 = sigma * u0;
526 const double sig_uL = sigma * uL;
527 const double sig_u_hi = sig_u0 > sig_uL ? sig_u0 : sig_uL;
528 const double sig_u_lo = sig_u0 < sig_uL ? sig_u0 : sig_uL;
529 const double q2_pos = (sig_u_hi > 0.0 ? (sig_u_hi / denom12_min) : 0.0) * kRoundPad;
530 const double q2_neg = (sig_u_lo < 0.0 ? (-sig_u_lo / denom12_min) : 0.0) * kRoundPad;
531
532 const double d_xy_sq = d_xy * d_xy;
533 const double q1_1_sq = q1_1 * q1_1;
534 const double q12_1_sq = q12_1 * q12_1;
535 const double q2_1_sq = q2_1 * q2_1;
536 const double q1_1_cb = q1_1_sq * q1_1;
537 const double q2_1_cb = q2_1_sq * q2_1;
538 // Fail closed if any strictly positive planar power underflows to 0.0
539 // (prevents returning non-conservative zero Q2/Q3 certificates).
540 if(!std::isfinite(q1_1) || !(q1_1 > 0.0) ||
541 !std::isfinite(q12_1) || !(q12_1 > 0.0) ||
542 !std::isfinite(q2_1) || (p_max > 0.0 && !(q2_1 > 0.0)) ||
543 !std::isfinite(d_xy_sq) || !(d_xy_sq > 0.0) ||
544 !std::isfinite(q1_1_sq) || !(q1_1_sq > 0.0) ||
545 !std::isfinite(q12_1_sq) || !(q12_1_sq > 0.0) ||
546 !std::isfinite(q1_1_cb) || !(q1_1_cb > 0.0) ||
547 (q2_1 > 0.0 && (!std::isfinite(q2_1_sq) || !(q2_1_sq > 0.0) ||
548 !std::isfinite(q2_1_cb) || !(q2_1_cb > 0.0)))) {
550 }
551
552 const double q1_2 =
553 ((link1_ * C_max * q1_1_sq + link2_ * q12_1_sq) / (link1_ * s_min)) * kRoundPad;
554 const double q12_2 =
555 ((link1_ * q1_1_sq + link2_ * C_max * q12_1_sq) / (link2_ * s_min)) * kRoundPad;
556 const double q2_2 =
557 ((d_xy_sq + link1_ * link2_ * C_max * q2_1_sq) / denom12_min) * kRoundPad;
558
559 const double q1_12_prod = q1_1 * q1_2;
560 const double q12_12_prod = q12_1 * q12_2;
561 const double q2_12_prod = q2_1 * q2_2;
562 const double q1_3 =
563 (q1_1_cb +
564 (3.0 * link1_ * C_max * q1_12_prod + 3.0 * link2_ * q12_12_prod) /
565 (link1_ * s_min)) * kRoundPad;
566 const double q2_3 =
567 (q2_1_cb + (3.0 * C_max / s_min) * q2_12_prod) * kRoundPad;
568
569 if(!std::isfinite(q1_2) || !(q1_2 > 0.0) ||
570 !std::isfinite(q12_2) || !(q12_2 > 0.0) ||
571 !std::isfinite(q2_2) || !(q2_2 > 0.0) ||
572 !std::isfinite(q1_12_prod) || !(q1_12_prod > 0.0) ||
573 !std::isfinite(q12_12_prod) || !(q12_12_prod > 0.0) ||
574 !std::isfinite(q1_3) || !(q1_3 > 0.0) ||
575 !std::isfinite(q2_3) ||
576 (q2_1 > 0.0 && (!std::isfinite(q2_12_prod) || !(q2_12_prod > 0.0) || !(q2_3 > 0.0)))) {
578 }
579
580 b.min_singularity_margin = s_min;
581 b.qs_pos[0] = q1_1;
582 b.qs_neg[0] = q1_1;
583 b.qs_abs[0] = q1_1;
584 b.qss_abs[0] = q1_2;
585 b.qsss_abs[0] = q1_3;
586
587 b.qs_pos[1] = q2_pos;
588 b.qs_neg[1] = q2_neg;
589 b.qs_abs[1] = q2_1;
590 b.qss_abs[1] = q2_2;
591 b.qsss_abs[1] = q2_3;
592
593 // Exact unwrapped joint ranges over [0, path_length]:
594 // t2 is monotone in r, anchored at t2_0's 2*pi branch offset.
595 // atan2(sin, cos) keeps full 53-bit accuracy even as c -> +/-1.
596 const double t2_abs_lo = std::atan2(sin_at_rmax, c_max);
597 const double t2_abs_hi = std::atan2(sin_at_rmin, c_min);
598 const double t2_principal_0 =
599 sigma * std::atan2(std::fabs(s2_0), std::cos(t2_0));
600 const double t2_wrap_offset = t2_0 - t2_principal_0;
601 const double t2_a = t2_wrap_offset + sigma * t2_abs_lo;
602 const double t2_b = t2_wrap_offset + sigma * t2_abs_hi;
603 b.q_min[1] = (t2_a < t2_b ? t2_a : t2_b) - 1.0e-12;
604 b.q_max[1] = (t2_a > t2_b ? t2_a : t2_b) + 1.0e-12;
605
606 // t1(s) = phi(s) - psi(r(s)); polar angle phi(s) is strictly monotone with
607 // |delta_phi| < pi (segment avoids origin since r_min > 1e-12), and psi(r)
608 // has at most one interior stationary radius r* = sqrt(L1^2 - L2^2) (when L1 > L2).
609 const double delta_phi = std::atan2(x0 * yL - y0 * xL, x0 * xL + y0 * yL);
610 auto eval_psi = [&](double rad) -> double {
611 const double c_val =
612 (rad * rad - link1_ * link1_ - link2_ * link2_) / two_l1_l2;
613 const double omc = ((r_outer - rad) * (r_outer + rad)) / two_l1_l2;
614 const double opc = ((rad - r_inner) * (rad + r_inner)) / two_l1_l2;
615 const double s_abs =
616 (omc > 0.0 && opc > 0.0) ? std::sqrt(omc * opc) : 0.0;
617 return sigma * std::atan2(link2_ * s_abs, link1_ + link2_ * c_val);
618 };
619 const double psi_0 = eval_psi(r0);
620 double psi_lo = eval_psi(r_min);
621 double psi_hi = eval_psi(r_max);
622 if(psi_lo > psi_hi) {
623 const double tmp = psi_lo;
624 psi_lo = psi_hi;
625 psi_hi = tmp;
626 }
627 if(link1_ > link2_) {
628 const double r_star = std::sqrt(link1_ * link1_ - link2_ * link2_);
629 if(r_star > r_min && r_star < r_max) {
630 const double psi_star = eval_psi(r_star);
631 if(psi_star < psi_lo) psi_lo = psi_star;
632 if(psi_star > psi_hi) psi_hi = psi_star;
633 }
634 }
635 const double dphi_lo = delta_phi < 0.0 ? delta_phi : 0.0;
636 const double dphi_hi = delta_phi > 0.0 ? delta_phi : 0.0;
637 b.q_min[0] = t1_0 + dphi_lo - (psi_hi - psi_0) - 1.0e-12;
638 b.q_max[0] = t1_0 + dphi_hi - (psi_lo - psi_0) + 1.0e-12;
639
640 if(with_z_) {
641 const double z0 = start_joints[2];
642 const double zL = z0 + unit_direction.z * path_length;
643 if(!std::isfinite(zL)) {
645 }
646 b.qs_pos[2] = unit_direction.z > 0.0 ? unit_direction.z : 0.0;
647 b.qs_neg[2] = unit_direction.z < 0.0 ? -unit_direction.z : 0.0;
648 b.qs_abs[2] = std::fabs(unit_direction.z);
649 b.qss_abs[2] = 0.0;
650 b.qsss_abs[2] = 0.0;
651 b.q_min[2] = z0 < zL ? z0 : zL;
652 b.q_max[2] = z0 > zL ? z0 : zL;
653 }
654
655 bounds_out = b;
656 return rt::ErrorCode::ok;
657 }
658
659 rt::ErrorCode forward_velocity(const double *joints,
660 const double *joint_velocities,
661 std::size_t joint_count,
662 geom::Vec3 &linear_velocity_out) const override
663 {
664 const std::size_t expected = this->joint_count();
665 if(!valid_ || joint_count != expected || joints == nullptr ||
666 joint_velocities == nullptr) {
668 }
669 for(std::size_t i = 0; i < expected; ++i) {
670 if(!std::isfinite(joints[i]) || !std::isfinite(joint_velocities[i])) {
672 }
673 }
674 const double t1 = joints[0];
675 const double t12 = t1 + joints[1];
676 const double w1 = joint_velocities[0];
677 const double w12 = w1 + joint_velocities[1];
678 if(!std::isfinite(t12) || !std::isfinite(w12)) {
680 }
681 const double vx = -link1_ * std::sin(t1) * w1 - link2_ * std::sin(t12) * w12;
682 const double vy = link1_ * std::cos(t1) * w1 + link2_ * std::cos(t12) * w12;
683 const double vz = with_z_ ? joint_velocities[2] : 0.0;
684 if(!std::isfinite(vx) || !std::isfinite(vy) || !std::isfinite(vz)) {
686 }
687 linear_velocity_out = {vx, vy, vz};
688 return rt::ErrorCode::ok;
689 }
690
691private:
692 double link1_ = 1.0;
693 double link2_ = 1.0;
694 bool with_z_ = false;
695 bool valid_ = false;
696};
697
698} // namespace plcopen::core::kin
CartesianGantryDifferential(std::size_t axis_count, const double *scale, const double *offset)
rt::ErrorCode forward_velocity(const double *joints, const double *joint_velocities, std::size_t joint_count, geom::Vec3 &linear_velocity_out) const override
rt::ErrorCode line_derivative_bounds(geom::Vec3 plugin_point, geom::Vec3 unit_direction, double path_length, const double *start_joints, std::size_t joint_count, LineDerivativeBounds &bounds_out) const override
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 override
ScaraDifferential(double link1, double link2, bool with_z)
rt::ErrorCode line_derivative_bounds(geom::Vec3 plugin_point, geom::Vec3 unit_direction, double path_length, const double *start_joints, std::size_t joint_count, LineDerivativeBounds &bounds_out) const override
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 override
rt::ErrorCode forward_velocity(const double *joints, const double *joint_velocities, std::size_t joint_count, geom::Vec3 &linear_velocity_out) const override
static constexpr double FkConsistencyTolerance
std::size_t joint_count() const override
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 line_derivative_bounds(geom::Vec3 plugin_point, geom::Vec3 unit_direction, double path_length, const double *start_joints, std::size_t joint_count, LineDerivativeBounds &bounds_out) const
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)
bool is_finite_vec3(geom::Vec3 v)
bool is_unit_direction(geom::Vec3 d)