12 command.command_id = next_command_id_++;
18 if(
command.relative || !queue_.empty() || joint_window_.active_) {
21 const bool is_pose = pose_frames_.pose_kinematics_ !=
nullptr;
22 if(!is_pose && pose_frames_.kinematics_ ==
nullptr) {
25 if(is_pose && !pose_window_enabled_) {
28 if(
command.tool_number != active_tool_ ||
29 command.payload_number != active_payload_) {
32 command.transition_velocity = 0.0;
33 command.transition_parameter = 0.0;
42 last_blend_degraded_id_ =
command.command_id;
45 const bool extend = cartesian_.window_active_;
51 active_command_.dynamic_pcs ||
52 cartesian_.takeover_active_ ||
53 cartesian_.active_segment_.arc_path || cartesian_.active_segment_.chain ||
54 cartesian_.active_segment_.angle_driven ||
55 cartesian_.active_segment_.pose != is_pose || active_path_length_ <= 0.0) {
58 }
else if(cartesian_.window_stopping_ || cartesian_.window_pose_ != is_pose) {
62 auto copy_rot = [](
const double src[3][3],
double dst[3][3]) {
63 for(
int r = 0;
r < 3; ++
r) {
64 for(
int c = 0; c < 3; ++c) {
65 dst[
r][c] = src[
r][c];
69 auto advance_rot = [](
const double start_r[3][3],
const double axis[3],
70 double angle,
double out_r[3][3]) {
71 double delta_r[3][3] = {};
77 geom::Vec3 target_point = cartesian_part(
command.target);
78 geom::RigidTransform target_pose{};
85 target_pose =
geom::compose(pose_frames_.workpiece_frame_, target_pose);
87 target_point = target_pose.translation;
92 target_point = target_point - pose_frames_.tool_offset_;
96 geom::Vec3 tail_end{};
97 geom::Vec3 tail_dir{};
98 double tail_room = 0.0;
99 std::size_t tail_index = 0;
100 double tail_end_rot[3][3] = {{1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {0.0, 0.0, 1.0}};
101 double live_entry_v = 0.0;
102 double live_entry_a = 0.0;
108 live_s = live.position < 0.0 ? 0.0 : live.position;
109 live_s = live_s > active_path_length_ ? active_path_length_ : live_s;
110 live_entry_v = live.velocity < 0.0 ? 0.0 : live.velocity;
111 live_entry_a = live.acceleration;
112 tail_end = geom::Vec3{cartesian_.active_segment_.start.x + cartesian_.active_segment_.delta.x,
113 cartesian_.active_segment_.start.y + cartesian_.active_segment_.delta.y,
114 cartesian_.active_segment_.start.z + cartesian_.active_segment_.delta.z};
115 const double len =
geom::norm(cartesian_.active_segment_.delta);
119 tail_dir = geom::Vec3{cartesian_.active_segment_.delta.x /
len,
120 cartesian_.active_segment_.delta.y /
len,
121 cartesian_.active_segment_.delta.z /
len};
122 tail_room = active_path_length_ - live_s;
124 advance_rot(cartesian_.active_segment_.rotation_start,
125 cartesian_.active_segment_.axis,
126 cartesian_.active_segment_.angle, tail_end_rot);
129 tail_index = cartesian_.window_.size() - 1;
130 const CartPiece &tail = cartesian_.window_[tail_index];
131 tail_end = geom::Vec3{tail.start.x + tail.dir.x * tail.length,
132 tail.start.y + tail.dir.y * tail.length,
133 tail.start.z + tail.dir.z * tail.length};
136 advance_rot(tail.rotation_start, tail.axis, tail.angle, tail_end_rot);
138 if(tail_index == cartesian_.piece_index_) {
140 cartesian_.window_[tail_index].profile,
142 live_s = live.position < 0.0 ? 0.0 : live.position;
143 live_s = live_s > tail.length ? tail.length : live_s;
144 live_entry_v = live.velocity < 0.0 ? 0.0 : live.velocity;
145 live_entry_a = live.acceleration;
146 tail_room = tail.length - live_s;
147 }
else if(tail_index > cartesian_.piece_index_) {
148 tail_room = tail.length;
155 copy_rot(tail_end_rot, target_pose.rotation);
157 double leg_axis[3] = {0.0, 0.0, 1.0};
158 double leg_angle = 0.0;
161 leg_axis, leg_angle);
162 if(leg_angle >= 3.14159265358979323846 - 1e-6) {
167 const geom::Vec3 out_vec = target_point - tail_end;
169 bool degrade =
false;
171 double corner_cap = 0.0;
172 geom::QuinticBlendSegment corner{};
173 bool passthrough =
false;
177 const geom::Vec3 t1{out_vec.x / len_b, out_vec.y / len_b,
180 tail_dir.x * t1.x + tail_dir.y * t1.y + tail_dir.z * t1.z;
183 }
else if(dot >= 1.0 - 1e-9) {
186 const geom::Vec3 diff{t1.x - tail_dir.x, t1.y - tail_dir.y,
189 trim =
command.transition_parameter * 96.0 / (23.0 * turn);
190 const double room = tail_room < len_b ? tail_room : len_b;
191 if(trim > 0.5 * room) {
194 if(trim <= 1e-9 || tail_room <= trim) {
197 const geom::Vec3 entry{tail_end.x - tail_dir.x *
trim,
198 tail_end.y - tail_dir.y *
trim,
199 tail_end.z - tail_dir.z *
trim};
200 const geom::Vec3 exit{tail_end.x + t1.x *
trim,
201 tail_end.y + t1.y *
trim,
202 tail_end.z + t1.z *
trim};
203 const rt::Result<geom::QuinticBlendSegment> blend =
209 corner = blend.value();
210 const double axis_accel =
214 corner_cap = corner.max_curvature > 1e-12
215 ? std::sqrt(axis_accel /
216 corner.max_curvature)
223 const std::size_t base_pieces = extend ? cartesian_.window_.size() : 1;
225 base_pieces + (passthrough ? 1 : 2) > CartWindowPieces) {
229 if(!degrade && !passthrough &&
command.transition_velocity > 0.0 &&
230 command.transition_velocity < corner_cap) {
231 corner_cap =
command.transition_velocity;
235 if(!degrade && extend && cartesian_.window_[cartesian_.piece_index_].corner) {
242 CartStorage &staging = cartesian_.staging_window_;
244 std::size_t stage_start_index = 0;
245 double stage_entry_v = 0.0;
246 double stage_entry_a = 0.0;
247 double stage_acc = 0.0;
248 double stage_dec = 0.0;
249 double stage_jerk = 0.0;
250 double entry_rot[3][3] = {{1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {0.0, 0.0, 1.0}};
254 first.pose = is_pose;
255 first.start = geom::Vec3{
256 cartesian_.active_segment_.start.x + tail_dir.x * live_s,
257 cartesian_.active_segment_.start.y + tail_dir.y * live_s,
258 cartesian_.active_segment_.start.z + tail_dir.z * live_s};
259 first.dir = tail_dir;
260 first.length = (active_path_length_ - live_s) - (passthrough ? 0.0 : trim);
261 first.cap = active_command_.velocity;
263 const double f_live = live_s / active_path_length_;
265 (active_path_length_ - (passthrough ? 0.0 :
trim)) /
267 advance_rot(cartesian_.active_segment_.rotation_start,
268 cartesian_.active_segment_.axis,
269 cartesian_.active_segment_.angle * f_live,
270 first.rotation_start);
271 for(
int d = 0;
d < 3; ++
d) {
272 first.axis[
d] = cartesian_.active_segment_.axis[
d];
274 first.angle = cartesian_.active_segment_.angle * (f_end - f_live);
275 advance_rot(first.rotation_start, first.axis, first.angle, entry_rot);
277 staging.push_back(first);
278 stage_start_index = 0;
279 stage_entry_v = live_entry_v;
280 stage_entry_a = live_entry_a;
281 stage_acc = active_command_.acceleration;
282 stage_dec = active_command_.deceleration;
283 stage_jerk = active_command_.jerk;
285 staging.copy_from(cartesian_.window_);
286 stage_start_index = cartesian_.piece_index_;
287 const std::size_t stage_tail_index = tail_index;
288 CartPiece &
current = staging[stage_start_index];
291 double s_cur = live.position < 0.0 ? 0.0 : live.position;
293 const double orig_cur_len =
current.length;
298 if(is_pose && orig_cur_len > 1e-12) {
299 const double f_cur = s_cur / orig_cur_len;
300 double next_r[3][3] = {};
302 current.angle * f_cur, next_r);
303 copy_rot(next_r,
current.rotation_start);
304 current.angle *= (1.0 - f_cur);
306 stage_entry_v = live.velocity < 0.0 ? 0.0 : live.velocity;
307 stage_entry_a = live.acceleration;
308 stage_acc = cartesian_.window_acceleration_;
309 stage_dec = cartesian_.window_deceleration_;
310 stage_jerk = cartesian_.window_jerk_;
312 CartPiece &tail_piece = staging[stage_tail_index];
313 const double pre_trim_len = tail_piece.length;
315 tail_piece.length -=
trim;
316 if(is_pose && pre_trim_len > 1e-12) {
317 const double f_keep = tail_piece.length / pre_trim_len;
318 tail_piece.angle *= f_keep;
322 advance_rot(tail_piece.rotation_start, tail_piece.axis,
323 tail_piece.angle, entry_rot);
328 CartPiece corner_piece{};
329 CartPiece line_piece{};
330 const geom::Vec3 t1{out_vec.x / len_b, out_vec.y / len_b,
333 corner_piece.corner =
true;
334 corner_piece.pose = is_pose;
335 corner_piece.blend = corner;
336 corner_piece.length = corner.length;
337 corner_piece.cap = corner_cap;
339 const double f_exit =
trim / len_b;
340 double exit_rot[3][3] = {};
341 advance_rot(tail_end_rot, leg_axis, leg_angle * f_exit, exit_rot);
342 copy_rot(entry_rot, corner_piece.rotation_start);
344 corner_piece.axis, corner_piece.angle);
345 if(corner_piece.angle >= 3.14159265358979323846 - 1e-6) {
349 line_piece.pose =
true;
350 copy_rot(exit_rot, line_piece.rotation_start);
351 for(
int d = 0;
d < 3; ++
d) {
352 line_piece.axis[
d] = leg_axis[
d];
354 line_piece.angle = leg_angle * (1.0 - f_exit);
357 line_piece.pose =
true;
358 copy_rot(tail_end_rot, line_piece.rotation_start);
359 for(
int d = 0;
d < 3; ++
d) {
360 line_piece.axis[
d] = leg_axis[
d];
362 line_piece.angle = leg_angle;
364 line_piece.start = passthrough
366 : geom::Vec3{tail_end.x + t1.x *
trim,
367 tail_end.y + t1.y *
trim,
368 tail_end.z + t1.z *
trim};
370 line_piece.length = passthrough ? len_b : len_b -
trim;
371 line_piece.cap =
command.velocity;
372 if(passthrough &&
command.transition_velocity > 0.0 &&
373 command.transition_velocity < line_piece.cap) {
374 line_piece.cap =
command.transition_velocity;
381 for(std::size_t i = 0; i < axes_.size(); ++i) {
382 chain[i] = active_finish_[i];
385 for(std::size_t i = 0; i < axes_.size(); ++i) {
390 auto solve_sample_ik = [&](
const geom::RigidTransform &tcp,
391 const double *seed_in,
394 const geom::RigidTransform flange =
397 pose.position[0] = flange.translation.x;
398 pose.position[1] = flange.translation.y;
399 pose.position[2] = flange.translation.z;
400 copy_rot(flange.rotation,
pose.rotation);
402 pose, seed_in, pose_frames_.pose_max_joint_step_, q_out);
406 if(pose_frames_.pose_kinematics_->singularity_margin(q_out) <
407 pose_frames_.pose_min_margin_) {
412 tcp.translation, seed_in, axes_.size(), q_out);
416 if(pose_frames_.kinematics_->singularity_margin(q_out, axes_.size()) <
417 pose_frames_.kinematics_min_margin_) {
422 gp.size = axes_.size();
423 for(std::size_t i = 0; i < axes_.size(); ++i) {
424 gp.value[i] = q_out[i];
426 return preflight_member_targets(gp);
432 constexpr int BackSteps = 8;
434 double entry_axis[3] = {0.0, 0.0, 1.0};
435 double entry_ang = 0.0;
438 entry_axis, entry_ang);
440 for(
int k = 1; k <= BackSteps; ++k) {
442 static_cast<double>(k) /
static_cast<double>(BackSteps);
443 geom::RigidTransform tcp{};
444 tcp.translation = geom::Vec3{tail_end.x - tail_dir.x * (
trim * frac),
445 tail_end.y - tail_dir.y * (trim * frac),
446 tail_end.z - tail_dir.z * (
trim * frac)};
448 advance_rot(tail_end_rot, entry_axis, entry_ang * frac,
451 const rt::ErrorCode solved = solve_sample_ik(tcp, chain, q_back);
455 for(std::size_t i = 0; i < axes_.size(); ++i) {
456 chain[i] = q_back[i];
461 auto prevalidate_piece = [&](CartPiece &piece) ->
rt::ErrorCode {
462 constexpr int Samples = 32;
464 double worst_step = 0.0;
465 for(
int k = 0; k <= Samples; ++k) {
467 piece.length *
static_cast<double>(k) /
static_cast<double>(Samples);
468 geom::RigidTransform tcp{};
470 cart_piece_pose(piece, s, tcp);
472 tcp.translation = cart_piece_point(piece, s);
479 for(std::size_t i = 0; i < axes_.size(); ++i) {
480 const double step = std::fabs(q[i] - chain[i]);
481 if(step > worst_step) {
486 for(std::size_t i = 0; i < axes_.size(); ++i) {
490 if(is_pose && worst_step > 0.0 && piece.length > 0.0) {
491 const double per_unit =
492 worst_step / (piece.length /
static_cast<double>(Samples));
493 const double allowed =
494 0.5 * pose_frames_.pose_max_joint_step_ / per_unit;
495 if(allowed < piece.cap) {
503 const rt::ErrorCode corner_ok = prevalidate_piece(corner_piece);
507 staging.push_back(corner_piece);
513 staging.push_back(line_piece);
515 stage_acc = stage_acc <
command.acceleration ? stage_acc :
command.acceleration;
516 stage_dec = stage_dec <
command.deceleration ? stage_dec :
command.deceleration;
517 stage_jerk = stage_jerk <
command.jerk ? stage_jerk :
command.jerk;
519 if(!cart_window_rebuild_storage(staging, stage_start_index, stage_entry_v,
520 stage_entry_a, stage_acc, stage_dec,
526 cartesian_.window_.swap(staging);
529 for(std::size_t i = 0; i < axes_.size(); ++i) {
530 cartesian_.window_joints_[i] = cartesian_.joints_[i];
533 cartesian_.window_active_ =
true;
534 cartesian_.window_pose_ = is_pose;
535 cartesian_.piece_index_ = 0;
537 cartesian_.piece_tick_ = 0;
538 cartesian_.window_entry_velocity_ = stage_entry_v;
539 cartesian_.window_entry_acceleration_ = stage_entry_a;
540 cartesian_.window_acceleration_ = stage_acc;
541 cartesian_.window_deceleration_ = stage_dec;
542 cartesian_.window_jerk_ = stage_jerk;
543 for(std::size_t i = 0; i < axes_.size(); ++i) {
546 cartesian_.window_last_id_ =
command.command_id;
557 command.transition_velocity = 0.0;
558 command.transition_parameter = 0.0;
567 last_blend_degraded_id_ =
command.command_id;
573 if(!std::isfinite(length) || length < 0.0) {
576 auto state_ok = [&](
double pos,
double vel) ->
bool {
577 return std::isfinite(pos) && std::isfinite(vel) &&
578 vel >= -1e-9 && pos >= -1e-7 && pos <= length + 1e-7;
580 auto insert_sorted_unique = [](
double *arr,
int &cnt,
int cap,
double val) {
581 for(
int i = 0; i < cnt; ++i) {
582 if(std::fabs(arr[i] - val) <= 1e-14) {
590 while(pos > 0 && arr[pos - 1] > val) {
591 arr[pos] = arr[pos - 1];
597 for(std::size_t idx = 0; idx < p.segment_count(); ++idx) {
601 !std::isfinite(seg.
c1) || !std::isfinite(seg.
c2) ||
602 !std::isfinite(seg.
c3) || !std::isfinite(seg.
c4) ||
603 !std::isfinite(seg.
c5)) {
619 const double d0 = seg.
c0;
620 const double d1 = seg.
c1 * dur_d;
621 const double d2 = (seg.
c2 * dur_d) * dur_d;
622 const double d3 = ((seg.
c3 * dur_d) * dur_d) * dur_d;
623 const double d4 = (((seg.
c4 * dur_d) * dur_d) * dur_d) * dur_d;
624 const double d5 = ((((seg.
c5 * dur_d) * dur_d) * dur_d) * dur_d) * dur_d;
625 const double v0 = seg.
c1;
626 const double v1 = 2.0 * (seg.
c2 * dur_d);
627 const double v2 = 3.0 * ((seg.
c3 * dur_d) * dur_d);
628 const double v3 = 4.0 * (((seg.
c4 * dur_d) * dur_d) * dur_d);
629 const double v4 = 5.0 * ((((seg.
c5 * dur_d) * dur_d) * dur_d) * dur_d);
630 if(!std::isfinite(d1) || !std::isfinite(d2) || !std::isfinite(d3) ||
631 !std::isfinite(d4) || !std::isfinite(d5) || !std::isfinite(v1) ||
632 !std::isfinite(v2) || !std::isfinite(v3) || !std::isfinite(v4)) {
635 auto eval_p_u = [&](
double u) ->
double {
636 return d0 + u * (d1 + u * (d2 + u * (d3 + u * (d4 + u * d5))));
638 auto eval_v_u = [&](
double u) ->
double {
639 return v0 + u * (v1 + u * (v2 + u * (v3 + u * v4)));
641 auto eval_a_u = [&](
double u) ->
double {
642 return v1 + u * (2.0 * v2 + u * (3.0 * v3 + u * (4.0 * v4)));
644 auto check_u = [&](
double u) ->
bool {
645 if(!std::isfinite(u) || u <= 0.0 || u >= 1.0) {
648 if(!state_ok(eval_p_u(u), eval_v_u(u))) {
651 const double x = u * dur_d;
652 const double xf = std::floor(x);
653 if(xf > 0.0 && xf < dur_d) {
654 const double uf = xf / dur_d;
655 if(!state_ok(eval_p_u(uf), eval_v_u(uf)))
return false;
657 const double xc = std::ceil(x);
658 if(xc > 0.0 && xc < dur_d) {
659 const double uc = xc / dur_d;
660 if(!state_ok(eval_p_u(uc), eval_v_u(uc)))
return false;
667 double a_breaks[4] = {0.0, 1.0, 0.0, 0.0};
669 const double qa = 12.0 * v4;
670 const double qb = 6.0 * v3;
671 const double qc = 2.0 * v2;
672 double j_scale = std::fabs(qa);
673 if(std::fabs(qb) > j_scale) j_scale = std::fabs(qb);
674 if(std::fabs(qc) > j_scale) j_scale = std::fabs(qc);
676 if(std::fabs(qa) > 1.0e-15 * j_scale) {
677 const double disc = qb * qb - 4.0 * qa * qc;
678 if(std::isfinite(disc) && disc >= 0.0) {
679 const double sdisc = std::sqrt(disc);
680 const double q_tmp = -0.5 * (qb + (qb >= 0.0 ? sdisc : -sdisc));
681 const double r1 = q_tmp / qa;
682 const double r2 = q_tmp != 0.0 ? (qc / q_tmp) : 0.0;
683 if(std::isfinite(r1) && r1 > 0.0 && r1 < 1.0) {
684 insert_sorted_unique(a_breaks, a_cnt, 4, r1);
686 if(std::isfinite(r2) && r2 > 0.0 && r2 < 1.0) {
687 insert_sorted_unique(a_breaks, a_cnt, 4, r2);
691 const double rv = -qb / (2.0 * qa);
692 if(std::isfinite(rv) && rv > 0.0 && rv < 1.0) {
693 insert_sorted_unique(a_breaks, a_cnt, 4, rv);
696 }
else if(std::fabs(qb) > 1.0e-15 * j_scale) {
697 const double r1 = -qc / qb;
698 if(std::isfinite(r1) && r1 > 0.0 && r1 < 1.0) {
699 insert_sorted_unique(a_breaks, a_cnt, 4, r1);
707 double v_breaks[8] = {0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
709 for(
int k = 1; k + 1 < a_cnt; ++k) {
710 insert_sorted_unique(v_breaks, v_cnt, 8, a_breaks[k]);
712 for(
int k = 0; k + 1 < a_cnt; ++k) {
713 double lo = a_breaks[k];
714 double hi = a_breaks[k + 1];
715 const double alo = eval_a_u(lo);
716 const double ahi = eval_a_u(hi);
717 if(!std::isfinite(alo) || !std::isfinite(ahi)) {
720 if((alo < 0.0 && ahi > 0.0) || (alo > 0.0 && ahi < 0.0)) {
722 for(
int it = 0; it < 56; ++it) {
723 const double mid = 0.5 * (lo + hi);
724 const double fm = eval_a_u(mid);
725 if(!std::isfinite(fm))
return false;
726 if((fl < 0.0 && fm <= 0.0) || (fl > 0.0 && fm >= 0.0)) {
733 insert_sorted_unique(v_breaks, v_cnt, 8, 0.5 * (lo + hi));
739 for(
int k = 1; k + 1 < v_cnt; ++k) {
740 if(!check_u(v_breaks[k]))
return false;
742 for(
int k = 0; k + 1 < v_cnt; ++k) {
743 double lo = v_breaks[k];
744 double hi = v_breaks[k + 1];
745 const double vlo = eval_v_u(lo);
746 const double vhi = eval_v_u(hi);
747 if(!std::isfinite(vlo) || !std::isfinite(vhi)) {
750 if((vlo < 0.0 && vhi > 0.0) || (vlo > 0.0 && vhi < 0.0)) {
752 for(
int it = 0; it < 56; ++it) {
753 const double mid = 0.5 * (lo + hi);
754 const double fm = eval_v_u(mid);
755 if(!std::isfinite(fm))
return false;
756 if((fl < 0.0 && fm <= 0.0) || (fl > 0.0 && fm >= 0.0)) {
763 if(!check_u(0.5 * (lo + hi)))
return false;
772inline bool AxisGroup::cart_window_rebuild_storage(CartStorage &storage,
773 std::size_t start_index,
774 double entry_velocity,
775 double entry_acceleration,
780 const std::size_t count = storage.size();
781 if(start_index >= count) {
786 double v = entry_velocity;
787 for(std::size_t i = start_index; i < count; ++i) {
788 CartPiece &piece = storage[i];
790 v = v < piece.cap ? v : piece.cap;
797 reach = reach < piece.cap ? reach : piece.cap;
798 if(pose_frames_.cartesian_velocity_limit_ > 0.0 &&
799 reach > pose_frames_.cartesian_velocity_limit_) {
800 reach = pose_frames_.cartesian_velocity_limit_;
807 for(std::size_t r = count;
r > start_index; --
r) {
808 CartPiece &piece = storage[
r - 1];
810 v = v < piece.cap ? v : piece.cap;
811 piece.v_out = piece.v_out < v ? piece.v_out : v;
812 piece.v_in = piece.v_out;
816 piece.v_out = piece.v_out < v ? piece.v_out : v;
819 piece.v_in = piece.v_in < reach ? piece.v_in : reach;
822 constexpr double MaxWindowCycles = 1.0e15;
824 for(std::size_t i = start_index; i <
count; ++i) {
825 CartPiece &piece = storage[i];
827 if(piece.length <= 0.0) {
831 if(!std::isfinite(piece.v_in) || piece.v_in <= 0.0) {
834 const double raw_cycles = piece.length / piece.v_in;
835 if(!std::isfinite(raw_cycles) || raw_cycles < 0.0 ||
836 raw_cycles > MaxWindowCycles) {
839 const std::int64_t ceiled =
840 static_cast<std::int64_t
>(std::ceil(raw_cycles));
841 piece.duration = ceiled < 1 ? 1 : ceiled;
844 const bool live_entry = i == start_index;
845 const double entry_v = live_entry ? entry_velocity : piece.v_in;
846 const double entry_a = live_entry ? entry_acceleration : 0.0;
847 double cap = piece.cap;
848 if(pose_frames_.cartesian_velocity_limit_ > 0.0 &&
849 cap > pose_frames_.cartesian_velocity_limit_) {
850 cap = pose_frames_.cartesian_velocity_limit_;
859 const double v_coast =
860 entry_a > 0.0 ? entry_v + (entry_a * entry_a) / (2.0 * jerk) : entry_v;
861 const bool entry_overspeed = live_entry &&
862 (v_coast > cap + 1e-12 ||
863 v_coast > piece.v_out + 1e-12);
864 if(live_entry && entry_v > cap) {
867 const otg::Limits1D lim{cap, acc, dec, jerk};
869 {0.0, entry_v, entry_a}, {piece.length, piece.v_out, 0.0},
874 piece.profile = profile.value();
875 piece.duration = piece.profile.duration_cycles();
876 const bool initial_ok =
878 const double avg_v = entry_v > piece.v_out
879 ? entry_v : (piece.v_out > 1e-12 ? piece.v_out : entry_v);
881 const double raw_ideal = std::ceil(piece.length / avg_v);
882 if(std::isfinite(raw_ideal) && raw_ideal >= 1.0 &&
883 raw_ideal <= MaxWindowCycles) {
884 const std::int64_t ideal =
static_cast<std::int64_t
>(raw_ideal);
885 if(piece.duration > ideal + 4 || !initial_ok) {
886 for(std::int64_t t = ideal; t <= ideal + 4; ++t) {
887 const rt::Result<otg::Profile1D> ft =
889 {0.0, entry_v, entry_a},
890 {piece.length, piece.v_out, 0.0}, lim, t);
891 if(ft && (!entry_overspeed ||
893 piece.profile = ft.value();
894 piece.duration = ft.value().duration_cycles();
908inline geom::Vec3 AxisGroup::cart_piece_point(
const CartPiece &piece,
double s)
const
914 const double clamped =
s < 0.0 ? 0.0 : (
s > piece.length ? piece.length :
s);
915 return geom::Vec3{piece.start.x + piece.dir.x * clamped,
916 piece.start.y + piece.dir.y * clamped,
917 piece.start.z + piece.dir.z * clamped};
920inline void AxisGroup::cart_piece_pose(
const CartPiece &piece,
double s,
921 geom::RigidTransform &tcp)
const
923 tcp.translation = cart_piece_point(piece, s);
924 const double clamped =
s < 0.0 ? 0.0 : (
s > piece.length ? piece.length :
s);
925 const double frac = piece.length > 1e-15 ? clamped / piece.length : 1.0;
926 double rel[3][3] = {};
931inline bool AxisGroup::cart_window_emit(geom::Vec3 point)
935 pose_frames_.kinematics_->inverse(point, cartesian_.window_joints_, axes_.size(), q);
937 pose_frames_.kinematics_->singularity_margin(q, axes_.size()) <
938 pose_frames_.kinematics_min_margin_) {
942 cartesian_.last_error_ = solved;
944 set_group_error(solved);
947 for(std::size_t i = 0; i < axes_.size(); ++i) {
948 axes_[i]->set_synchronized_position(q[i]);
949 cartesian_.window_joints_[i] = q[i];
954inline bool AxisGroup::cart_window_emit_pose(
const geom::RigidTransform &tcp)
957 const geom::RigidTransform flange =
960 pose.position[0] = flange.translation.x;
961 pose.position[1] = flange.translation.y;
962 pose.position[2] = flange.translation.z;
963 for(
int r = 0;
r < 3; ++
r) {
964 for(
int c = 0; c < 3; ++c) {
965 pose.rotation[
r][c] = flange.rotation[
r][c];
968 rt::ErrorCode solved = pose_frames_.pose_kinematics_->inverse(
969 pose, cartesian_.window_joints_, pose_frames_.pose_max_joint_step_, q);
971 pose_frames_.pose_kinematics_->singularity_margin(q) <
972 pose_frames_.pose_min_margin_) {
976 cartesian_.last_error_ = solved;
978 set_group_error(solved);
981 for(std::size_t i = 0; i < axes_.size(); ++i) {
982 axes_[i]->set_synchronized_position(q[i]);
983 cartesian_.window_joints_[i] = q[i];
988inline void AxisGroup::cart_window_cycle()
990 if(cartesian_.window_stopping_) {
991 ++cartesian_.halt_tick_;
994 if(cartesian_.window_pose_) {
995 geom::RigidTransform tcp{};
996 cart_window_pose_at(cartesian_.halt_origin_ + state.position, tcp);
997 if(!cart_window_emit_pose(tcp)) {
1001 const geom::Vec3 point =
1002 cart_window_point_at(cartesian_.halt_origin_ + state.position);
1003 if(!cart_window_emit(point)) {
1007 if(cartesian_.halt_tick_ >= cartesian_.halt_duration_) {
1008 cart_window_reset();
1009 clear_axes_synchronized();
1011 start_next_queued();
1016 ++cartesian_.piece_tick_;
1017 CartPiece &piece = cartesian_.window_[cartesian_.piece_index_];
1020 s = piece.v_in *
static_cast<double>(cartesian_.piece_tick_);
1021 s =
s > piece.length ? piece.length :
s;
1027 if(cartesian_.window_pose_) {
1028 geom::RigidTransform tcp{};
1029 cart_piece_pose(piece, s, tcp);
1030 if(!cart_window_emit_pose(tcp)) {
1034 if(!cart_window_emit(cart_piece_point(piece, s))) {
1038 if(cartesian_.piece_tick_ >= piece.duration) {
1039 if(cartesian_.piece_index_ + 1 < cartesian_.window_.size()) {
1040 ++cartesian_.piece_index_;
1041 cartesian_.piece_tick_ = 0;
1043 const CartPiece &next = cartesian_.window_[cartesian_.piece_index_];
1044 cartesian_.window_entry_velocity_ = next.v_in;
1045 cartesian_.window_entry_acceleration_ = 0.0;
1047 cart_window_reset();
1048 clear_axes_synchronized();
1050 start_next_queued();
1056inline geom::Vec3 AxisGroup::cart_window_point_at(
double composite)
const
1058 double remaining = composite;
1059 for(std::size_t i = cartesian_.piece_index_; i < cartesian_.window_.size(); ++i) {
1060 const CartPiece &piece = cartesian_.window_[i];
1061 double offset = 0.0;
1062 if(i == cartesian_.piece_index_) {
1063 offset = cartesian_.halt_piece_offset_;
1065 const double available = piece.length - offset;
1066 if(remaining <= available) {
1067 return cart_piece_point(piece, offset + remaining);
1069 remaining -= available;
1071 const CartPiece &
last = cartesian_.window_[cartesian_.window_.size() - 1];
1072 return cart_piece_point(last,
last.length);
1075inline void AxisGroup::cart_window_pose_at(
double composite,
1076 geom::RigidTransform &tcp)
const
1078 double remaining = composite;
1079 for(std::size_t i = cartesian_.piece_index_; i < cartesian_.window_.size(); ++i) {
1080 const CartPiece &piece = cartesian_.window_[i];
1081 double offset = 0.0;
1082 if(i == cartesian_.piece_index_) {
1083 offset = cartesian_.halt_piece_offset_;
1085 const double available = piece.length - offset;
1086 if(remaining <= available) {
1087 cart_piece_pose(piece, offset + remaining, tcp);
1090 remaining -= available;
1092 const CartPiece &
last = cartesian_.window_[cartesian_.window_.size() - 1];
1093 cart_piece_pose(last,
last.length, tcp);
1096inline rt::ErrorCode AxisGroup::cart_window_stop(
double deceleration,
double jerk)
1098 if(!std::isfinite(deceleration) || deceleration <= 0.0 ||
1099 !std::isfinite(jerk) || jerk <= 0.0) {
1102 CartPiece &piece = cartesian_.window_[cartesian_.piece_index_];
1103 otg::State1D state{};
1105 double s = piece.v_in *
static_cast<double>(cartesian_.piece_tick_);
1106 s =
s > piece.length ? piece.length :
s;
1107 state = {
s, piece.v_in, 0.0};
1112 double remaining = piece.length - state.position;
1113 for(std::size_t i = cartesian_.piece_index_ + 1; i < cartesian_.window_.size(); ++i) {
1114 remaining += cartesian_.window_[i].length;
1116 const otg::Limits1D halt_limits{state.velocity > 1e-12 ? state.velocity
1118 deceleration, deceleration, jerk};
1119 double target = state.velocity * state.velocity / (2.0 * deceleration) +
1120 state.velocity * (deceleration / jerk);
1121 if(target > remaining) {
1124 rt::Result<otg::Profile1D>
halt =
1126 for(
int attempt = 0; attempt < 8; ++attempt) {
1128 {target, 0.0, 0.0}, halt_limits);
1129 if(
halt || target >= remaining) {
1132 target = target * 1.5 < remaining ? target * 1.5 : remaining;
1135 if(std::fabs(state.velocity) <= 1e-12 &&
1136 std::fabs(state.acceleration) <= 1e-12) {
1137 cart_window_reset();
1138 clear_axes_synchronized();
1147 cartesian_.halt_profile_ =
halt.value();
1148 cartesian_.halt_duration_ = cartesian_.halt_profile_.duration_cycles();
1149 cartesian_.halt_tick_ = 0;
1150 cartesian_.halt_origin_ = 0.0;
1151 cartesian_.halt_piece_offset_ = state.position;
1152 cartesian_.window_stopping_ =
true;
1158inline void AxisGroup::cart_window_reset()
1160 cartesian_.window_.clear();
1161 cartesian_.staging_window_.clear();
1162 cartesian_.window_active_ =
false;
1163 cartesian_.window_stopping_ =
false;
1164 cartesian_.window_pose_ =
false;
1165 cartesian_.piece_index_ = 0;
1166 cartesian_.piece_tick_ = 0;
1167 cartesian_.halt_tick_ = 0;
1168 cartesian_.halt_duration_ = 0;
1169 cartesian_.halt_origin_ = 0.0;
1170 cartesian_.halt_piece_offset_ = 0.0;
1183 command.command_id = next_command_id_++;
1190 (pose_frames_.kinematics_ ==
nullptr && pose_frames_.pose_kinematics_ ==
nullptr)) {
1198 active_command_.dynamic_pcs ||
1199 cartesian_.takeover_active_ ||
1200 cartesian_.active_segment_.arc_path || cartesian_.active_segment_.angle_driven ||
1201 cartesian_.active_segment_.chain || joint_window_.active_ || !queue_.empty() ||
1202 active_path_length_ <= 0.0) {
1207 geom::Vec3 target_point = cartesian_part(
command.target);
1208 geom::RigidTransform target_pose{};
1209 if(pose_frames_.pose_kinematics_ !=
nullptr) {
1215 target_pose =
geom::compose(pose_frames_.workpiece_frame_, target_pose);
1217 target_point = target_pose.translation;
1222 target_point = target_point - pose_frames_.tool_offset_;
1226 const otg::State1D live =
1228 const double s_live = live.position < 0.0
1230 : (live.position > active_path_length_
1231 ? active_path_length_
1233 const double remaining = active_path_length_ - s_live;
1234 const geom::Vec3 live_point =
1235 cartesian_point_at(cartesian_.active_segment_, s_live / active_path_length_);
1236 const geom::Vec3 corner_point = geom::Vec3{
1237 cartesian_.active_segment_.start.x + cartesian_.active_segment_.delta.x,
1238 cartesian_.active_segment_.start.y + cartesian_.active_segment_.delta.y,
1239 cartesian_.active_segment_.start.z + cartesian_.active_segment_.delta.z};
1240 const double active_len =
geom::norm(cartesian_.active_segment_.delta);
1241 if(active_len <= 1e-12) {
1244 const geom::Vec3 t0{cartesian_.active_segment_.delta.x / active_len,
1245 cartesian_.active_segment_.delta.y / active_len,
1246 cartesian_.active_segment_.delta.z / active_len};
1247 const geom::Vec3 out_vec = target_point - corner_point;
1249 const double dot = len_b > 1e-12
1250 ? (t0.x * out_vec.x + t0.y * out_vec.y +
1255 bool degrade =
false;
1257 bool passthrough =
false;
1258 if(len_b <= 1e-12 || dot <= -0.999) {
1260 }
else if(dot >= 1.0 - 1e-9) {
1263 const geom::Vec3 t1{out_vec.x / len_b, out_vec.y / len_b,
1266 trim =
command.transition_parameter * 96.0 / (23.0 * turn);
1267 const double room = remaining < len_b ? remaining : len_b;
1268 if(trim > 0.5 * room) {
1271 if(trim <= 1e-9 || remaining <= trim) {
1276 CartesianSegment segment{};
1277 rt::Result<otg::Profile1D> chain_profile =
1280 const geom::Vec3 t1{out_vec.x / len_b, out_vec.y / len_b,
1282 segment.pose = cartesian_.active_segment_.pose;
1283 segment.chain =
true;
1284 segment.start = live_point;
1287 segment.line1 = remaining;
1288 segment.exit_point = corner_point;
1290 const geom::Vec3 entry{corner_point.x - t0.x *
trim,
1291 corner_point.y - t0.y *
trim,
1292 corner_point.z - t0.z *
trim};
1293 const geom::Vec3 exit{corner_point.x + t1.x *
trim,
1294 corner_point.y + t1.y *
trim,
1295 corner_point.z + t1.z *
trim};
1296 const rt::Result<geom::QuinticBlendSegment> blend =
1298 command.transition_parameter);
1302 segment.corner = blend.value();
1303 segment.line1 = remaining -
trim;
1304 segment.exit_point = exit;
1308 segment.line2 = len_b -
trim;
1309 segment.delta = target_point - live_point;
1310 segment.length = segment.line1 + segment.corner.length + segment.line2;
1312 if(!degrade && segment.pose) {
1313 const geom::RigidTransform live_tcp = pose_start_tcp(cartesian_.joints_);
1315 for(
int i = 0; i < 3; ++i) {
1316 for(
int j = 0; j < 3; ++j) {
1317 target_pose.rotation[i][j] = live_tcp.rotation[i][j];
1321 for(
int i = 0; i < 3; ++i) {
1322 for(
int j = 0; j < 3; ++j) {
1323 segment.rotation_start[i][j] = live_tcp.rotation[i][j];
1327 segment.axis, segment.angle);
1328 if(segment.angle >= 3.14159265358979323846 - 1e-6) {
1338 fused.velocity = fused.velocity < active_command_.velocity
1340 : active_command_.velocity;
1341 fused.acceleration = fused.acceleration < active_command_.acceleration
1342 ? fused.acceleration
1343 : active_command_.acceleration;
1344 fused.deceleration = fused.deceleration < active_command_.deceleration
1345 ? fused.deceleration
1346 : active_command_.deceleration;
1348 fused.jerk < active_command_.jerk ? fused.jerk : active_command_.jerk;
1349 if(
command.transition_velocity > 0.0 &&
1350 command.transition_velocity < fused.velocity) {
1353 fused.velocity =
command.transition_velocity;
1355 if(segment.corner.max_curvature > 1e-12) {
1356 const double axis_accel =
1357 fused.acceleration < fused.deceleration ? fused.acceleration
1358 : fused.deceleration;
1359 const double cap = std::sqrt(axis_accel / segment.corner.max_curvature);
1360 if(cap < fused.velocity) {
1361 fused.velocity = cap;
1364 double chain_seed[
MaxAxes] = {};
1365 for(std::size_t i = 0; i < axes_.size(); ++i) {
1366 chain_seed[i] = cartesian_.joints_[i];
1369 prevalidate_cartesian(fused, segment, chain_seed);
1374 {0.0, live.velocity, live.acceleration},
1375 {fused.cart.length, 0.0, 0.0},
1376 {fused.velocity, fused.acceleration, fused.deceleration, fused.jerk});
1377 if(!chain_profile) {
1384 {0.0, 0.0, 0.0}, {len_b, 0.0, 0.0},
1388 const std::int64_t baseline = (active_duration_ - active_tick_) +
1389 tail.value().duration_cycles();
1390 if(chain_profile.value().duration_cycles() >= baseline) {
1398 active_command_ = fused;
1399 cartesian_.active_segment_ = fused.cart;
1401 active_arc_ = geom::ArcSegment{};
1402 active_path_length_ = fused.cart.length;
1403 active_profile_ = chain_profile.value();
1405 active_duration_ = active_profile_.duration_cycles();
1406 for(std::size_t i = 0; i < axes_.size(); ++i) {
1407 active_start_[i] = axes_[i]->snapshot().command_position;
1408 active_finish_[i] = fused.target.value[i];
1418 command.transition_velocity = 0.0;
1419 command.transition_parameter = 0.0;
1428 last_blend_degraded_id_ =
command.command_id;
1439 segment_start_joints(
command, chain);
1441 CartesianSegment segment{};
1442 if(pose_frames_.pose_kinematics_ !=
nullptr) {
1443 segment.pose =
true;
1448 const geom::RigidTransform start = pose_start_tcp(chain);
1449 geom::RigidTransform target = requested;
1451 geom::Vec3 displacement = requested.translation;
1455 target.translation = start.translation + displacement;
1457 for(
int i = 0; i < 3; ++i) {
1458 for(
int j = 0; j < 3; ++j) {
1459 target.rotation[i][j] = start.rotation[i][j];
1468 target =
geom::compose(pose_frames_.workpiece_frame_, target);
1471 for(
int i = 0; i < 3; ++i) {
1472 for(
int j = 0; j < 3; ++j) {
1473 target.rotation[i][j] = start.rotation[i][j];
1478 segment.start = start.translation;
1479 segment.delta = target.translation - start.translation;
1480 for(
int i = 0; i < 3; ++i) {
1481 for(
int j = 0; j < 3; ++j) {
1482 segment.rotation_start[i][j] = start.rotation[i][j];
1487 if(segment.angle >= 3.14159265358979323846 - 1e-6) {
1491 if(segment.length < 1e-12 && segment.angle > 0.0) {
1492 segment.angle_driven =
true;
1493 segment.length = segment.angle;
1496 geom::Vec3 point = cartesian_part(
command.target);
1499 pose_frames_.kinematics_->forward(chain, axes_.size(), start);
1507 point = start + point;
1512 point = point - pose_frames_.tool_offset_;
1514 segment.start = start;
1515 segment.delta = point - start;
1519 return prevalidate_cartesian(
command, segment, chain);
1533 if(spatial && pose_frames_.pose_kinematics_ ==
nullptr &&
1534 (pose_frames_.kinematics_ ==
nullptr ||
1535 pose_frames_.kinematics_->cartesian_count() != 3)) {
1540 segment_start_joints(
command, chain);
1542 CartesianSegment segment{};
1544 geom::Vec3 aux_point =
1546 geom::Vec3 target_point = cartesian_part(
command.target);
1547 if(pose_frames_.pose_kinematics_ !=
nullptr) {
1548 segment.pose =
true;
1553 const geom::RigidTransform start = pose_start_tcp(chain);
1555 geom::RigidTransform target = requested;
1557 geom::Vec3 target_delta = requested.translation;
1564 target.translation = start.translation + target_delta;
1566 aux_point = start.translation + aux_point;
1569 for(
int i = 0; i < 3; ++i) {
1570 for(
int j = 0; j < 3; ++j) {
1571 target.rotation[i][j] = start.rotation[i][j];
1580 target =
geom::compose(pose_frames_.workpiece_frame_, target);
1586 for(
int i = 0; i < 3; ++i) {
1587 for(
int j = 0; j < 3; ++j) {
1588 target.rotation[i][j] = start.rotation[i][j];
1593 target_point = target.translation;
1594 for(
int i = 0; i < 3; ++i) {
1595 for(
int j = 0; j < 3; ++j) {
1596 segment.rotation_start[i][j] = start.rotation[i][j];
1601 if(segment.angle >= 3.14159265358979323846 - 1e-6) {
1607 pose_frames_.kinematics_->forward(chain, axes_.size(), start);
1620 aux_point = start + aux_point;
1622 target_point = start + target_point;
1631 aux_point = aux_point - pose_frames_.tool_offset_;
1633 target_point = target_point - pose_frames_.tool_offset_;
1638 rt::Result<geom::ArcSegment>
arc =
1642 const geom::Vec3 normal_mcs =
1648 target_point, normal_mcs, ccw);
1651 target_point, normal_mcs, ccw);
1654 command.circ_radius, normal_mcs, ccw);
1661 const geom::Vec3 plane_finish{target_point.x, target_point.y, 0.0};
1663 const geom::Vec3 plane_aux{aux_point.x, aux_point.y, 0.0};
1664 constexpr double PointTolerance = 1e-12;
1665 if(
geom::norm(plane_aux - plane_start) <= PointTolerance ||
1666 geom::norm(plane_finish - plane_aux) <= PointTolerance ||
1667 geom::norm(plane_finish - plane_start) <= PointTolerance) {
1674 if(
arc.value().radius >
geom::norm(plane_finish - plane_start) * 1e6) {
1680 if(derived !=
command.path_choice) {
1686 arc.value().start_angle,
1688 const double fraction = via_sweep /
arc.value().sweep;
1691 if(std::fabs(aux_point.z - expected_z) >
command.tolerance) {
1696 const geom::Vec3 plane_center{aux_point.x, aux_point.y, 0.0};
1710 segment.arc_path =
true;
1711 segment.arc =
arc.value();
1714 segment.length =
arc.value().length;
1716 return prevalidate_cartesian(
command, segment, chain);
1730 command.transition_parameter != 0.0) {
1733 if(pose_frames_.kinematics_ ==
nullptr && pose_frames_.pose_kinematics_ ==
nullptr) {
1739inline void AxisGroup::segment_start_joints(
const GroupCommand &
command,
double *chain)
const
1742 for(std::size_t i = 0; i < axes_.size(); ++i) {
1743 chain[i] =
aborting ? axes_[i]->snapshot().command_position
1748inline geom::RigidTransform AxisGroup::pose_start_tcp(
const double *chain)
const
1750 kin::Pose6 flange{};
1751 pose_frames_.pose_kinematics_->forward(chain, flange);
1752 geom::RigidTransform start{};
1753 start.translation = geom::Vec3{flange.position[0], flange.position[1],
1754 flange.position[2]};
1755 for(
int i = 0; i < 3; ++i) {
1756 for(
int j = 0; j < 3; ++j) {
1757 start.rotation[i][j] = flange.rotation[i][j];
1769 constexpr int Samples = 32;
1771 double worst_step = 0.0;
1772 segment.interior_soft_limits_ok =
true;
1773 cartesian_.pending_takeover_grid_valid_ =
false;
1774 const bool enforce_companion_soft_limits =
1775 pose_frames_.translation_path_differential_ !=
nullptr &&
1776 !segment.pose && !segment.arc_path && !segment.chain;
1777 const bool record_takeover_grid =
1779 !segment.pose && !segment.arc_path && !segment.chain &&
1780 axes_.size() >= 2 && axes_.size() <= 3;
1782 for(
int k = 0; k <= Samples; ++k) {
1783 const double fraction =
1784 static_cast<double>(k) /
static_cast<double>(Samples);
1787 geom::RigidTransform tcp{};
1788 cartesian_pose_at(segment, fraction, tcp);
1789 const geom::RigidTransform flange_target =
1792 pose.position[0] = flange_target.translation.x;
1793 pose.position[1] = flange_target.translation.y;
1794 pose.position[2] = flange_target.translation.z;
1795 for(
int i = 0; i < 3; ++i) {
1796 for(
int j = 0; j < 3; ++j) {
1797 pose.rotation[i][j] = flange_target.rotation[i][j];
1800 solved = pose_frames_.pose_kinematics_->inverse(
pose, chain,
1801 pose_frames_.pose_max_joint_step_, q);
1803 pose_frames_.pose_kinematics_->singularity_margin(q) < pose_frames_.pose_min_margin_) {
1807 const geom::Vec3
sample = cartesian_point_at(segment, fraction);
1808 solved = pose_frames_.kinematics_->inverse(sample, chain, axes_.size(), q);
1810 pose_frames_.kinematics_->singularity_margin(q, axes_.size()) <
1811 pose_frames_.kinematics_min_margin_) {
1819 for(std::size_t i = 0; i < axes_.size(); ++i) {
1820 const double step = std::fabs(q[i] - chain[i]);
1821 if(step > worst_step) {
1826 for(std::size_t i = 0; i < axes_.size(); ++i) {
1828 axes_[i]->motion_limits(), q[i])) {
1829 segment.interior_soft_limits_ok =
false;
1830 if(enforce_companion_soft_limits) {
1835 if(record_takeover_grid) {
1836 cartesian_.pending_takeover_grid_[
static_cast<std::size_t
>(k)][i] = q[i];
1842 if(segment.pose && worst_step > 0.0 && segment.length > 0.0) {
1843 const double per_unit =
1844 worst_step / (segment.length /
static_cast<double>(Samples));
1845 const double allowed = 0.5 * pose_frames_.pose_max_joint_step_ / per_unit;
1846 if(allowed <
command.velocity) {
1850 for(std::size_t i = 0; i < axes_.size(); ++i) {
1851 command.target.value[i] = q[i];
1853 command.target.size = axes_.size();
1854 if(pose_frames_.cartesian_velocity_limit_ > 0.0 &&
1855 pose_frames_.cartesian_velocity_limit_ <
command.velocity) {
1856 command.velocity = pose_frames_.cartesian_velocity_limit_;
1863 cartesian_.pending_takeover_grid_valid_ =
true;
1868inline geom::Vec3 AxisGroup::cartesian_point_at(
const CartesianSegment &segment,
1869 double fraction)
const
1872 const double s = fraction * segment.length;
1873 if(s <= segment.line1) {
1874 return geom::Vec3{segment.start.x + segment.dir1.x *
s,
1875 segment.start.y + segment.dir1.y *
s,
1876 segment.start.z + segment.dir1.z *
s};
1878 const double in_corner =
s - segment.line1;
1879 if(in_corner <= segment.corner.length) {
1884 const double tail =
s - segment.line1 - segment.corner.length;
1885 return geom::Vec3{segment.exit_point.x + segment.dir2.x * tail,
1886 segment.exit_point.y + segment.dir2.y * tail,
1887 segment.exit_point.z + segment.dir2.z * tail};
1889 if(segment.arc_path) {
1891 geom::sample(segment.arc, fraction * segment.arc.length);
1892 if(!segment.arc.spatial) {
1893 point.z = segment.start.z + segment.delta.z * fraction;
1897 return geom::Vec3{segment.start.x + segment.delta.x * fraction,
1898 segment.start.y + segment.delta.y * fraction,
1899 segment.start.z + segment.delta.z * fraction};
1904 geom::RigidTransform &tcp)
const
1906 tcp.translation = cartesian_point_at(segment, fraction);
1917inline bool AxisGroup::cartesian_cycle(
double ratio)
1921 if(cartesian_.active_segment_.pose) {
1922 geom::RigidTransform tcp{};
1923 cartesian_pose_at(cartesian_.active_segment_, ratio, tcp);
1924 const geom::RigidTransform flange_target =
1927 pose.position[0] = flange_target.translation.x;
1928 pose.position[1] = flange_target.translation.y;
1929 pose.position[2] = flange_target.translation.z;
1930 for(
int i = 0; i < 3; ++i) {
1931 for(
int j = 0; j < 3; ++j) {
1932 pose.rotation[i][j] = flange_target.rotation[i][j];
1935 solved = pose_frames_.pose_kinematics_->inverse(
pose, cartesian_.joints_,
1936 pose_frames_.pose_max_joint_step_, q);
1938 pose_frames_.pose_kinematics_->singularity_margin(q) < pose_frames_.pose_min_margin_) {
1942 const geom::Vec3 point = cartesian_point_at(cartesian_.active_segment_, ratio);
1943 solved = pose_frames_.kinematics_->inverse(point, cartesian_.joints_, axes_.size(), q);
1945 pose_frames_.kinematics_->singularity_margin(q, axes_.size()) <
1946 pose_frames_.kinematics_min_margin_) {
1951 cartesian_.last_error_ = solved;
1953 set_group_error(solved);
1956 if(cartesian_.takeover_active_) {
1960 for(std::size_t i = 0; i < axes_.size(); ++i) {
1961 const double offset =
1962 (i < 3 && cartesian_.takeover_residuals_[i].segment_count() > 0)
1967 axes_[i]->set_synchronized_position(q[i] + offset);
1968 cartesian_.joints_[i] = q[i];
1970 if(!cartesian_.takeover_stopping_ &&
1971 active_tick_ >= cartesian_.takeover_residual_duration_) {
1972 cartesian_.takeover_active_ =
false;
1973 cartesian_.takeover_tcp_tube_ = 0.0;
1977 for(std::size_t i = 0; i < axes_.size(); ++i) {
1978 axes_[i]->set_synchronized_position(q[i]);
1979 cartesian_.joints_[i] = q[i];
1990 cartesian_.clear_staged_takeover();
1991 const double longest =
command.cart.length;
1992 if(!
command.cart.interior_soft_limits_ok) {
1995 if(pose_frames_.kinematics_ ==
nullptr ||
1996 pose_frames_.pose_kinematics_ !=
nullptr ||
1997 pose_frames_.translation_path_differential_ ==
nullptr ||
1998 axes_.size() < 2 || axes_.size() > 3 ||
2003 CartesianTakeoverInput
input{};
2004 input.kinematics = pose_frames_.kinematics_;
2005 input.differential = pose_frames_.translation_path_differential_;
2006 input.min_singularity_margin = pose_frames_.kinematics_min_margin_;
2007 input.joint_count = axes_.size();
2009 input.unit_direction = {
2010 command.cart.delta.x / longest,
2011 command.cart.delta.y / longest,
2012 command.cart.delta.z / longest,
2014 input.path_length = longest;
2015 input.has_path_delta =
true;
2017 if(cartesian_.pending_takeover_grid_valid_) {
2018 input.has_prevalidated_grid =
true;
2019 input.prevalidated_grid = cartesian_.pending_takeover_grid_;
2021 input.has_continuous_seed = !connector_.captured_from_output_history();
2022 for(std::size_t i = 0; i < axes_.size(); ++i) {
2023 input.start_joints[i] = axes_[i]->snapshot().command_position;
2024 input.captured_velocity[i] = connector_.captured_output_velocity()[i];
2025 input.captured_acceleration[i] = connector_.captured_output_acceleration()[i];
2026 input.continuous_velocity[i] = connector_.captured_analytic_velocity()[i];
2027 input.continuous_acceleration[i] = connector_.captured_analytic_acceleration()[i];
2028 input.member_limits[i] = axes_[i]->motion_limits();
2030 input.base_command_limits = {
2032 input.max_tcp_tube = pose_frames_.cartesian_takeover_tcp_tube_limit_;
2034 CartesianTakeoverPlan
plan{};
2035 CartesianTakeoverCallCounts calls{};
2036 otg::Limits1D safe_limits{};
2037 bool safe_limits_proven =
false;
2039 input, plan, calls, &safe_limits, &safe_limits_proven);
2041 cartesian_.staged_takeover_valid_ =
true;
2042 cartesian_.staged_takeover_is_connector_ =
true;
2043 cartesian_.staged_takeover_has_safe_envelope_ = safe_limits_proven;
2044 cartesian_.staged_base_profile_ =
plan.base_profile;
2045 cartesian_.staged_base_duration_ =
plan.base_duration;
2046 cartesian_.staged_residuals_ =
plan.residual_profiles;
2047 cartesian_.staged_residual_duration_ =
plan.residual_duration;
2048 cartesian_.staged_tcp_tube_ =
plan.tcp_tube;
2049 if(safe_limits_proven) {
2050 cartesian_.staged_safe_limits_ = safe_limits;
2055 if(safe_limits_proven) {
2056 const rt::Result<otg::Profile1D> rest =
2061 cartesian_.staged_takeover_valid_ =
true;
2062 cartesian_.staged_takeover_is_connector_ =
false;
2063 cartesian_.staged_takeover_has_safe_envelope_ =
true;
2064 cartesian_.staged_base_profile_ = rest.value();
2065 cartesian_.staged_base_duration_ = rest.value().duration_cycles();
2066 cartesian_.staged_safe_limits_ = safe_limits;
2073 const rt::Result<otg::Profile1D> short_rest =
2075 constexpr std::size_t kMaxInvBudget =
2080 input, short_rest.value(), calls, kMaxInvBudget)) {
2081 cartesian_.staged_takeover_valid_ =
true;
2082 cartesian_.staged_takeover_is_connector_ =
false;
2083 cartesian_.staged_takeover_has_safe_envelope_ =
false;
2084 cartesian_.staged_base_profile_ = short_rest.value();
2085 cartesian_.staged_base_duration_ = short_rest.value().duration_cycles();
2094inline rt::ErrorCode AxisGroup::plan_cartesian_connector_stop(
double deceleration,
2097 if(!cartesian_.takeover_active_ || !active_ ||
2099 cartesian_.active_segment_.pose ||
2100 cartesian_.active_segment_.arc_path ||
2101 cartesian_.active_segment_.chain ||
2102 pose_frames_.kinematics_ ==
nullptr ||
2104 axes_.size() < 2 || axes_.size() > 3) {
2108 const otg::State1D live_base =
2110 std::array<double, MaxAxes> out_v{};
2111 std::array<double, MaxAxes> out_a{};
2112 current_member_output_state(out_v, out_a);
2114 CartesianTakeoverStopInput stop_in{};
2115 stop_in.kinematics = pose_frames_.kinematics_;
2116 stop_in.min_singularity_margin = pose_frames_.kinematics_min_margin_;
2117 stop_in.joint_count = axes_.size();
2118 stop_in.start_point = cartesian_.active_segment_.start;
2119 stop_in.unit_direction = {
2120 cartesian_.active_segment_.delta.x / active_path_length_,
2121 cartesian_.active_segment_.delta.y / active_path_length_,
2122 cartesian_.active_segment_.delta.z / active_path_length_,
2124 stop_in.path_length = active_path_length_;
2125 stop_in.has_path_delta =
true;
2126 stop_in.path_delta = cartesian_.active_segment_.delta;
2127 stop_in.live_base_state = live_base;
2128 for(std::size_t i = 0; i < axes_.size(); ++i) {
2129 stop_in.live_base_joints[i] = cartesian_.joints_[i];
2130 stop_in.live_output_joints[i] = axes_[i]->snapshot().command_position;
2131 stop_in.captured_velocity[i] = out_v[i];
2132 stop_in.captured_acceleration[i] = out_a[i];
2133 if(cartesian_.takeover_residuals_[i].segment_count() > 0) {
2135 cartesian_.takeover_residuals_[i],
2138 stop_in.live_residuals[i] = otg::State1D{};
2140 stop_in.member_limits[i] = axes_[i]->motion_limits();
2142 stop_in.base_stop_limits = {
2143 std::max(active_command_.velocity, std::fabs(live_base.velocity)),
2144 active_command_.acceleration,
2148 stop_in.max_tcp_tube = pose_frames_.cartesian_takeover_tcp_tube_limit_;
2150 CartesianTakeoverStopPlan stop_plan{};
2156 active_profile_ = stop_plan.base_profile;
2158 active_duration_ = stop_plan.duration;
2159 override_paused_ =
false;
2160 cartesian_.takeover_residuals_ = stop_plan.residual_profiles;
2161 cartesian_.takeover_residual_duration_ = stop_plan.duration;
2162 cartesian_.takeover_tcp_tube_ = stop_plan.tcp_tube;
2163 cartesian_.takeover_active_ =
true;
2164 cartesian_.takeover_stopping_ =
true;
rt::Result< std::uint32_t > halt(double deceleration=1.0, double jerk=1.0, BufferMode buffer_mode=BufferMode::aborting)
static constexpr std::size_t MaxAxes
static bool verify_profile_monotonic(const otg::Profile1D &p, double length)
double cart_tail_joints_[MaxAxes]
static bool inside_soft_limits(const MotionLimits &m, double pos)
static rt::Result< otg::Profile1D > plan_rest_base_profile(double path_length, const otg::Limits1D &limits)
static constexpr std::size_t MaxCandidates
static constexpr double StateZeroTolerance
static rt::ErrorCode plan_stop(const CartesianTakeoverStopInput &in, CartesianTakeoverStopPlan &plan_out)
static constexpr std::int64_t MaxReplayCycles
static bool verify_rest_start_replay(const CartesianTakeoverInput &in, const otg::Profile1D &rest_profile, CartesianTakeoverCallCounts &calls, std::size_t max_inv_budget)
static rt::ErrorCode plan(const CartesianTakeoverInput &in, CartesianTakeoverPlan &best_out, CartesianTakeoverCallCounts &calls, otg::Limits1D *safe_base_limits_out=nullptr, bool *safe_base_limits_proven_out=nullptr)
static constexpr CycleTick from_cycles(std::int64_t cycles)
static constexpr Result success(const T &value)
static constexpr Result failure(ErrorCode error)
Vec3 transform_rotate(const RigidTransform &transform, Vec3 v)
rt::Result< ArcSegment > make_arc_center_xy(Vec3 start, Vec3 center, Vec3 finish, bool counter_clockwise)
void rotation_multiply(const double a[3][3], const double b[3][3], double out[3][3])
Vec3 transform_point(const RigidTransform &transform, Vec3 p)
Vec3 sample(LineSegment line, double arclength)
rt::Result< ArcSegment > make_spatial_arc_center(Vec3 start, Vec3 center, Vec3 finish, Vec3 plane_normal, bool counter_clockwise)
double quintic_parameter_at_length(const QuinticBlendSegment &blend, double arclength)
double dot(Vec3 lhs, Vec3 rhs)
void rodrigues(const double axis[3], double angle, double out[3][3])
rt::Result< ArcSegment > make_arc_radius_xy(Vec3 start, Vec3 finish, double signed_radius, bool counter_clockwise)
rt::Result< ArcSegment > make_arc(Vec3 start, Vec3 via, Vec3 finish)
rt::Result< ArcSegment > make_spatial_arc_border(Vec3 start, Vec3 via, Vec3 finish, Vec3 ref_normal, bool counter_clockwise)
double angle_of(Vec3 point, Vec3 center)
RigidTransform make_rpy_transform(double x, double y, double z, double roll, double pitch, double yaw)
Vec3 quintic_point(const QuinticBlendSegment &blend, double u)
double normalize_sweep(double sweep, double orientation)
void relative_axis_angle(const double a[3][3], const double b[3][3], double axis[3], double &angle)
RigidTransform compose(const RigidTransform &a, const RigidTransform &b)
rt::Result< QuinticBlendSegment > make_quintic_blend(Vec3 start, Vec3 corner, Vec3 finish, double tolerance)
rt::Result< ArcSegment > make_spatial_arc_radius(Vec3 start, Vec3 finish, double signed_radius, Vec3 plane_normal, bool counter_clockwise)
rt::Result< Profile1D > plan(State1D from, Target1D to, Limits1D limits)
rt::Result< Profile1D > plan_time_optimal(State1D from, Target1D to, Limits1D limits)
rt::Result< Profile1D > solve_fixed_time(State1D from, Target1D to, Limits1D limits, std::int64_t total_cycles)
bool is_finite(State1D state)
State1D sample(const Profile1D &profile, rt::CycleTick tick)
double jerk_reachable_speed(double entry, double distance, double bound, double jerk)
constexpr std::size_t count
std::string trim(std::string_view text)
std::int64_t duration_cycles