plcopen 0.22.0
载入中...
搜索中...
未找到
topp_jerk.h
浏览该文件的文档.
1#pragma once
2
3// TOPP-RA Layer 2 (jerk-aware) path parameterization.
4//
5// Given a path q(s) with known q_s, q_ss, q_sss:
6// |q_s_i · ṡ| ≤ v_max_i (velocity)
7// |q_ss_i · ṡ² + q_s_i · s̈| ≤ a_max_i (acceleration)
8// |q_sss_i · ṡ³ + 3·q_ss_i·ṡ·s̈ + q_s_i·s⃛| ≤ j_max_i (jerk, linear in s⃛)
9//
10// Method: forward S-curve from start + backward S-curve from end (deceleration
11// envelope on time-reversed path), pointwise minimum, trapezoidal time
12// integration. Planning-domain only.
13
14#include <algorithm>
15#include <cmath>
16#include <cstddef>
17
18#include "geom/geometry.h"
19#include "plan/topp.h"
20#include "rt/error.h"
21
22namespace plcopen::core::plan
23{
24
26{
27 double max_velocity = 0.0;
28 double max_acceleration = 0.0;
29 double max_jerk = 0.0;
30};
31
33{
34
36{
37 double lo = -1e30;
38 double hi = 1e30;
39 bool feasible() const { return lo <= hi + 1e-15; }
40};
41
43 const double q_s[3], const double q_ss[3],
44 double x, const ToppJerkAxisLimits limits[3])
45{
46 Interval iv;
47 for(int i = 0; i < 3; ++i) {
48 const double abs_qs = std::fabs(q_s[i]);
49 if(abs_qs <= 1e-15) {
50 if(std::fabs(q_ss[i] * x) > limits[i].max_acceleration + 1e-10) {
51 return {1.0, -1.0};
52 }
53 continue;
54 }
55 double lo_i = (-limits[i].max_acceleration - q_ss[i] * x) / q_s[i];
56 double hi_i = (+limits[i].max_acceleration - q_ss[i] * x) / q_s[i];
57 if(q_s[i] < 0.0) {
58 std::swap(lo_i, hi_i);
59 }
60 iv.lo = std::max(iv.lo, lo_i);
61 iv.hi = std::min(iv.hi, hi_i);
62 }
63 return iv;
64}
65
67 const double q_s[3], const double q_ss[3], const double q_sss[3],
68 double x, double a, const ToppJerkAxisLimits limits[3])
69{
70 const double v = std::sqrt(std::max(x, 0.0));
71 Interval iv;
72 for(int i = 0; i < 3; ++i) {
73 const double centripetal = q_sss[i] * v * v * v + 3.0 * q_ss[i] * v * a;
74 const double abs_qs = std::fabs(q_s[i]);
75 if(abs_qs <= 1e-15) {
76 if(std::fabs(centripetal) > limits[i].max_jerk + 1e-10) {
77 return {1.0, -1.0};
78 }
79 continue;
80 }
81 double lo_i = (-limits[i].max_jerk - centripetal) / q_s[i];
82 double hi_i = (+limits[i].max_jerk - centripetal) / q_s[i];
83 if(q_s[i] < 0.0) {
84 std::swap(lo_i, hi_i);
85 }
86 iv.lo = std::max(iv.lo, lo_i);
87 iv.hi = std::min(iv.hi, hi_i);
88 }
89 return iv;
90}
91
92inline double effective_jerk_max(const double q_s[3], const ToppJerkAxisLimits limits[3])
93{
94 double j_eff = 1e30;
95 for(int i = 0; i < 3; ++i) {
96 const double abs_qs = std::fabs(q_s[i]);
97 if(abs_qs > 1e-15 && limits[i].max_jerk > 0.0) {
98 j_eff = std::min(j_eff, limits[i].max_jerk / abs_qs);
99 }
100 }
101 return j_eff;
102}
103
104inline double effective_accel_max(const double q_s[3], const ToppJerkAxisLimits limits[3])
105{
106 double a_eff = 1e30;
107 for(int i = 0; i < 3; ++i) {
108 const double abs_qs = std::fabs(q_s[i]);
109 if(abs_qs > 1e-15 && limits[i].max_acceleration > 0.0) {
110 a_eff = std::min(a_eff, limits[i].max_acceleration / abs_qs);
111 }
112 }
113 return a_eff;
114}
115
117{
118 double x;
119 double a;
120};
121
122// S-curve startup from rest: time-domain solution for the first grid step
123// where da/ds = j/v is singular at v=0.
124inline StartupResult scurve_startup(double ds, double j_eff, double a_eff)
125{
126 const double t_jerk = std::cbrt(6.0 * ds / j_eff);
127 const double a_at_end = j_eff * t_jerk;
128
129 if(a_at_end <= a_eff) {
130 const double v_exit = 0.5 * j_eff * t_jerk * t_jerk;
131 return {v_exit * v_exit, a_at_end};
132 }
133
134 const double t1 = a_eff / j_eff;
135 const double v1 = 0.5 * j_eff * t1 * t1;
136 const double ds_remain = ds - j_eff * t1 * t1 * t1 / 6.0;
137 if(ds_remain <= 0.0) {
138 return {v1 * v1, a_eff};
139 }
140 const double disc = v1 * v1 + 2.0 * a_eff * ds_remain;
141 const double v_exit = std::sqrt(std::max(disc, 0.0));
142 return {v_exit * v_exit, a_eff};
143}
144
145} // namespace topp_jerk_detail
146
148 const ToppJerkAxisLimits limits[3],
149 int grid_size)
150{
151 if(grid_size < 2) {
153 }
154 const double L = path.length();
155 if(L <= 0.0) {
157 }
158
159 constexpr int MaxGrid = 1024;
160 if(grid_size + 1 > MaxGrid) {
162 }
163
164 const int N = grid_size;
165 const double ds = L / static_cast<double>(N);
166
167 double x_mvc[MaxGrid];
168 double qs_arr[MaxGrid][3];
169 double qss_arr[MaxGrid][3];
170 double qsss_arr[MaxGrid][3];
171
172 for(int k = 0; k <= N; ++k) {
173 const double s = ds * static_cast<double>(k);
174 const geom::Vec3 q_s = path.path_derivative(s);
175 const geom::Vec3 q_ss = path.path_second_derivative(s);
176 const geom::Vec3 q_sss = path.path_third_derivative(s);
177
178 qs_arr[k][0] = q_s.x;
179 qs_arr[k][1] = q_s.y;
180 qs_arr[k][2] = q_s.z;
181 qss_arr[k][0] = q_ss.x;
182 qss_arr[k][1] = q_ss.y;
183 qss_arr[k][2] = q_ss.z;
184 qsss_arr[k][0] = q_sss.x;
185 qsss_arr[k][1] = q_sss.y;
186 qsss_arr[k][2] = q_sss.z;
187
188 ToppAxisLimits l1[3];
189 for(int i = 0; i < 3; ++i) {
190 l1[i] = {limits[i].max_velocity, limits[i].max_acceleration};
191 }
192 double mvc = topp_detail::velocity_limit_squared(q_s, l1);
193 const double cs[3] = {q_s.x, q_s.y, q_s.z};
194 const double css[3] = {q_ss.x, q_ss.y, q_ss.z};
195 for(int i = 0; i < 3; ++i) {
196 mvc = std::min(mvc,
198 cs[i], css[i], l1[i].max_acceleration));
199 }
200 mvc = std::min(mvc, topp_detail::cross_axis_velocity_limit(cs, css, l1));
201 x_mvc[k] = mvc;
202 }
203
204 // Forward S-curve: accelerate from rest with max jerk, clamped to MVC.
205 double x_fwd[MaxGrid];
206 double a_fwd[MaxGrid];
207 x_fwd[0] = 0.0;
208 a_fwd[0] = 0.0;
209
210 for(int k = 0; k < N; ++k) {
211 const double v_k = std::sqrt(std::max(x_fwd[k], 0.0));
212 double x_next, a_next;
213
214 if(v_k < 1e-15) {
215 const double j_eff =
216 topp_jerk_detail::effective_jerk_max(qs_arr[k], limits);
217 const double a_eff =
218 topp_jerk_detail::effective_accel_max(qs_arr[k], limits);
219
220 if(j_eff <= 1e-30 || a_eff <= 1e-30) {
222 }
223
224 const auto su = topp_jerk_detail::scurve_startup(ds, j_eff, a_eff);
225 x_next = su.x;
226 a_next = su.a;
227 } else {
228 x_next = x_fwd[k] + 2.0 * a_fwd[k] * ds;
229
230 const auto ji = topp_jerk_detail::jerk_interval(
231 qs_arr[k], qss_arr[k], qsss_arr[k],
232 x_fwd[k], a_fwd[k], limits);
233 const double j = ji.feasible() ? ji.hi : 0.0;
234 a_next = a_fwd[k] + j * ds / v_k;
235 }
236
237 if(x_next > x_mvc[k + 1]) {
238 x_next = x_mvc[k + 1];
239 a_next = (x_next - x_fwd[k]) / (2.0 * ds);
240 }
241
243 qs_arr[k + 1], qss_arr[k + 1], std::max(x_next, 0.0), limits);
244 if(ai.feasible()) {
245 a_next = std::max(a_next, ai.lo);
246 a_next = std::min(a_next, ai.hi);
247 }
248
249 x_fwd[k + 1] = std::max(x_next, 0.0);
250 a_fwd[k + 1] = a_next;
251 }
252
253 // Deceleration envelope: forward S-curve from end on time-reversed path.
254 // Reversed derivatives: q_s' = -q_s, q_ss' = q_ss, q_sss' = -q_sss.
255 double x_dec[MaxGrid];
256 double a_dec[MaxGrid];
257 x_dec[N] = 0.0;
258 a_dec[N] = 0.0;
259
260 for(int k = N - 1; k >= 0; --k) {
261 const double v_prev = std::sqrt(std::max(x_dec[k + 1], 0.0));
262 double x_next, a_next;
263
264 if(v_prev < 1e-15) {
265 const double j_eff =
266 topp_jerk_detail::effective_jerk_max(qs_arr[k + 1], limits);
267 const double a_eff =
268 topp_jerk_detail::effective_accel_max(qs_arr[k + 1], limits);
269
270 if(j_eff <= 1e-30 || a_eff <= 1e-30) {
272 }
273
274 const auto su = topp_jerk_detail::scurve_startup(ds, j_eff, a_eff);
275 x_next = su.x;
276 a_next = su.a;
277 } else {
278 x_next = x_dec[k + 1] + 2.0 * a_dec[k + 1] * ds;
279
280 const double qs_r[3] = {
281 -qs_arr[k + 1][0], -qs_arr[k + 1][1], -qs_arr[k + 1][2]};
282 const double qsss_r[3] = {
283 -qsss_arr[k + 1][0], -qsss_arr[k + 1][1], -qsss_arr[k + 1][2]};
284 const auto ji = topp_jerk_detail::jerk_interval(
285 qs_r, qss_arr[k + 1], qsss_r,
286 x_dec[k + 1], a_dec[k + 1], limits);
287 const double j = ji.feasible() ? ji.hi : 0.0;
288 a_next = a_dec[k + 1] + j * ds / v_prev;
289 }
290
291 if(x_next > x_mvc[k]) {
292 x_next = x_mvc[k];
293 a_next = (x_next - x_dec[k + 1]) / (2.0 * ds);
294 }
295
296 const double qs_out[3] = {
297 -qs_arr[k][0], -qs_arr[k][1], -qs_arr[k][2]};
299 qs_out, qss_arr[k], std::max(x_next, 0.0), limits);
300 if(ai.feasible()) {
301 a_next = std::max(a_next, ai.lo);
302 a_next = std::min(a_next, ai.hi);
303 }
304
305 x_dec[k] = std::max(x_next, 0.0);
306 a_dec[k] = a_next;
307 }
308
309 // Time integration using pointwise min of forward and backward profiles.
310 double total_time = 0.0;
311 for(int k = 0; k < N; ++k) {
312 const double x_k = std::min(x_fwd[k], x_dec[k]);
313 const double x_k1 = std::min(x_fwd[k + 1], x_dec[k + 1]);
314 const double sd_k = std::sqrt(std::max(x_k, 0.0));
315 const double sd_k1 = std::sqrt(std::max(x_k1, 0.0));
316 const double sum = sd_k + sd_k1;
317 if(sum < 1e-30) {
319 }
320 total_time += 2.0 * ds / sum;
321 }
322
324}
325
326} // namespace plcopen::core::plan
static constexpr Result success(const T &value)
定义 error.h:24
static constexpr Result failure(ErrorCode error)
定义 error.h:29
double acceleration_velocity_limit(double q_s_i, double q_ss_i, double a_max_i)
定义 topp.h:54
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
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< ToppResult > solve_topp_ra_jerk(const geom::PathSegment &path, const ToppJerkAxisLimits limits[3], int grid_size)
Vec3 path_derivative(double arclength) const
Vec3 path_third_derivative(double arclength) const
Vec3 path_second_derivative(double arclength) const