plcopen 0.22.0
载入中...
搜索中...
未找到
group_takeover_connector.h
浏览该文件的文档.
1#pragma once
2
3#include <algorithm>
4#include <array>
5#include <cmath>
6#include <cstddef>
7#include <cstdint>
8
9#include "geom/geometry.h"
10#include "otg/profile1d.h"
11#include "otg/time_optimal.h"
12#include "rt/cycle.h"
13#include "rt/error.h"
14
15// Y7 aborting-takeover connector (KB-051/052/053 and Y7b1 joint-domain
16// circular extension). Extracted from AxisGroup as the first behavior
17// cluster of the split plan (doc/planning/axis-group-split-plan-2026-07-09):
18// state and planning/sampling logic move verbatim; AxisGroup keeps its
19// public API, error codes and cycle outputs bit-identical. The class owns
20// the captured pre-takeover velocity/acceleration vectors, the lateral
21// decay profile and the tolerance-tube radius; planning runs at submit
22// (planning domain), sampling is O(1) per cycle.
23
24namespace plcopen::core::axis
25{
26
27template <std::size_t MaxAxes>
29{
30public:
31 bool active() const
32 {
33 return active_;
34 }
35
36 double tube_radius() const
37 {
38 return active_ ? r_tube_ : 0.0;
39 }
40
41 bool vector_mode() const
42 {
43 return active_ && vector_mode_;
44 }
45
46 void sample_lateral_velocities(std::int64_t tick,
47 std::array<double, MaxAxes> &velocities,
48 std::size_t axis_count) const
49 {
50 velocities.fill(0.0);
51 if(!active_) {
52 return;
53 }
54 if(vector_mode_) {
55 for(std::size_t i = 0; i < axis_count; ++i) {
56 velocities[i] = otg::sample(lateral_profiles_[i],
58 .velocity;
59 }
60 return;
61 }
62 const double v_lat =
63 otg::sample(lateral_profile_, rt::CycleTick::from_cycles(tick)).velocity;
64 for(std::size_t i = 0; i < axis_count; ++i) {
65 velocities[i] = lateral_dir_[i] * v_lat;
66 }
67 }
68
70 {
71 return has_velocity_;
72 }
73
75 {
76 return captured_vector_state_;
77 }
78
80 {
81 return captured_output_history_only_;
82 }
83
84 const std::array<double, MaxAxes> &captured_output_velocity() const
85 {
86 return output_velocity_;
87 }
88
89 const std::array<double, MaxAxes> &captured_output_acceleration() const
90 {
91 return output_acceleration_;
92 }
93
94 const std::array<double, MaxAxes> &captured_analytic_velocity() const
95 {
96 return velocity_;
97 }
98
99 const std::array<double, MaxAxes> &captured_analytic_acceleration() const
100 {
101 return acceleration_;
102 }
103
105 {
106 active_ = false;
107 vector_mode_ = false;
108 }
109
111 {
112 has_velocity_ = false;
113 captured_vector_state_ = false;
114 captured_output_history_only_ = false;
115 }
116
117 // Single-shot handoff of the captured takeover state: reports whether a
118 // capture was pending and always clears the flag (the pre-refactor code
119 // cleared it unconditionally before deciding on the connector).
121 {
122 const bool had = has_velocity_;
123 has_velocity_ = false;
124 return had;
125 }
126
127 // AxisGroup emits positions at integer cycles. Keep the last two output
128 // differences separate from the analytical path derivatives: the latter
129 // seed the continuous profiles, while these values validate the first
130 // post-takeover discrete acceleration and jerk without a false boundary
131 // mismatch.
133 const std::array<double, MaxAxes> &velocity,
134 const std::array<double, MaxAxes> &acceleration,
135 std::size_t axis_count)
136 {
137 if(!has_velocity_) {
138 return;
139 }
140 for(std::size_t i = 0; i < axis_count; ++i) {
141 output_velocity_[i] = velocity[i];
142 output_acceleration_[i] = acceleration[i];
143 }
144 }
145
146 // Y7b2a: Cartesian sources have no cross-plugin analytical member
147 // derivatives, but AxisGroup already records their exact discrete member
148 // output history. Capture that history as both the connector initial state
149 // and its first-sample validation baseline. The joint-domain target still
150 // supplies the analytical path derivatives used by the planner.
152 bool motion_active,
153 const std::array<double, MaxAxes> &velocity,
154 const std::array<double, MaxAxes> &acceleration,
155 std::size_t axis_count)
156 {
157 has_velocity_ = false;
158 captured_vector_state_ = false;
159 if(!motion_active) {
160 return;
161 }
162 velocity_.fill(0.0);
163 acceleration_.fill(0.0);
164 output_velocity_.fill(0.0);
165 output_acceleration_.fill(0.0);
166 for(std::size_t i = 0; i < axis_count; ++i) {
167 velocity_[i] = velocity[i];
168 acceleration_[i] = acceleration[i];
169 output_velocity_[i] = velocity[i];
170 output_acceleration_[i] = acceleration[i];
171 }
172 captured_vector_state_ = true;
173 captured_output_history_only_ = true;
174 has_velocity_ = true;
175 }
176
177 // Y7 (KB-051/052 fix): capture the per-axis velocity AND acceleration
178 // vectors of the current plain linear motion before abort_motion()
179 // destroys it. KB-052: acceleration capture ensures a_s0 = <qdd, t_hat>
180 // continuity (v2.1 decision #1). During a connector, the composite
181 // state (along-path + lateral) is returned so re-entrant aborting
182 // decomposes correctly.
183 void capture(bool motion_active, bool linear_path, double path_length,
184 const otg::Profile1D &along_profile, std::int64_t tick,
185 const std::array<double, MaxAxes> &start,
186 const std::array<double, MaxAxes> &finish,
187 std::size_t axis_count)
188 {
189 has_velocity_ = false;
190 captured_vector_state_ = false;
191 if(!motion_active || !linear_path || path_length <= 0.0) {
192 return;
193 }
194 const otg::State1D along =
195 otg::sample(along_profile, rt::CycleTick::from_cycles(tick));
196 std::array<double, MaxAxes> q_s{};
197 std::array<double, MaxAxes> q_ss{};
198 for(std::size_t i = 0; i < axis_count; ++i) {
199 q_s[i] = (finish[i] - start[i]) / path_length;
200 }
201 captured_vector_state_ = active_ && vector_mode_;
202 captured_output_history_only_ = false;
203 capture_state(along, q_s, q_ss, tick, axis_count);
204 }
205
206 // Y7b1: circular paths are also scalar arc-length paths. The first two
207 // members use the analytical arc derivatives; higher members follow the
208 // arc fraction linearly.
209 void capture_circular(bool motion_active, const geom::ArcSegment &arc,
210 double path_length,
211 const otg::Profile1D &along_profile,
212 std::int64_t tick,
213 const std::array<double, MaxAxes> &start,
214 const std::array<double, MaxAxes> &finish,
215 std::size_t axis_count)
216 {
217 has_velocity_ = false;
218 captured_vector_state_ = false;
219 if(!motion_active || path_length <= 0.0 || axis_count < 2) {
220 return;
221 }
222 const otg::State1D along =
223 otg::sample(along_profile, rt::CycleTick::from_cycles(tick));
224 std::array<double, MaxAxes> q_s{};
225 std::array<double, MaxAxes> q_ss{};
226 circular_derivatives(arc, along.position, path_length, start, finish,
227 axis_count, q_s, q_ss, nullptr);
228 captured_vector_state_ = true;
229 captured_output_history_only_ = false;
230 capture_state(along, q_s, q_ss, tick, axis_count);
231 }
232
233private:
234 void capture_state(const otg::State1D &along,
235 const std::array<double, MaxAxes> &q_s,
236 const std::array<double, MaxAxes> &q_ss,
237 std::int64_t tick, std::size_t axis_count)
238 {
239 for(std::size_t i = 0; i < axis_count; ++i) {
240 velocity_[i] = along.velocity * q_s[i];
241 acceleration_[i] = q_ss[i] * along.velocity * along.velocity +
242 q_s[i] * along.acceleration;
243 }
244 if(active_ && vector_mode_) {
245 for(std::size_t i = 0; i < axis_count; ++i) {
246 const otg::State1D lat = otg::sample(
247 lateral_profiles_[i], rt::CycleTick::from_cycles(tick));
248 velocity_[i] += lat.velocity;
249 acceleration_[i] += lat.acceleration;
250 }
251 } else if(active_) {
252 const otg::State1D lat = otg::sample(
253 lateral_profile_, rt::CycleTick::from_cycles(tick));
254 for(std::size_t i = 0; i < axis_count; ++i) {
255 velocity_[i] += lat.velocity * lateral_dir_[i];
256 acceleration_[i] += lat.acceleration * lateral_dir_[i];
257 }
258 }
259 output_velocity_ = velocity_;
260 output_acceleration_ = acceleration_;
261 has_velocity_ = true;
262 }
263
264 rt::ErrorCode plan_synchronized_residual_profiles(
265 const otg::Limits1D &limits,
266 const std::array<otg::State1D, MaxAxes> &residuals,
267 std::size_t axis_count, double state_threshold,
268 std::array<otg::Profile1D, MaxAxes> &profiles,
269 std::int64_t &duration) const
270 {
271 duration = 0;
272 std::array<bool, MaxAxes> active_residuals{};
273 for(std::size_t i = 0; i < axis_count; ++i) {
274 const otg::State1D residual = residuals[i];
275 if(std::fabs(residual.position) <= state_threshold &&
276 std::fabs(residual.velocity) <= state_threshold &&
277 std::fabs(residual.acceleration) <= state_threshold) {
278 continue;
279 }
280 active_residuals[i] = true;
281 const rt::Result<otg::Profile1D> optimal =
282 otg::plan_time_optimal(residual, {0.0, 0.0, 0.0}, limits);
283 if(!optimal) {
284 return optimal.error();
285 }
286 duration = std::max(duration, optimal.value().duration_cycles());
287 }
288 if(duration == 0) {
289 profiles = {};
290 return rt::ErrorCode::ok;
291 }
292
293 std::array<otg::Profile1D, MaxAxes> planned{};
294 for(std::size_t i = 0; i < axis_count; ++i) {
295 if(!active_residuals[i]) {
296 continue;
297 }
298 const rt::Result<otg::Profile1D> fixed =
299 otg::solve_fixed_time(residuals[i], {0.0, 0.0, 0.0}, limits,
300 duration);
301 if(!fixed) {
302 return fixed.error();
303 }
304 planned[i] = fixed.value();
305 }
306 profiles = planned;
307 return rt::ErrorCode::ok;
308 }
309
310public:
311
312 // Y7 connector planner: decomposes the takeover velocity into along-path
313 // and lateral components, plans both profiles with beta-split limits.
314 // KB-052: projects acceleration onto new tangent for a_s0 continuity.
315 // KB-053: rejects connector when stopping distance exceeds path length.
316 // On the aligned path (zero connector) only the group's active profile
317 // is replanned and this object stays inactive.
318 rt::ErrorCode plan(const otg::Limits1D &limits, double longest,
319 std::size_t axis_count,
320 const std::array<double, MaxAxes> &start,
321 const std::array<double, MaxAxes> &finish,
322 otg::Profile1D &active_profile,
323 std::int64_t &active_duration)
324 {
325 constexpr double kBeta = 0.5;
326 constexpr double kAlignedThreshold = 1e-9;
327
328 // New scalar-path derivative. Linear group paths use the longest-axis
329 // parameter (KB-054), so this vector is not necessarily Euclidean
330 // unit length.
331 std::array<double, MaxAxes> q_s{};
332 if(longest > 0.0) {
333 for(std::size_t i = 0; i < axis_count; ++i) {
334 q_s[i] = (finish[i] - start[i]) / longest;
335 }
336 }
337
338 // Least-squares projection onto q_s. Dividing by |q_s|^2 is required
339 // for diagonal longest-axis paths, where q_s is not unit length.
340 double q_s_norm_sq = 0.0;
341 double s_dot_0 = 0.0;
342 double a_s0 = 0.0;
343 for(std::size_t i = 0; i < axis_count; ++i) {
344 q_s_norm_sq += q_s[i] * q_s[i];
345 s_dot_0 += velocity_[i] * q_s[i];
346 a_s0 += acceleration_[i] * q_s[i];
347 }
348 if(q_s_norm_sq > 0.0) {
349 s_dot_0 /= q_s_norm_sq;
350 a_s0 /= q_s_norm_sq;
351 }
352
353 // Lateral residual velocity.
354 double lat_speed_sq = 0.0;
355 std::array<double, MaxAxes> lat_vel{};
356 for(std::size_t i = 0; i < axis_count; ++i) {
357 lat_vel[i] = velocity_[i] - s_dot_0 * q_s[i];
358 lat_speed_sq += lat_vel[i] * lat_vel[i];
359 }
360 const double lat_speed = std::sqrt(lat_speed_sq);
361
362 if(lat_speed <= kAlignedThreshold) {
363 // Aligned takeover: no lateral residual, plan along-path with
364 // full limits from the projected velocity+acceleration (zero
365 // connector). KB-052: a_s0 included.
366 if(longest > 0.0) {
368 {0.0, s_dot_0, a_s0}, {longest, 0.0, 0.0}, limits);
369 if(!along) {
370 return along.error();
371 }
372 active_profile = along.value();
373 active_duration = active_profile.duration_cycles();
374 } else {
375 active_profile = otg::Profile1D{};
376 active_duration = 1;
377 }
378 return rt::ErrorCode::ok;
379 }
380
381 // Non-aligned takeover: plan both profiles with beta-split limits.
382 for(std::size_t i = 0; i < axis_count; ++i) {
383 lateral_dir_[i] = lat_vel[i] / lat_speed;
384 }
385
386 // Lateral decay: from (pos=0, vel=lat_speed) to (pos=0, vel=0).
387 // The profile overshoots, peaks at R_tube, then returns to zero.
388 const otg::Limits1D lat_limits{
389 (1.0 - kBeta) * limits.max_velocity,
390 (1.0 - kBeta) * limits.max_acceleration,
391 (1.0 - kBeta) * limits.max_deceleration,
392 (1.0 - kBeta) * limits.max_jerk,
393 };
395 otg::plan_time_optimal({0.0, lat_speed, 0.0}, {0.0, 0.0, 0.0},
396 lat_limits);
397 if(!lat) {
398 return lat.error();
399 }
400 lateral_profile_ = lat.value();
401 duration_ = lateral_profile_.duration_cycles();
402
403 // R_tube = peak lateral displacement during the decay profile.
404 double max_disp = 0.0;
405 for(std::int64_t t = 1; t <= duration_; ++t) {
406 const otg::State1D s =
407 otg::sample(lateral_profile_, rt::CycleTick::from_cycles(t));
408 const double d = std::fabs(s.position);
409 if(d > max_disp) {
410 max_disp = d;
411 }
412 }
413 r_tube_ = max_disp;
414
415 // Along-path: from (pos=0, vel=s_dot_0, acc=a_s0) to
416 // (pos=longest, vel=0, acc=0) with beta-split limits.
417 // KB-052: a_s0 included for acceleration continuity.
418 const otg::Limits1D along_limits{
419 kBeta * limits.max_velocity,
420 kBeta * limits.max_acceleration,
421 kBeta * limits.max_deceleration,
422 kBeta * limits.max_jerk,
423 };
424
425 // Clamp a_s0 to the beta-split acceleration limits so the OTG
426 // entry state is admissible under the split budget.
427 if(a_s0 > along_limits.max_acceleration) {
428 a_s0 = along_limits.max_acceleration;
429 } else if(a_s0 < -along_limits.max_deceleration) {
430 a_s0 = -along_limits.max_deceleration;
431 }
432
433 // KB-053: reject the connector when the along-path stopping
434 // distance far exceeds the path length. The rest-start fallback
435 // is safe (starts from zero velocity, no cliff).
436 if(longest > 0.0) {
437 const double stop_dist =
438 otg::detail::ramp_between(s_dot_0, 0.0, along_limits).distance;
439 if(stop_dist > longest * 1.5) {
441 }
443 {0.0, s_dot_0, a_s0}, {longest, 0.0, 0.0}, along_limits);
444 if(!along) {
445 return along.error();
446 }
447 active_profile = along.value();
448 active_duration =
449 std::max(active_profile.duration_cycles(), duration_);
450 } else {
451 // Zero-distance: all velocity is lateral, along-path is trivial.
452 active_profile = otg::Profile1D{};
453 active_duration = duration_;
454 }
455 active_ = true;
456 return rt::ErrorCode::ok;
457 }
458
459 // A curved source can carry member acceleration that is not parallel to
460 // its tangent. When its successor is linear, preserve that full vector in
461 // per-member residual profiles; the legacy scalar linear connector remains
462 // unchanged for plain linear sources and their replay contract.
464 const otg::Limits1D &limits, double path_length,
465 std::size_t axis_count,
466 const std::array<double, MaxAxes> &start,
467 const std::array<double, MaxAxes> &finish,
468 otg::Profile1D &active_profile,
469 std::int64_t &active_duration,
470 bool euclidean_metric = false)
471 {
472 constexpr double kBeta = 0.5;
473 constexpr double kStateThreshold = 1e-12;
474
475 active_ = false;
476 vector_mode_ = true;
477 duration_ = 0;
478 r_tube_ = 0.0;
479 for(std::size_t i = 0; i < axis_count; ++i) {
480 lateral_profiles_[i] = otg::Profile1D{};
481 }
482 if(path_length <= 0.0) {
483 vector_mode_ = false;
485 }
486
487 std::array<double, MaxAxes> q_s{};
488 double q_s_norm_sq = 0.0;
489 double s_dot_0 = 0.0;
490 double a_s0 = 0.0;
491 for(std::size_t i = 0; i < axis_count; ++i) {
492 q_s[i] = (finish[i] - start[i]) / path_length;
493 q_s_norm_sq += q_s[i] * q_s[i];
494 s_dot_0 += velocity_[i] * q_s[i];
495 a_s0 += acceleration_[i] * q_s[i];
496 }
497 if(q_s_norm_sq <= 1e-18) {
498 vector_mode_ = false;
500 }
501 s_dot_0 /= q_s_norm_sq;
502 a_s0 /= q_s_norm_sq;
503
504 double entry_analytic_l2_sq = 0.0;
505 double entry_actual_l2_sq = 0.0;
506 for(std::size_t i = 0; i < axis_count; ++i) {
507 entry_analytic_l2_sq += velocity_[i] * velocity_[i];
508 entry_actual_l2_sq += output_velocity_[i] * output_velocity_[i];
509 }
510 const double entry_analytic_l2 = std::sqrt(entry_analytic_l2_sq);
511 const double entry_actual_l2 = std::sqrt(entry_actual_l2_sq);
512
513 bool has_residual = false;
514 for(std::size_t i = 0; i < axis_count; ++i) {
515 const double residual_velocity =
516 velocity_[i] - q_s[i] * s_dot_0;
517 const double residual_acceleration =
518 acceleration_[i] - q_s[i] * a_s0;
519 if(std::fabs(residual_velocity) > kStateThreshold ||
520 std::fabs(residual_acceleration) > kStateThreshold) {
521 has_residual = true;
522 break;
523 }
524 }
525 if(!has_residual) {
527 {0.0, s_dot_0, a_s0}, {path_length, 0.0, 0.0}, limits);
528 if(!along) {
529 vector_mode_ = false;
530 return along.error();
531 }
532 const std::int64_t along_duration =
533 along.value().duration_cycles();
534 if(!validate_linear_profiles(
535 limits, path_length, q_s, axis_count, along.value(),
536 lateral_profiles_, output_velocity_, output_acceleration_,
537 along_duration, euclidean_metric, entry_analytic_l2,
538 entry_actual_l2)) {
539 vector_mode_ = false;
541 }
542 active_profile = along.value();
543 active_duration = along_duration;
544 vector_mode_ = false;
545 return rt::ErrorCode::ok;
546 }
547
548 const otg::Limits1D along_limits{
549 kBeta * limits.max_velocity,
550 kBeta * limits.max_acceleration,
551 kBeta * limits.max_deceleration,
552 kBeta * limits.max_jerk,
553 };
554 if(a_s0 > along_limits.max_acceleration) {
555 a_s0 = along_limits.max_acceleration;
556 } else if(a_s0 < -along_limits.max_deceleration) {
557 a_s0 = -along_limits.max_deceleration;
558 }
559 const double stop_dist =
560 otg::detail::ramp_between(s_dot_0, 0.0, along_limits).distance;
561 if(stop_dist > path_length) {
562 vector_mode_ = false;
564 }
566 {0.0, s_dot_0, a_s0}, {path_length, 0.0, 0.0}, along_limits);
567 if(!along) {
568 vector_mode_ = false;
569 return along.error();
570 }
571 active_profile = along.value();
572 active_duration = active_profile.duration_cycles();
573
574 const double lateral_v_limit =
575 euclidean_metric && axis_count > 1
576 ? ((1.0 - kBeta) * limits.max_velocity) /
577 std::sqrt(static_cast<double>(axis_count))
578 : (1.0 - kBeta) * limits.max_velocity;
579 const otg::Limits1D lateral_limits{
580 lateral_v_limit,
581 (1.0 - kBeta) * limits.max_acceleration,
582 (1.0 - kBeta) * limits.max_deceleration,
583 (1.0 - kBeta) * limits.max_jerk,
584 };
585 std::array<otg::State1D, MaxAxes> residuals{};
586 for(std::size_t i = 0; i < axis_count; ++i) {
587 residuals[i] = {
588 0.0,
589 velocity_[i] - q_s[i] * s_dot_0,
590 acceleration_[i] - q_s[i] * a_s0,
591 };
592 }
593 std::array<otg::Profile1D, MaxAxes> planned_lateral{};
594 std::int64_t planned_duration = 0;
595 const rt::ErrorCode lateral_error = plan_synchronized_residual_profiles(
596 lateral_limits, residuals, axis_count, kStateThreshold,
597 planned_lateral, planned_duration);
598 if(lateral_error != rt::ErrorCode::ok) {
599 vector_mode_ = false;
600 return lateral_error;
601 }
602 lateral_profiles_ = planned_lateral;
603 duration_ = planned_duration;
604 active_duration = std::max(active_duration, duration_);
605 if(!validate_linear_profiles(limits, path_length, q_s, axis_count,
606 active_profile, lateral_profiles_,
607 output_velocity_, output_acceleration_,
608 active_duration, euclidean_metric,
609 entry_analytic_l2, entry_actual_l2)) {
610 otg::Limits1D bounded_limits = along_limits;
611 bounded_limits.max_velocity = std::max(
612 bounded_limits.max_velocity, std::fabs(s_dot_0));
613 otg::Profile1D bounded_profile{};
614 rt::ErrorCode bounded = make_bounded_quintic(
615 {0.0, s_dot_0, a_s0}, {path_length, 0.0, 0.0},
616 bounded_limits, bounded_profile);
617 std::int64_t bounded_duration = std::max(
618 bounded_profile.duration_cycles(), duration_);
619 bool bounded_valid =
620 bounded == rt::ErrorCode::ok &&
621 validate_linear_profiles(
622 limits, path_length, q_s, axis_count, bounded_profile,
623 lateral_profiles_, output_velocity_, output_acceleration_,
624 bounded_duration, euclidean_metric, entry_analytic_l2,
625 entry_actual_l2);
626 if(!bounded_valid) {
627 bounded = make_bounded_stop_then_go(
628 {0.0, s_dot_0, a_s0}, {path_length, 0.0, 0.0},
629 bounded_limits, bounded_profile);
630 bounded_duration = std::max(
631 bounded_profile.duration_cycles(), duration_);
632 bounded_valid =
633 bounded == rt::ErrorCode::ok &&
634 validate_linear_profiles(
635 limits, path_length, q_s, axis_count,
636 bounded_profile, lateral_profiles_, output_velocity_,
637 output_acceleration_, bounded_duration,
638 euclidean_metric, entry_analytic_l2, entry_actual_l2);
639 }
640 if(!bounded_valid) {
641 vector_mode_ = false;
643 }
644 active_profile = bounded_profile;
645 active_duration = bounded_duration;
646 }
647 for(std::int64_t tick = 1; tick <= duration_; ++tick) {
648 double radius_sq = 0.0;
649 for(std::size_t i = 0; i < axis_count; ++i) {
650 const otg::State1D lateral = otg::sample(
651 lateral_profiles_[i], rt::CycleTick::from_cycles(tick));
652 radius_sq += lateral.position * lateral.position;
653 }
654 r_tube_ = std::max(r_tube_, std::sqrt(radius_sq));
655 }
656 active_ = duration_ > 0;
657 return rt::ErrorCode::ok;
658 }
659
660 // Y7b1 circular planner. The base path keeps the analytical curved
661 // derivatives while each member's orthogonal residual returns to zero in
662 // a fixed-capacity Profile1D. All planning and chain-rule validation runs
663 // at submit time; the cycle path only samples the resulting profiles.
665 const otg::Limits1D &limits, double path_length,
666 std::size_t axis_count,
667 const std::array<double, MaxAxes> &start,
668 const std::array<double, MaxAxes> &finish,
669 const geom::ArcSegment &arc,
670 otg::Profile1D &active_profile,
671 std::int64_t &active_duration)
672 {
673 constexpr double kBeta = 0.5;
674 constexpr double kStateThreshold = 1e-12;
675
676 active_ = false;
677 vector_mode_ = true;
678 duration_ = 0;
679 r_tube_ = 0.0;
680 for(std::size_t i = 0; i < axis_count; ++i) {
681 lateral_profiles_[i] = otg::Profile1D{};
682 }
683 if(path_length <= 0.0 || axis_count < 2) {
684 vector_mode_ = false;
686 }
687
688 std::array<double, MaxAxes> q_s{};
689 std::array<double, MaxAxes> q_ss{};
690 circular_derivatives(arc, 0.0, path_length, start, finish, axis_count,
691 q_s, q_ss, nullptr);
692 double q_s_norm_sq = 0.0;
693 double s_dot_0 = 0.0;
694 for(std::size_t i = 0; i < axis_count; ++i) {
695 q_s_norm_sq += q_s[i] * q_s[i];
696 s_dot_0 += velocity_[i] * q_s[i];
697 }
698 if(q_s_norm_sq <= 1e-18) {
699 vector_mode_ = false;
701 }
702 s_dot_0 /= q_s_norm_sq;
703 // Arc sampling is defined only on [0,L]. A negative projection stays
704 // in the residual connector instead of driving a clamped base path.
705 if(s_dot_0 < 0.0) {
706 s_dot_0 = 0.0;
707 }
708
709 double a_s0 = 0.0;
710 for(std::size_t i = 0; i < axis_count; ++i) {
711 a_s0 += (acceleration_[i] -
712 q_ss[i] * s_dot_0 * s_dot_0) * q_s[i];
713 }
714 a_s0 /= q_s_norm_sq;
715 if(s_dot_0 <= kStateThreshold && a_s0 < 0.0) {
716 // The arc has no negative-length extension. Keep an outward
717 // boundary acceleration in the residual instead of asking the
718 // scalar profile to move into a clamped region.
719 a_s0 = 0.0;
720 }
721
722 const otg::Limits1D along_limits = circular_along_limits(
723 limits, kBeta, arc, path_length, start, finish, axis_count);
724 if(along_limits.max_velocity <= 0.0 ||
725 along_limits.max_acceleration <= 0.0 ||
726 along_limits.max_deceleration <= 0.0 ||
727 along_limits.max_jerk <= 0.0) {
728 vector_mode_ = false;
730 }
731 if(a_s0 > along_limits.max_acceleration) {
732 a_s0 = along_limits.max_acceleration;
733 } else if(a_s0 < -along_limits.max_deceleration) {
734 a_s0 = -along_limits.max_deceleration;
735 }
736
737 const double stop_dist =
738 otg::detail::ramp_between(s_dot_0, 0.0, along_limits).distance;
739 if(stop_dist > path_length) {
740 vector_mode_ = false;
742 }
744 {0.0, s_dot_0, a_s0}, {path_length, 0.0, 0.0}, along_limits);
745 if(!along) {
746 vector_mode_ = false;
747 return along.error();
748 }
749 active_profile = along.value();
750 active_duration = active_profile.duration_cycles();
751
752 const otg::Limits1D lateral_limits{
753 (1.0 - kBeta) * limits.max_velocity,
754 (1.0 - kBeta) * limits.max_acceleration,
755 (1.0 - kBeta) * limits.max_deceleration,
756 (1.0 - kBeta) * limits.max_jerk,
757 };
758 std::array<otg::State1D, MaxAxes> residuals{};
759 for(std::size_t i = 0; i < axis_count; ++i) {
760 const double base_acceleration =
761 q_ss[i] * s_dot_0 * s_dot_0 + q_s[i] * a_s0;
762 residuals[i] = {
763 0.0,
764 velocity_[i] - q_s[i] * s_dot_0,
765 acceleration_[i] - base_acceleration,
766 };
767 }
768 std::array<otg::Profile1D, MaxAxes> planned_lateral{};
769 std::int64_t planned_duration = 0;
770 const rt::ErrorCode lateral_error = plan_synchronized_residual_profiles(
771 lateral_limits, residuals, axis_count, kStateThreshold,
772 planned_lateral, planned_duration);
773 if(lateral_error != rt::ErrorCode::ok) {
774 vector_mode_ = false;
775 return lateral_error;
776 }
777 lateral_profiles_ = planned_lateral;
778 duration_ = planned_duration;
779 active_duration = std::max(active_duration, duration_);
780
781 if(!validate_circular_profiles(limits, path_length, axis_count,
782 start, finish, arc, active_profile,
783 lateral_profiles_, output_velocity_,
784 output_acceleration_, active_duration)) {
785 // Integer-quantized time-optimal corrections can cross the finite
786 // arc endpoint with non-zero speed before returning to the target.
787 // The runtime clamp would turn that into an acceleration cliff.
788 // Build a scalar-feasible quintic at the correct duration scale
789 // (doubling+bisection for zero boundary acceleration, bounded
790 // geometric growth otherwise), then accept it only if the complete
791 // member-space output validates at every cycle.
792 otg::Limits1D bounded_limits = along_limits;
793 bounded_limits.max_velocity = std::max(
794 bounded_limits.max_velocity, std::fabs(s_dot_0));
795 otg::Profile1D bounded_profile{};
796 rt::ErrorCode bounded = make_bounded_quintic(
797 {0.0, s_dot_0, a_s0}, {path_length, 0.0, 0.0},
798 bounded_limits, bounded_profile);
799 std::int64_t bounded_duration = std::max(
800 bounded_profile.duration_cycles(), duration_);
801 bool bounded_valid =
802 bounded == rt::ErrorCode::ok &&
803 validate_circular_profiles(
804 limits, path_length, axis_count, start, finish, arc,
805 bounded_profile, lateral_profiles_, output_velocity_,
806 output_acceleration_, bounded_duration);
807 if(!bounded_valid) {
808 bounded = make_bounded_stop_then_go(
809 {0.0, s_dot_0, a_s0}, {path_length, 0.0, 0.0},
810 bounded_limits, bounded_profile);
811 bounded_duration = std::max(
812 bounded_profile.duration_cycles(), duration_);
813 bounded_valid =
814 bounded == rt::ErrorCode::ok &&
815 validate_circular_profiles(
816 limits, path_length, axis_count, start, finish, arc,
817 bounded_profile, lateral_profiles_, output_velocity_,
818 output_acceleration_, bounded_duration);
819 }
820 if(!bounded_valid) {
821 vector_mode_ = false;
823 }
824 active_profile = bounded_profile;
825 active_duration = bounded_duration;
826 }
827
828 for(std::int64_t tick = 1; tick <= duration_; ++tick) {
829 double radius_sq = 0.0;
830 for(std::size_t i = 0; i < axis_count; ++i) {
831 const otg::State1D lateral = otg::sample(
832 lateral_profiles_[i], rt::CycleTick::from_cycles(tick));
833 radius_sq += lateral.position * lateral.position;
834 }
835 r_tube_ = std::max(r_tube_, std::sqrt(radius_sq));
836 }
837 active_ = duration_ > 0;
838 return rt::ErrorCode::ok;
839 }
840
841 // Re-plan both parts of a live vector connector on a linear target for
842 // GroupStop. A stop that cannot fit before the finite target is rejected;
843 // AxisGroup then freezes at the last commanded point instead of discarding
844 // the member residual.
846 const otg::Limits1D &limits, double path_length,
847 std::size_t axis_count,
848 const std::array<double, MaxAxes> &start,
849 const std::array<double, MaxAxes> &finish,
850 const otg::Profile1D &current_along_profile,
851 std::int64_t current_tick,
852 const std::array<double, MaxAxes> &initial_output_velocity,
853 const std::array<double, MaxAxes> &initial_output_acceleration,
854 otg::Profile1D &halt_profile,
855 std::int64_t &halt_duration,
856 bool euclidean_metric = false)
857 {
858 constexpr double kBeta = 0.5;
859 constexpr double kStateThreshold = 1e-12;
860 if(!active_ || !vector_mode_ || path_length <= 0.0) {
862 }
863
864 std::array<double, MaxAxes> q_s{};
865 for(std::size_t i = 0; i < axis_count; ++i) {
866 q_s[i] = (finish[i] - start[i]) / path_length;
867 }
868 const otg::Limits1D along_limits{
869 kBeta * limits.max_velocity,
870 kBeta * limits.max_acceleration,
871 kBeta * limits.max_deceleration,
872 kBeta * limits.max_jerk,
873 };
874 const otg::State1D along_state = otg::sample(
875 current_along_profile, rt::CycleTick::from_cycles(current_tick));
876 std::array<otg::State1D, MaxAxes> live_lateral{};
877 for(std::size_t i = 0; i < axis_count; ++i) {
878 live_lateral[i] = otg::sample(
879 lateral_profiles_[i], rt::CycleTick::from_cycles(current_tick));
880 }
881
882 otg::Profile1D stopped_along{};
883 otg::State1D reduced_along = along_state;
884 rt::ErrorCode reduced = append_acceleration_zeroing(
885 stopped_along, reduced_along, along_limits.max_jerk);
886 if(reduced != rt::ErrorCode::ok) {
887 return reduced;
888 }
889 double brake_velocity = reduced_along.velocity;
890 if(brake_velocity < 0.0) {
891 brake_velocity = 0.0;
892 }
893 const double stop_position =
894 reduced_along.position +
895 otg::detail::ramp_between(brake_velocity, 0.0, along_limits)
896 .distance;
897 const double path_tolerance =
898 1e-12 * std::max(1.0, path_length);
899 if(stop_position > path_length + path_tolerance) {
901 }
903 reduced_along, {stop_position, 0.0, 0.0}, along_limits);
904 if(!along_tail) {
905 return along_tail.error();
906 }
907 reduced = append_profile(stopped_along, along_tail.value());
908 if(reduced != rt::ErrorCode::ok) {
909 return reduced;
910 }
911
912 // A residual can reverse while the composed member velocity still
913 // brakes. Use the stricter signed bound so the independent residual
914 // stop cannot exceed either requested member acceleration envelope.
915 const double lateral_acceleration_limit =
916 (1.0 - kBeta) *
917 std::min(limits.max_acceleration, limits.max_deceleration);
918 const double lateral_v_limit =
919 euclidean_metric && axis_count > 1
920 ? ((1.0 - kBeta) * limits.max_velocity) /
921 std::sqrt(static_cast<double>(axis_count))
922 : (1.0 - kBeta) * limits.max_velocity;
923 const otg::Limits1D lateral_limits{
924 lateral_v_limit,
925 lateral_acceleration_limit,
926 lateral_acceleration_limit,
927 (1.0 - kBeta) * limits.max_jerk,
928 };
929 std::array<otg::Profile1D, MaxAxes> stopped_lateral{};
930 std::int64_t lateral_duration = 0;
931 for(std::size_t i = 0; i < axis_count; ++i) {
932 const otg::State1D live = live_lateral[i];
933 if(std::fabs(live.position) <= kStateThreshold &&
934 std::fabs(live.velocity) <= kStateThreshold &&
935 std::fabs(live.acceleration) <= kStateThreshold) {
936 continue;
937 }
938 otg::State1D reduced_lateral = live;
939 reduced = append_acceleration_zeroing(
940 stopped_lateral[i], reduced_lateral,
941 lateral_limits.max_jerk);
942 if(reduced != rt::ErrorCode::ok) {
943 return reduced;
944 }
945 const rt::Result<otg::Profile1D> lateral_tail =
947 reduced_lateral, {0.0, 0.0, 0.0}, lateral_limits);
948 if(!lateral_tail) {
949 return lateral_tail.error();
950 }
951 reduced = append_profile(stopped_lateral[i], lateral_tail.value());
952 if(reduced != rt::ErrorCode::ok) {
953 return reduced;
954 }
955 lateral_duration = std::max(
956 lateral_duration, stopped_lateral[i].duration_cycles());
957 }
958
959 const std::int64_t total_duration = std::max(
960 stopped_along.duration_cycles(), lateral_duration);
961 otg::Limits1D validation_limits = limits;
962 for(std::size_t i = 0; i < axis_count; ++i) {
963 const double analytic_acceleration =
964 q_s[i] * along_state.acceleration +
965 live_lateral[i].acceleration;
966 validation_limits.max_acceleration = std::max(
967 validation_limits.max_acceleration,
968 std::max(initial_output_acceleration[i],
969 analytic_acceleration));
970 validation_limits.max_deceleration = std::max(
971 validation_limits.max_deceleration,
972 std::max(-initial_output_acceleration[i],
973 -analytic_acceleration));
974 }
975 double entry_analytic_l2_sq = 0.0;
976 double entry_actual_l2_sq = 0.0;
977 for(std::size_t i = 0; i < axis_count; ++i) {
978 const double analytic_v =
979 q_s[i] * along_state.velocity + live_lateral[i].velocity;
980 entry_analytic_l2_sq += analytic_v * analytic_v;
981 entry_actual_l2_sq +=
982 initial_output_velocity[i] * initial_output_velocity[i];
983 }
984 const double entry_analytic_l2 = std::sqrt(entry_analytic_l2_sq);
985 const double entry_actual_l2 = std::sqrt(entry_actual_l2_sq);
986 if(!validate_linear_profiles(
987 validation_limits, path_length, q_s, axis_count,
988 stopped_along, stopped_lateral, initial_output_velocity,
989 initial_output_acceleration, total_duration, euclidean_metric,
990 entry_analytic_l2, entry_actual_l2)) {
992 }
993
994 halt_profile = stopped_along;
995 halt_duration = total_duration;
996 lateral_profiles_ = stopped_lateral;
997 duration_ = lateral_duration;
998 r_tube_ = 0.0;
999 for(std::int64_t tick = 0; tick <= duration_; ++tick) {
1000 double radius_sq = 0.0;
1001 for(std::size_t i = 0; i < axis_count; ++i) {
1002 const otg::State1D lateral = otg::sample(
1003 lateral_profiles_[i], rt::CycleTick::from_cycles(tick));
1004 radius_sq += lateral.position * lateral.position;
1005 }
1006 r_tube_ = std::max(r_tube_, std::sqrt(radius_sq));
1007 }
1008 active_ = duration_ > 0;
1009 vector_mode_ = active_;
1010 return rt::ErrorCode::ok;
1011 }
1012
1013 // Re-plan both parts of a live circular connector for GroupStop. The
1014 // scalar brake and every member residual start from their sampled state,
1015 // so resetting AxisGroup's active tick does not discard or rewind the
1016 // tolerance-tube offset.
1018 const otg::Limits1D &limits, double path_length,
1019 std::size_t axis_count,
1020 const std::array<double, MaxAxes> &start,
1021 const std::array<double, MaxAxes> &finish,
1022 const geom::ArcSegment &arc,
1023 const otg::Profile1D &current_along_profile,
1024 std::int64_t current_tick,
1025 const std::array<double, MaxAxes> &initial_output_velocity,
1026 const std::array<double, MaxAxes> &initial_output_acceleration,
1027 otg::Profile1D &halt_profile,
1028 std::int64_t &halt_duration)
1029 {
1030 constexpr double kBeta = 0.5;
1031 constexpr double kStateThreshold = 1e-12;
1032 if(!active_ || !vector_mode_ || path_length <= 0.0 || axis_count < 2) {
1034 }
1035
1036 const otg::State1D along_state = otg::sample(
1037 current_along_profile, rt::CycleTick::from_cycles(current_tick));
1038 const otg::Limits1D along_limits = circular_along_limits(
1039 limits, kBeta, arc, path_length, start, finish, axis_count);
1040 if(along_limits.max_velocity <= 0.0 ||
1041 along_limits.max_acceleration <= 0.0 ||
1042 along_limits.max_deceleration <= 0.0 ||
1043 along_limits.max_jerk <= 0.0) {
1045 }
1046
1047 std::array<otg::State1D, MaxAxes> live_lateral{};
1048 for(std::size_t i = 0; i < axis_count; ++i) {
1049 live_lateral[i] = otg::sample(
1050 lateral_profiles_[i], rt::CycleTick::from_cycles(current_tick));
1051 }
1052
1053 otg::Profile1D stopped_along{};
1054 otg::State1D reduced_along = along_state;
1055 rt::ErrorCode reduced = append_acceleration_zeroing(
1056 stopped_along, reduced_along, along_limits.max_jerk);
1057 if(reduced != rt::ErrorCode::ok) {
1058 return reduced;
1059 }
1060 double brake_velocity = reduced_along.velocity;
1061 if(brake_velocity < 0.0) {
1062 brake_velocity = 0.0;
1063 }
1064 const double stop_position =
1065 reduced_along.position +
1066 otg::detail::ramp_between(brake_velocity, 0.0, along_limits)
1067 .distance;
1068 if(stop_position < -1e-9 || stop_position > path_length + 1e-9) {
1070 }
1072 reduced_along, {stop_position, 0.0, 0.0}, along_limits);
1073 if(!along_tail) {
1074 return along_tail.error();
1075 }
1076 reduced = append_profile(stopped_along, along_tail.value());
1077 if(reduced != rt::ErrorCode::ok) {
1078 return reduced;
1079 }
1080
1081 const otg::Limits1D lateral_limits{
1082 (1.0 - kBeta) * limits.max_velocity,
1083 (1.0 - kBeta) * limits.max_acceleration,
1084 (1.0 - kBeta) * limits.max_deceleration,
1085 (1.0 - kBeta) * limits.max_jerk,
1086 };
1087 std::array<otg::Profile1D, MaxAxes> stopped_lateral{};
1088 std::int64_t lateral_duration = 0;
1089 for(std::size_t i = 0; i < axis_count; ++i) {
1090 const otg::State1D live = live_lateral[i];
1091 if(std::fabs(live.position) <= kStateThreshold &&
1092 std::fabs(live.velocity) <= kStateThreshold &&
1093 std::fabs(live.acceleration) <= kStateThreshold) {
1094 continue;
1095 }
1096 otg::State1D reduced_lateral = live;
1097 reduced = append_acceleration_zeroing(
1098 stopped_lateral[i], reduced_lateral,
1099 lateral_limits.max_jerk);
1100 if(reduced != rt::ErrorCode::ok) {
1101 return reduced;
1102 }
1103 const rt::Result<otg::Profile1D> lateral_tail =
1105 reduced_lateral, {0.0, 0.0, 0.0}, lateral_limits);
1106 if(!lateral_tail) {
1107 return lateral_tail.error();
1108 }
1109 reduced = append_profile(stopped_lateral[i], lateral_tail.value());
1110 if(reduced != rt::ErrorCode::ok) {
1111 return reduced;
1112 }
1113 lateral_duration = std::max(
1114 lateral_duration, stopped_lateral[i].duration_cycles());
1115 }
1116
1117 const std::int64_t total_duration = std::max(
1118 stopped_along.duration_cycles(), lateral_duration);
1119 otg::Limits1D validation_limits = limits;
1120 std::array<double, MaxAxes> q_s{};
1121 std::array<double, MaxAxes> q_ss{};
1122 circular_derivatives(arc, along_state.position, path_length, start,
1123 finish, axis_count, q_s, q_ss, nullptr);
1124 for(std::size_t i = 0; i < axis_count; ++i) {
1125 const double analytic_acceleration =
1126 q_ss[i] * along_state.velocity * along_state.velocity +
1127 q_s[i] * along_state.acceleration +
1128 live_lateral[i].acceleration;
1129 validation_limits.max_acceleration = std::max(
1130 validation_limits.max_acceleration,
1131 std::max(initial_output_acceleration[i],
1132 analytic_acceleration));
1133 validation_limits.max_deceleration = std::max(
1134 validation_limits.max_deceleration,
1135 std::max(-initial_output_acceleration[i],
1136 -analytic_acceleration));
1137 }
1138 if(!validate_circular_profiles(
1139 validation_limits, path_length, axis_count, start, finish, arc,
1140 stopped_along, stopped_lateral, initial_output_velocity,
1141 initial_output_acceleration, total_duration)) {
1143 }
1144
1145 halt_profile = stopped_along;
1146 halt_duration = total_duration;
1147 lateral_profiles_ = stopped_lateral;
1148 duration_ = lateral_duration;
1149 r_tube_ = 0.0;
1150 for(std::int64_t tick = 0; tick <= duration_; ++tick) {
1151 double radius_sq = 0.0;
1152 for(std::size_t i = 0; i < axis_count; ++i) {
1153 const otg::State1D lateral = otg::sample(
1154 lateral_profiles_[i], rt::CycleTick::from_cycles(tick));
1155 radius_sq += lateral.position * lateral.position;
1156 }
1157 r_tube_ = std::max(r_tube_, std::sqrt(radius_sq));
1158 }
1159 active_ = duration_ > 0;
1160 vector_mode_ = active_;
1161 return rt::ErrorCode::ok;
1162 }
1163
1164 void sample_lateral_offsets(std::int64_t tick,
1165 std::array<double, MaxAxes> &offsets,
1166 std::size_t axis_count)
1167 {
1168 offsets.fill(0.0);
1169 if(!active_ || !vector_mode_) {
1170 return;
1171 }
1172 for(std::size_t i = 0; i < axis_count; ++i) {
1173 offsets[i] = otg::sample(
1174 lateral_profiles_[i], rt::CycleTick::from_cycles(tick)).position;
1175 }
1176 if(tick >= duration_) {
1177 active_ = false;
1178 }
1179 }
1180
1181 // Y7 (KB-051): during the connector, the cycle output is the along-path
1182 // position plus this lateral decay offset. After the lateral profile
1183 // completes, its position is zero and the connector deactivates; the
1184 // motion continues as pure along-path interpolation. O(1) per cycle.
1185 double sample_lateral_offset(std::int64_t tick)
1186 {
1187 if(!active_) {
1188 return 0.0;
1189 }
1190 const otg::State1D lat =
1191 otg::sample(lateral_profile_, rt::CycleTick::from_cycles(tick));
1192 if(tick >= duration_) {
1193 active_ = false;
1194 }
1195 return lat.position;
1196 }
1197
1198 double lateral_dir(std::size_t index) const
1199 {
1200 return lateral_dir_[index];
1201 }
1202
1203private:
1204 static rt::ErrorCode make_bounded_quintic(
1205 const otg::State1D &start, const otg::Target1D &finish,
1206 const otg::Limits1D &limits, otg::Profile1D &profile)
1207 {
1208 profile = otg::Profile1D{};
1210 profile, start, finish, limits);
1211 }
1212
1213 static rt::ErrorCode make_bounded_stop_then_go(
1214 const otg::State1D &start, const otg::Target1D &finish,
1215 const otg::Limits1D &limits, otg::Profile1D &profile)
1216 {
1217 constexpr double kTolerance = 1e-12;
1218 if(finish.velocity != 0.0 || finish.acceleration != 0.0) {
1220 }
1221
1222 profile = otg::Profile1D{};
1223 otg::State1D stopped = start;
1224 rt::ErrorCode planned = append_acceleration_zeroing(
1225 profile, stopped, limits.max_jerk);
1226 if(planned != rt::ErrorCode::ok) {
1227 return planned;
1228 }
1229 planned = otg::detail::push_ramp(
1230 profile, stopped, 0.0, limits,
1232 if(planned != rt::ErrorCode::ok) {
1233 return planned;
1234 }
1235
1236 const double position_tolerance =
1237 kTolerance * std::max(1.0, std::fabs(finish.position));
1238 if(stopped.position > finish.position + position_tolerance) {
1240 }
1242 profile, stopped, finish, limits);
1243 }
1244
1245 static rt::ErrorCode append_profile(otg::Profile1D &destination,
1246 const otg::Profile1D &source)
1247 {
1248 for(std::size_t i = 0; i < source.segment_count(); ++i) {
1249 const rt::ErrorCode added = destination.add_segment(source.segment(i));
1250 if(added != rt::ErrorCode::ok) {
1251 return added;
1252 }
1253 }
1254 return rt::ErrorCode::ok;
1255 }
1256
1257 static rt::ErrorCode append_acceleration_zeroing(
1258 otg::Profile1D &profile, otg::State1D &state, double max_jerk)
1259 {
1260 if(state.acceleration == 0.0) {
1261 return rt::ErrorCode::ok;
1262 }
1263 const double cycles =
1264 std::ceil(std::fabs(state.acceleration) / max_jerk);
1266 profile, state, -state.acceleration / cycles, cycles);
1267 }
1268
1269 static bool validate_linear_profiles(
1270 const otg::Limits1D &limits,
1271 double path_length,
1272 const std::array<double, MaxAxes> &q_s,
1273 std::size_t axis_count,
1274 const otg::Profile1D &along_profile,
1275 const std::array<otg::Profile1D, MaxAxes> &lateral_profiles,
1276 const std::array<double, MaxAxes> &initial_velocity,
1277 const std::array<double, MaxAxes> &initial_acceleration,
1278 std::int64_t total_duration,
1279 bool euclidean_metric = false,
1280 double entry_analytic_l2 = 0.0,
1281 double entry_actual_l2 = 0.0)
1282 {
1283 constexpr double kTolerance = 1e-9;
1284 const double path_tolerance =
1285 1e-12 * std::max(1.0, path_length);
1286 const double analytic_l2_cap =
1287 entry_analytic_l2 > limits.max_velocity ? entry_analytic_l2
1288 : limits.max_velocity;
1289 const double actual_l2_cap =
1290 entry_actual_l2 > limits.max_velocity ? entry_actual_l2
1291 : limits.max_velocity;
1292 bool analytic_in_l2_budget =
1293 entry_analytic_l2 <= limits.max_velocity + kTolerance;
1294 bool actual_in_l2_budget =
1295 entry_actual_l2 <= limits.max_velocity + kTolerance;
1296 std::array<double, MaxAxes> previous_lateral_acceleration{};
1297 std::array<double, MaxAxes> previous_position{};
1298 std::array<double, MaxAxes> previous_velocity = initial_velocity;
1299 std::array<double, MaxAxes> previous_acceleration =
1300 initial_acceleration;
1301 otg::State1D previous_along{};
1302 bool terminal_clamp_entered = false;
1303 for(std::int64_t tick = 0; tick <= total_duration; ++tick) {
1304 const otg::State1D along = otg::sample(
1305 along_profile, rt::CycleTick::from_cycles(tick));
1306 if(terminal_clamp_entered &&
1307 along.position < path_length - path_tolerance) {
1308 return false;
1309 }
1310 if(along.position >= path_length - path_tolerance) {
1311 terminal_clamp_entered = true;
1312 }
1313 const double sampled_path =
1314 std::min(along.position, path_length);
1315 const double scalar_jerk =
1316 tick == 0
1317 ? 0.0
1318 : along.acceleration - previous_along.acceleration;
1319 double analytic_v_sq = 0.0;
1320 double actual_v_sq = 0.0;
1321 for(std::size_t i = 0; i < axis_count; ++i) {
1322 const otg::State1D lateral = otg::sample(
1323 lateral_profiles[i], rt::CycleTick::from_cycles(tick));
1324 const double lateral_jerk =
1325 tick == 0
1326 ? 0.0
1327 : lateral.acceleration -
1328 previous_lateral_acceleration[i];
1329 const double velocity =
1330 q_s[i] * along.velocity + lateral.velocity;
1331 const double acceleration =
1332 q_s[i] * along.acceleration + lateral.acceleration;
1333 const double jerk =
1334 q_s[i] * scalar_jerk + lateral_jerk;
1335 analytic_v_sq += velocity * velocity;
1336 if(along.position <= path_length + path_tolerance) {
1337 if(std::fabs(velocity) >
1338 limits.max_velocity + kTolerance ||
1339 acceleration >
1340 limits.max_acceleration + kTolerance ||
1341 acceleration <
1342 -limits.max_deceleration - kTolerance ||
1343 (tick > 0 &&
1344 std::fabs(jerk) >
1345 limits.max_jerk + kTolerance)) {
1346 return false;
1347 }
1348 }
1349
1350 const double position =
1351 q_s[i] * sampled_path + lateral.position;
1352 if(tick > 0) {
1353 const double actual_velocity =
1354 position - previous_position[i];
1355 const double actual_acceleration =
1356 actual_velocity - previous_velocity[i];
1357 const double actual_jerk =
1358 actual_acceleration - previous_acceleration[i];
1359 actual_v_sq += actual_velocity * actual_velocity;
1360 if(std::fabs(actual_velocity) >
1361 limits.max_velocity + kTolerance ||
1362 actual_acceleration >
1363 limits.max_acceleration + kTolerance ||
1364 actual_acceleration <
1365 -limits.max_deceleration - kTolerance ||
1366 std::fabs(actual_jerk) >
1367 limits.max_jerk + kTolerance) {
1368 return false;
1369 }
1370 previous_velocity[i] = actual_velocity;
1371 previous_acceleration[i] = actual_acceleration;
1372 }
1373 previous_position[i] = position;
1374 previous_lateral_acceleration[i] = lateral.acceleration;
1375 }
1376 if(euclidean_metric) {
1377 if(along.position <= path_length + path_tolerance) {
1378 const double analytic_l2 = std::sqrt(analytic_v_sq);
1379 const double cap_now = analytic_in_l2_budget
1380 ? limits.max_velocity
1381 : analytic_l2_cap;
1382 if(analytic_l2 > cap_now + kTolerance) {
1383 return false;
1384 }
1385 if(analytic_l2 <= limits.max_velocity + kTolerance) {
1386 analytic_in_l2_budget = true;
1387 }
1388 }
1389 if(tick > 0) {
1390 const double actual_l2 = std::sqrt(actual_v_sq);
1391 const double cap_now = actual_in_l2_budget
1392 ? limits.max_velocity
1393 : actual_l2_cap;
1394 if(actual_l2 > cap_now + kTolerance) {
1395 return false;
1396 }
1397 if(actual_l2 <= limits.max_velocity + kTolerance) {
1398 actual_in_l2_budget = true;
1399 }
1400 }
1401 }
1402 previous_along = along;
1403 }
1404 return true;
1405 }
1406
1407 static void circular_derivatives(
1408 const geom::ArcSegment &arc, double arclength, double path_length,
1409 const std::array<double, MaxAxes> &start,
1410 const std::array<double, MaxAxes> &finish,
1411 std::size_t axis_count,
1412 std::array<double, MaxAxes> &q_s,
1413 std::array<double, MaxAxes> &q_ss,
1414 std::array<double, MaxAxes> *q_sss)
1415 {
1416 q_s.fill(0.0);
1417 q_ss.fill(0.0);
1418 if(q_sss != nullptr) {
1419 q_sss->fill(0.0);
1420 }
1421 const geom::Vec3 first = geom::path_derivative(arc, arclength);
1422 const geom::Vec3 second = geom::path_second_derivative(arc, arclength);
1423 q_s[0] = first.x;
1424 q_s[1] = first.y;
1425 q_ss[0] = second.x;
1426 q_ss[1] = second.y;
1427 if(q_sss != nullptr) {
1428 const geom::Vec3 third = geom::path_third_derivative(arc, arclength);
1429 (*q_sss)[0] = third.x;
1430 (*q_sss)[1] = third.y;
1431 }
1432 for(std::size_t i = 2; i < axis_count; ++i) {
1433 q_s[i] = (finish[i] - start[i]) / path_length;
1434 }
1435 }
1436
1437 static otg::Limits1D circular_along_limits(
1438 const otg::Limits1D &limits, double beta,
1439 const geom::ArcSegment &arc, double path_length,
1440 const std::array<double, MaxAxes> &start,
1441 const std::array<double, MaxAxes> &finish,
1442 std::size_t axis_count)
1443 {
1444 double max_qs = 1.0;
1445 for(std::size_t i = 2; i < axis_count; ++i) {
1446 max_qs = std::max(max_qs,
1447 std::fabs((finish[i] - start[i]) / path_length));
1448 }
1449 const double max_qss = 1.0 / arc.radius;
1450 const double max_qsss = max_qss * max_qss;
1451 const double curvature_acceleration =
1452 0.5 * beta * std::min(limits.max_acceleration,
1453 limits.max_deceleration);
1454 const double jerk_budget = beta * limits.max_jerk;
1455
1456 otg::Limits1D result{
1457 beta * limits.max_velocity / max_qs,
1458 0.5 * beta * limits.max_acceleration / max_qs,
1459 0.5 * beta * limits.max_deceleration / max_qs,
1460 jerk_budget / (3.0 * max_qs),
1461 };
1462 result.max_velocity = std::min(
1463 result.max_velocity,
1464 std::sqrt(curvature_acceleration / max_qss));
1465 result.max_velocity = std::min(
1466 result.max_velocity,
1467 std::cbrt(jerk_budget / (3.0 * max_qsss)));
1468 if(result.max_velocity > 1e-15) {
1469 const double cross_term_cap =
1470 jerk_budget / (9.0 * max_qss * result.max_velocity);
1471 result.max_acceleration =
1472 std::min(result.max_acceleration, cross_term_cap);
1473 result.max_deceleration =
1474 std::min(result.max_deceleration, cross_term_cap);
1475 }
1476 return result;
1477 }
1478
1479 bool validate_circular_profiles(
1480 const otg::Limits1D &limits, double path_length,
1481 std::size_t axis_count,
1482 const std::array<double, MaxAxes> &start,
1483 const std::array<double, MaxAxes> &finish,
1484 const geom::ArcSegment &arc,
1485 const otg::Profile1D &along_profile,
1486 const std::array<otg::Profile1D, MaxAxes> &lateral_profiles,
1487 const std::array<double, MaxAxes> &initial_velocity,
1488 const std::array<double, MaxAxes> &initial_acceleration,
1489 std::int64_t total_duration) const
1490 {
1491 constexpr double kTolerance = 1e-9;
1492 const double path_tolerance =
1493 1e-12 * std::max(1.0, path_length);
1494 std::array<double, MaxAxes> previous_lateral_acceleration{};
1495 std::array<double, MaxAxes> previous_position{};
1496 std::array<double, MaxAxes> previous_velocity = initial_velocity;
1497 std::array<double, MaxAxes> previous_acceleration =
1498 initial_acceleration;
1499 otg::State1D previous_along{};
1500 bool terminal_clamp_entered = false;
1501 for(std::int64_t tick = 0; tick <= total_duration; ++tick) {
1502 const otg::State1D along = otg::sample(
1503 along_profile, rt::CycleTick::from_cycles(tick));
1504 if(along.position < -path_tolerance ||
1505 (terminal_clamp_entered &&
1506 along.position < path_length - path_tolerance)) {
1507 return false;
1508 }
1509 if(along.position >= path_length - path_tolerance) {
1510 terminal_clamp_entered = true;
1511 }
1512 const double sampled_path =
1513 std::min(std::max(along.position, 0.0), path_length);
1514 const geom::Vec3 point = geom::sample(arc, sampled_path);
1515 std::array<double, MaxAxes> q_s{};
1516 std::array<double, MaxAxes> q_ss{};
1517 std::array<double, MaxAxes> q_sss{};
1518 circular_derivatives(arc, sampled_path, path_length, start,
1519 finish, axis_count, q_s, q_ss, &q_sss);
1520 const double scalar_jerk =
1521 tick == 0 ? 0.0 : along.acceleration - previous_along.acceleration;
1522
1523 for(std::size_t i = 0; i < axis_count; ++i) {
1524 const otg::State1D lateral = otg::sample(
1525 lateral_profiles[i], rt::CycleTick::from_cycles(tick));
1526 const double lateral_jerk =
1527 tick == 0 ? 0.0
1528 : lateral.acceleration -
1529 previous_lateral_acceleration[i];
1530 const double velocity = q_s[i] * along.velocity +
1531 lateral.velocity;
1532 const double acceleration =
1533 q_ss[i] * along.velocity * along.velocity +
1534 q_s[i] * along.acceleration + lateral.acceleration;
1535 const double jerk =
1536 q_sss[i] * along.velocity * along.velocity * along.velocity +
1537 3.0 * q_ss[i] * along.velocity * along.acceleration +
1538 q_s[i] * scalar_jerk + lateral_jerk;
1539 if(along.position <= path_length + path_tolerance) {
1540 if(std::fabs(velocity) >
1541 limits.max_velocity + kTolerance ||
1542 acceleration >
1543 limits.max_acceleration + kTolerance ||
1544 acceleration <
1545 -limits.max_deceleration - kTolerance ||
1546 (tick > 0 &&
1547 std::fabs(jerk) >
1548 limits.max_jerk + kTolerance)) {
1549 return false;
1550 }
1551 }
1552
1553 double position = 0.0;
1554 if(i == 0) {
1555 position = point.x;
1556 } else if(i == 1) {
1557 position = point.y;
1558 } else {
1559 position = start[i] +
1560 (finish[i] - start[i]) *
1561 (sampled_path / path_length);
1562 }
1563 position += lateral.position;
1564 if(tick > 0) {
1565 const double actual_velocity =
1566 position - previous_position[i];
1567 const double actual_acceleration =
1568 actual_velocity - previous_velocity[i];
1569 const double actual_jerk =
1570 actual_acceleration - previous_acceleration[i];
1571 if(std::fabs(actual_velocity) >
1572 limits.max_velocity + kTolerance ||
1573 actual_acceleration >
1574 limits.max_acceleration + kTolerance ||
1575 actual_acceleration <
1576 -limits.max_deceleration - kTolerance ||
1577 std::fabs(actual_jerk) >
1578 limits.max_jerk + kTolerance) {
1579 return false;
1580 }
1581 previous_velocity[i] = actual_velocity;
1582 previous_acceleration[i] = actual_acceleration;
1583 }
1584 previous_position[i] = position;
1585 previous_lateral_acceleration[i] = lateral.acceleration;
1586 }
1587 previous_along = along;
1588 }
1589 return true;
1590 }
1591
1592 std::int64_t duration_ = 0;
1593 double r_tube_ = 0.0;
1594 std::array<double, MaxAxes> lateral_dir_{};
1595 std::array<double, MaxAxes> velocity_{};
1596 std::array<double, MaxAxes> acceleration_{};
1597 std::array<double, MaxAxes> output_velocity_{};
1598 std::array<double, MaxAxes> output_acceleration_{};
1599 std::array<otg::Profile1D, MaxAxes> lateral_profiles_{};
1600 otg::Profile1D lateral_profile_{};
1601 bool active_ = false;
1602 bool has_velocity_ = false;
1603 bool vector_mode_ = false;
1604 bool captured_vector_state_ = false;
1605 bool captured_output_history_only_ = false;
1606};
1607
1608} // namespace plcopen::core::axis
void capture_circular(bool motion_active, const geom::ArcSegment &arc, double path_length, const otg::Profile1D &along_profile, std::int64_t tick, const std::array< double, MaxAxes > &start, const std::array< double, MaxAxes > &finish, std::size_t axis_count)
rt::ErrorCode plan_linear_vector(const otg::Limits1D &limits, double path_length, std::size_t axis_count, const std::array< double, MaxAxes > &start, const std::array< double, MaxAxes > &finish, otg::Profile1D &active_profile, std::int64_t &active_duration, bool euclidean_metric=false)
void capture_output_history(bool motion_active, const std::array< double, MaxAxes > &velocity, const std::array< double, MaxAxes > &acceleration, std::size_t axis_count)
void sample_lateral_offsets(std::int64_t tick, std::array< double, MaxAxes > &offsets, std::size_t axis_count)
const std::array< double, MaxAxes > & captured_analytic_velocity() const
const std::array< double, MaxAxes > & captured_analytic_acceleration() const
rt::ErrorCode plan(const otg::Limits1D &limits, double longest, std::size_t axis_count, const std::array< double, MaxAxes > &start, const std::array< double, MaxAxes > &finish, otg::Profile1D &active_profile, std::int64_t &active_duration)
rt::ErrorCode plan_circular(const otg::Limits1D &limits, double path_length, std::size_t axis_count, const std::array< double, MaxAxes > &start, const std::array< double, MaxAxes > &finish, const geom::ArcSegment &arc, otg::Profile1D &active_profile, std::int64_t &active_duration)
const std::array< double, MaxAxes > & captured_output_velocity() const
rt::ErrorCode plan_linear_vector_stop(const otg::Limits1D &limits, double path_length, std::size_t axis_count, const std::array< double, MaxAxes > &start, const std::array< double, MaxAxes > &finish, const otg::Profile1D &current_along_profile, std::int64_t current_tick, const std::array< double, MaxAxes > &initial_output_velocity, const std::array< double, MaxAxes > &initial_output_acceleration, otg::Profile1D &halt_profile, std::int64_t &halt_duration, bool euclidean_metric=false)
void sample_lateral_velocities(std::int64_t tick, std::array< double, MaxAxes > &velocities, std::size_t axis_count) const
void capture(bool motion_active, bool linear_path, double path_length, const otg::Profile1D &along_profile, std::int64_t tick, const std::array< double, MaxAxes > &start, const std::array< double, MaxAxes > &finish, std::size_t axis_count)
rt::ErrorCode plan_circular_stop(const otg::Limits1D &limits, double path_length, std::size_t axis_count, const std::array< double, MaxAxes > &start, const std::array< double, MaxAxes > &finish, const geom::ArcSegment &arc, const otg::Profile1D &current_along_profile, std::int64_t current_tick, const std::array< double, MaxAxes > &initial_output_velocity, const std::array< double, MaxAxes > &initial_output_acceleration, otg::Profile1D &halt_profile, std::int64_t &halt_duration)
void set_captured_output_state(const std::array< double, MaxAxes > &velocity, const std::array< double, MaxAxes > &acceleration, std::size_t axis_count)
const std::array< double, MaxAxes > & captured_output_acceleration() const
std::int64_t duration_cycles() const
static constexpr CycleTick from_cycles(std::int64_t cycles)
定义 cycle.h:42
constexpr const T & value() const
定义 error.h:49
constexpr ErrorCode error() const
定义 error.h:44
Vec3 path_derivative(LineSegment line)
Vec3 sample(LineSegment line, double arclength)
Vec3 path_second_derivative(LineSegment)
Vec3 path_third_derivative(LineSegment)
rt::ErrorCode push_quintic_correction(Profile1D &profile, State1D state, Target1D to, const Limits1D &limits)
rt::ErrorCode push_cubic_phase(Profile1D &profile, State1D &state, double jerk, double continuous_duration, bool force_finish_derivatives=false, double finish_velocity=0.0, double finish_acceleration=0.0)
RampTiming ramp_between(double va, double vb, const Limits1D &limits)
rt::ErrorCode push_ramp(Profile1D &profile, State1D &state, double vb, const Limits1D &limits, RampRounding rounding, bool force_finish_derivatives=false)
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)
State1D sample(const Profile1D &profile, rt::CycleTick tick)