plcopen 0.22.0
载入中...
搜索中...
未找到
topp_executor.h
浏览该文件的文档.
1#pragma once
2
3// Y3 TOPP-RA executor integration (algorithm contract §3).
4//
5// Bridge between TOPP planning-domain solvers (return optimal_time) and
6// the RT-domain executor (needs Profile1D with integer-tick segments).
7//
8// Pipeline:
9// 1. TOPP solver → ToppTimedResult (optimal_time, velocity profile)
10// 2. build_topp_timed_profile() → Profile1D via solve_fixed_time
11// 3. verify_joint_limits() → check per-axis limits along the path
12// 4. sample_profiled_path() → RT sampling (existing infrastructure)
13//
14// The scalar Profile1D distributes time uniformly (curvature-unaware).
15// TOPP's value is the globally optimal duration — joint-limit safety
16// is verified post-hoc at grid points, with fallback to scalar OTG.
17
18#include <algorithm>
19#include <cmath>
20#include <cstdint>
21
22#include "geom/geometry.h"
23#include "otg/profile1d.h"
24#include "otg/time_optimal.h"
25#include "plan/topp.h"
26#include "plan/topp_jerk.h"
27#include "rt/error.h"
28
29namespace plcopen::core::plan
30{
31
33{
34 static constexpr int MaxGrid = 1024;
35 double sdot_sq[MaxGrid] = {};
37 int grid_points = 0;
38 double ds = 0.0;
39 double path_length = 0.0;
40 double optimal_time = 0.0;
41 std::int64_t quantized_cycles = 0;
42
43 double sdot_at(int k) const
44 {
45 if(k < 0 || k >= grid_points) {
46 return 0.0;
47 }
48 return std::sqrt(std::max(sdot_sq[k], 0.0));
49 }
50
51 double interpolate_sdot(double s) const
52 {
53 if(ds <= 0.0 || grid_points < 2) {
54 return 0.0;
55 }
56 const double fk = s / ds;
57 const int k = static_cast<int>(fk);
58 if(k < 0) {
59 return sdot_at(0);
60 }
61 if(k >= grid_points - 1) {
62 return sdot_at(grid_points - 1);
63 }
64 const double t = fk - static_cast<double>(k);
65 return (1.0 - t) * sdot_at(k) + t * sdot_at(k + 1);
66 }
67};
68
70{
71
72inline bool valid_l1_limits(const ToppAxisLimits limits[3])
73{
74 for(int i = 0; i < 3; ++i) {
75 if(!std::isfinite(limits[i].max_velocity) || limits[i].max_velocity < 0.0 ||
76 !std::isfinite(limits[i].max_acceleration) || limits[i].max_acceleration < 0.0) {
77 return false;
78 }
79 }
80 return true;
81}
82
83inline bool valid_l2_limits(const ToppJerkAxisLimits limits[3])
84{
85 for(int i = 0; i < 3; ++i) {
86 if(!std::isfinite(limits[i].max_velocity) || limits[i].max_velocity < 0.0 ||
87 !std::isfinite(limits[i].max_acceleration) || limits[i].max_acceleration < 0.0 ||
88 !std::isfinite(limits[i].max_jerk) || limits[i].max_jerk < 0.0) {
89 return false;
90 }
91 }
92 return true;
93}
94
96 const double x_profile[],
97 int grid_size,
98 double ds,
99 double path_length)
100{
101 if(grid_size < 2 || grid_size + 1 > ToppVelocityProfile::MaxGrid ||
102 !std::isfinite(ds) || ds <= 0.0 || !std::isfinite(path_length) || path_length <= 0.0) {
105 }
107 vp.cumulative_time[0] = 0.0;
108 double total_time = 0.0;
109 for(int k = 0; k < grid_size; ++k) {
110 if(!std::isfinite(x_profile[k]) || !std::isfinite(x_profile[k + 1])) {
113 }
114 const double sd_k = std::sqrt(std::max(x_profile[k], 0.0));
115 const double sd_k1 = std::sqrt(std::max(x_profile[k + 1], 0.0));
116 const double sum = sd_k + sd_k1;
117 if(sum < 1e-30) {
120 }
121 total_time += 2.0 * ds / sum;
122 if(!std::isfinite(total_time) || total_time > 9.0e15) {
125 }
126 vp.cumulative_time[k + 1] = total_time;
127 }
128 if(!std::isfinite(total_time) || total_time <= 0.0 || total_time > 9.0e15) {
131 }
132 for(int k = 0; k <= grid_size; ++k) {
133 vp.sdot_sq[k] = std::max(x_profile[k], 0.0);
134 }
135 vp.grid_points = grid_size + 1;
136 vp.ds = ds;
137 vp.path_length = path_length;
138 vp.optimal_time = total_time;
139 vp.quantized_cycles = std::max<std::int64_t>(
140 1, static_cast<std::int64_t>(std::ceil(total_time)));
142}
143
145 const geom::PathSegment &path,
146 const ToppAxisLimits limits[3],
147 int grid_size)
148{
149 if(grid_size < 2 || !valid_l1_limits(limits)) {
152 }
153 const double L = path.length();
154 if(L <= 0.0) {
156 vp.grid_points = 1;
158 }
159
160 constexpr int MaxGrid = ToppVelocityProfile::MaxGrid;
161 if(grid_size + 1 > MaxGrid) {
164 }
165
166 const int N = grid_size;
167 const double ds = L / static_cast<double>(N);
168
169 double x_mvc[MaxGrid];
170 geom::Vec3 qs[MaxGrid];
171 geom::Vec3 qss[MaxGrid];
172
173 for(int k = 0; k <= N; ++k) {
174 const double s = ds * static_cast<double>(k);
175 qs[k] = path.path_derivative(s);
176 qss[k] = path.path_second_derivative(s);
177
178 double mvc = topp_detail::velocity_limit_squared(qs[k], limits);
179 const double cs[3] = {qs[k].x, qs[k].y, qs[k].z};
180 const double css[3] = {qss[k].x, qss[k].y, qss[k].z};
181 for(int i = 0; i < 3; ++i) {
182 mvc = std::min(mvc,
184 cs[i], css[i], limits[i].max_acceleration));
185 }
186 mvc = std::min(mvc,
188 x_mvc[k] = mvc;
189 }
190
191 double x_back[MaxGrid];
192 x_back[N] = 0.0;
193 for(int k = N - 1; k >= 0; --k) {
194 const double cs[3] = {qs[k].x, qs[k].y, qs[k].z};
195 const double css[3] = {qss[k].x, qss[k].y, qss[k].z};
196 double x_max = 1e30;
197 for(int i = 0; i < 3; ++i) {
198 x_max = std::min(x_max,
200 cs[i], css[i], limits[i].max_acceleration,
201 x_back[k + 1], ds));
202 }
203 x_max = std::min(x_max, x_mvc[k]);
204 x_back[k] = std::max(x_max, 0.0);
205 }
206
207 double x_fwd[MaxGrid];
208 x_fwd[0] = 0.0;
209 for(int k = 0; k < N; ++k) {
210 const double cs[3] = {qs[k].x, qs[k].y, qs[k].z};
211 const double css[3] = {qss[k].x, qss[k].y, qss[k].z};
212 double x_next = 1e30;
213 for(int i = 0; i < 3; ++i) {
214 x_next = std::min(x_next,
216 cs[i], css[i], limits[i].max_acceleration,
217 x_fwd[k], ds));
218 }
219 x_next = std::min(x_next, x_back[k + 1]);
220 x_next = std::min(x_next, x_mvc[k + 1]);
221 x_fwd[k + 1] = std::max(x_next, 0.0);
222 }
223
224 return finalize_topp_profile(x_fwd, N, ds, L);
225}
226
228 const double q_s[3],
229 const double q_ss[3],
230 const ToppJerkAxisLimits limits[3],
231 double x_prev,
232 double ds,
233 double &x_next,
234 double &a_next)
235{
236 x_next = std::max(x_next, 0.0);
237 auto ai = topp_jerk_detail::acceleration_interval(q_s, q_ss, x_next, limits);
238 if(!ai.feasible()) {
239 double scale = 1.0 - 1e-9;
240 for(int attempt = 0; attempt < 8 && !ai.feasible(); ++attempt) {
241 x_next *= scale;
242 ai = topp_jerk_detail::acceleration_interval(q_s, q_ss, x_next, limits);
243 scale *= 0.99;
244 }
245 if(!ai.feasible()) {
246 return false;
247 }
248 a_next = (x_next - x_prev) / (2.0 * ds);
249 }
250 a_next = std::max(a_next, ai.lo);
251 a_next = std::min(a_next, ai.hi);
252 return true;
253}
254
255// Layer 2 jerk-shaped phase-plane velocity envelope (KB-062 symmetric S-curve
256// forward/backward sweep projected into L1 velocity + acceleration reachability).
257// Note: piecewise-constant-acceleration sampling guarantees velocity and total
258// acceleration (including centripetal q_ss*sdot^2) bounds across all grid cells,
259// while continuous-time C2 jerk continuity across phase-plane switch/grid
260// junctions is NOT claimed.
262 const geom::PathSegment &path,
263 const ToppJerkAxisLimits limits[3],
264 int grid_size)
265{
266 if(grid_size < 2 || !valid_l2_limits(limits)) {
269 }
270 const double L = path.length();
271 if(L <= 0.0) {
273 vp.grid_points = 1;
275 }
276
277 constexpr int MaxGrid = ToppVelocityProfile::MaxGrid;
278 if(grid_size + 1 > MaxGrid) {
281 }
282
283 const int N = grid_size;
284 const double ds = L / static_cast<double>(N);
285
286 ToppAxisLimits l1[3];
287 for(int i = 0; i < 3; ++i) {
288 l1[i] = {limits[i].max_velocity, limits[i].max_acceleration};
289 }
290
291 double x_mvc[MaxGrid];
292 double qs_arr[MaxGrid][3];
293 double qss_arr[MaxGrid][3];
294 double qsss_arr[MaxGrid][3];
295
296 for(int k = 0; k <= N; ++k) {
297 const double s = ds * static_cast<double>(k);
298 const geom::Vec3 q_s = path.path_derivative(s);
299 const geom::Vec3 q_ss = path.path_second_derivative(s);
300 const geom::Vec3 q_sss = path.path_third_derivative(s);
301
302 qs_arr[k][0] = q_s.x;
303 qs_arr[k][1] = q_s.y;
304 qs_arr[k][2] = q_s.z;
305 qss_arr[k][0] = q_ss.x;
306 qss_arr[k][1] = q_ss.y;
307 qss_arr[k][2] = q_ss.z;
308 qsss_arr[k][0] = q_sss.x;
309 qsss_arr[k][1] = q_sss.y;
310 qsss_arr[k][2] = q_sss.z;
311
312 double mvc = topp_detail::velocity_limit_squared(q_s, l1);
313 for(int i = 0; i < 3; ++i) {
314 mvc = std::min(mvc,
316 qs_arr[k][i], qss_arr[k][i], l1[i].max_acceleration));
317 }
318 mvc = std::min(mvc,
320 qs_arr[k], qss_arr[k], l1));
321 x_mvc[k] = mvc;
322 }
323
324 // L1 acceleration reachability envelope so interval transitions stay
325 // feasible under centripetal + tangential acceleration bounds.
326 double x_l1_back[MaxGrid];
327 x_l1_back[N] = 0.0;
328 for(int k = N - 1; k >= 0; --k) {
329 double x_max = 1e30;
330 for(int i = 0; i < 3; ++i) {
331 x_max = std::min(x_max,
333 qs_arr[k][i], qss_arr[k][i], l1[i].max_acceleration,
334 x_l1_back[k + 1], ds));
335 }
336 x_l1_back[k] = std::max(std::min(x_max, x_mvc[k]), 0.0);
337 }
338
339 double x_l1_fwd[MaxGrid];
340 x_l1_fwd[0] = 0.0;
341 for(int k = 0; k < N; ++k) {
342 double x_next = 1e30;
343 for(int i = 0; i < 3; ++i) {
344 x_next = std::min(x_next,
346 qs_arr[k][i], qss_arr[k][i], l1[i].max_acceleration,
347 x_l1_fwd[k], ds));
348 }
349 x_next = std::min(x_next, x_l1_back[k + 1]);
350 x_l1_fwd[k + 1] = std::max(std::min(x_next, x_mvc[k + 1]), 0.0);
351 }
352
353 double x_fwd[MaxGrid];
354 double a_fwd[MaxGrid];
355 x_fwd[0] = 0.0;
356 a_fwd[0] = 0.0;
357 for(int k = 0; k < N; ++k) {
358 const double v_k = std::sqrt(std::max(x_fwd[k], 0.0));
359 double x_next = 0.0;
360 double a_next = 0.0;
361 if(v_k < 1e-15) {
362 const double j_eff =
363 topp_jerk_detail::effective_jerk_max(qs_arr[k], limits);
364 const double a_eff =
365 topp_jerk_detail::effective_accel_max(qs_arr[k], limits);
366 if(j_eff <= 1e-30 || a_eff <= 1e-30) {
369 }
370 const auto su = topp_jerk_detail::scurve_startup(ds, j_eff, a_eff);
371 x_next = su.x;
372 a_next = su.a;
373 } else {
374 x_next = x_fwd[k] + 2.0 * a_fwd[k] * ds;
375 const auto ji = topp_jerk_detail::jerk_interval(
376 qs_arr[k], qss_arr[k], qsss_arr[k],
377 x_fwd[k], a_fwd[k], limits);
378 const double j = ji.feasible() ? ji.hi : 0.0;
379 a_next = a_fwd[k] + j * ds / v_k;
380 }
381 if(x_next > x_l1_fwd[k + 1]) {
382 x_next = x_l1_fwd[k + 1];
383 a_next = (x_next - x_fwd[k]) / (2.0 * ds);
384 }
386 qs_arr[k + 1], qss_arr[k + 1], limits, x_fwd[k], ds, x_next, a_next)) {
389 }
390 x_fwd[k + 1] = x_next;
391 a_fwd[k + 1] = a_next;
392 }
393
394 double x_dec[MaxGrid];
395 double a_dec[MaxGrid];
396 x_dec[N] = 0.0;
397 a_dec[N] = 0.0;
398 for(int k = N - 1; k >= 0; --k) {
399 const double v_prev = std::sqrt(std::max(x_dec[k + 1], 0.0));
400 double x_next = 0.0;
401 double a_next = 0.0;
402 if(v_prev < 1e-15) {
403 const double j_eff =
404 topp_jerk_detail::effective_jerk_max(qs_arr[k + 1], limits);
405 const double a_eff =
406 topp_jerk_detail::effective_accel_max(qs_arr[k + 1], limits);
407 if(j_eff <= 1e-30 || a_eff <= 1e-30) {
410 }
411 const auto su = topp_jerk_detail::scurve_startup(ds, j_eff, a_eff);
412 x_next = su.x;
413 a_next = su.a;
414 } else {
415 x_next = x_dec[k + 1] + 2.0 * a_dec[k + 1] * ds;
416 const double qs_r[3] = {
417 -qs_arr[k + 1][0], -qs_arr[k + 1][1], -qs_arr[k + 1][2]};
418 const double qsss_r[3] = {
419 -qsss_arr[k + 1][0], -qsss_arr[k + 1][1], -qsss_arr[k + 1][2]};
420 const auto ji = topp_jerk_detail::jerk_interval(
421 qs_r, qss_arr[k + 1], qsss_r,
422 x_dec[k + 1], a_dec[k + 1], limits);
423 const double j = ji.feasible() ? ji.hi : 0.0;
424 a_next = a_dec[k + 1] + j * ds / v_prev;
425 }
426 if(x_next > x_l1_back[k]) {
427 x_next = x_l1_back[k];
428 a_next = (x_next - x_dec[k + 1]) / (2.0 * ds);
429 }
430 const double qs_out[3] = {
431 -qs_arr[k][0], -qs_arr[k][1], -qs_arr[k][2]};
433 qs_out, qss_arr[k], limits, x_dec[k + 1], ds, x_next, a_next)) {
436 }
437 x_dec[k] = x_next;
438 a_dec[k] = a_next;
439 }
440
441 double x_min[MaxGrid];
442 for(int k = 0; k <= N; ++k) {
443 x_min[k] = std::min({x_fwd[k], x_dec[k], x_l1_fwd[k], x_l1_back[k]});
444 }
445 // Final L1 backward + forward projection ensures every grid interval
446 // transition across the forward/backward switch point remains strictly
447 // inside velocity and centripetal+tangential acceleration bounds.
448 x_min[N] = 0.0;
449 for(int k = N - 1; k >= 0; --k) {
450 double x_max = x_min[k];
451 for(int i = 0; i < 3; ++i) {
452 x_max = std::min(x_max,
454 qs_arr[k][i], qss_arr[k][i], l1[i].max_acceleration,
455 x_min[k + 1], ds));
456 }
457 x_min[k] = std::max(x_max, 0.0);
458 }
459 x_min[0] = 0.0;
460 for(int k = 0; k < N; ++k) {
461 double x_max = x_min[k + 1];
462 for(int i = 0; i < 3; ++i) {
463 x_max = std::min(x_max,
465 qs_arr[k][i], qss_arr[k][i], l1[i].max_acceleration,
466 x_min[k], ds));
467 }
468 x_min[k + 1] = std::max(x_max, 0.0);
469 }
470 return finalize_topp_profile(x_min, N, ds, L);
471}
472
474 const ToppVelocityProfile &vp,
475 rt::CycleTick tick)
476{
477 const std::int64_t m = tick.cycles();
478 if(m <= 0) {
479 return {0.0, 0.0, 0.0};
480 }
481 if(m >= vp.quantized_cycles) {
482 return {vp.path_length, 0.0, 0.0};
483 }
484
485 const double total_time = vp.cumulative_time[vp.grid_points - 1];
486 const double eta = total_time / static_cast<double>(vp.quantized_cycles);
487 const double tau = static_cast<double>(m) * eta;
488
489 int lo = 0;
490 int hi = vp.grid_points - 2;
491 while(lo < hi) {
492 const int mid = lo + (hi - lo + 1) / 2;
493 if(vp.cumulative_time[mid] <= tau) {
494 lo = mid;
495 } else {
496 hi = mid - 1;
497 }
498 }
499
500 const double span = vp.cumulative_time[lo + 1] - vp.cumulative_time[lo];
501 const double sd_0 = vp.sdot_at(lo);
502 const double sd_1 = vp.sdot_at(lo + 1);
503 const double a_s = span > 0.0 ? (sd_1 - sd_0) / span : 0.0;
504 const double step_u = tau - vp.cumulative_time[lo];
505 const double s_base = vp.ds * static_cast<double>(lo);
506 double s = s_base + sd_0 * step_u + 0.5 * a_s * step_u * step_u;
507 if(s < 0.0) {
508 s = 0.0;
509 } else if(s > vp.path_length) {
510 s = vp.path_length;
511 }
512 const double sdot_cycle = eta * (sd_0 + a_s * step_u);
513 const double sddot_cycle = eta * eta * a_s;
514 return {s, sdot_cycle, sddot_cycle};
515}
516
517} // namespace topp_executor_detail
518
525
527{
529 vp.quantized_cycles <= 0 || !std::isfinite(vp.path_length) ||
530 vp.path_length <= 0.0 || !std::isfinite(vp.ds) || vp.ds <= 0.0 ||
531 !std::isfinite(vp.optimal_time) || vp.optimal_time <= 0.0 ||
532 vp.optimal_time > 9.0e15 ||
533 !std::isfinite(vp.cumulative_time[0]) || vp.cumulative_time[0] < 0.0 ||
534 !std::isfinite(vp.sdot_sq[0]) || vp.sdot_sq[0] < 0.0) {
535 return false;
536 }
537 for(int k = 0; k + 1 < vp.grid_points; ++k) {
538 if(!std::isfinite(vp.cumulative_time[k + 1]) ||
539 vp.cumulative_time[k + 1] <= vp.cumulative_time[k] ||
540 !std::isfinite(vp.sdot_sq[k + 1]) || vp.sdot_sq[k + 1] < 0.0) {
541 return false;
542 }
543 }
544 const double total = vp.cumulative_time[vp.grid_points - 1];
545 if(!std::isfinite(total) || total <= 0.0 ||
546 std::fabs(total - vp.optimal_time) > 1e-6 * std::max(1.0, vp.optimal_time)) {
547 return false;
548 }
549 return true;
550}
551
553{
554 if(!is_valid_topp_profile(vp)) {
555 return {};
556 }
558}
559
561 const geom::PathSegment &path,
562 const ToppVelocityProfile &vp,
563 rt::CycleTick tick)
564{
565 if(path.length() <= 0.0) {
566 return {};
567 }
568 const otg::State1D scalar = sample_topp_cycle(vp, tick);
569 const geom::Vec3 pos = path.sample(scalar.position);
570 const geom::Vec3 q_s = path.path_derivative(scalar.position);
571 const geom::Vec3 q_ss = path.path_second_derivative(scalar.position);
572 const geom::Vec3 vel = q_s * scalar.velocity;
573 const geom::Vec3 acc =
574 q_ss * (scalar.velocity * scalar.velocity) + q_s * scalar.acceleration;
575 return {pos, vel, acc};
576}
577
578// Planning-domain post-hoc verifier for a curvature-aware ToppVelocityProfile.
579// Default tolerance = 1.02 accounts for finite-grid phase-plane discretization
580// margin; strict hardware callers should scale limits or pass tolerance = 1.0.
581// Verification work is resource-bounded to at most MaxVerifySamples ticks plus
582// all grid-node transition ticks, using O(log N) binary search per sample.
584 const geom::PathSegment &path,
585 const ToppVelocityProfile &vp,
586 const ToppAxisLimits limits[3],
587 double tolerance = 1.02)
588{
589 if(!std::isfinite(tolerance) || tolerance <= 0.0 ||
591 return false;
592 }
593 const double length = path.length();
594 if(length <= 0.0) {
595 return vp.path_length == 0.0 && vp.quantized_cycles == 0;
596 }
597 if(!is_valid_topp_profile(vp)) {
598 return false;
599 }
600 if(std::fabs(vp.path_length - length) > 1e-9 * std::max(1.0, length)) {
601 return false;
602 }
603
604 auto check_tick = [&](std::int64_t m) -> bool {
605 const otg::State1D scalar =
608 const geom::Vec3 q_s = path.path_derivative(scalar.position);
609 const geom::Vec3 q_ss = path.path_second_derivative(scalar.position);
610 const geom::Vec3 vel_v = q_s * scalar.velocity;
611 const geom::Vec3 acc_v =
612 q_ss * (scalar.velocity * scalar.velocity) + q_s * scalar.acceleration;
613 const double vel[3] = {vel_v.x, vel_v.y, vel_v.z};
614 const double acc[3] = {acc_v.x, acc_v.y, acc_v.z};
615 for(int i = 0; i < 3; ++i) {
616 if(!std::isfinite(vel[i]) || !std::isfinite(acc[i])) {
617 return false;
618 }
619 if(limits[i].max_velocity > 0.0 &&
620 std::fabs(vel[i]) > limits[i].max_velocity * tolerance) {
621 return false;
622 }
623 if(limits[i].max_acceleration > 0.0 &&
624 std::fabs(acc[i]) > limits[i].max_acceleration * tolerance) {
625 return false;
626 }
627 }
628 return true;
629 };
630
631 constexpr std::int64_t MaxVerifySamples = 100000;
632 const std::int64_t stride =
633 vp.quantized_cycles > MaxVerifySamples
634 ? (vp.quantized_cycles / MaxVerifySamples + 1)
635 : 1;
636 for(std::int64_t m = 0; m <= vp.quantized_cycles; m += stride) {
637 if(!check_tick(m)) {
638 return false;
639 }
640 }
641 if(!check_tick(vp.quantized_cycles)) {
642 return false;
643 }
644 if(stride > 1) {
645 const double total_time = vp.cumulative_time[vp.grid_points - 1];
646 const double inv_eta = static_cast<double>(vp.quantized_cycles) / total_time;
647 for(int k = 1; k + 1 < vp.grid_points; ++k) {
648 const std::int64_t m = static_cast<std::int64_t>(
649 std::llround(vp.cumulative_time[k] * inv_eta));
650 if(m >= 0 && m <= vp.quantized_cycles && !check_tick(m)) {
651 return false;
652 }
653 }
654 }
655 return true;
656}
657
658// Compute effective scalar limits from per-axis limits and worst-case tangent
659// envelope across 17 uniform samples along the path.
661 const geom::PathSegment &path,
662 const ToppAxisLimits limits[3])
663{
664 double worst_qs[3] = {0.0, 0.0, 0.0};
665 const double length = path.length();
666 if(length > 0.0) {
667 constexpr int Samples = 16;
668 for(int k = 0; k <= Samples; ++k) {
669 const double s = length * static_cast<double>(k) / static_cast<double>(Samples);
670 const geom::Vec3 q_s = path.path_derivative(s);
671 worst_qs[0] = std::max(worst_qs[0], std::fabs(q_s.x));
672 worst_qs[1] = std::max(worst_qs[1], std::fabs(q_s.y));
673 worst_qs[2] = std::max(worst_qs[2], std::fabs(q_s.z));
674 }
675 }
676
677 double v_eff = 1e30;
678 double a_eff = 1e30;
679 for(int i = 0; i < 3; ++i) {
680 const double a = worst_qs[i];
681 if(a > 1e-15) {
682 if(limits[i].max_velocity > 0.0) {
683 v_eff = std::min(v_eff, limits[i].max_velocity / a);
684 }
685 a_eff = std::min(a_eff, limits[i].max_acceleration / a);
686 }
687 }
688 if(v_eff > 1e29) {
689 v_eff = 1.0;
690 }
691 if(a_eff > 1e29) {
692 a_eff = 1.0;
693 }
694
695 return {v_eff, a_eff, a_eff, a_eff * a_eff / std::max(v_eff * 0.01, 1e-15)};
696}
697
699 const geom::PathSegment &path,
700 const ToppJerkAxisLimits limits[3])
701{
702 double worst_qs[3] = {0.0, 0.0, 0.0};
703 const double length = path.length();
704 if(length > 0.0) {
705 constexpr int Samples = 16;
706 for(int k = 0; k <= Samples; ++k) {
707 const double s = length * static_cast<double>(k) / static_cast<double>(Samples);
708 const geom::Vec3 q_s = path.path_derivative(s);
709 worst_qs[0] = std::max(worst_qs[0], std::fabs(q_s.x));
710 worst_qs[1] = std::max(worst_qs[1], std::fabs(q_s.y));
711 worst_qs[2] = std::max(worst_qs[2], std::fabs(q_s.z));
712 }
713 }
714
715 double v_eff = 1e30;
716 double a_eff = 1e30;
717 double j_eff = 1e30;
718 for(int i = 0; i < 3; ++i) {
719 const double a = worst_qs[i];
720 if(a > 1e-15) {
721 if(limits[i].max_velocity > 0.0) {
722 v_eff = std::min(v_eff, limits[i].max_velocity / a);
723 }
724 a_eff = std::min(a_eff, limits[i].max_acceleration / a);
725 j_eff = std::min(j_eff, limits[i].max_jerk / a);
726 }
727 }
728 if(v_eff > 1e29) {
729 v_eff = 1.0;
730 }
731 if(a_eff > 1e29) {
732 a_eff = 1.0;
733 }
734 if(j_eff > 1e29) {
735 j_eff = a_eff * 10.0;
736 }
737
738 return {v_eff, a_eff, a_eff, j_eff};
739}
740
741// Verify that the Profile1D trajectory respects per-axis joint limits
742// when mapped through the given path geometry.
743// Returns true if all sampled points are within tolerance.
745 const otg::Profile1D &profile,
746 const geom::PathSegment &path,
747 const ToppAxisLimits limits[3],
748 int num_samples = 64,
749 double tolerance = 1.01)
750{
751 const std::int64_t total = profile.duration_cycles();
752 if(total <= 0) {
753 return true;
754 }
755
756 const int step = std::max(
757 static_cast<int>(total / static_cast<std::int64_t>(num_samples)), 1);
758
759 for(std::int64_t t = 0; t <= total; t += step) {
760 const otg::State1D st =
762 const double s = st.position;
763 const double sdot = st.velocity;
764
765 if(s < 0.0 || s > path.length() * 1.001) {
766 continue;
767 }
768
769 const geom::Vec3 q_s = path.path_derivative(
770 std::min(s, path.length()));
771 const double qs[3] = {q_s.x, q_s.y, q_s.z};
772
773 for(int i = 0; i < 3; ++i) {
774 const double vi = std::fabs(qs[i] * sdot);
775 if(limits[i].max_velocity > 0.0 &&
776 vi > limits[i].max_velocity * tolerance) {
777 return false;
778 }
779 }
780 }
781 return true;
782}
783
792
794 const geom::PathSegment &path,
795 const ToppAxisLimits limits[3],
796 int grid_size = 128)
797{
799 path, limits, grid_size);
800 if(!topp) {
801 return rt::Result<ToppProfileResult>::failure(topp.error());
802 }
804 r.velocity_profile = topp.value();
805 r.topp_time = r.velocity_profile.optimal_time;
806 r.quantized_cycles = r.velocity_profile.quantized_cycles;
807 r.curvature_verified = verify_topp_joint_limits(
808 path, r.velocity_profile, limits);
810}
811
813 const geom::PathSegment &path,
814 const ToppJerkAxisLimits limits[3],
815 int grid_size = 128)
816{
818 path, limits, grid_size);
819 if(!topp) {
820 return rt::Result<ToppProfileResult>::failure(topp.error());
821 }
822 ToppAxisLimits verify_limits[3];
823 for(int i = 0; i < 3; ++i) {
824 verify_limits[i] = {limits[i].max_velocity, limits[i].max_acceleration};
825 }
827 r.velocity_profile = topp.value();
828 r.topp_time = r.velocity_profile.optimal_time;
829 r.quantized_cycles = r.velocity_profile.quantized_cycles;
830 r.curvature_verified = verify_topp_joint_limits(
831 path, r.velocity_profile, verify_limits);
833}
834
835// Full TOPP executor pipeline: solve TOPP → quantize → build Profile1D.
836//
837// For acceleration-limited TOPP (Layer 1). Uses solve_fixed_time with
838// ceil(T_topp) cycles as the prescribed duration. The resulting Profile1D
839// traverses path_length in exactly that many cycles.
840//
841// If the TOPP time is shorter than the scalar OTG minimum (rare — happens
842// when effective scalar limits are more conservative than the per-axis
843// TOPP limits at non-midpoint sections), the scalar OTG minimum wins.
845 const geom::PathSegment &path,
846 const ToppAxisLimits limits[3],
847 int grid_size = 128)
848{
850 path, limits, grid_size);
851 if(!topp) {
852 return rt::Result<ToppProfileResult>::failure(topp.error());
853 }
854
855 const ToppVelocityProfile &vp = topp.value();
856 if(vp.path_length <= 0.0) {
858 r.curvature_verified = true;
860 }
861
862 const otg::Limits1D scalar = effective_scalar_limits(path, limits);
863
864 const auto scalar_min = otg::plan_time_optimal(
865 {0.0, 0.0, 0.0}, {vp.path_length, 0.0, 0.0}, scalar);
866 const std::int64_t t_scalar_min =
867 scalar_min ? scalar_min.value().duration_cycles() : 1;
868
869 if(!std::isfinite(vp.optimal_time) || vp.optimal_time > 9.0e15) {
871 }
872 std::int64_t total_cycles =
873 static_cast<std::int64_t>(std::ceil(vp.optimal_time));
874 if(total_cycles < t_scalar_min) {
875 total_cycles = t_scalar_min;
876 }
877 if(total_cycles < 1) {
878 total_cycles = 1;
879 }
880
881 const auto profile = otg::solve_fixed_time(
882 {0.0, 0.0, 0.0},
883 {vp.path_length, 0.0, 0.0},
884 scalar,
885 total_cycles);
886 if(!profile) {
887 return rt::Result<ToppProfileResult>::failure(profile.error());
888 }
889
891 r.profile = profile.value();
892 r.velocity_profile = vp;
893 r.topp_time = vp.optimal_time;
894 r.quantized_cycles = total_cycles;
895 r.curvature_verified = verify_joint_limits(
896 r.profile, path, limits);
897
899}
900
901// Full TOPP executor pipeline with jerk-aware solver (Layer 2).
903 const geom::PathSegment &path,
904 const ToppJerkAxisLimits limits[3],
905 int grid_size = 128)
906{
908 path, limits, grid_size);
909 if(!topp) {
910 return rt::Result<ToppProfileResult>::failure(topp.error());
911 }
912
913 const double T = topp.value().optimal_time;
914 const double L = path.length();
915 if(L <= 0.0) {
917 r.velocity_profile = topp.value();
918 r.curvature_verified = true;
920 }
921
922 const otg::Limits1D scalar = effective_scalar_limits(path, limits);
923
924 const auto scalar_min = otg::plan_time_optimal(
925 {0.0, 0.0, 0.0}, {L, 0.0, 0.0}, scalar);
926 const std::int64_t t_scalar_min =
927 scalar_min ? scalar_min.value().duration_cycles() : 1;
928
929 if(!std::isfinite(T) || T > 9.0e15) {
931 }
932 std::int64_t total_cycles =
933 static_cast<std::int64_t>(std::ceil(T));
934 if(total_cycles < t_scalar_min) {
935 total_cycles = t_scalar_min;
936 }
937 if(total_cycles < 1) {
938 total_cycles = 1;
939 }
940
941 const auto profile = otg::solve_fixed_time(
942 {0.0, 0.0, 0.0},
943 {L, 0.0, 0.0},
944 scalar,
945 total_cycles);
946 if(!profile) {
947 return rt::Result<ToppProfileResult>::failure(profile.error());
948 }
949
950 ToppAxisLimits verify_limits[3];
951 for(int i = 0; i < 3; ++i) {
952 verify_limits[i] = {limits[i].max_velocity, limits[i].max_acceleration};
953 }
954
956 r.profile = profile.value();
957 r.velocity_profile = topp.value();
958 r.topp_time = T;
959 r.quantized_cycles = total_cycles;
960 r.curvature_verified = verify_joint_limits(
961 r.profile, path, verify_limits);
962
964}
965
966} // namespace plcopen::core::plan
std::int64_t duration_cycles() const
constexpr std::int64_t cycles() const
定义 cycle.h:47
static constexpr CycleTick from_cycles(std::int64_t cycles)
定义 cycle.h:42
static constexpr Result success(const T &value)
定义 error.h:24
static constexpr Result failure(ErrorCode error)
定义 error.h:29
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)
double axis_backward_reach(double q_s_i, double q_ss_i, double a_max_i, double x_next, double ds)
定义 topp.h:103
double acceleration_velocity_limit(double q_s_i, double q_ss_i, double a_max_i)
定义 topp.h:54
double axis_forward_reach(double q_s_i, double q_ss_i, double a_max_i, double x_k, double ds)
定义 topp.h:124
double velocity_limit_squared(geom::Vec3 q_s, const ToppAxisLimits limits[3])
定义 topp.h:40
double cross_axis_velocity_limit(const double q_s[3], const double q_ss[3], const ToppAxisLimits limits[3])
定义 topp.h:69
rt::Result< ToppVelocityProfile > solve_topp_profile_l1(const geom::PathSegment &path, const ToppAxisLimits limits[3], int grid_size)
rt::Result< ToppVelocityProfile > finalize_topp_profile(const double x_profile[], int grid_size, double ds, double path_length)
otg::State1D sample_topp_cycle_unchecked(const ToppVelocityProfile &vp, rt::CycleTick tick)
rt::Result< ToppVelocityProfile > solve_topp_profile_l2_jerk(const geom::PathSegment &path, const ToppJerkAxisLimits limits[3], int grid_size)
bool valid_l2_limits(const ToppJerkAxisLimits limits[3])
bool valid_l1_limits(const ToppAxisLimits limits[3])
bool enforce_feasible_accel_state(const double q_s[3], const double q_ss[3], const ToppJerkAxisLimits limits[3], double x_prev, double ds, double &x_next, double &a_next)
Interval jerk_interval(const double q_s[3], const double q_ss[3], const double q_sss[3], double x, double a, const ToppJerkAxisLimits limits[3])
double effective_jerk_max(const double q_s[3], const ToppJerkAxisLimits limits[3])
StartupResult scurve_startup(double ds, double j_eff, double a_eff)
Interval acceleration_interval(const double q_s[3], const double q_ss[3], double x, const ToppJerkAxisLimits limits[3])
double effective_accel_max(const double q_s[3], const ToppJerkAxisLimits limits[3])
rt::Result< ToppProfileResult > plan_topp_curvature_aware(const geom::PathSegment &path, const ToppAxisLimits limits[3], int grid_size=128)
rt::Result< ToppProfileResult > plan_topp_profiled_jerk(const geom::PathSegment &path, const ToppJerkAxisLimits limits[3], int grid_size=128)
otg::State1D sample_topp_cycle(const ToppVelocityProfile &vp, rt::CycleTick tick)
bool verify_joint_limits(const otg::Profile1D &profile, const geom::PathSegment &path, const ToppAxisLimits limits[3], int num_samples=64, double tolerance=1.01)
rt::Result< ToppProfileResult > plan_topp_curvature_aware_jerk(const geom::PathSegment &path, const ToppJerkAxisLimits limits[3], int grid_size=128)
ToppJointSample sample_topp_joint_state(const geom::PathSegment &path, const ToppVelocityProfile &vp, rt::CycleTick tick)
rt::Result< ToppProfileResult > plan_topp_profiled(const geom::PathSegment &path, const ToppAxisLimits limits[3], int grid_size=128)
bool verify_topp_joint_limits(const geom::PathSegment &path, const ToppVelocityProfile &vp, const ToppAxisLimits limits[3], double tolerance=1.02)
otg::Limits1D effective_scalar_limits(const geom::PathSegment &path, const ToppAxisLimits limits[3])
bool is_valid_topp_profile(const ToppVelocityProfile &vp)
Vec3 sample(double arclength) const
Vec3 path_derivative(double arclength) const
Vec3 path_third_derivative(double arclength) const
Vec3 path_second_derivative(double arclength) const