plcopen 0.22.0
载入中...
搜索中...
未找到
topp.h
浏览该文件的文档.
1#pragma once
2
3// TOPP-RA Layer 1 (acceleration-limited) path parameterization.
4//
5// Algorithm contract §3: TOPP-RA on scalar path parameter s, state = ṡ,
6// constraints = per-axis velocity and acceleration limits.
7//
8// Given a path q(s) with known q_s = dq/ds and q_ss = d²q/ds², computes
9// the time-optimal rest-to-rest traversal time subject to:
10// |q_s_i · ṡ| ≤ v_max_i (velocity)
11// |q_ss_i · ṡ² + q_s_i · s̈| ≤ a_max_i (acceleration, linear in s̈)
12//
13// Method: discretize path, backward reachability pass, forward integration.
14// Planning-domain only; no RT constraints.
15
16#include <algorithm>
17#include <cmath>
18#include <cstddef>
19
20#include "geom/geometry.h"
21#include "rt/error.h"
22
23namespace plcopen::core::plan
24{
25
27{
28 double max_velocity = 0.0;
29 double max_acceleration = 0.0;
30};
31
33{
34 double optimal_time = 0.0;
35};
36
37namespace topp_detail
38{
39
40inline double velocity_limit_squared(geom::Vec3 q_s, const ToppAxisLimits limits[3])
41{
42 double x_max = 1e30;
43 const double c[3] = {q_s.x, q_s.y, q_s.z};
44 for(int i = 0; i < 3; ++i) {
45 const double a = std::fabs(c[i]);
46 if(a > 1e-15 && limits[i].max_velocity > 0.0) {
47 const double bound = limits[i].max_velocity / a;
48 x_max = std::min(x_max, bound * bound);
49 }
50 }
51 return x_max;
52}
53
54inline double acceleration_velocity_limit(double q_s_i, double q_ss_i, double a_max_i)
55{
56 if(std::fabs(q_s_i) > 1e-15) {
57 return 1e30;
58 }
59 if(std::fabs(q_ss_i) <= 1e-15) {
60 return 1e30;
61 }
62 return a_max_i / std::fabs(q_ss_i);
63}
64
65// Cross-axis feasibility velocity limit.
66// When two axes have different q_ss/q_s ratios, their acceleration bounds
67// on s̈ diverge with increasing ṡ², eventually becoming incompatible.
68// Returns the maximum ṡ² at which the intersection [α, β] is non-empty.
70 const double q_s[3], const double q_ss[3],
71 const ToppAxisLimits limits[3])
72{
73 double x_max = 1e30;
74 for(int i = 0; i < 3; ++i) {
75 const double abs_ai = std::fabs(q_s[i]);
76 if(abs_ai <= 1e-15) {
77 continue;
78 }
79 const double ci = q_ss[i] / q_s[i];
80 for(int j = i + 1; j < 3; ++j) {
81 const double abs_aj = std::fabs(q_s[j]);
82 if(abs_aj <= 1e-15) {
83 continue;
84 }
85 const double cj = q_ss[j] / q_s[j];
86 const double dc = std::fabs(ci - cj);
87 if(dc <= 1e-15) {
88 continue;
89 }
90 const double R = limits[i].max_acceleration / abs_ai +
91 limits[j].max_acceleration / abs_aj;
92 x_max = std::min(x_max, R / dc);
93 }
94 }
95 return x_max;
96}
97
98// Per-axis backward reachable ṡ² from x_next over interval ds.
99// Uses minimum forward acceleration (maximum deceleration):
100// u_lo = -a_max/|q_s| - (q_ss/q_s)·x
101// x_next = x + 2·u_lo·ds
102// Solves for x.
103inline double axis_backward_reach(double q_s_i, double q_ss_i, double a_max_i,
104 double x_next, double ds)
105{
106 const double abs_a = std::fabs(q_s_i);
107 if(abs_a <= 1e-15) {
108 return 1e30;
109 }
110 const double c0 = -a_max_i / abs_a;
111 const double c1 = -q_ss_i / q_s_i;
112 const double denom = 1.0 + 2.0 * c1 * ds;
113 if(denom <= 1e-15) {
114 return 1e30;
115 }
116 const double result = (x_next - 2.0 * c0 * ds) / denom;
117 return std::max(result, 0.0);
118}
119
120// Per-axis forward reachable ṡ² from x_k over interval ds.
121// Uses maximum forward acceleration:
122// u_hi = a_max/|q_s| - (q_ss/q_s)·x
123// x_next = x + 2·u_hi·ds
124inline double axis_forward_reach(double q_s_i, double q_ss_i, double a_max_i,
125 double x_k, double ds)
126{
127 const double abs_a = std::fabs(q_s_i);
128 if(abs_a <= 1e-15) {
129 return 1e30;
130 }
131 const double d0 = a_max_i / abs_a;
132 const double d1 = -q_ss_i / q_s_i;
133 return x_k * (1.0 + 2.0 * d1 * ds) + 2.0 * d0 * ds;
134}
135
136} // namespace topp_detail
137
138// Compute acceleration-limited TOPP-RA for a single path segment.
139// Boundary conditions: rest-to-rest.
140// limits: per-axis [x, y, z] velocity and acceleration limits.
141// grid_size: number of intervals (grid points = grid_size + 1, max 1023).
143 const ToppAxisLimits limits[3],
144 int grid_size)
145{
146 if(grid_size < 2) {
148 }
149 const double L = path.length();
150 if(L <= 0.0) {
152 }
153
154 constexpr int MaxGrid = 1024;
155 if(grid_size + 1 > MaxGrid) {
157 }
158
159 const int N = grid_size;
160 const double ds = L / static_cast<double>(N);
161
162 double x_mvc[MaxGrid];
163 geom::Vec3 qs[MaxGrid];
164 geom::Vec3 qss[MaxGrid];
165
166 for(int k = 0; k <= N; ++k) {
167 const double s = ds * static_cast<double>(k);
168 qs[k] = path.path_derivative(s);
169 qss[k] = path.path_second_derivative(s);
170
171 double mvc = topp_detail::velocity_limit_squared(qs[k], limits);
172 const double cs[3] = {qs[k].x, qs[k].y, qs[k].z};
173 const double css[3] = {qss[k].x, qss[k].y, qss[k].z};
174 for(int i = 0; i < 3; ++i) {
175 mvc = std::min(mvc,
177 cs[i], css[i], limits[i].max_acceleration));
178 }
179 mvc = std::min(mvc, topp_detail::cross_axis_velocity_limit(cs, css, limits));
180 x_mvc[k] = mvc;
181 }
182
183 // Backward pass: controllable velocity profile.
184 double x_back[MaxGrid];
185 x_back[N] = 0.0;
186
187 for(int k = N - 1; k >= 0; --k) {
188 const double cs[3] = {qs[k].x, qs[k].y, qs[k].z};
189 const double css[3] = {qss[k].x, qss[k].y, qss[k].z};
190
191 double x_max = 1e30;
192 for(int i = 0; i < 3; ++i) {
193 x_max = std::min(x_max,
195 cs[i], css[i], limits[i].max_acceleration,
196 x_back[k + 1], ds));
197 }
198 x_max = std::min(x_max, x_mvc[k]);
199 x_back[k] = std::max(x_max, 0.0);
200 }
201
202 // Forward pass: optimal velocity profile.
203 double x_fwd[MaxGrid];
204 x_fwd[0] = 0.0;
205
206 for(int k = 0; k < N; ++k) {
207 const double cs[3] = {qs[k].x, qs[k].y, qs[k].z};
208 const double css[3] = {qss[k].x, qss[k].y, qss[k].z};
209
210 double x_next = 1e30;
211 for(int i = 0; i < 3; ++i) {
212 x_next = std::min(x_next,
214 cs[i], css[i], limits[i].max_acceleration,
215 x_fwd[k], ds));
216 }
217 x_next = std::min(x_next, x_back[k + 1]);
218 x_next = std::min(x_next, x_mvc[k + 1]);
219 x_fwd[k + 1] = std::max(x_next, 0.0);
220 }
221
222 // Time integration (trapezoidal rule in s).
223 double total_time = 0.0;
224 for(int k = 0; k < N; ++k) {
225 const double sd_k = std::sqrt(std::max(x_fwd[k], 0.0));
226 const double sd_k1 = std::sqrt(std::max(x_fwd[k + 1], 0.0));
227 const double sum = sd_k + sd_k1;
228 if(sum < 1e-30) {
230 }
231 total_time += 2.0 * ds / sum;
232 }
233
235}
236
237} // namespace plcopen::core::plan
static constexpr Result success(const T &value)
定义 error.h:24
static constexpr Result failure(ErrorCode error)
定义 error.h:29
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< ToppResult > solve_topp_ra(const geom::PathSegment &path, const ToppAxisLimits limits[3], int grid_size)
定义 topp.h:142
Vec3 path_derivative(double arclength) const
Vec3 path_second_derivative(double arclength) const