plcopen 0.22.0
载入中...
搜索中...
未找到
group_pose_frames_impl.h
浏览该文件的文档.
1#pragma once
2
3// AxisGroup batch 4 frame/pose/kinematics implementation.
4// Included by group.h after AxisGroup is complete.
5
7{
8
9inline rt::ErrorCode AxisGroup::set_workpiece_frame(double x, double y, double z, double rot_z)
10{
11 return set_workpiece_frame_rpy(x, y, z, 0.0, 0.0, rot_z);
12}
13
15 double y,
16 double z,
17 double roll,
18 double pitch,
19 double yaw)
20{
21 if(status_ != GroupStatus::standby || !queue_.empty() || !std::isfinite(x) ||
22 !std::isfinite(y) || !std::isfinite(z) || !std::isfinite(roll) ||
23 !std::isfinite(pitch) || !std::isfinite(yaw)) {
25 }
26 cancel_tracking();
27 pose_frames_.workpiece_frame_ = geom::make_rpy_transform(x, y, z, roll, pitch, yaw);
28 const double echo[6] = {x, y, z, roll, pitch, yaw};
29 for(int i = 0; i < 6; ++i) {
30 pose_frames_.workpiece_frame_rpy_[i] = echo[i];
31 }
32 return rt::ErrorCode::ok;
33}
34
36 double y,
37 double z,
38 double roll,
39 double pitch,
40 double yaw)
41{
42 if(numbered_tool_mode_) return rt::ErrorCode::precondition_failed;
43 if(status_ != GroupStatus::standby || !queue_.empty() || !std::isfinite(x) ||
44 !std::isfinite(y) || !std::isfinite(z) || !std::isfinite(roll) ||
45 !std::isfinite(pitch) || !std::isfinite(yaw)) {
47 }
48 pose_frames_.pose_tool_ = geom::make_rpy_transform(x, y, z, roll, pitch, yaw);
49 pose_frames_.pose_tool_inverse_ = geom::invert(pose_frames_.pose_tool_);
50 const double echo[6] = {x, y, z, roll, pitch, yaw};
51 for(int i = 0; i < 6; ++i) {
52 pose_frames_.tool_transform_rpy_[i] = echo[i];
53 }
54 return rt::ErrorCode::ok;
55}
56
58 double min_singularity_margin,
59 double max_joint_step)
60{
61 if(status_ != GroupStatus::standby || !queue_.empty() ||
62 !std::isfinite(min_singularity_margin) || min_singularity_margin < 0.0 ||
63 !std::isfinite(max_joint_step) || max_joint_step <= 0.0) {
65 }
66 if(plugin != nullptr && (pose_frames_.kinematics_ != nullptr ||
67 axes_.size() < 6 || axes_.size() > MaxAxes ||
68 plugin->joint_count() != axes_.size())) {
70 }
71 pose_frames_.pose_kinematics_ = plugin;
72 pose_frames_.pose_min_margin_ = min_singularity_margin;
73 pose_frames_.pose_max_joint_step_ = max_joint_step;
74 pose_frames_.translation_path_differential_ = nullptr;
75 return rt::ErrorCode::ok;
76}
77
78inline void AxisGroup::workpiece_frame_rpy(double out[6]) const
79{
80 for(int i = 0; i < 6; ++i) {
81 out[i] = pose_frames_.workpiece_frame_rpy_[i];
82 }
83}
84
85inline void AxisGroup::tool_transform_rpy(double out[6]) const
86{
87 for(int i = 0; i < 6; ++i) {
88 out[i] = pose_frames_.tool_transform_rpy_[i];
89 }
90}
91
93{
94 return pose_frames_.tool_offset_;
95}
96
98 PositionSource source,
99 GroupPosition &out,
100 bool *gimbal_lock) const
101{
102 if(gimbal_lock != nullptr) {
103 *gimbal_lock = false;
104 }
105 switch(cs) {
106 case CoordSystem::acs:
107 case CoordSystem::mcs:
108 case CoordSystem::pcs:
109 break;
110 default:
112 }
113 if(axes_.size() == 0 || status_ == GroupStatus::disabled) {
115 }
116 out.size = axes_.size();
117 double joints[MaxAxes] = {};
118 for(std::size_t i = 0; i < axes_.size(); ++i) {
119 const AxisSnapshot &snapshot = axes_[i]->snapshot();
120 joints[i] = source == PositionSource::actual ? snapshot.actual_position
121 : snapshot.command_position;
122 out.value[i] = joints[i];
123 }
124 if(cs == CoordSystem::acs) {
125 return rt::ErrorCode::ok;
126 }
127
128 if(pose_frames_.pose_kinematics_ != nullptr) {
129 kin::Pose6 flange{};
130 pose_frames_.pose_kinematics_->forward(joints, flange);
132 pose.translation = geom::Vec3{flange.position[0], flange.position[1],
133 flange.position[2]};
134 for(int i = 0; i < 3; ++i) {
135 for(int j = 0; j < 3; ++j) {
136 pose.rotation[i][j] = flange.rotation[i][j];
137 }
138 }
139 const geom::RigidTransform &tool = active_tool_transform_applies()
140 ? pose_frames_.active_pose_tool_
141 : pose_frames_.pose_tool_;
142 pose = geom::compose(pose, tool);
143 if(cs == CoordSystem::pcs) {
144 pose = geom::compose(geom::invert(pose_frames_.workpiece_frame_), pose);
145 }
146 out.value[0] = pose.translation.x;
147 out.value[1] = pose.translation.y;
148 out.value[2] = pose.translation.z;
149 double roll = 0.0;
150 double pitch = 0.0;
151 double yaw = 0.0;
152 const bool gimbal = geom::extract_rpy(pose.rotation, roll, pitch, yaw);
153 out.value[3] = roll;
154 out.value[4] = pitch;
155 out.value[5] = yaw;
156 if(gimbal_lock != nullptr) {
157 *gimbal_lock = gimbal;
158 }
159 return rt::ErrorCode::ok;
160 }
161
162 geom::Vec3 point{};
163 if(pose_frames_.kinematics_ != nullptr) {
164 const rt::ErrorCode forwarded =
165 pose_frames_.kinematics_->forward(joints, axes_.size(), point);
166 if(forwarded != rt::ErrorCode::ok) {
167 return forwarded;
168 }
169 } else {
170 point = cartesian_part(out);
171 }
172 point = point + (active_tool_transform_applies() ? pose_frames_.active_tool_offset_
173 : pose_frames_.tool_offset_);
174 if(cs == CoordSystem::pcs) {
175 point = geom::transform_point(geom::invert(pose_frames_.workpiece_frame_), point);
176 }
177 store_cartesian_part(out, point);
178 return rt::ErrorCode::ok;
179}
180
182 CoordSystem cs,
183 const GroupPosition &joints,
184 const GroupPosition &joint_velocities,
185 CartesianTwist &out) const
186{
187 switch(cs) {
188 case CoordSystem::acs:
189 case CoordSystem::mcs:
190 case CoordSystem::pcs:
191 break;
192 default:
194 }
195 if(axes_.empty() || status_ == GroupStatus::disabled ||
196 joints.size != axes_.size() || joint_velocities.size != axes_.size() ||
197 !finite(joints) || !finite(joint_velocities)) {
199 }
200 if(cs == CoordSystem::acs) {
202 acs.linear.x = joint_velocities.value[0];
203 acs.linear.y = axes_.size() > 1 ? joint_velocities.value[1] : 0.0;
204 acs.linear.z = axes_.size() > 2 ? joint_velocities.value[2] : 0.0;
205 acs.angular.x = axes_.size() > 3 ? joint_velocities.value[3] : 0.0;
206 acs.angular.y = axes_.size() > 4 ? joint_velocities.value[4] : 0.0;
207 acs.angular.z = axes_.size() > 5 ? joint_velocities.value[5] : 0.0;
208 out = acs;
209 return rt::ErrorCode::ok;
210 }
211
212 // Scale-stable Euclidean norm of joint velocity vector.
213 double max_dq = 0.0;
214 for(std::size_t i = 0; i < axes_.size(); ++i) {
215 const double a = std::fabs(joint_velocities.value[i]);
216 if(a > max_dq) max_dq = a;
217 }
218 if(max_dq == 0.0) {
219 out = CartesianTwist{};
220 return rt::ErrorCode::ok;
221 }
222
223 geom::Vec3 v_mcs{};
224 geom::Vec3 w_mcs{};
225 bool resolved_analytic = false;
226 if(pose_frames_.pose_kinematics_ == nullptr && pose_frames_.kinematics_ == nullptr) {
227 v_mcs = {joint_velocities.value[0],
228 axes_.size() > 1 ? joint_velocities.value[1] : 0.0,
229 axes_.size() > 2 ? joint_velocities.value[2] : 0.0};
230 resolved_analytic = true;
231 } else if(pose_frames_.pose_kinematics_ == nullptr &&
232 pose_frames_.translation_path_differential_ != nullptr) {
233 const rt::ErrorCode fv_rc =
234 pose_frames_.translation_path_differential_->forward_velocity(
235 joints.value.data(), joint_velocities.value.data(), axes_.size(), v_mcs);
236 if(fv_rc == rt::ErrorCode::ok) {
237 resolved_analytic = true;
238 } else if(fv_rc != rt::ErrorCode::unsupported) {
239 return fv_rc;
240 }
241 }
242 if(!resolved_analytic) {
243 double scaled_sq = 0.0;
244 for(std::size_t i = 0; i < axes_.size(); ++i) {
245 const double r = joint_velocities.value[i] / max_dq;
246 scaled_sq += r * r;
247 }
248 const double root = std::sqrt(scaled_sq);
249 const double dq_norm = max_dq * root;
250 if(!std::isfinite(dq_norm)) {
252 }
253 if(dq_norm <= 1e-15) {
254 out = CartesianTwist{};
255 return rt::ErrorCode::ok;
256 }
257 constexpr double Step = 1e-7;
258 const double scale = dq_norm / (2.0 * Step);
259 if(!std::isfinite(scale)) {
261 }
262 double q_plus[MaxAxes] = {};
263 double q_minus[MaxAxes] = {};
264 for(std::size_t i = 0; i < axes_.size(); ++i) {
265 const double dir = (joint_velocities.value[i] / max_dq) / root;
266 q_plus[i] = joints.value[i] + Step * dir;
267 q_minus[i] = joints.value[i] - Step * dir;
268 }
269 if(pose_frames_.pose_kinematics_ != nullptr) {
270 const geom::RigidTransform &tool = active_tool_transform_applies()
271 ? pose_frames_.active_pose_tool_
272 : pose_frames_.pose_tool_;
273 auto eval_tcp = [&](const double *q, geom::RigidTransform &tcp) {
274 kin::Pose6 flange{};
275 pose_frames_.pose_kinematics_->forward(q, flange);
277 fl.translation = {flange.position[0], flange.position[1], flange.position[2]};
278 for(int r = 0; r < 3; ++r) {
279 for(int c = 0; c < 3; ++c) {
280 fl.rotation[r][c] = flange.rotation[r][c];
281 }
282 }
283 tcp = geom::compose(fl, tool);
284 };
285 // Exactly 2 forward() calls (spec §2 #11): spatial relative rotation
286 // R_plus * R_minus^T = exp(2*h*[w_mcs]_x) + O(h^3).
287 geom::RigidTransform t_plus{};
288 geom::RigidTransform t_minus{};
289 eval_tcp(q_plus, t_plus);
290 eval_tcp(q_minus, t_minus);
291
292 v_mcs = (t_plus.translation - t_minus.translation) * scale;
293 double r_sp[3][3] = {};
294 for(int r = 0; r < 3; ++r) {
295 for(int c = 0; c < 3; ++c) {
296 r_sp[r][c] = t_plus.rotation[r][0] * t_minus.rotation[c][0] +
297 t_plus.rotation[r][1] * t_minus.rotation[c][1] +
298 t_plus.rotation[r][2] * t_minus.rotation[c][2];
299 }
300 }
301 w_mcs = {
302 0.5 * (r_sp[2][1] - r_sp[1][2]) * scale,
303 0.5 * (r_sp[0][2] - r_sp[2][0]) * scale,
304 0.5 * (r_sp[1][0] - r_sp[0][1]) * scale,
305 };
306 } else {
307 geom::Vec3 p_plus{};
308 geom::Vec3 p_minus{};
309 const rt::ErrorCode r1 =
310 pose_frames_.kinematics_->forward(q_plus, axes_.size(), p_plus);
311 if(r1 != rt::ErrorCode::ok) {
312 return r1;
313 }
314 const rt::ErrorCode r2 =
315 pose_frames_.kinematics_->forward(q_minus, axes_.size(), p_minus);
316 if(r2 != rt::ErrorCode::ok) {
317 return r2;
318 }
319 v_mcs = (p_plus - p_minus) * scale;
320 }
321 }
322
323 CartesianTwist result{v_mcs, w_mcs};
324 if(cs == CoordSystem::pcs) {
325 const geom::RigidTransform inv_wp = geom::invert(pose_frames_.workpiece_frame_);
326 result.linear = geom::transform_rotate(inv_wp, v_mcs);
327 result.angular = geom::transform_rotate(inv_wp, w_mcs);
328 }
329 if(!std::isfinite(result.linear.x) || !std::isfinite(result.linear.y) ||
330 !std::isfinite(result.linear.z) || !std::isfinite(result.angular.x) ||
331 !std::isfinite(result.angular.y) || !std::isfinite(result.angular.z)) {
333 }
334 out = result;
335 return rt::ErrorCode::ok;
336}
337
338inline rt::ErrorCode AxisGroup::command_member_velocities(GroupPosition &velocities) const
339{
340 velocities = GroupPosition{};
341 velocities.size = axes_.size();
342 if(status_ == GroupStatus::disabled || status_ == GroupStatus::errorstop ||
343 status_ == GroupStatus::standby || status_ == GroupStatus::interrupted) {
344 return rt::ErrorCode::ok;
345 }
346 if(direct_path_.direct_active_ || jog_active_ || group_sync_active_) {
347 for(std::size_t i = 0; i < axes_.size(); ++i) {
348 velocities.value[i] = axes_[i]->snapshot().command_velocity;
349 }
350 return rt::ErrorCode::ok;
351 }
352 if(joint_window_.active_ && !joint_window_.segments_.empty()) {
353 // Analytical chain-rule derivative dq/ds = B'(u) * du/ds matching the
354 // executor's exact piecewise-linear cumulative-table inversion u(s) in
355 // sample_window_node (u = (low + (s - S_low)/(S_{low+1} - S_low)) / M,
356 // hence du/ds = 1 / (M * (S_{low+1} - S_low))).
357 auto eval_blend_ds_derivative = [&](const WindowNode &node, double s_val,
358 std::array<double, MaxAxes> &dq_ds) {
359 dq_ds.fill(0.0);
360 const double clamped =
361 s_val < 0.0 ? 0.0
362 : (s_val > node.curve_length ? node.curve_length : s_val);
363 std::size_t low = 0;
364 for(std::size_t k = 1; k < BlendTableSize; ++k) {
365 if(node.cumulative[k] >= clamped) {
366 low = k - 1;
367 break;
368 }
369 low = k - 1;
370 }
371 const double seg_l = node.cumulative[low + 1] - node.cumulative[low];
372 if(!(seg_l > 0.0)) {
373 return;
374 }
375 const double frac = (clamped - node.cumulative[low]) / seg_l;
376 const double inv_m = 1.0 / static_cast<double>(BlendTableSize - 1);
377 const double u = (static_cast<double>(low) + frac) * inv_m;
378 const double du_ds = inv_m / seg_l;
379 const double v = 1.0 - u;
380 const double v2 = v * v;
381 const double u2 = u * u;
382 const double d1w0 = 5.0 * v2 * v2;
383 const double d1w1 = 20.0 * v2 * v * u;
384 const double d1w2 = 30.0 * v2 * u2;
385 const double d1w3 = 20.0 * v * u2 * u;
386 const double d1w4 = 5.0 * u2 * u2;
387 for(std::size_t i = 0; i < axes_.size(); ++i) {
388 const double e0 = node.ctrl[1][i] - node.ctrl[0][i];
389 const double e1 = node.ctrl[2][i] - node.ctrl[1][i];
390 const double e2 = node.ctrl[3][i] - node.ctrl[2][i];
391 const double e3 = node.ctrl[4][i] - node.ctrl[3][i];
392 const double e4 = node.ctrl[5][i] - node.ctrl[4][i];
393 const double bp =
394 e0 * d1w0 + e1 * d1w1 + e2 * d1w2 + e3 * d1w3 + e4 * d1w4;
395 dq_ds[i] = bp * du_ds;
396 }
397 };
398
399 if(joint_window_.stopping_) {
400 const otg::State1D halt = otg::sample(
401 joint_window_.stop_profile_,
402 rt::CycleTick::from_cycles(joint_window_.tick_));
403 double remaining = joint_window_.stop_origin_ + halt.position;
404 bool in_curve = joint_window_.in_curve_;
405 std::size_t idx = joint_window_.index_;
406 while(idx < joint_window_.segments_.size()) {
407 const WindowSegment &seg = joint_window_.segments_[idx];
408 if(!in_curve) {
409 const double len = seg.line_length();
410 if(remaining <= len || idx + 1 >= joint_window_.segments_.size()) {
411 if(seg.kind == WindowKind::arc && seg.full_length > 0.0) {
412 const double s = remaining < 0.0 ? 0.0
413 : (remaining > seg.full_length
414 ? seg.full_length
415 : remaining);
416 const geom::Vec3 d = geom::path_derivative(seg.arc_geom, s);
417 velocities.value[0] = d.x * halt.velocity;
418 velocities.value[1] = d.y * halt.velocity;
419 for(std::size_t i = 2; i < axes_.size(); ++i) {
420 velocities.value[i] =
421 ((seg.target[i] - seg.entry[i]) / seg.full_length) *
422 halt.velocity;
423 }
424 } else {
425 for(std::size_t i = 0; i < axes_.size(); ++i) {
426 velocities.value[i] = seg.dir[i] * halt.velocity;
427 }
428 }
429 return rt::ErrorCode::ok;
430 }
431 remaining -= len;
432 if(seg.node.has_curve) {
433 in_curve = true;
434 } else {
435 ++idx;
436 }
437 } else {
438 if(remaining <= seg.node.curve_length && seg.node.curve_length > 0.0) {
439 std::array<double, MaxAxes> dq_ds{};
440 eval_blend_ds_derivative(seg.node, remaining, dq_ds);
441 for(std::size_t i = 0; i < axes_.size(); ++i) {
442 velocities.value[i] = dq_ds[i] * halt.velocity;
443 }
444 return rt::ErrorCode::ok;
445 }
446 remaining -= seg.node.curve_length;
447 in_curve = false;
448 ++idx;
449 }
450 }
451 return rt::ErrorCode::ok;
452 }
453 const WindowSegment &seg = joint_window_.segments_[joint_window_.index_];
454 double s_live = 0.0;
455 double v_live = 0.0;
456 double a_live = 0.0;
457 window_live_state(s_live, v_live, a_live);
458 if(joint_window_.in_curve_ && seg.node.curve_length > 0.0) {
459 std::array<double, MaxAxes> dq_ds{};
460 eval_blend_ds_derivative(seg.node, s_live, dq_ds);
461 for(std::size_t i = 0; i < axes_.size(); ++i) {
462 velocities.value[i] = dq_ds[i] * v_live;
463 }
464 return rt::ErrorCode::ok;
465 }
466 if(seg.kind == WindowKind::arc && seg.full_length > 0.0) {
467 const double s =
468 s_live < 0.0 ? 0.0 : (s_live > seg.full_length ? seg.full_length : s_live);
469 const geom::Vec3 d = geom::path_derivative(seg.arc_geom, s);
470 velocities.value[0] = d.x * v_live;
471 velocities.value[1] = d.y * v_live;
472 for(std::size_t i = 2; i < axes_.size(); ++i) {
473 velocities.value[i] =
474 ((seg.target[i] - seg.entry[i]) / seg.full_length) * v_live;
475 }
476 return rt::ErrorCode::ok;
477 }
478 for(std::size_t i = 0; i < axes_.size(); ++i) {
479 velocities.value[i] = seg.dir[i] * v_live;
480 }
481 return rt::ErrorCode::ok;
482 }
483 // Solve instantaneous joint velocity dq from analytical Cartesian twist
484 // xi = (v_tcp, w_tcp) via the scale-adaptive geometric Jacobian J(q)*dq = xi
485 // (minimum-norm J^T (J J^T)^{-1} xi for redundant N > 6). Avoids fixed-ds
486 // 2-IK differencing cancellation at large offsets and numerical-IK jitter.
487 auto solve_cartesian_joint_velocities =
488 [&](bool is_pose, geom::Vec3 v_tcp, geom::Vec3 w_tcp,
489 GroupPosition &dq_out) -> rt::ErrorCode {
490 if(v_tcp.x == 0.0 && v_tcp.y == 0.0 && v_tcp.z == 0.0 &&
491 w_tcp.x == 0.0 && w_tcp.y == 0.0 && w_tcp.z == 0.0) {
492 return rt::ErrorCode::ok;
493 }
494 if(!std::isfinite(v_tcp.x) || !std::isfinite(v_tcp.y) || !std::isfinite(v_tcp.z) ||
495 !std::isfinite(w_tcp.x) || !std::isfinite(w_tcp.y) || !std::isfinite(w_tcp.z)) {
497 }
498 const std::size_t n = axes_.size();
499 if(!is_pose && pose_frames_.kinematics_ == nullptr) {
500 dq_out.value[0] = v_tcp.x;
501 if(n > 1) dq_out.value[1] = v_tcp.y;
502 if(n > 2) dq_out.value[2] = v_tcp.z;
503 return rt::ErrorCode::ok;
504 }
505 auto solve_linear_system = [](int dim, double a[6][6], double b[6],
506 double x[6]) -> bool {
507 double max_diag = 0.0;
508 for(int r = 0; r < dim; ++r) {
509 if(!std::isfinite(a[r][r]) || !std::isfinite(b[r])) {
510 return false;
511 }
512 const double d = std::fabs(a[r][r]);
513 if(d > max_diag) max_diag = d;
514 }
515 if(!(max_diag > 1e-24)) {
516 return false;
517 }
518 const double piv_tol = 1e-12 * max_diag;
519 for(int col = 0; col < dim; ++col) {
520 int piv_row = col;
521 double best = std::fabs(a[col][col]);
522 for(int r = col + 1; r < dim; ++r) {
523 const double v = std::fabs(a[r][col]);
524 if(v > best) {
525 best = v;
526 piv_row = r;
527 }
528 }
529 if(!std::isfinite(best) || best <= piv_tol) {
530 return false;
531 }
532 if(piv_row != col) {
533 for(int c = col; c < dim; ++c) {
534 const double tmp = a[col][c];
535 a[col][c] = a[piv_row][c];
536 a[piv_row][c] = tmp;
537 }
538 const double tb = b[col];
539 b[col] = b[piv_row];
540 b[piv_row] = tb;
541 }
542 const double diag = a[col][col];
543 for(int r = col + 1; r < dim; ++r) {
544 const double factor = a[r][col] / diag;
545 a[r][col] = 0.0;
546 for(int c = col + 1; c < dim; ++c) {
547 a[r][c] -= factor * a[col][c];
548 }
549 b[r] -= factor * b[col];
550 }
551 }
552 for(int r = dim - 1; r >= 0; --r) {
553 double sum = b[r];
554 for(int c = r + 1; c < dim; ++c) {
555 sum -= a[r][c] * x[c];
556 }
557 x[r] = sum / a[r][r];
558 if(!std::isfinite(x[r])) {
559 return false;
560 }
561 }
562 return true;
563 };
564
565 double q_cur[MaxAxes] = {};
566 double q_plus[MaxAxes] = {};
567 double q_minus[MaxAxes] = {};
568 for(std::size_t i = 0; i < n; ++i) {
569 q_cur[i] = axes_[i]->snapshot().command_position;
570 if(!std::isfinite(q_cur[i])) {
572 }
573 q_plus[i] = q_cur[i];
574 q_minus[i] = q_cur[i];
575 }
576
577 constexpr double kMachineEps = 2.220446049250313e-16;
578 constexpr double kMaxStep = 1.0e-2;
579 if(is_pose && pose_frames_.pose_kinematics_ != nullptr) {
580 double jac[6][MaxAxes] = {};
581 const geom::RigidTransform &tool = pose_frames_.active_pose_tool_;
582 auto eval_pose_col = [&](std::size_t col_j, double h_step,
583 double col_out[6],
584 double &noise_out) -> rt::ErrorCode {
585 q_plus[col_j] = q_cur[col_j] + h_step;
586 q_minus[col_j] = q_cur[col_j] - h_step;
587 const double span = q_plus[col_j] - q_minus[col_j];
588 if(!std::isfinite(span) || !(span > 0.0)) {
589 q_plus[col_j] = q_cur[col_j];
590 q_minus[col_j] = q_cur[col_j];
592 }
593 kin::Pose6 fp{}, fm{};
594 pose_frames_.pose_kinematics_->forward(q_plus, fp);
595 pose_frames_.pose_kinematics_->forward(q_minus, fm);
596 q_plus[col_j] = q_cur[col_j];
597 q_minus[col_j] = q_cur[col_j];
598
599 geom::RigidTransform tp{}, tm{};
600 tp.translation = {fp.position[0], fp.position[1], fp.position[2]};
601 tm.translation = {fm.position[0], fm.position[1], fm.position[2]};
602 for(int r = 0; r < 3; ++r) {
603 for(int c = 0; c < 3; ++c) {
604 tp.rotation[r][c] = fp.rotation[r][c];
605 tm.rotation[r][c] = fm.rotation[r][c];
606 }
607 }
608 tp = geom::compose(tp, tool);
609 tm = geom::compose(tm, tool);
610 const double inv_span = 1.0 / span;
611 col_out[0] = (tp.translation.x - tm.translation.x) * inv_span;
612 col_out[1] = (tp.translation.y - tm.translation.y) * inv_span;
613 col_out[2] = (tp.translation.z - tm.translation.z) * inv_span;
614 const double mx = std::fabs(tp.translation.x) > std::fabs(tm.translation.x)
615 ? std::fabs(tp.translation.x)
616 : std::fabs(tm.translation.x);
617 const double my = std::fabs(tp.translation.y) > std::fabs(tm.translation.y)
618 ? std::fabs(tp.translation.y)
619 : std::fabs(tm.translation.y);
620 const double mz = std::fabs(tp.translation.z) > std::fabs(tm.translation.z)
621 ? std::fabs(tp.translation.z)
622 : std::fabs(tm.translation.z);
623 double m_max = mx > my ? mx : my;
624 if(mz > m_max) m_max = mz;
625 if(1.0 > m_max) m_max = 1.0;
626 const double rx = mx / m_max;
627 const double ry = my / m_max;
628 const double rz = mz / m_max;
629 const double rr = 1.0 / m_max;
630 noise_out = kMachineEps * m_max *
631 std::sqrt(rx * rx + ry * ry + rz * rz + 3.0 * rr * rr) * inv_span;
632 double r_rel[3][3] = {};
633 for(int r = 0; r < 3; ++r) {
634 for(int c = 0; c < 3; ++c) {
635 r_rel[r][c] = tp.rotation[r][0] * tm.rotation[c][0] +
636 tp.rotation[r][1] * tm.rotation[c][1] +
637 tp.rotation[r][2] * tm.rotation[c][2];
638 }
639 }
640 col_out[3] = 0.5 * (r_rel[2][1] - r_rel[1][2]) * inv_span;
641 col_out[4] = 0.5 * (r_rel[0][2] - r_rel[2][0]) * inv_span;
642 col_out[5] = 0.5 * (r_rel[1][0] - r_rel[0][1]) * inv_span;
643 for(int r = 0; r < 6; ++r) {
644 if(!std::isfinite(col_out[r])) {
646 }
647 }
648 return rt::ErrorCode::ok;
649 };
650 for(std::size_t j = 0; j < n; ++j) {
651 const double q_scale = std::fabs(q_cur[j]) > 1.0 ? std::fabs(q_cur[j]) : 1.0;
652 double h = 512.0 * kMachineEps * q_scale;
653 if(h < 1.0e-7) h = 1.0e-7;
654 if(!std::isfinite(h) || h > kMaxStep) {
656 }
657 bool converged = false;
658 for(int attempt = 0; attempt < 6; ++attempt) {
659 if(attempt > 0) {
660 if(h >= kMaxStep) {
661 break;
662 }
663 h *= 16.0;
664 if(h > kMaxStep) {
665 h = kMaxStep;
666 }
667 }
668 // Keep both steps strictly <= kMaxStep with non-power-of-2 ratio (1.5x).
669 const double h2 = (1.5 * h <= kMaxStep) ? (1.5 * h) : (h / 1.5);
670 double c1[6] = {};
671 double c2[6] = {};
672 double noise1 = 0.0;
673 double noise2 = 0.0;
674 const rt::ErrorCode e1 = eval_pose_col(j, h, c1, noise1);
675 if(e1 != rt::ErrorCode::ok) return e1;
676 const rt::ErrorCode e2 = eval_pose_col(j, h2, c2, noise2);
677 if(e2 != rt::ErrorCode::ok) return e2;
678 double max_c = 0.0;
679 for(int r = 0; r < 6; ++r) {
680 const double a1 = std::fabs(c1[r]);
681 const double a2 = std::fabs(c2[r]);
682 if(a1 > max_c) max_c = a1;
683 if(a2 > max_c) max_c = a2;
684 }
685 const bool at_cap = (attempt == 5 || h >= kMaxStep);
686 if(max_c > 0.0) {
687 double diff_sq = 0.0, n1_sq = 0.0, n2_sq = 0.0;
688 for(int r = 0; r < 6; ++r) {
689 const double s1 = c1[r] / max_c;
690 const double s2 = c2[r] / max_c;
691 const double d = s1 - s2;
692 diff_sq += d * d;
693 n1_sq += s1 * s1;
694 n2_sq += s2 * s2;
695 }
696 const double scale_n = max_c * std::sqrt(n1_sq > n2_sq ? n1_sq : n2_sq);
697 const double diff_n = max_c * std::sqrt(diff_sq);
698 if(std::isfinite(noise1) && std::isfinite(noise2) &&
699 noise1 <= 2.5e-4 * scale_n &&
700 noise2 <= 2.5e-4 * scale_n &&
701 diff_n <= 1.0e-3 * scale_n) {
702 for(int r = 0; r < 6; ++r) jac[r][j] = c1[r];
703 converged = true;
704 break;
705 }
706 } else if(at_cap) {
707 for(int r = 0; r < 6; ++r) jac[r][j] = 0.0;
708 converged = true;
709 break;
710 }
711 }
712 if(!converged) {
714 }
715 }
716 double jjt[6][6] = {};
717 for(int r = 0; r < 6; ++r) {
718 for(int c = 0; c < 6; ++c) {
719 double s = 0.0;
720 for(std::size_t j = 0; j < n; ++j) {
721 s += jac[r][j] * jac[c][j];
722 }
723 jjt[r][c] = s;
724 }
725 }
726 double rhs[6] = {v_tcp.x, v_tcp.y, v_tcp.z, w_tcp.x, w_tcp.y, w_tcp.z};
727 double y[6] = {};
728 if(!solve_linear_system(6, jjt, rhs, y)) {
730 }
731 for(std::size_t j = 0; j < n; ++j) {
732 double s = 0.0;
733 for(int r = 0; r < 6; ++r) {
734 s += jac[r][j] * y[r];
735 }
736 if(!std::isfinite(s)) {
738 }
739 dq_out.value[j] = s;
740 }
741 return rt::ErrorCode::ok;
742 }
743
744 std::size_t m = pose_frames_.kinematics_->cartesian_count();
745 if(m == 0) {
747 }
748 if(m > 3) m = 3;
749 if(m > n) m = n;
750 double jac[3][MaxAxes] = {};
751 auto eval_trans_col = [&](std::size_t col_j, double h_step,
752 double col_out[3],
753 double &noise_out) -> rt::ErrorCode {
754 q_plus[col_j] = q_cur[col_j] + h_step;
755 q_minus[col_j] = q_cur[col_j] - h_step;
756 const double span = q_plus[col_j] - q_minus[col_j];
757 if(!std::isfinite(span) || !(span > 0.0)) {
758 q_plus[col_j] = q_cur[col_j];
759 q_minus[col_j] = q_cur[col_j];
761 }
762 geom::Vec3 pp{}, pm{};
763 const rt::ErrorCode r1 = pose_frames_.kinematics_->forward(q_plus, n, pp);
764 if(r1 != rt::ErrorCode::ok) {
765 q_plus[col_j] = q_cur[col_j];
766 q_minus[col_j] = q_cur[col_j];
767 return r1;
768 }
769 const rt::ErrorCode r2 = pose_frames_.kinematics_->forward(q_minus, n, pm);
770 q_plus[col_j] = q_cur[col_j];
771 q_minus[col_j] = q_cur[col_j];
772 if(r2 != rt::ErrorCode::ok) return r2;
773 const double inv_span = 1.0 / span;
774 col_out[0] = (pp.x - pm.x) * inv_span;
775 col_out[1] = m > 1 ? (pp.y - pm.y) * inv_span : 0.0;
776 col_out[2] = m > 2 ? (pp.z - pm.z) * inv_span : 0.0;
777 const double mx = std::fabs(pp.x) > std::fabs(pm.x) ? std::fabs(pp.x) : std::fabs(pm.x);
778 const double my = m > 1 ? (std::fabs(pp.y) > std::fabs(pm.y) ? std::fabs(pp.y) : std::fabs(pm.y)) : 0.0;
779 const double mz = m > 2 ? (std::fabs(pp.z) > std::fabs(pm.z) ? std::fabs(pp.z) : std::fabs(pm.z)) : 0.0;
780 double m_max = mx > my ? mx : my;
781 if(mz > m_max) m_max = mz;
782 if(m_max > 0.0) {
783 const double rx = mx / m_max;
784 const double ry = my / m_max;
785 const double rz = mz / m_max;
786 noise_out = kMachineEps * m_max * std::sqrt(rx * rx + ry * ry + rz * rz) * inv_span;
787 } else {
788 noise_out = 0.0;
789 }
790 for(std::size_t r = 0; r < m; ++r) {
791 if(!std::isfinite(col_out[r])) {
793 }
794 }
795 return rt::ErrorCode::ok;
796 };
797 for(std::size_t j = 0; j < n; ++j) {
798 const double q_scale = std::fabs(q_cur[j]) > 1.0 ? std::fabs(q_cur[j]) : 1.0;
799 double h = 512.0 * kMachineEps * q_scale;
800 if(h < 1.0e-7) h = 1.0e-7;
801 if(!std::isfinite(h) || h > kMaxStep) {
803 }
804 bool converged = false;
805 for(int attempt = 0; attempt < 6; ++attempt) {
806 if(attempt > 0) {
807 if(h >= kMaxStep) {
808 break;
809 }
810 h *= 16.0;
811 if(h > kMaxStep) {
812 h = kMaxStep;
813 }
814 }
815 // Keep both steps strictly <= kMaxStep with non-power-of-2 ratio (1.5x).
816 const double h2 = (1.5 * h <= kMaxStep) ? (1.5 * h) : (h / 1.5);
817 double c1[3] = {};
818 double c2[3] = {};
819 double noise1 = 0.0;
820 double noise2 = 0.0;
821 const rt::ErrorCode e1 = eval_trans_col(j, h, c1, noise1);
822 if(e1 != rt::ErrorCode::ok) return e1;
823 const rt::ErrorCode e2 = eval_trans_col(j, h2, c2, noise2);
824 if(e2 != rt::ErrorCode::ok) return e2;
825 double max_c = 0.0;
826 for(std::size_t r = 0; r < m; ++r) {
827 const double a1 = std::fabs(c1[r]);
828 const double a2 = std::fabs(c2[r]);
829 if(a1 > max_c) max_c = a1;
830 if(a2 > max_c) max_c = a2;
831 }
832 const bool at_cap = (attempt == 5 || h >= kMaxStep);
833 if(max_c > 0.0) {
834 double diff_sq = 0.0, n1_sq = 0.0, n2_sq = 0.0;
835 for(std::size_t r = 0; r < m; ++r) {
836 const double s1 = c1[r] / max_c;
837 const double s2 = c2[r] / max_c;
838 const double d = s1 - s2;
839 diff_sq += d * d;
840 n1_sq += s1 * s1;
841 n2_sq += s2 * s2;
842 }
843 const double scale_n = max_c * std::sqrt(n1_sq > n2_sq ? n1_sq : n2_sq);
844 const double diff_n = max_c * std::sqrt(diff_sq);
845 if(std::isfinite(noise1) && std::isfinite(noise2) &&
846 noise1 <= 2.5e-4 * scale_n &&
847 noise2 <= 2.5e-4 * scale_n &&
848 diff_n <= 1.0e-3 * scale_n) {
849 for(std::size_t r = 0; r < m; ++r) jac[r][j] = c1[r];
850 converged = true;
851 break;
852 }
853 } else if(at_cap) {
854 for(std::size_t r = 0; r < m; ++r) jac[r][j] = 0.0;
855 converged = true;
856 break;
857 }
858 }
859 if(!converged) {
861 }
862 }
863 double jjt[6][6] = {};
864 for(std::size_t r = 0; r < m; ++r) {
865 for(std::size_t c = 0; c < m; ++c) {
866 double s = 0.0;
867 for(std::size_t j = 0; j < n; ++j) {
868 s += jac[r][j] * jac[c][j];
869 }
870 jjt[r][c] = s;
871 }
872 }
873 double rhs[6] = {v_tcp.x, v_tcp.y, v_tcp.z, 0.0, 0.0, 0.0};
874 double y[6] = {};
875 if(!solve_linear_system(static_cast<int>(m), jjt, rhs, y)) {
877 }
878 for(std::size_t j = 0; j < n; ++j) {
879 double s = 0.0;
880 for(std::size_t r = 0; r < m; ++r) {
881 s += jac[r][j] * y[r];
882 }
883 if(!std::isfinite(s)) {
885 }
886 dq_out.value[j] = s;
887 }
888 return rt::ErrorCode::ok;
889 };
890
891 if(cartesian_.window_active_ && !cartesian_.window_.empty()) {
892 std::size_t active_idx = cartesian_.piece_index_;
893 double s_local = 0.0;
894 double sdot = 0.0;
895 if(cartesian_.window_stopping_) {
896 const otg::State1D halt = otg::sample(
897 cartesian_.halt_profile_,
898 rt::CycleTick::from_cycles(cartesian_.halt_tick_));
899 sdot = halt.velocity;
900 double remaining = cartesian_.halt_origin_ + halt.position;
901 for(std::size_t i = cartesian_.piece_index_; i < cartesian_.window_.size(); ++i) {
902 const CartPiece &p = cartesian_.window_[i];
903 const double offset =
904 i == cartesian_.piece_index_ ? cartesian_.halt_piece_offset_ : 0.0;
905 const double avail = p.length - offset;
906 if(remaining <= avail || i + 1 == cartesian_.window_.size()) {
907 active_idx = i;
908 s_local = offset + remaining;
909 if(s_local > p.length) s_local = p.length;
910 break;
911 }
912 remaining -= avail;
913 }
914 } else {
915 const CartPiece &piece = cartesian_.window_[active_idx];
916 if(piece.corner) {
917 s_local = piece.v_in * static_cast<double>(cartesian_.piece_tick_);
918 if(s_local > piece.length) s_local = piece.length;
919 sdot = cartesian_.piece_tick_ < piece.duration ? piece.v_in : 0.0;
920 } else {
921 const otg::State1D st = otg::sample(
922 piece.profile, rt::CycleTick::from_cycles(cartesian_.piece_tick_));
923 s_local = st.position;
924 sdot = st.velocity;
925 }
926 }
927 const CartPiece &piece = cartesian_.window_[active_idx];
928 const geom::Vec3 tangent_v =
929 piece.corner ? geom::path_derivative(piece.blend, s_local) : piece.dir;
930 const geom::Vec3 v_tcp = tangent_v * sdot;
931 geom::Vec3 w_tcp{};
932 if(piece.pose && piece.length > 1e-15 && piece.angle > 0.0) {
933 const double rate = (piece.angle / piece.length) * sdot;
934 const geom::Vec3 w_body{piece.axis[0] * rate, piece.axis[1] * rate,
935 piece.axis[2] * rate};
936 geom::RigidTransform r_start{};
937 for(int r = 0; r < 3; ++r) {
938 for(int c = 0; c < 3; ++c) {
939 r_start.rotation[r][c] = piece.rotation_start[r][c];
940 }
941 }
942 w_tcp = geom::transform_rotate(r_start, w_body);
943 }
944 return solve_cartesian_joint_velocities(piece.pose, v_tcp, w_tcp, velocities);
945 }
946 if(active_ && active_kind_ == GroupPathKind::cartesian_linear &&
947 active_path_length_ > 0.0) {
948 const otg::State1D st =
949 otg::sample(active_profile_, rt::CycleTick::from_cycles(active_tick_));
950 const double s =
951 st.position < 0.0 ? 0.0
952 : (st.position > active_path_length_ ? active_path_length_
953 : st.position);
954 const CartesianSegment &seg = cartesian_.active_segment_;
955 if(cartesian_.takeover_active_) {
956 // Math-Max Step 5 (KB-109): active Cartesian takeover outputs
957 // q_out = q_base(s) + r(t), so composite commanded joint velocity
958 // is q_s(q_base) * s_dot + r_dot.
959 if(pose_frames_.translation_path_differential_ == nullptr ||
960 axes_.size() < 2 || axes_.size() > 3) {
962 }
963 double q_s[3] = {};
964 if(st.velocity != 0.0) {
965 const double ratio = s / active_path_length_;
966 const geom::Vec3 x_base = cartesian_point_at(seg, ratio);
967 const geom::Vec3 unit_dir = seg.delta * (1.0 / active_path_length_);
968 double q_ss[3] = {};
969 const rt::ErrorCode drc =
970 pose_frames_.translation_path_differential_->line_derivatives(
971 x_base, unit_dir, cartesian_.joints_, axes_.size(), q_s, q_ss);
972 if(drc != rt::ErrorCode::ok) {
973 return drc;
974 }
975 }
976 for(std::size_t i = 0; i < axes_.size(); ++i) {
977 const double r_dot =
978 (i < 3 && cartesian_.takeover_residuals_[i].segment_count() > 0)
979 ? otg::sample(cartesian_.takeover_residuals_[i],
980 rt::CycleTick::from_cycles(active_tick_))
981 .velocity
982 : 0.0;
983 const double dq_i = q_s[i] * st.velocity + r_dot;
984 if(!std::isfinite(dq_i)) {
986 }
987 velocities.value[i] = dq_i;
988 }
989 return rt::ErrorCode::ok;
990 }
991 geom::Vec3 v_tcp{};
992 geom::Vec3 w_tcp{};
993 if(seg.angle_driven) {
994 const geom::Vec3 w_body{seg.axis[0] * st.velocity, seg.axis[1] * st.velocity,
995 seg.axis[2] * st.velocity};
996 geom::RigidTransform r_start{};
997 for(int r = 0; r < 3; ++r) {
998 for(int c = 0; c < 3; ++c) {
999 r_start.rotation[r][c] = seg.rotation_start[r][c];
1000 }
1001 }
1002 w_tcp = geom::transform_rotate(r_start, w_body);
1003 } else {
1004 geom::Vec3 tangent_v{};
1005 if(seg.chain) {
1006 if(s <= seg.line1) {
1007 tangent_v = seg.dir1;
1008 } else if(s <= seg.line1 + seg.corner.length) {
1009 tangent_v = geom::path_derivative(seg.corner, s - seg.line1);
1010 } else {
1011 tangent_v = seg.dir2;
1012 }
1013 } else if(seg.arc_path) {
1014 tangent_v = geom::path_derivative(seg.arc, s);
1015 if(!seg.arc.spatial) {
1016 tangent_v.z = seg.delta.z / seg.length;
1017 }
1018 } else {
1019 tangent_v = seg.delta * (1.0 / seg.length);
1020 }
1021 v_tcp = tangent_v * st.velocity;
1022 if(seg.pose && seg.angle > 0.0) {
1023 const double rate = (seg.angle / seg.length) * st.velocity;
1024 const geom::Vec3 w_body{seg.axis[0] * rate, seg.axis[1] * rate,
1025 seg.axis[2] * rate};
1026 geom::RigidTransform r_start{};
1027 for(int r = 0; r < 3; ++r) {
1028 for(int c = 0; c < 3; ++c) {
1029 r_start.rotation[r][c] = seg.rotation_start[r][c];
1030 }
1031 }
1032 w_tcp = geom::transform_rotate(r_start, w_body);
1033 }
1034 }
1035 if(active_command_.dynamic_pcs) {
1036 const geom::RigidTransform delta = geom::compose(
1037 pose_frames_.workpiece_frame_,
1038 geom::invert(pose_frames_.active_dynamic_reference_frame_));
1039 v_tcp = geom::transform_rotate(delta, v_tcp);
1040 w_tcp = geom::transform_rotate(delta, w_tcp);
1041 }
1042 return solve_cartesian_joint_velocities(seg.pose, v_tcp, w_tcp, velocities);
1043 }
1044 if(active_ && active_path_length_ > 0.0) {
1045 const otg::State1D state =
1046 otg::sample(active_profile_, rt::CycleTick::from_cycles(active_tick_));
1047 std::array<double, MaxAxes> lat_v{};
1048 connector_.sample_lateral_velocities(active_tick_, lat_v, axes_.size());
1049 if(active_kind_ == GroupPathKind::circular) {
1050 const double s = state.position < 0.0
1051 ? 0.0
1052 : (state.position > active_path_length_
1053 ? active_path_length_
1054 : state.position);
1055 const geom::Vec3 d = geom::path_derivative(active_arc_, s);
1056 velocities.value[0] = d.x * state.velocity + lat_v[0];
1057 velocities.value[1] = d.y * state.velocity + lat_v[1];
1058 for(std::size_t i = 2; i < axes_.size(); ++i) {
1059 velocities.value[i] =
1060 ((active_finish_[i] - active_start_[i]) / active_path_length_) *
1061 state.velocity +
1062 lat_v[i];
1063 }
1064 } else {
1065 for(std::size_t i = 0; i < axes_.size(); ++i) {
1066 velocities.value[i] =
1067 ((active_finish_[i] - active_start_[i]) / active_path_length_) *
1068 state.velocity +
1069 lat_v[i];
1070 }
1071 }
1072 }
1073 return rt::ErrorCode::ok;
1074}
1075
1077 PositionSource source,
1078 CartesianTwist &out) const
1079{
1080 switch(cs) {
1081 case CoordSystem::acs:
1082 case CoordSystem::mcs:
1083 case CoordSystem::pcs:
1084 break;
1085 default:
1087 }
1088 if(axes_.empty() || status_ == GroupStatus::disabled) {
1090 }
1091
1092 if(source == PositionSource::actual) {
1093 GroupPosition q{};
1094 GroupPosition dq{};
1095 q.size = axes_.size();
1096 dq.size = axes_.size();
1097 for(std::size_t i = 0; i < axes_.size(); ++i) {
1098 const AxisSnapshot &snap = axes_[i]->snapshot();
1099 q.value[i] = snap.actual_position;
1100 dq.value[i] = snap.actual_velocity;
1101 }
1102 return read_cartesian_twist_from_joints(cs, q, dq, out);
1103 }
1104
1105 if(status_ == GroupStatus::standby || status_ == GroupStatus::interrupted ||
1106 status_ == GroupStatus::errorstop) {
1107 out = CartesianTwist{};
1108 return rt::ErrorCode::ok;
1109 }
1110
1111 // Analytical commanded TCP twist during active Cartesian window or segment.
1112 if(cs != CoordSystem::acs && cartesian_.window_active_ && !cartesian_.window_.empty()) {
1113 std::size_t active_idx = cartesian_.piece_index_;
1114 double s_local = 0.0;
1115 double sdot = 0.0;
1116 if(cartesian_.window_stopping_) {
1118 cartesian_.halt_profile_,
1119 rt::CycleTick::from_cycles(cartesian_.halt_tick_));
1120 sdot = halt.velocity;
1121 double remaining = cartesian_.halt_origin_ + halt.position;
1122 for(std::size_t i = cartesian_.piece_index_; i < cartesian_.window_.size(); ++i) {
1123 const CartPiece &p = cartesian_.window_[i];
1124 const double offset =
1125 i == cartesian_.piece_index_ ? cartesian_.halt_piece_offset_ : 0.0;
1126 const double avail = p.length - offset;
1127 if(remaining <= avail || i + 1 == cartesian_.window_.size()) {
1128 active_idx = i;
1129 s_local = offset + remaining;
1130 if(s_local > p.length) s_local = p.length;
1131 break;
1132 }
1133 remaining -= avail;
1134 }
1135 } else {
1136 const CartPiece &piece = cartesian_.window_[active_idx];
1137 if(piece.corner) {
1138 s_local = piece.v_in * static_cast<double>(cartesian_.piece_tick_);
1139 if(s_local > piece.length) s_local = piece.length;
1140 sdot = cartesian_.piece_tick_ < piece.duration ? piece.v_in : 0.0;
1141 } else {
1142 const otg::State1D st = otg::sample(
1143 piece.profile, rt::CycleTick::from_cycles(cartesian_.piece_tick_));
1144 s_local = st.position;
1145 sdot = st.velocity;
1146 }
1147 }
1148 const CartPiece &piece = cartesian_.window_[active_idx];
1149 const geom::Vec3 tangent_v =
1150 piece.corner ? geom::path_derivative(piece.blend, s_local) : piece.dir;
1151 const geom::Vec3 v_mcs = tangent_v * sdot;
1152 geom::Vec3 w_mcs{};
1153 if(piece.pose && piece.length > 1e-15 && piece.angle > 0.0) {
1154 const double rate = (piece.angle / piece.length) * sdot;
1155 const geom::Vec3 w_body{piece.axis[0] * rate, piece.axis[1] * rate,
1156 piece.axis[2] * rate};
1157 geom::RigidTransform r_start{};
1158 for(int r = 0; r < 3; ++r) {
1159 for(int c = 0; c < 3; ++c) {
1160 r_start.rotation[r][c] = piece.rotation_start[r][c];
1161 }
1162 }
1163 w_mcs = geom::transform_rotate(r_start, w_body);
1164 }
1165 CartesianTwist result{v_mcs, w_mcs};
1166 if(cs == CoordSystem::pcs) {
1167 const geom::RigidTransform inv_wp = geom::invert(pose_frames_.workpiece_frame_);
1168 result.linear = geom::transform_rotate(inv_wp, v_mcs);
1169 result.angular = geom::transform_rotate(inv_wp, w_mcs);
1170 }
1171 if(!std::isfinite(result.linear.x) || !std::isfinite(result.linear.y) ||
1172 !std::isfinite(result.linear.z) || !std::isfinite(result.angular.x) ||
1173 !std::isfinite(result.angular.y) || !std::isfinite(result.angular.z)) {
1175 }
1176 out = result;
1177 return rt::ErrorCode::ok;
1178 }
1179
1180 if(cs != CoordSystem::acs && active_ && !cartesian_.takeover_active_ &&
1181 active_kind_ == GroupPathKind::cartesian_linear && active_path_length_ > 0.0) {
1182 const otg::State1D st =
1183 otg::sample(active_profile_, rt::CycleTick::from_cycles(active_tick_));
1184 const double s =
1185 st.position < 0.0 ? 0.0
1186 : (st.position > active_path_length_ ? active_path_length_
1187 : st.position);
1188 const CartesianSegment &seg = cartesian_.active_segment_;
1189 geom::Vec3 v_mcs{};
1190 geom::Vec3 w_mcs{};
1191 if(seg.angle_driven) {
1192 v_mcs = {};
1193 const geom::Vec3 w_body{seg.axis[0] * st.velocity, seg.axis[1] * st.velocity,
1194 seg.axis[2] * st.velocity};
1195 geom::RigidTransform r_start{};
1196 for(int r = 0; r < 3; ++r) {
1197 for(int c = 0; c < 3; ++c) {
1198 r_start.rotation[r][c] = seg.rotation_start[r][c];
1199 }
1200 }
1201 w_mcs = geom::transform_rotate(r_start, w_body);
1202 } else {
1203 geom::Vec3 tangent_v{};
1204 if(seg.chain) {
1205 if(s <= seg.line1) {
1206 tangent_v = seg.dir1;
1207 } else if(s <= seg.line1 + seg.corner.length) {
1208 tangent_v = geom::path_derivative(seg.corner, s - seg.line1);
1209 } else {
1210 tangent_v = seg.dir2;
1211 }
1212 } else if(seg.arc_path) {
1213 tangent_v = geom::path_derivative(seg.arc, s);
1214 if(!seg.arc.spatial) {
1215 tangent_v.z = seg.delta.z / seg.length;
1216 }
1217 } else {
1218 tangent_v = seg.delta * (1.0 / seg.length);
1219 }
1220 v_mcs = tangent_v * st.velocity;
1221 if(seg.pose && seg.angle > 0.0) {
1222 const double rate = (seg.angle / seg.length) * st.velocity;
1223 const geom::Vec3 w_body{seg.axis[0] * rate, seg.axis[1] * rate,
1224 seg.axis[2] * rate};
1225 geom::RigidTransform r_start{};
1226 for(int r = 0; r < 3; ++r) {
1227 for(int c = 0; c < 3; ++c) {
1228 r_start.rotation[r][c] = seg.rotation_start[r][c];
1229 }
1230 }
1231 w_mcs = geom::transform_rotate(r_start, w_body);
1232 }
1233 }
1234 if(active_command_.dynamic_pcs) {
1235 const geom::RigidTransform delta = geom::compose(
1236 pose_frames_.workpiece_frame_,
1237 geom::invert(pose_frames_.active_dynamic_reference_frame_));
1238 v_mcs = geom::transform_rotate(delta, v_mcs);
1239 w_mcs = geom::transform_rotate(delta, w_mcs);
1240 }
1241 CartesianTwist result{v_mcs, w_mcs};
1242 if(cs == CoordSystem::pcs) {
1243 const geom::RigidTransform inv_wp = geom::invert(pose_frames_.workpiece_frame_);
1244 result.linear = geom::transform_rotate(inv_wp, v_mcs);
1245 result.angular = geom::transform_rotate(inv_wp, w_mcs);
1246 }
1247 if(!std::isfinite(result.linear.x) || !std::isfinite(result.linear.y) ||
1248 !std::isfinite(result.linear.z) || !std::isfinite(result.angular.x) ||
1249 !std::isfinite(result.angular.y) || !std::isfinite(result.angular.z)) {
1251 }
1252 out = result;
1253 return rt::ErrorCode::ok;
1254 }
1255
1256 GroupPosition q{};
1257 GroupPosition dq{};
1258 q.size = axes_.size();
1259 for(std::size_t i = 0; i < axes_.size(); ++i) {
1260 q.value[i] = axes_[i]->snapshot().command_position;
1261 }
1262 const rt::ErrorCode vel_ok = command_member_velocities(dq);
1263 if(vel_ok != rt::ErrorCode::ok) {
1264 return vel_ok;
1265 }
1266 return read_cartesian_twist_from_joints(cs, q, dq, out);
1267}
1268
1269inline rt::ErrorCode AxisGroup::set_tool_offset(double x, double y, double z)
1270{
1271 if(numbered_tool_mode_) return rt::ErrorCode::precondition_failed;
1272 if(status_ != GroupStatus::standby || !queue_.empty()) {
1274 }
1275 if(!std::isfinite(x) || !std::isfinite(y) || !std::isfinite(z)) {
1277 }
1278 pose_frames_.tool_offset_ = geom::Vec3{x, y, z};
1279 return rt::ErrorCode::ok;
1280}
1281
1283{
1284 if(status_ != GroupStatus::standby || !queue_.empty() || !std::isfinite(limit) ||
1285 limit < 0.0) {
1287 }
1288 pose_frames_.cartesian_velocity_limit_ = limit;
1289 return rt::ErrorCode::ok;
1290}
1291
1293 double min_singularity_margin)
1294{
1295 if(status_ != GroupStatus::standby || !queue_.empty() ||
1296 !std::isfinite(min_singularity_margin) || min_singularity_margin < 0.0) {
1298 }
1299 if(plugin != nullptr &&
1300 (pose_frames_.pose_kinematics_ != nullptr || plugin->joint_count() != axes_.size() ||
1301 plugin->cartesian_count() != plugin->joint_count() ||
1302 plugin->cartesian_count() < 2 || plugin->cartesian_count() > 3)) {
1304 }
1305 pose_frames_.kinematics_ = plugin;
1306 pose_frames_.kinematics_min_margin_ = min_singularity_margin;
1307 pose_frames_.translation_path_differential_ = nullptr;
1308 return rt::ErrorCode::ok;
1309}
1310
1312 const kin::TranslationPathDifferential *differential)
1313{
1314 if(status_ != GroupStatus::standby || !queue_.empty()) {
1316 }
1317 if(differential == nullptr) {
1318 pose_frames_.translation_path_differential_ = nullptr;
1319 return rt::ErrorCode::ok;
1320 }
1321 if(pose_frames_.kinematics_ == nullptr ||
1322 pose_frames_.pose_kinematics_ != nullptr ||
1323 differential->joint_count() != axes_.size()) {
1325 }
1326 pose_frames_.translation_path_differential_ = differential;
1327 return rt::ErrorCode::ok;
1328}
1329
1331{
1332 return pose_frames_.translation_path_differential_;
1333}
1334
1336{
1337 if(status_ != GroupStatus::standby || !queue_.empty() ||
1338 !std::isfinite(max_tcp_tube) || max_tcp_tube <= 0.0) {
1340 }
1341 pose_frames_.cartesian_takeover_tcp_tube_limit_ = max_tcp_tube;
1342 return rt::ErrorCode::ok;
1343}
1344
1346{
1347 return pose_frames_.cartesian_takeover_tcp_tube_limit_;
1348}
1349
1351{ return pose_frames_.kinematics_; }
1352
1354{ return pose_frames_.pose_kinematics_; }
1355
1357{
1358 if(pose_frames_.pose_kinematics_ != nullptr) {
1359 return {KinTransformKind::pose, nullptr, pose_frames_.pose_kinematics_};
1360 }
1361 if(pose_frames_.kinematics_ != nullptr) {
1362 return {KinTransformKind::kinematics, pose_frames_.kinematics_, nullptr};
1363 }
1364 return {};
1365}
1366
1368 const ToolData &transform,
1369 ExecutionMode execution_mode)
1370{
1371 if(execution_mode != ExecutionMode::immediately) return rt::ErrorCode::unsupported;
1372 if(coordinate_system == CoordSystem::pcs) {
1373 return set_workpiece_frame_rpy(transform.value[0], transform.value[1],
1374 transform.value[2], transform.value[3],
1375 transform.value[4], transform.value[5]);
1376 }
1377 if(coordinate_system == CoordSystem::tcs) {
1378 return set_tool_transform_rpy(transform.value[0], transform.value[1],
1379 transform.value[2], transform.value[3],
1380 transform.value[4], transform.value[5]);
1381 }
1383}
1384
1386 ToolData &transform) const
1387{
1388 if(coordinate_system == CoordSystem::pcs) {
1389 workpiece_frame_rpy(transform.value.data());
1390 return rt::ErrorCode::ok;
1391 }
1392 if(coordinate_system == CoordSystem::tcs) {
1393 tool_transform_rpy(transform.value.data());
1394 return rt::ErrorCode::ok;
1395 }
1397}
1398
1400 CoordSystem source,
1401 CoordSystem target,
1402 GroupPosition &output,
1403 bool &singular_position) const
1404{
1405 singular_position = false;
1406 if(position.size != axes_.size() || axes_.empty()) {
1408 }
1409 for(std::size_t i = 0; i < position.size; ++i) {
1410 if(!std::isfinite(position.value[i])) return rt::ErrorCode::invalid_argument;
1411 }
1412 const auto supported = [](CoordSystem system) {
1413 return system == CoordSystem::acs || system == CoordSystem::mcs ||
1414 system == CoordSystem::pcs;
1415 };
1416 if(!supported(source) || !supported(target)) return rt::ErrorCode::unsupported;
1417 if(source == target) {
1418 output = position;
1419 return rt::ErrorCode::ok;
1420 }
1421
1422 GroupPosition mcs = position;
1423 if(source == CoordSystem::acs) {
1424 if(pose_frames_.pose_kinematics_ != nullptr) {
1425 kin::Pose6 flange{};
1426 pose_frames_.pose_kinematics_->forward(position.value.data(), flange);
1427 geom::RigidTransform transform{};
1428 transform.translation = {flange.position[0], flange.position[1],
1429 flange.position[2]};
1430 for(int row = 0; row < 3; ++row) {
1431 for(int column = 0; column < 3; ++column) {
1432 transform.rotation[row][column] = flange.rotation[row][column];
1433 }
1434 }
1435 const geom::RigidTransform &tool = active_tool_transform_applies()
1436 ? pose_frames_.active_pose_tool_
1437 : pose_frames_.pose_tool_;
1438 transform = geom::compose(transform, tool);
1439 mcs.value[0] = transform.translation.x;
1440 mcs.value[1] = transform.translation.y;
1441 mcs.value[2] = transform.translation.z;
1442 geom::extract_rpy(transform.rotation, mcs.value[3], mcs.value[4],
1443 mcs.value[5]);
1444 } else {
1445 geom::Vec3 point{};
1446 if(pose_frames_.kinematics_ != nullptr) {
1447 const rt::ErrorCode result = pose_frames_.kinematics_->forward(
1448 position.value.data(), position.size, point);
1449 if(result != rt::ErrorCode::ok) return result;
1450 } else {
1451 point = {position.value[0], position.value[1], position.value[2]};
1452 }
1453 point = point + (active_tool_transform_applies() ? pose_frames_.active_tool_offset_
1454 : pose_frames_.tool_offset_);
1455 mcs.value[0] = point.x;
1456 mcs.value[1] = point.y;
1457 mcs.value[2] = point.z;
1458 }
1459 } else if(source == CoordSystem::pcs) {
1460 if(pose_frames_.pose_kinematics_ != nullptr) {
1462 position.value[0], position.value[1], position.value[2],
1463 position.value[3], position.value[4], position.value[5]);
1464 transform = geom::compose(pose_frames_.workpiece_frame_, transform);
1465 mcs.value[0] = transform.translation.x;
1466 mcs.value[1] = transform.translation.y;
1467 mcs.value[2] = transform.translation.z;
1468 geom::extract_rpy(transform.rotation, mcs.value[3], mcs.value[4],
1469 mcs.value[5]);
1470 } else {
1471 const geom::Vec3 point = geom::transform_point(
1472 pose_frames_.workpiece_frame_, {position.value[0], position.value[1],
1473 position.value[2]});
1474 mcs.value[0] = point.x;
1475 mcs.value[1] = point.y;
1476 mcs.value[2] = point.z;
1477 }
1478 }
1479
1480 if(target == CoordSystem::mcs) {
1481 output = mcs;
1482 return rt::ErrorCode::ok;
1483 }
1484 if(target == CoordSystem::pcs) {
1485 output = mcs;
1486 if(pose_frames_.pose_kinematics_ != nullptr) {
1488 mcs.value[0], mcs.value[1], mcs.value[2], mcs.value[3],
1489 mcs.value[4], mcs.value[5]);
1490 transform = geom::compose(geom::invert(pose_frames_.workpiece_frame_), transform);
1491 output.value[0] = transform.translation.x;
1492 output.value[1] = transform.translation.y;
1493 output.value[2] = transform.translation.z;
1494 geom::extract_rpy(transform.rotation, output.value[3], output.value[4],
1495 output.value[5]);
1496 } else {
1497 const geom::Vec3 point = geom::transform_point(
1498 geom::invert(pose_frames_.workpiece_frame_),
1499 {mcs.value[0], mcs.value[1], mcs.value[2]});
1500 output.value[0] = point.x;
1501 output.value[1] = point.y;
1502 output.value[2] = point.z;
1503 }
1504 return rt::ErrorCode::ok;
1505 }
1506
1507 output = mcs;
1508 if(pose_frames_.pose_kinematics_ != nullptr) {
1510 mcs.value[0], mcs.value[1], mcs.value[2], mcs.value[3], mcs.value[4],
1511 mcs.value[5]);
1512 const geom::RigidTransform &tool_inverse = active_tool_transform_applies()
1513 ? pose_frames_.active_pose_tool_inverse_
1514 : pose_frames_.pose_tool_inverse_;
1515 const geom::RigidTransform flange = geom::compose(tcp, tool_inverse);
1516 kin::Pose6 pose{};
1517 pose.position[0] = flange.translation.x;
1518 pose.position[1] = flange.translation.y;
1519 pose.position[2] = flange.translation.z;
1520 for(int row = 0; row < 3; ++row) {
1521 for(int column = 0; column < 3; ++column) {
1522 pose.rotation[row][column] = flange.rotation[row][column];
1523 }
1524 }
1525 double seed[MaxAxes] = {};
1526 for(std::size_t i = 0; i < axes_.size(); ++i) {
1527 seed[i] = axes_[i]->snapshot().command_position;
1528 }
1529 const rt::ErrorCode result = pose_frames_.pose_kinematics_->inverse(
1530 pose, seed, pose_frames_.pose_max_joint_step_, output.value.data());
1531 if(result != rt::ErrorCode::ok) return result;
1532 singular_position =
1533 pose_frames_.pose_kinematics_->singularity_margin(output.value.data()) < pose_frames_.pose_min_margin_;
1534 } else {
1535 const geom::Vec3 tool = active_tool_transform_applies() ? pose_frames_.active_tool_offset_
1536 : pose_frames_.tool_offset_;
1537 const geom::Vec3 point{mcs.value[0] - tool.x, mcs.value[1] - tool.y,
1538 mcs.value[2] - tool.z};
1539 if(pose_frames_.kinematics_ != nullptr) {
1540 double seed[MaxAxes] = {};
1541 for(std::size_t i = 0; i < axes_.size(); ++i) {
1542 seed[i] = axes_[i]->snapshot().command_position;
1543 }
1544 const rt::ErrorCode result = pose_frames_.kinematics_->inverse(
1545 point, seed, axes_.size(), output.value.data());
1546 if(result != rt::ErrorCode::ok) return result;
1547 singular_position = pose_frames_.kinematics_->singularity_margin(
1548 output.value.data(), axes_.size()) <
1549 pose_frames_.kinematics_min_margin_;
1550 } else {
1551 output.value[0] = point.x;
1552 output.value[1] = point.y;
1553 output.value[2] = point.z;
1554 }
1555 }
1556 return rt::ErrorCode::ok;
1557}
1558
1559inline rt::ErrorCode AxisGroup::validate_kinematics(const KinTransformRef &transform,
1560 double min_singularity_margin,
1561 double max_joint_step) const
1562{
1563 if(!std::isfinite(min_singularity_margin) || min_singularity_margin < 0.0) {
1565 }
1566 switch(transform.kind) {
1568 return transform.kinematics == nullptr && transform.pose == nullptr
1572 if(transform.pose == nullptr || transform.kinematics != nullptr) {
1574 }
1575 if(!std::isfinite(max_joint_step) || max_joint_step <= 0.0 ||
1576 axes_.size() < 6 || axes_.size() > MaxAxes ||
1577 transform.pose->joint_count() != axes_.size()) {
1579 }
1580 return rt::ErrorCode::ok;
1582 if(transform.kinematics == nullptr || transform.pose != nullptr ||
1583 transform.kinematics->joint_count() != axes_.size() ||
1584 transform.kinematics->cartesian_count() !=
1585 transform.kinematics->joint_count() ||
1586 transform.kinematics->cartesian_count() < 2 ||
1587 transform.kinematics->cartesian_count() > 3) {
1589 }
1590 return rt::ErrorCode::ok;
1591 default:
1593 }
1594}
1595
1596inline rt::ErrorCode AxisGroup::apply_kinematics(const KinTransformRef &transform,
1597 double min_singularity_margin,
1598 double max_joint_step)
1599{
1600 const rt::ErrorCode valid = validate_kinematics(
1601 transform, min_singularity_margin, max_joint_step);
1602 if(valid != rt::ErrorCode::ok) return valid;
1603 if(transform.kind == KinTransformKind::none) {
1604 pose_frames_.kinematics_ = nullptr;
1605 pose_frames_.pose_kinematics_ = nullptr;
1606 pose_frames_.kinematics_min_margin_ = 0.0;
1607 pose_frames_.pose_min_margin_ = 0.0;
1608 pose_frames_.pose_max_joint_step_ = 0.01;
1609 } else if(transform.kind == KinTransformKind::kinematics) {
1610 pose_frames_.kinematics_ = transform.kinematics;
1611 pose_frames_.kinematics_min_margin_ = min_singularity_margin;
1612 pose_frames_.pose_kinematics_ = nullptr;
1613 pose_frames_.pose_min_margin_ = 0.0;
1614 pose_frames_.pose_max_joint_step_ = 0.01;
1615 } else {
1616 pose_frames_.pose_kinematics_ = transform.pose;
1617 pose_frames_.pose_min_margin_ = min_singularity_margin;
1618 pose_frames_.pose_max_joint_step_ = max_joint_step;
1619 pose_frames_.kinematics_ = nullptr;
1620 pose_frames_.kinematics_min_margin_ = 0.0;
1621 }
1622 pose_frames_.translation_path_differential_ = nullptr;
1623 return rt::ErrorCode::ok;
1624}
1625
1626inline rt::ErrorCode AxisGroup::validate_coordinate_transform(CoordSystem coordinate_system,
1627 const ToolData &transform) const
1628{
1629 if(coordinate_system != CoordSystem::pcs && coordinate_system != CoordSystem::tcs) {
1631 }
1632 if(coordinate_system == CoordSystem::tcs && numbered_tool_mode_) {
1634 }
1635 for(double value : transform.value) {
1636 if(!std::isfinite(value)) return rt::ErrorCode::invalid_argument;
1637 }
1638 return rt::ErrorCode::ok;
1639}
1640
1641inline rt::ErrorCode AxisGroup::apply_coordinate_transform(CoordSystem coordinate_system,
1642 const ToolData &transform)
1643{
1644 const rt::ErrorCode valid = validate_coordinate_transform(
1645 coordinate_system, transform);
1646 if(valid != rt::ErrorCode::ok) return valid;
1647 if(coordinate_system == CoordSystem::pcs) {
1648 cancel_tracking();
1649 pose_frames_.workpiece_frame_ = geom::make_rpy_transform(
1650 transform.value[0], transform.value[1], transform.value[2],
1651 transform.value[3], transform.value[4], transform.value[5]);
1652 for(int i = 0; i < 6; ++i) {
1653 pose_frames_.workpiece_frame_rpy_[i] = transform.value[i];
1654 }
1655 return rt::ErrorCode::ok;
1656 }
1657 pose_frames_.pose_tool_ = geom::make_rpy_transform(
1658 transform.value[0], transform.value[1], transform.value[2],
1659 transform.value[3], transform.value[4], transform.value[5]);
1660 pose_frames_.pose_tool_inverse_ = geom::invert(pose_frames_.pose_tool_);
1661 for(int i = 0; i < 6; ++i) {
1662 pose_frames_.tool_transform_rpy_[i] = transform.value[i];
1663 }
1664 return rt::ErrorCode::ok;
1665}
1666
1667inline void AxisGroup::snapshot_active_tool_transform()
1668{
1669 pose_frames_.active_pose_tool_ = pose_frames_.pose_tool_;
1670 pose_frames_.active_pose_tool_inverse_ = pose_frames_.pose_tool_inverse_;
1671 pose_frames_.active_tool_offset_ = pose_frames_.tool_offset_;
1672}
1673
1674inline bool AxisGroup::active_tool_transform_applies() const
1675{
1676 return status_ == GroupStatus::moving || status_ == GroupStatus::stopping ||
1677 status_ == GroupStatus::interrupted;
1678}
1679
1680inline geom::Vec3 AxisGroup::cartesian_part(const GroupPosition &position)
1681{
1682 return geom::Vec3{position.value[0],
1683 position.size > 1 ? position.value[1] : 0.0,
1684 position.size > 2 ? position.value[2] : 0.0};
1685}
1686
1687inline rt::ErrorCode AxisGroup::select_orientation_interpolation(GroupCommand &command) const
1688{
1689 switch(command.orientation_mode) {
1691 return rt::ErrorCode::ok;
1694 if(pose_frames_.pose_kinematics_ == nullptr) {
1696 }
1697 if(command.coord_system != CoordSystem::mcs &&
1698 command.coord_system != CoordSystem::pcs) {
1700 }
1701 command.interpolation_space = InterpolationSpace::cartesian;
1702 return rt::ErrorCode::ok;
1703 default:
1705 }
1706}
1707
1708inline void AxisGroup::store_cartesian_part(GroupPosition &position, geom::Vec3 point)
1709{
1710 position.value[0] = point.x;
1711 if(position.size > 1) {
1712 position.value[1] = point.y;
1713 }
1714 if(position.size > 2) {
1715 position.value[2] = point.z;
1716 }
1717}
1718
1719inline rt::ErrorCode AxisGroup::apply_coordinate_frame(GroupCommand &command) const
1720{
1721 switch(command.coord_system) {
1722 case CoordSystem::acs:
1723 return rt::ErrorCode::ok;
1724 case CoordSystem::mcs:
1725 case CoordSystem::pcs:
1726 break;
1727 default:
1729 }
1730
1731 const bool pcs = command.coord_system == CoordSystem::pcs;
1732 const bool circular = command.path_kind == GroupPathKind::circular;
1733
1734 // Orientation batch (approved matrix, decision #6): the pose
1735 // pipeline consumes [x,y,z,roll,pitch,yaw] targets on 6-joint
1736 // groups. v1 is submit_linear + absolute only; relative, circular,
1737 // and blending transitions report explicit unsupported. The frame
1738 // and tool compose on the pose, the analytic inverse (seeded by the
1739 // segment start joints, KB-041 gates) lands the 6 ACS joint targets,
1740 // and the in-segment interpolation stays a joint-space line
1741 // (declared boundary, orientation edition).
1742 if(pose_frames_.pose_kinematics_ != nullptr) {
1743 if(circular || command.relative ||
1744 command.buffer_mode == BufferMode::blending_low ||
1745 command.buffer_mode == BufferMode::blending_high) {
1747 }
1748 geom::RigidTransform target = geom::make_rpy_transform(
1749 command.target.value[0], command.target.value[1],
1750 command.target.value[2], command.target.value[3],
1751 command.target.value[4], command.target.value[5]);
1752 if(pcs) {
1753 target = geom::compose(pose_frames_.workpiece_frame_, target);
1754 }
1755 const geom::RigidTransform flange = geom::compose(target, pose_frames_.pose_tool_inverse_);
1756
1757 kin::Pose6 pose{};
1758 pose.position[0] = flange.translation.x;
1759 pose.position[1] = flange.translation.y;
1760 pose.position[2] = flange.translation.z;
1761 for(int i = 0; i < 3; ++i) {
1762 for(int j = 0; j < 3; ++j) {
1763 pose.rotation[i][j] = flange.rotation[i][j];
1764 }
1765 }
1766
1767 double seed[MaxAxes] = {};
1768 for(std::size_t i = 0; i < axes_.size(); ++i) {
1769 seed[i] = queued_finish(i);
1770 }
1771 double joints[MaxAxes] = {};
1772 const rt::ErrorCode inverted =
1773 pose_frames_.pose_kinematics_->inverse(pose, seed, pose_frames_.pose_max_joint_step_, joints);
1774 if(inverted != rt::ErrorCode::ok) {
1775 return inverted;
1776 }
1777 if(pose_frames_.pose_kinematics_->singularity_margin(joints) < pose_frames_.pose_min_margin_) {
1779 }
1780 for(std::size_t i = 0; i < axes_.size(); ++i) {
1781 command.target.value[i] = joints[i];
1782 }
1783 command.target.size = axes_.size();
1784 command.coord_system = CoordSystem::acs;
1785 return rt::ErrorCode::ok;
1786 }
1787
1788 // Kinematics-configured pipeline (approved kinematics matrix): the
1789 // Cartesian point goes through the workpiece frame and tool offset,
1790 // then the inverse solution — seeded with the segment start joints —
1791 // becomes the ACS joint target. v1 solves endpoints and aux points
1792 // only; the in-segment interpolation stays joint-space (declared
1793 // boundary: an MCS line is a joint-space line, not a Cartesian line,
1794 // on nonlinear mechanisms).
1795 if(pose_frames_.kinematics_ != nullptr) {
1796 double seed[MaxAxes] = {};
1797 for(std::size_t i = 0; i < axes_.size(); ++i) {
1798 seed[i] = queued_finish(i);
1799 }
1800 rt::ErrorCode solved =
1801 solve_cartesian_target(command.target, command.relative, pcs, seed);
1802 if(solved != rt::ErrorCode::ok) {
1803 return solved;
1804 }
1805 if(circular && command.circ_mode != CircMode::radius) {
1806 solved = solve_cartesian_target(command.aux, command.relative, pcs, seed);
1807 if(solved != rt::ErrorCode::ok) {
1808 return solved;
1809 }
1810 }
1811 command.relative = false;
1812 command.coord_system = CoordSystem::acs;
1813
1814 // Dual-space limiting (BS3.6): sample the joint-space chord
1815 // through the forward solution; the worst Cartesian displacement
1816 // per path-parameter step scales the command velocity down. The
1817 // path parameter uses the same resolved linear_metric as start().
1818 if(pose_frames_.cartesian_velocity_limit_ > 0.0 && !circular) {
1819 double longest = 0.0;
1820 for(std::size_t i = 0; i < axes_.size(); ++i) {
1821 const double travel = std::fabs(command.target.value[i] - seed[i]);
1822 if(travel > longest) {
1823 longest = travel;
1824 }
1825 }
1826 if(command.linear_metric == LinearPathMetric::euclidean && longest > 0.0) {
1827 double scaled_sq = 0.0;
1828 for(std::size_t i = 0; i < axes_.size(); ++i) {
1829 const double r = (command.target.value[i] - seed[i]) / longest;
1830 scaled_sq += r * r;
1831 }
1832 longest = longest * std::sqrt(scaled_sq);
1833 }
1834 if(longest > 0.0) {
1835 constexpr int Samples = 16;
1836 double joints[MaxAxes] = {};
1837 geom::Vec3 previous{};
1838 double worst_ratio = 0.0;
1839 for(int step = 0; step <= Samples; ++step) {
1840 const double fraction =
1841 static_cast<double>(step) / static_cast<double>(Samples);
1842 for(std::size_t i = 0; i < axes_.size(); ++i) {
1843 joints[i] =
1844 seed[i] + fraction * (command.target.value[i] - seed[i]);
1845 }
1846 geom::Vec3 cartesian{};
1847 const rt::ErrorCode forwarded =
1848 pose_frames_.kinematics_->forward(joints, axes_.size(), cartesian);
1849 if(forwarded != rt::ErrorCode::ok) {
1850 return forwarded;
1851 }
1852 if(step > 0) {
1853 const double chord = geom::norm(cartesian - previous);
1854 const double parameter_step =
1855 longest / static_cast<double>(Samples);
1856 const double ratio = chord / parameter_step;
1857 if(ratio > worst_ratio) {
1858 worst_ratio = ratio;
1859 }
1860 }
1861 previous = cartesian;
1862 }
1863 if(worst_ratio > 0.0) {
1864 const double allowed = pose_frames_.cartesian_velocity_limit_ / worst_ratio;
1865 if(allowed < command.velocity) {
1866 command.velocity = allowed;
1867 }
1868 }
1869 }
1870 }
1871 return rt::ErrorCode::ok;
1872 }
1873
1874 if(command.relative) {
1875 geom::Vec3 direction = cartesian_part(command.target);
1876 if(pcs) {
1877 direction = geom::transform_rotate(pose_frames_.workpiece_frame_, direction);
1878 }
1879 store_cartesian_part(command.target, direction);
1880 if(circular && command.circ_mode != CircMode::radius) {
1881 geom::Vec3 aux = cartesian_part(command.aux);
1882 if(pcs) {
1883 aux = geom::transform_rotate(pose_frames_.workpiece_frame_, aux);
1884 }
1885 store_cartesian_part(command.aux, aux);
1886 }
1887 } else {
1888 geom::Vec3 point = cartesian_part(command.target);
1889 if(pcs) {
1890 point = geom::transform_point(pose_frames_.workpiece_frame_, point);
1891 }
1892 store_cartesian_part(command.target,
1893 point - pose_frames_.tool_offset_);
1894 if(circular && command.circ_mode != CircMode::radius) {
1895 geom::Vec3 aux = cartesian_part(command.aux);
1896 if(pcs) {
1897 aux = geom::transform_point(pose_frames_.workpiece_frame_, aux);
1898 }
1899 store_cartesian_part(command.aux, aux - pose_frames_.tool_offset_);
1900 }
1901 }
1902 command.coord_system = CoordSystem::acs;
1903 return rt::ErrorCode::ok;
1904}
1905
1906inline rt::ErrorCode AxisGroup::solve_cartesian_target(GroupPosition &position,
1907 bool relative,
1908 bool pcs,
1909 const double *seed) const
1910{
1911 geom::Vec3 point = cartesian_part(position);
1912 if(relative) {
1913 if(pcs) {
1914 point = geom::transform_rotate(pose_frames_.workpiece_frame_, point);
1915 }
1916 geom::Vec3 start{};
1917 const rt::ErrorCode forwarded =
1918 pose_frames_.kinematics_->forward(seed, axes_.size(), start);
1919 if(forwarded != rt::ErrorCode::ok) {
1920 return forwarded;
1921 }
1922 point = start + point;
1923 } else {
1924 if(pcs) {
1925 point = geom::transform_point(pose_frames_.workpiece_frame_, point);
1926 }
1927 point = point - pose_frames_.tool_offset_;
1928 }
1929
1930 double joints[MaxAxes] = {};
1931 const rt::ErrorCode inverted =
1932 pose_frames_.kinematics_->inverse(point, seed, axes_.size(), joints);
1933 if(inverted != rt::ErrorCode::ok) {
1934 return inverted;
1935 }
1936 if(pose_frames_.kinematics_->singularity_margin(joints, axes_.size()) < pose_frames_.kinematics_min_margin_) {
1938 }
1939 for(std::size_t i = 0; i < axes_.size(); ++i) {
1940 position.value[i] = joints[i];
1941 }
1942 return rt::ErrorCode::ok;
1943}
1944
1945} // namespace plcopen::core::axis
rt::ErrorCode set_workpiece_frame_rpy(double x, double y, double z, double roll, double pitch, double yaw)
const kin::PoseKinematics * pose_kinematics_plugin() const
rt::Result< std::uint32_t > halt(double deceleration=1.0, double jerk=1.0, BufferMode buffer_mode=BufferMode::aborting)
rt::ErrorCode read_cartesian_twist(CoordSystem cs, PositionSource source, CartesianTwist &out) const
rt::ErrorCode set_cartesian_takeover_tcp_tube_limit(double max_tcp_tube)
const kin::Kinematics * kinematics_plugin() const
rt::ErrorCode set_workpiece_frame(double x, double y, double z, double rot_z)
static constexpr std::size_t MaxAxes
const kin::TranslationPathDifferential * translation_path_differential() const
rt::ErrorCode set_kinematics(const kin::Kinematics *plugin, double min_singularity_margin=0.0)
rt::ErrorCode transform_position(const GroupPosition &position, CoordSystem source, CoordSystem target, GroupPosition &output, bool &singular_position) const
rt::ErrorCode set_cartesian_velocity_limit(double limit)
rt::ErrorCode set_coordinate_transform(CoordSystem coordinate_system, const ToolData &transform, ExecutionMode execution_mode)
rt::ErrorCode read_cartesian_twist_from_joints(CoordSystem cs, const GroupPosition &joints, const GroupPosition &joint_velocities, CartesianTwist &out) const
rt::ErrorCode coordinate_transform(CoordSystem coordinate_system, ToolData &transform) const
rt::ErrorCode set_tool_offset(double x, double y, double z)
void workpiece_frame_rpy(double out[6]) const
rt::ErrorCode read_cartesian(CoordSystem cs, PositionSource source, GroupPosition &out, bool *gimbal_lock=nullptr) const
rt::ErrorCode set_pose_kinematics(const kin::PoseKinematics *plugin, double min_singularity_margin, double max_joint_step)
rt::ErrorCode set_translation_path_differential(const kin::TranslationPathDifferential *differential)
rt::ErrorCode set_tool_transform_rpy(double x, double y, double z, double roll, double pitch, double yaw)
virtual std::size_t cartesian_count() const =0
virtual std::size_t joint_count() const =0
virtual std::size_t joint_count() const =0
virtual std::size_t joint_count() const =0
static constexpr CycleTick from_cycles(std::int64_t cycles)
定义 cycle.h:42
Vec3 transform_rotate(const RigidTransform &transform, Vec3 v)
定义 frame.h:99
Vec3 transform_point(const RigidTransform &transform, Vec3 p)
Vec3 path_derivative(LineSegment line)
RigidTransform invert(const RigidTransform &transform)
double norm(Vec3 value)
bool extract_rpy(const double rotation[3][3], double &roll, double &pitch, double &yaw)
RigidTransform make_rpy_transform(double x, double y, double z, double roll, double pitch, double yaw)
定义 frame.h:71
RigidTransform compose(const RigidTransform &a, const RigidTransform &b)
State1D sample(const Profile1D &profile, rt::CycleTick tick)
std::array< double, MaxAxes > value
定义 group.h:30
const kin::Kinematics * kinematics
const kin::PoseKinematics * pose
std::array< double, 6 > value