plcopen 0.22.0
载入中...
搜索中...
未找到
path.h
浏览该文件的文档.
1#pragma once
2
3#include <cmath>
4#include <cstddef>
5
6#include "geom/geometry.h"
7#include "otg/time_optimal.h"
8#include "rt/error.h"
9#include "rt/static_vector.h"
10
12{
13
14// Approved look-ahead v2 (jerk correction): the largest exit speed whose
15// jerk-limited ramp from `entry` fits inside `distance`. The trapezoid value
16// sqrt(entry^2 + 2*bound*distance) bounds the bisection from above — jerk
17// phases only add ramp distance per delta-v — so the v2 scan is never more
18// optimistic than the v1 trapezoid scan (monotone tightening by
19// construction). Planning-domain only: the scans run at submit time.
20inline double jerk_reachable_speed(double entry,
21 double distance,
22 double bound,
23 double jerk)
24{
25 if(distance <= 0.0 || bound <= 0.0 || jerk <= 0.0) {
26 return entry;
27 }
28 const double trapezoid = std::sqrt(entry * entry + 2.0 * bound * distance);
29 const otg::Limits1D limits{trapezoid + 1.0, bound, bound, jerk};
30 if(otg::detail::ramp_between(entry, trapezoid, limits).distance <= distance) {
31 return trapezoid;
32 }
33 double low = entry;
34 double high = trapezoid;
35 for(int iteration = 0; iteration < 48; ++iteration) {
36 const double middle = 0.5 * (low + high);
37 if(otg::detail::ramp_between(entry, middle, limits).distance <= distance) {
38 low = middle;
39 } else {
40 high = middle;
41 }
42 }
43 return low;
44}
45
46template <std::size_t Capacity> class PathBuffer
47{
48public:
50 {
51 return segments_.push_back(segment);
52 }
53
55 {
56 if(segments_.empty()) {
58 }
59 for(std::size_t i = 1; i < segments_.size(); ++i) {
60 segments_[i - 1] = segments_[i];
61 }
62 return segments_.pop_back();
63 }
64
65 bool empty() const
66 {
67 return segments_.empty();
68 }
69
70 bool full() const
71 {
72 return segments_.full();
73 }
74
75 std::size_t size() const
76 {
77 return segments_.size();
78 }
79
80 const geom::PathSegment &segment(std::size_t index) const
81 {
82 return segments_[index];
83 }
84
85 double total_length() const
86 {
87 double total = 0.0;
88 for(std::size_t i = 0; i < segments_.size(); ++i) {
89 total += segments_[i].length();
90 }
91 return total;
92 }
93
94 geom::Vec3 sample(double arclength) const
95 {
96 double remaining = arclength;
97 for(std::size_t i = 0; i < segments_.size(); ++i) {
98 const double length = segments_[i].length();
99 if(remaining <= length) {
100 return segments_[i].sample(remaining);
101 }
102 remaining -= length;
103 }
104
105 if(segments_.empty()) {
106 return {};
107 }
108 return segments_[segments_.size() - 1].sample(segments_[segments_.size() - 1].length());
109 }
110
111private:
113};
114
120
121template <std::size_t Capacity>
123 double max_speed,
124 double acceleration_limit,
125 std::size_t window)
126{
127 if(max_speed <= 0.0 || acceleration_limit <= 0.0 || window == 0) {
129 }
130
132 const std::size_t count = path.size() < window ? path.size() : window;
133 for(std::size_t i = 0; i < count; ++i) {
134 plan.entry_speed.push_back(max_speed);
135 plan.exit_speed.push_back(max_speed);
136 }
137
138 if(count == 0) {
140 }
141
142 plan.entry_speed[0] = 0.0;
143 plan.exit_speed[count - 1] = 0.0;
144
145 for(std::size_t i = 0; i < count; ++i) {
146 const double reachable =
147 std::sqrt(plan.entry_speed[i] * plan.entry_speed[i] +
148 2.0 * acceleration_limit * path.segment(i).length());
149 if(plan.exit_speed[i] > reachable) {
150 plan.exit_speed[i] = reachable;
151 }
152 if(i + 1 < count && plan.entry_speed[i + 1] > plan.exit_speed[i]) {
153 plan.entry_speed[i + 1] = plan.exit_speed[i];
154 }
155 }
156
157 for(std::size_t reverse = count; reverse > 0; --reverse) {
158 const std::size_t i = reverse - 1;
159 const double reachable =
160 std::sqrt(plan.exit_speed[i] * plan.exit_speed[i] +
161 2.0 * acceleration_limit * path.segment(i).length());
162 if(plan.entry_speed[i] > reachable) {
163 plan.entry_speed[i] = reachable;
164 }
165 if(i > 0 && plan.exit_speed[i - 1] > plan.entry_speed[i]) {
166 plan.exit_speed[i - 1] = plan.entry_speed[i];
167 }
168 }
169
171}
172
174{
175 bool enabled = false;
176 // Collinear junction: no curve is needed and the pass-through join keeps
177 // a non-zero corner speed (approved blending matrix).
178 bool passthrough = false;
179 // Reflex corner (~180 degrees): geometric blending degrades to a BUFFERED
180 // full stop; the degradation is reported, never silent.
182 // Blend distance from the corner along each adjacent segment.
183 double radius = 0.0;
184 double allowed_deviation = 0.0;
186};
187
188// A4 v1 (approved blending matrix): tolerance-band corner blending with a
189// symmetric quintic Bezier (C2). The blend distance is sized so the closed
190// form midpoint deviation (23/96)*d*|t1-t0| meets the tolerance exactly, then
191// truncated to half of the shorter adjacent segment (the actual deviation
192// only shrinks, never exceeds the tolerance).
194 const geom::PathSegment &after,
195 double tolerance)
196{
197 if(tolerance <= 0.0 || !std::isfinite(tolerance)) {
198 return {};
199 }
200
201 const geom::Vec3 t0 = before.tangent(before.length());
202 const geom::Vec3 t1 = after.tangent(0.0);
203 const double alignment = geom::dot(t0, t1);
204 if(alignment > 0.999) {
205 BlendDecision decision{};
206 decision.passthrough = true;
207 return decision;
208 }
209 if(alignment < -0.999) {
210 BlendDecision decision{};
211 decision.degraded_to_buffered = true;
212 return decision;
213 }
214
215 const double turn = geom::norm(t1 - t0);
216 if(turn <= 1e-12) {
217 BlendDecision decision{};
218 decision.passthrough = true;
219 return decision;
220 }
221 const double exact_distance = tolerance * 96.0 / (23.0 * turn);
222 const double shortest = before.length() < after.length() ? before.length() : after.length();
223 const double distance = exact_distance < shortest * 0.5 ? exact_distance : shortest * 0.5;
224 const geom::Vec3 corner = before.finish();
225 const geom::Vec3 start = corner - t0 * distance;
226 const geom::Vec3 finish = corner + t1 * distance;
228 geom::make_quintic_blend(start, corner, finish, tolerance);
229 if(!curve) {
230 BlendDecision decision{};
231 decision.degraded_to_buffered = true;
232 return decision;
233 }
234
235 BlendDecision decision{};
236 decision.enabled = true;
237 decision.allowed_deviation = tolerance;
238 decision.radius = distance;
239 decision.curve = geom::as_path_segment(curve.value());
240 return decision;
241}
242
243} // namespace plcopen::core::plan
rt::ErrorCode push(geom::PathSegment segment)
定义 path.h:49
const geom::PathSegment & segment(std::size_t index) const
定义 path.h:80
std::size_t size() const
定义 path.h:75
geom::Vec3 sample(double arclength) const
定义 path.h:94
constexpr const T & value() const
定义 error.h:49
PathSegment as_path_segment(LineSegment line)
double norm(Vec3 value)
double dot(Vec3 lhs, Vec3 rhs)
rt::Result< QuinticBlendSegment > make_quintic_blend(Vec3 start, Vec3 corner, Vec3 finish, double tolerance)
RampTiming ramp_between(double va, double vb, const Limits1D &limits)
rt::Result< LookAheadPlan< Capacity > > compute_lookahead(const PathBuffer< Capacity > &path, double max_speed, double acceleration_limit, std::size_t window)
定义 path.h:122
BlendDecision decide_blend(const geom::PathSegment &before, const geom::PathSegment &after, double tolerance)
定义 path.h:193
double jerk_reachable_speed(double entry, double distance, double bound, double jerk)
定义 path.h:20
Vec3 tangent(double arclength) const
rt::StaticVector< double, Capacity > exit_speed
定义 path.h:118
rt::StaticVector< double, Capacity > entry_speed
定义 path.h:117