22 !std::isfinite(y) || !std::isfinite(z) || !std::isfinite(roll) ||
23 !std::isfinite(pitch) || !std::isfinite(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];
44 !std::isfinite(y) || !std::isfinite(z) || !std::isfinite(roll) ||
45 !std::isfinite(pitch) || !std::isfinite(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];
58 double min_singularity_margin,
59 double max_joint_step)
62 !std::isfinite(min_singularity_margin) || min_singularity_margin < 0.0 ||
63 !std::isfinite(max_joint_step) || max_joint_step <= 0.0) {
66 if(plugin !=
nullptr && (pose_frames_.kinematics_ !=
nullptr ||
67 axes_.size() < 6 || axes_.size() >
MaxAxes ||
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;
80 for(
int i = 0; i < 6; ++i) {
81 out[i] = pose_frames_.workpiece_frame_rpy_[i];
87 for(
int i = 0; i < 6; ++i) {
88 out[i] = pose_frames_.tool_transform_rpy_[i];
94 return pose_frames_.tool_offset_;
100 bool *gimbal_lock)
const
102 if(gimbal_lock !=
nullptr) {
103 *gimbal_lock =
false;
116 out.
size = axes_.size();
118 for(std::size_t i = 0; i < axes_.size(); ++i) {
122 out.
value[i] = joints[i];
128 if(pose_frames_.pose_kinematics_ !=
nullptr) {
130 pose_frames_.pose_kinematics_->forward(joints, flange);
134 for(
int i = 0; i < 3; ++i) {
135 for(
int j = 0; j < 3; ++j) {
140 ? pose_frames_.active_pose_tool_
141 : pose_frames_.pose_tool_;
154 out.
value[4] = pitch;
156 if(gimbal_lock !=
nullptr) {
157 *gimbal_lock = gimbal;
163 if(pose_frames_.kinematics_ !=
nullptr) {
165 pose_frames_.kinematics_->forward(joints, axes_.size(), point);
170 point = cartesian_part(out);
172 point = point + (active_tool_transform_applies() ? pose_frames_.active_tool_offset_
173 : pose_frames_.tool_offset_);
177 store_cartesian_part(out, point);
196 joints.
size != axes_.size() || joint_velocities.
size != axes_.size() ||
197 !finite(joints) || !finite(joint_velocities)) {
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;
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;
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) {
234 pose_frames_.translation_path_differential_->forward_velocity(
235 joints.
value.data(), joint_velocities.
value.data(), axes_.size(), v_mcs);
237 resolved_analytic =
true;
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;
248 const double root = std::sqrt(scaled_sq);
249 const double dq_norm = max_dq * root;
250 if(!std::isfinite(dq_norm)) {
253 if(dq_norm <= 1e-15) {
257 constexpr double Step = 1e-7;
258 const double scale = dq_norm / (2.0 * Step);
259 if(!std::isfinite(scale)) {
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;
269 if(pose_frames_.pose_kinematics_ !=
nullptr) {
271 ? pose_frames_.active_pose_tool_
272 : pose_frames_.pose_tool_;
275 pose_frames_.pose_kinematics_->forward(q, flange);
278 for(
int r = 0; r < 3; ++r) {
279 for(
int c = 0; c < 3; ++c) {
289 eval_tcp(q_plus, t_plus);
290 eval_tcp(q_minus, t_minus);
293 double r_sp[3][3] = {};
294 for(
int r = 0; r < 3; ++r) {
295 for(
int c = 0; c < 3; ++c) {
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,
310 pose_frames_.kinematics_->forward(q_plus, axes_.size(), p_plus);
315 pose_frames_.kinematics_->forward(q_minus, axes_.size(), p_minus);
319 v_mcs = (p_plus - p_minus) * scale;
329 if(!std::isfinite(result.
linear.
x) || !std::isfinite(result.
linear.
y) ||
341 velocities.
size = axes_.size();
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;
352 if(joint_window_.active_ && !joint_window_.segments_.empty()) {
357 auto eval_blend_ds_derivative = [&](
const WindowNode &node,
double s_val,
358 std::array<double, MaxAxes> &dq_ds) {
360 const double clamped =
362 : (s_val > node.curve_length ? node.curve_length : s_val);
364 for(std::size_t k = 1; k < BlendTableSize; ++k) {
365 if(node.cumulative[k] >= clamped) {
371 const double seg_l = node.cumulative[low + 1] - node.cumulative[low];
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];
394 e0 * d1w0 + e1 * d1w1 + e2 * d1w2 + e3 * d1w3 + e4 * d1w4;
395 dq_ds[i] = bp * du_ds;
399 if(joint_window_.stopping_) {
401 joint_window_.stop_profile_,
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];
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
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) *
425 for(std::size_t i = 0; i < axes_.size(); ++i) {
426 velocities.
value[i] = seg.dir[i] *
halt.velocity;
432 if(seg.node.has_curve) {
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;
446 remaining -= seg.node.curve_length;
453 const WindowSegment &seg = joint_window_.segments_[joint_window_.index_];
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;
466 if(seg.kind == WindowKind::arc && seg.full_length > 0.0) {
468 s_live < 0.0 ? 0.0 : (s_live > seg.full_length ? seg.full_length : s_live);
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;
478 for(std::size_t i = 0; i < axes_.size(); ++i) {
479 velocities.
value[i] = seg.dir[i] * v_live;
487 auto solve_cartesian_joint_velocities =
488 [&](
bool is_pose, geom::Vec3 v_tcp, geom::Vec3 w_tcp,
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) {
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)) {
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;
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])) {
512 const double d = std::fabs(a[r][r]);
513 if(d > max_diag) max_diag =
d;
515 if(!(max_diag > 1e-24)) {
518 const double piv_tol = 1e-12 * max_diag;
519 for(
int col = 0; col < dim; ++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]);
529 if(!std::isfinite(best) || best <= piv_tol) {
533 for(
int c = col; c < dim; ++c) {
534 const double tmp = a[col][c];
535 a[col][c] = a[piv_row][c];
538 const double tb = b[col];
542 const double diag = a[col][col];
543 for(
int r = col + 1;
r < dim; ++
r) {
544 const double factor = a[
r][col] / diag;
546 for(
int c = col + 1; c < dim; ++c) {
547 a[
r][c] -= factor * a[col][c];
549 b[
r] -= factor * b[col];
552 for(
int r = dim - 1;
r >= 0; --
r) {
554 for(
int c = r + 1; c < dim; ++c) {
555 sum -= a[
r][c] * x[c];
557 x[
r] = sum / a[
r][
r];
558 if(!std::isfinite(x[r])) {
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])) {
573 q_plus[i] = q_cur[i];
574 q_minus[i] = q_cur[i];
577 constexpr double kMachineEps = 2.220446049250313e-16;
578 constexpr double kMaxStep = 1.0e-2;
579 if(is_pose && pose_frames_.pose_kinematics_ !=
nullptr) {
581 const geom::RigidTransform &tool = pose_frames_.active_pose_tool_;
582 auto eval_pose_col = [&](std::size_t col_j,
double h_step,
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];
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];
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];
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];
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])) {
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) {
657 bool converged =
false;
658 for(
int attempt = 0; attempt < 6; ++attempt) {
669 const double h2 = (1.5 * h <= kMaxStep) ? (1.5 * h) : (h / 1.5);
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;
685 const bool at_cap = (attempt == 5 || h >= kMaxStep);
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;
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];
707 for(
int r = 0;
r < 6; ++
r) jac[r][j] = 0.0;
716 double jjt[6][6] = {};
717 for(
int r = 0;
r < 6; ++
r) {
718 for(
int c = 0; c < 6; ++c) {
720 for(std::size_t j = 0; j <
n; ++j) {
721 s += jac[
r][j] * jac[c][j];
726 double rhs[6] = {v_tcp.x, v_tcp.y, v_tcp.z, w_tcp.x, w_tcp.y, w_tcp.z};
728 if(!solve_linear_system(6, jjt, rhs, y)) {
731 for(std::size_t j = 0; j <
n; ++j) {
733 for(
int r = 0;
r < 6; ++
r) {
734 s += jac[
r][j] * y[
r];
736 if(!std::isfinite(s)) {
744 std::size_t m = pose_frames_.kinematics_->cartesian_count();
751 auto eval_trans_col = [&](std::size_t col_j,
double h_step,
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];
762 geom::Vec3 pp{}, pm{};
763 const rt::ErrorCode r1 = pose_frames_.kinematics_->forward(q_plus, n, pp);
765 q_plus[col_j] = q_cur[col_j];
766 q_minus[col_j] = q_cur[col_j];
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];
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;
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;
790 for(std::size_t r = 0;
r < m; ++
r) {
791 if(!std::isfinite(col_out[r])) {
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) {
804 bool converged =
false;
805 for(
int attempt = 0; attempt < 6; ++attempt) {
816 const double h2 = (1.5 * h <= kMaxStep) ? (1.5 * h) : (h / 1.5);
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;
832 const bool at_cap = (attempt == 5 || h >= kMaxStep);
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;
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];
854 for(std::size_t r = 0;
r < m; ++
r) jac[r][j] = 0.0;
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) {
867 for(std::size_t j = 0; j <
n; ++j) {
868 s += jac[
r][j] * jac[c][j];
873 double rhs[6] = {v_tcp.x, v_tcp.y, v_tcp.z, 0.0, 0.0, 0.0};
875 if(!solve_linear_system(
static_cast<int>(m), jjt, rhs, y)) {
878 for(std::size_t j = 0; j <
n; ++j) {
880 for(std::size_t r = 0;
r < m; ++
r) {
881 s += jac[
r][j] * y[
r];
883 if(!std::isfinite(s)) {
891 if(cartesian_.window_active_ && !cartesian_.window_.empty()) {
892 std::size_t active_idx = cartesian_.piece_index_;
893 double s_local = 0.0;
895 if(cartesian_.window_stopping_) {
897 cartesian_.halt_profile_,
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()) {
908 s_local = offset + remaining;
909 if(s_local >
p.length) s_local =
p.length;
915 const CartPiece &piece = cartesian_.window_[active_idx];
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;
923 s_local = st.position;
927 const CartPiece &piece = cartesian_.window_[active_idx];
928 const geom::Vec3 tangent_v =
930 const geom::Vec3 v_tcp = tangent_v * sdot;
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];
944 return solve_cartesian_joint_velocities(piece.pose, v_tcp, w_tcp, velocities);
947 active_path_length_ > 0.0) {
948 const otg::State1D st =
951 st.position < 0.0 ? 0.0
952 : (st.position > active_path_length_ ? active_path_length_
954 const CartesianSegment &seg = cartesian_.active_segment_;
955 if(cartesian_.takeover_active_) {
959 if(pose_frames_.translation_path_differential_ ==
nullptr ||
960 axes_.size() < 2 || axes_.size() > 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_);
970 pose_frames_.translation_path_differential_->line_derivatives(
971 x_base, unit_dir, cartesian_.joints_, axes_.size(), q_s, q_ss);
976 for(std::size_t i = 0; i < axes_.size(); ++i) {
978 (i < 3 && cartesian_.takeover_residuals_[i].segment_count() > 0)
983 const double dq_i = q_s[i] * st.velocity + r_dot;
984 if(!std::isfinite(dq_i)) {
987 velocities.
value[i] = dq_i;
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];
1004 geom::Vec3 tangent_v{};
1006 if(s <= seg.line1) {
1007 tangent_v = seg.dir1;
1008 }
else if(s <= seg.line1 + seg.corner.length) {
1011 tangent_v = seg.dir2;
1013 }
else if(seg.arc_path) {
1015 if(!seg.arc.spatial) {
1016 tangent_v.z = seg.delta.z / seg.length;
1019 tangent_v = seg.delta * (1.0 / seg.length);
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];
1035 if(active_command_.dynamic_pcs) {
1037 pose_frames_.workpiece_frame_,
1038 geom::invert(pose_frames_.active_dynamic_reference_frame_));
1042 return solve_cartesian_joint_velocities(seg.pose, v_tcp, w_tcp, velocities);
1044 if(active_ && active_path_length_ > 0.0) {
1045 const otg::State1D state =
1047 std::array<double, MaxAxes> lat_v{};
1048 connector_.sample_lateral_velocities(active_tick_, lat_v, axes_.size());
1050 const double s = state.position < 0.0
1052 : (state.position > active_path_length_
1053 ? active_path_length_
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_) *
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_) *
1095 q.
size = axes_.size();
1096 dq.
size = axes_.size();
1097 for(std::size_t i = 0; i < axes_.size(); ++i) {
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;
1116 if(cartesian_.window_stopping_) {
1118 cartesian_.halt_profile_,
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()) {
1129 s_local = offset + remaining;
1130 if(s_local > p.length) s_local = p.length;
1136 const CartPiece &piece = cartesian_.window_[active_idx];
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;
1144 s_local =
st.position;
1148 const CartPiece &piece = cartesian_.window_[active_idx];
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};
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];
1171 if(!std::isfinite(result.
linear.
x) || !std::isfinite(result.
linear.
y) ||
1172 !std::isfinite(result.
linear.
z) || !std::isfinite(result.
angular.
x) ||
1185 st.position < 0.0 ? 0.0
1186 : (
st.position > active_path_length_ ? active_path_length_
1194 seg.
axis[2] *
st.velocity};
1196 for(
int r = 0; r < 3; ++r) {
1197 for(
int c = 0; c < 3; ++c) {
1205 if(s <= seg.
line1) {
1206 tangent_v = seg.
dir1;
1210 tangent_v = seg.
dir2;
1220 v_mcs = tangent_v *
st.velocity;
1224 seg.
axis[2] * rate};
1226 for(
int r = 0; r < 3; ++r) {
1227 for(
int c = 0; c < 3; ++c) {
1234 if(active_command_.dynamic_pcs) {
1236 pose_frames_.workpiece_frame_,
1237 geom::invert(pose_frames_.active_dynamic_reference_frame_));
1247 if(!std::isfinite(result.
linear.
x) || !std::isfinite(result.
linear.
y) ||
1248 !std::isfinite(result.
linear.
z) || !std::isfinite(result.
angular.
x) ||
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;
1275 if(!std::isfinite(x) || !std::isfinite(y) || !std::isfinite(z)) {
1278 pose_frames_.tool_offset_ =
geom::Vec3{x, y, z};
1288 pose_frames_.cartesian_velocity_limit_ = limit;
1293 double min_singularity_margin)
1296 !std::isfinite(min_singularity_margin) || min_singularity_margin < 0.0) {
1299 if(plugin !=
nullptr &&
1300 (pose_frames_.pose_kinematics_ !=
nullptr || plugin->
joint_count() != axes_.size() ||
1305 pose_frames_.kinematics_ = plugin;
1306 pose_frames_.kinematics_min_margin_ = min_singularity_margin;
1307 pose_frames_.translation_path_differential_ =
nullptr;
1317 if(differential ==
nullptr) {
1318 pose_frames_.translation_path_differential_ =
nullptr;
1321 if(pose_frames_.kinematics_ ==
nullptr ||
1322 pose_frames_.pose_kinematics_ !=
nullptr ||
1326 pose_frames_.translation_path_differential_ = differential;
1332 return pose_frames_.translation_path_differential_;
1338 !std::isfinite(max_tcp_tube) || max_tcp_tube <= 0.0) {
1341 pose_frames_.cartesian_takeover_tcp_tube_limit_ = max_tcp_tube;
1347 return pose_frames_.cartesian_takeover_tcp_tube_limit_;
1351{
return pose_frames_.kinematics_; }
1354{
return pose_frames_.pose_kinematics_; }
1358 if(pose_frames_.pose_kinematics_ !=
nullptr) {
1361 if(pose_frames_.kinematics_ !=
nullptr) {
1403 bool &singular_position)
const
1405 singular_position =
false;
1406 if(position.
size != axes_.size() || axes_.empty()) {
1409 for(std::size_t i = 0; i < position.
size; ++i) {
1417 if(source == target) {
1424 if(pose_frames_.pose_kinematics_ !=
nullptr) {
1426 pose_frames_.pose_kinematics_->forward(position.
value.data(), flange);
1430 for(
int row = 0; row < 3; ++row) {
1431 for(
int column = 0; column < 3; ++column) {
1436 ? pose_frames_.active_pose_tool_
1437 : pose_frames_.pose_tool_;
1446 if(pose_frames_.kinematics_ !=
nullptr) {
1447 const rt::ErrorCode result = pose_frames_.kinematics_->forward(
1448 position.
value.data(), position.
size, point);
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;
1460 if(pose_frames_.pose_kinematics_ !=
nullptr) {
1464 transform =
geom::compose(pose_frames_.workpiece_frame_, transform);
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;
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]);
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;
1508 if(pose_frames_.pose_kinematics_ !=
nullptr) {
1513 ? pose_frames_.active_pose_tool_inverse_
1514 : pose_frames_.pose_tool_inverse_;
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];
1526 for(std::size_t i = 0; i < axes_.size(); ++i) {
1527 seed[i] = axes_[i]->snapshot().command_position;
1529 const rt::ErrorCode result = pose_frames_.pose_kinematics_->inverse(
1530 pose, seed, pose_frames_.pose_max_joint_step_, output.value.data());
1533 pose_frames_.pose_kinematics_->singularity_margin(output.value.data()) < pose_frames_.pose_min_margin_;
1535 const geom::Vec3 tool = active_tool_transform_applies() ? pose_frames_.active_tool_offset_
1536 : pose_frames_.tool_offset_;
1538 mcs.value[2] - tool.
z};
1539 if(pose_frames_.kinematics_ !=
nullptr) {
1541 for(std::size_t i = 0; i < axes_.size(); ++i) {
1542 seed[i] = axes_[i]->snapshot().command_position;
1544 const rt::ErrorCode result = pose_frames_.kinematics_->inverse(
1545 point, seed, axes_.size(), output.value.data());
1547 singular_position = pose_frames_.kinematics_->singularity_margin(
1548 output.value.data(), axes_.size()) <
1549 pose_frames_.kinematics_min_margin_;
1551 output.value[0] = point.
x;
1552 output.value[1] = point.
y;
1553 output.value[2] = point.
z;
1560 double min_singularity_margin,
1561 double max_joint_step)
const
1563 if(!std::isfinite(min_singularity_margin) || min_singularity_margin < 0.0) {
1566 switch(transform.
kind) {
1568 return transform.
kinematics ==
nullptr && transform.
pose ==
nullptr
1572 if(transform.
pose ==
nullptr || transform.
kinematics !=
nullptr) {
1575 if(!std::isfinite(max_joint_step) || max_joint_step <= 0.0 ||
1576 axes_.size() < 6 || axes_.size() >
MaxAxes ||
1582 if(transform.
kinematics ==
nullptr || transform.
pose !=
nullptr ||
1597 double min_singularity_margin,
1598 double max_joint_step)
1601 transform, min_singularity_margin, max_joint_step);
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;
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;
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;
1622 pose_frames_.translation_path_differential_ =
nullptr;
1635 for(
double value : transform.value) {
1645 coordinate_system, 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];
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];
1667inline void AxisGroup::snapshot_active_tool_transform()
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_;
1674inline bool AxisGroup::active_tool_transform_applies()
const
1680inline geom::Vec3 AxisGroup::cartesian_part(
const GroupPosition &position)
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};
1689 switch(
command.orientation_mode) {
1694 if(pose_frames_.pose_kinematics_ ==
nullptr) {
1708inline void AxisGroup::store_cartesian_part(
GroupPosition &position, geom::Vec3 point)
1710 position.value[0] = point.x;
1711 if(position.size > 1) {
1712 position.value[1] = point.y;
1714 if(position.size > 2) {
1715 position.value[2] = point.z;
1721 switch(
command.coord_system) {
1742 if(pose_frames_.pose_kinematics_ !=
nullptr) {
1753 target =
geom::compose(pose_frames_.workpiece_frame_, target);
1755 const geom::RigidTransform flange =
geom::compose(target, pose_frames_.pose_tool_inverse_);
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];
1768 for(std::size_t i = 0; i < axes_.size(); ++i) {
1769 seed[i] = queued_finish(i);
1773 pose_frames_.pose_kinematics_->inverse(
pose, seed, pose_frames_.pose_max_joint_step_, joints);
1777 if(pose_frames_.pose_kinematics_->singularity_margin(joints) < pose_frames_.pose_min_margin_) {
1780 for(std::size_t i = 0; i < axes_.size(); ++i) {
1781 command.target.value[i] = joints[i];
1783 command.target.size = axes_.size();
1795 if(pose_frames_.kinematics_ !=
nullptr) {
1797 for(std::size_t i = 0; i < axes_.size(); ++i) {
1798 seed[i] = queued_finish(i);
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) {
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;
1832 longest = longest * std::sqrt(scaled_sq);
1835 constexpr int Samples = 16;
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) {
1844 seed[i] + fraction * (
command.target.value[i] - seed[i]);
1848 pose_frames_.kinematics_->forward(joints, axes_.size(),
cartesian);
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;
1863 if(worst_ratio > 0.0) {
1864 const double allowed = pose_frames_.cartesian_velocity_limit_ / worst_ratio;
1865 if(allowed <
command.velocity) {
1875 geom::Vec3 direction = cartesian_part(
command.target);
1879 store_cartesian_part(
command.target, direction);
1881 geom::Vec3 aux = cartesian_part(
command.aux);
1885 store_cartesian_part(
command.aux, aux);
1888 geom::Vec3 point = cartesian_part(
command.target);
1892 store_cartesian_part(
command.target,
1893 point - pose_frames_.tool_offset_);
1895 geom::Vec3 aux = cartesian_part(
command.aux);
1899 store_cartesian_part(
command.aux, aux - pose_frames_.tool_offset_);
1909 const double *seed)
const
1911 geom::Vec3 point = cartesian_part(position);
1918 pose_frames_.kinematics_->forward(seed, axes_.size(), start);
1922 point = start + point;
1927 point = point - pose_frames_.tool_offset_;
1932 pose_frames_.kinematics_->inverse(point, seed, axes_.size(), joints);
1936 if(pose_frames_.kinematics_->singularity_margin(joints, axes_.size()) < pose_frames_.kinematics_min_margin_) {
1939 for(std::size_t i = 0; i < axes_.size(); ++i) {
1940 position.value[i] = joints[i];
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
double cartesian_takeover_tcp_tube_limit() 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)
geom::Vec3 tool_offset() const
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
KinTransformRef kin_transform() 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)
void tool_transform_rpy(double out[6]) const
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)
Vec3 transform_rotate(const RigidTransform &transform, Vec3 v)
Vec3 transform_point(const RigidTransform &transform, Vec3 p)
Vec3 path_derivative(LineSegment line)
RigidTransform invert(const RigidTransform &transform)
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)
RigidTransform compose(const RigidTransform &a, const RigidTransform &b)
State1D sample(const Profile1D &profile, rt::CycleTick tick)
double rotation_start[3][3]
geom::QuinticBlendSegment corner
std::array< double, MaxAxes > value