plcopen 0.22.0
载入中...
搜索中...
未找到
group_window_impl.h
浏览该文件的文档.
1#pragma once
2
3// AxisGroup batch 2 implementation. Included by group.h after AxisGroup is complete.
4
6{
7inline void AxisGroup::window_tangent(const WindowSegment &seg, bool at_exit,
8 std::array<double, MaxAxes> &out) const
9{
10 if(seg.kind == WindowKind::line || seg.kind == WindowKind::direct_line) {
11 out = seg.dir;
12 return;
13 }
14 const geom::Vec3 plane =
15 geom::tangent(seg.arc_geom, at_exit ? seg.arc_geom.length : 0.0);
16 out[0] = plane.x;
17 out[1] = plane.y;
18 double norm = plane.x * plane.x + plane.y * plane.y;
19 for(std::size_t i = 2; i < axes_.size(); ++i) {
20 const double slope = (seg.target[i] - seg.entry[i]) / seg.full_length;
21 out[i] = slope;
22 norm += slope * slope;
23 }
24 norm = std::sqrt(norm);
25 if(norm > 0.0) {
26 for(std::size_t i = 0; i < axes_.size(); ++i) {
27 out[i] /= norm;
28 }
29 }
30}
31
32inline rt::Result<std::uint32_t> AxisGroup::submit_blend(GroupCommand command)
33{
34 // v1 declared boundary: blending extends the active window (or the
35 // active plain linear command); a stopping window, plain queued
36 // commands, or anything else is an explicit error.
37 if(joint_window_.stopping_ || !queue_.empty()) {
39 }
40 if(command.tool_number != active_tool_ ||
41 command.payload_number != active_payload_) {
42 return degrade_blend(command);
43 }
44 if(!joint_window_.active_) {
45 if(!active_ || active_kind_ != GroupPathKind::linear ||
46 status_ != GroupStatus::moving || active_path_length_ <= 0.0) {
48 }
49 }
50 if(joint_window_.active_ && joint_window_.segments_.size() >= joint_window_.depth_) {
51 // Window capacity is a declared limit, never a silent drop.
53 }
54
55 // Predecessor tail geometry (window tail or the active linear command).
56 std::array<double, MaxAxes> pred_target{};
57 std::array<double, MaxAxes> pred_dir{};
58 double pred_full = 0.0;
59 double pred_trim_out_room = 0.0; // half-length truncation budget
60 bool pred_is_line = true;
61 otg::Limits1D pred_limits{};
62 if(joint_window_.active_) {
63 const WindowSegment &tail = joint_window_.segments_[joint_window_.segments_.size() - 1];
64 pred_target = tail.target;
65 window_tangent(tail, true, pred_dir);
66 pred_full = tail.full_length;
67 pred_limits = tail.limits;
68 pred_is_line = tail.kind == WindowKind::line ||
69 tail.kind == WindowKind::direct_line;
70 } else {
71 double length1 = 0.0;
72 for(std::size_t i = 0; i < axes_.size(); ++i) {
73 const double d = active_finish_[i] - active_start_[i];
74 pred_dir[i] = d;
75 pred_target[i] = active_finish_[i];
76 length1 += d * d;
77 }
78 length1 = std::sqrt(length1);
79 if(length1 <= 1e-12) {
81 }
82 for(std::size_t i = 0; i < axes_.size(); ++i) {
83 pred_dir[i] /= length1;
84 }
85 pred_full = length1;
86 const double scale1 = length1 / active_path_length_;
87 pred_limits = otg::Limits1D{active_command_.velocity * scale1,
88 active_command_.acceleration * scale1,
89 active_command_.deceleration * scale1,
90 active_command_.jerk * scale1};
91 }
92 pred_trim_out_room = pred_is_line ? pred_full * 0.5 : 0.0;
93
94 // Successor geometry.
95 double length2 = 0.0;
96 double longest2 = 0.0;
97 std::array<double, MaxAxes> u2{};
98 for(std::size_t i = 0; i < axes_.size(); ++i) {
99 const double d = command.target.value[i] - pred_target[i];
100 u2[i] = d;
101 length2 += d * d;
102 const double travel = std::fabs(d);
103 if(travel > longest2) {
104 longest2 = travel;
105 }
106 }
107 length2 = std::sqrt(length2);
108 if(length2 <= 1e-12) {
110 }
111 double alignment = 0.0;
112 for(std::size_t i = 0; i < axes_.size(); ++i) {
113 u2[i] /= length2;
114 alignment += pred_dir[i] * u2[i];
115 }
116 const double scale2 = longest2 > 0.0 ? length2 / longest2 : 1.0;
117 const otg::Limits1D limits2{command.velocity * scale2,
118 command.acceleration * scale2,
119 command.deceleration * scale2, command.jerk * scale2};
120
121 if(alignment < -0.999) {
122 // Reflex corner: degrade to a BUFFERED full-stop join, reported.
123 return degrade_blend(command);
124 }
125
126 // Corner construction (geometry frozen at creation).
127 WindowNode node{};
128 double trim = 0.0;
129 double corner_cap =
130 pred_limits.max_velocity < limits2.max_velocity ? pred_limits.max_velocity
131 : limits2.max_velocity;
132 if(alignment <= 0.999 && !pred_is_line) {
133 // No tolerance-band curve exists between an arc and a line (v3
134 // scope); a non-tangent junction degrades to a full-stop join.
135 return degrade_blend(command);
136 }
137 if(alignment <= 0.999) {
138 double turn = 0.0;
139 for(std::size_t i = 0; i < axes_.size(); ++i) {
140 const double diff = u2[i] - pred_dir[i];
141 turn += diff * diff;
142 }
143 turn = std::sqrt(turn);
144 trim = command.transition_parameter * 96.0 / (23.0 * turn);
145 if(trim > pred_trim_out_room) {
146 trim = pred_trim_out_room;
147 }
148 if(trim > length2 * 0.5) {
149 trim = length2 * 0.5;
150 }
151 for(std::size_t i = 0; i < axes_.size(); ++i) {
152 const double corner = pred_target[i];
153 node.ctrl[0][i] = corner - pred_dir[i] * trim;
154 node.ctrl[1][i] = corner - pred_dir[i] * (trim * 2.0 / 3.0);
155 node.ctrl[2][i] = corner - pred_dir[i] * (trim / 3.0);
156 node.ctrl[3][i] = corner + u2[i] * (trim / 3.0);
157 node.ctrl[4][i] = corner + u2[i] * (trim * 2.0 / 3.0);
158 node.ctrl[5][i] = corner + u2[i] * trim;
159 }
160 double accumulated = 0.0;
161 std::array<double, MaxAxes> previous{};
162 blend_point(node.ctrl, 0.0, previous);
163 node.cumulative[0] = 0.0;
164 for(std::size_t step = 1; step < BlendTableSize; ++step) {
165 const double u =
166 static_cast<double>(step) / static_cast<double>(BlendTableSize - 1);
167 std::array<double, MaxAxes> point{};
168 blend_point(node.ctrl, u, point);
169 double chord = 0.0;
170 for(std::size_t i = 0; i < axes_.size(); ++i) {
171 const double diff = point[i] - previous[i];
172 chord += diff * diff;
173 }
174 accumulated += std::sqrt(chord);
175 node.cumulative[step] = accumulated;
176 previous = point;
177 }
178 node.curve_length = accumulated;
179 if(!std::isfinite(node.curve_length) || node.curve_length <= 0.0) {
180 return degrade_blend(command);
181 }
182 double max_curvature = 0.0;
183 for(std::size_t step = 0; step <= 64; ++step) {
184 const double u = static_cast<double>(step) / 64.0;
185 const double curvature = blend_curvature(node.ctrl, u);
186 if(curvature > max_curvature) {
187 max_curvature = curvature;
188 }
189 }
190 if(max_curvature > 0.0) {
191 double junction = pred_limits.max_acceleration;
192 if(pred_limits.max_deceleration < junction) {
193 junction = pred_limits.max_deceleration;
194 }
195 if(limits2.max_acceleration < junction) {
196 junction = limits2.max_acceleration;
197 }
198 if(limits2.max_deceleration < junction) {
199 junction = limits2.max_deceleration;
200 }
201 const double geometric = std::sqrt(junction / max_curvature);
202 if(geometric < corner_cap) {
203 corner_cap = geometric;
204 }
205 }
206 node.has_curve = true;
207 }
208 if(command.transition_velocity > 0.0) {
209 const double transition_cap = command.transition_velocity * scale2;
210 if(transition_cap < corner_cap) {
211 corner_cap = transition_cap;
212 }
213 }
214 node.corner_cap = corner_cap;
215
216 // Baseline for the constructive cycle-time gate (captured pre-append).
217 const std::int64_t old_remaining = joint_window_.active_ ? window_remaining_cycles() : -1;
218
219 // Convert the active linear command into window segment zero.
220 bool converted = false;
221 if(!joint_window_.active_) {
222 if(!convert_active_linear_to_window()) {
224 }
225 converted = true;
226 }
227
228 // Append: tail gains the corner, the new segment enters the window.
229 WindowSegment &tail = joint_window_.segments_[joint_window_.segments_.size() - 1];
230 const double saved_trim_out = tail.trim_out;
231 const WindowNode saved_node = tail.node;
232 tail.trim_out = trim;
233 tail.node = node;
234
235 WindowSegment fresh{};
236 fresh.kind = command.direct_semantics ? WindowKind::direct_line
237 : WindowKind::line;
238 for(std::size_t i = 0; i < axes_.size(); ++i) {
239 fresh.entry[i] = node.has_curve ? node.ctrl[5][i] : pred_target[i];
240 fresh.dir[i] = u2[i];
241 fresh.target[i] = command.target.value[i];
242 }
243 fresh.full_length = length2;
244 fresh.trim_in = trim;
245 fresh.limits = limits2;
246 fresh.command_id = command.command_id;
247 if(joint_window_.segments_.size() >= joint_window_.depth_ ||
248 joint_window_.segments_.push_back(fresh) != rt::ErrorCode::ok) {
249 tail.trim_out = saved_trim_out;
250 tail.node = saved_node;
251 if(converted) {
252 joint_window_.segments_.clear();
253 }
255 }
256
257 bool late = false;
258 if(!window_rebuild(late)) {
259 joint_window_.segments_.pop_back();
260 WindowSegment &restore = joint_window_.segments_[joint_window_.segments_.size() - 1];
261 restore.trim_out = saved_trim_out;
262 restore.node = saved_node;
263 if(converted) {
264 joint_window_.segments_.clear();
265 } else if(!window_rebuild(late)) {
266 // Restoring the previous window must succeed; if the live
267 // state has drifted past a boundary, stop safely.
268 window_reset();
269 clear_axes_synchronized();
270 status_ = GroupStatus::standby;
271 abort_motion();
272 }
273 return degrade_blend(command);
274 }
275
276 // Constructive cycle-time gate (KB-031 carried over): the extended
277 // window must beat "previous window then a standalone full-stop move".
278 if(old_remaining >= 0) {
279 const rt::Result<otg::Profile1D> standalone = otg::plan_time_optimal(
280 {0.0, 0.0, 0.0}, {length2, 0.0, 0.0}, limits2);
281 if(standalone &&
282 window_remaining_cycles() >=
283 old_remaining + standalone.value().duration_cycles()) {
284 joint_window_.segments_.pop_back();
285 WindowSegment &restore = joint_window_.segments_[joint_window_.segments_.size() - 1];
286 restore.trim_out = saved_trim_out;
287 restore.node = saved_node;
288 if(!window_rebuild(late)) {
289 window_reset();
290 clear_axes_synchronized();
291 status_ = GroupStatus::standby;
292 abort_motion();
293 }
294 return degrade_blend(command);
295 }
296 }
297
298 if(converted) {
299 active_ = false;
300 joint_window_.active_ = true;
301 }
302 status_ = GroupStatus::moving;
304}
305
306// Seed the window from the active linear command: segment zero carries
307// the euclidean geometry and the live state (captured for the rebuild).
308inline bool AxisGroup::convert_active_linear_to_window()
309{
310 double length1 = 0.0;
311 std::array<double, MaxAxes> direction{};
312 for(std::size_t i = 0; i < axes_.size(); ++i) {
313 const double d = active_finish_[i] - active_start_[i];
314 direction[i] = d;
315 length1 += d * d;
316 }
317 length1 = std::sqrt(length1);
318 if(length1 <= 1e-12 || active_path_length_ <= 0.0) {
319 return false;
320 }
321 for(std::size_t i = 0; i < axes_.size(); ++i) {
322 direction[i] /= length1;
323 }
324 const double scale1 = length1 / active_path_length_;
325
326 WindowSegment seg0{};
327 seg0.kind = active_command_.direct_semantics ? WindowKind::direct_line
328 : WindowKind::line;
329 for(std::size_t i = 0; i < axes_.size(); ++i) {
330 seg0.entry[i] = active_start_[i];
331 seg0.dir[i] = direction[i];
332 seg0.target[i] = active_finish_[i];
333 }
334 seg0.full_length = length1;
335 seg0.limits = otg::Limits1D{active_command_.velocity * scale1,
336 active_command_.acceleration * scale1,
337 active_command_.deceleration * scale1,
338 active_command_.jerk * scale1};
339 seg0.command_id = active_command_.command_id;
340 const otg::State1D raw =
341 otg::sample(active_profile_, rt::CycleTick::from_cycles(active_tick_));
342 seg0.entry_velocity = raw.velocity * scale1; // updated by rebuild
343 seg0.profile = active_profile_; // replaced by rebuild
344 joint_window_.segments_.clear();
345 joint_window_.segments_.push_back(seg0);
346 joint_window_.seed_state_ = otg::State1D{raw.position * scale1, raw.velocity * scale1,
347 raw.acceleration * scale1};
348 joint_window_.index_ = 0;
349 joint_window_.in_curve_ = false;
350 joint_window_.tick_ = 0;
351 return true;
352}
353
354// A5 v2 (KB-033): append a KB-030 BORDER arc to the look-ahead window.
355// The junction must be tangent-continuous (no tolerance-band curve exists
356// between lines and arcs until v3); anything else degrades to a BUFFERED
357// full-stop join, reported. The arc segment's velocity limit is clamped
358// to the centripetal bound sqrt(a*R) for the whole segment.
359inline rt::Result<std::uint32_t> AxisGroup::submit_blend_arc(GroupCommand command,
360 const std::array<double, MaxAxes> &start_point)
361{
362 if(joint_window_.stopping_ || !queue_.empty()) {
364 }
365 if(command.tool_number != active_tool_ ||
366 command.payload_number != active_payload_) {
367 return degrade_blend(command);
368 }
369 if(!joint_window_.active_) {
370 if(!active_ || active_kind_ != GroupPathKind::linear ||
371 status_ != GroupStatus::moving || active_path_length_ <= 0.0) {
372 // Converting an active circular command is a declared v2
373 // boundary: only linear actives seed a window.
375 }
376 }
377 if(joint_window_.active_ && joint_window_.segments_.size() >= joint_window_.depth_) {
379 }
380
381 // Arc segment descriptor. KB-030 dynamics are already stated in the
382 // plane arc-length domain, so no metric conversion applies; the
383 // centripetal bound clamps the whole segment.
384 WindowSegment fresh{};
385 fresh.kind = WindowKind::arc;
386 fresh.arc_geom = command.arc;
387 fresh.full_length = command.arc.length;
388 for(std::size_t i = 0; i < axes_.size(); ++i) {
389 fresh.entry[i] = start_point[i];
390 fresh.target[i] = command.target.value[i];
391 }
392 fresh.entry[0] = command.arc.start.x;
393 fresh.entry[1] = command.arc.start.y;
394 fresh.limits = otg::Limits1D{command.velocity, command.acceleration,
395 command.deceleration, command.jerk};
396 double junction = fresh.limits.max_acceleration;
397 if(fresh.limits.max_deceleration < junction) {
398 junction = fresh.limits.max_deceleration;
399 }
400 const double centripetal = std::sqrt(junction * command.arc.radius);
401 if(centripetal < fresh.limits.max_velocity) {
402 fresh.limits.max_velocity = centripetal;
403 }
404 fresh.command_id = command.command_id;
405
406 // Predecessor exit tangent vs the arc entry tangent (N-dimensional).
407 std::array<double, MaxAxes> pred_tangent{};
408 otg::Limits1D pred_limits{};
409 if(joint_window_.active_) {
410 const WindowSegment &tail = joint_window_.segments_[joint_window_.segments_.size() - 1];
411 window_tangent(tail, true, pred_tangent);
412 pred_limits = tail.limits;
413 } else {
414 double length1 = 0.0;
415 for(std::size_t i = 0; i < axes_.size(); ++i) {
416 const double d = active_finish_[i] - active_start_[i];
417 pred_tangent[i] = d;
418 length1 += d * d;
419 }
420 length1 = std::sqrt(length1);
421 if(length1 <= 1e-12) {
423 }
424 for(std::size_t i = 0; i < axes_.size(); ++i) {
425 pred_tangent[i] /= length1;
426 }
427 const double scale1 = length1 / active_path_length_;
428 pred_limits = otg::Limits1D{active_command_.velocity * scale1,
429 active_command_.acceleration * scale1,
430 active_command_.deceleration * scale1,
431 active_command_.jerk * scale1};
432 }
433 std::array<double, MaxAxes> arc_tangent{};
434 window_tangent(fresh, false, arc_tangent);
435 double alignment = 0.0;
436 for(std::size_t i = 0; i < axes_.size(); ++i) {
437 alignment += pred_tangent[i] * arc_tangent[i];
438 }
439 if(alignment <= 0.999) {
440 // Non-tangent junction: full-stop join, reported (approved v2).
441 return degrade_blend(command);
442 }
443
444 // Pass-through node (no curve, no trims).
445 WindowNode node{};
446 node.corner_cap = pred_limits.max_velocity < fresh.limits.max_velocity
447 ? pred_limits.max_velocity
448 : fresh.limits.max_velocity;
449 if(command.transition_velocity > 0.0 &&
450 command.transition_velocity < node.corner_cap) {
451 node.corner_cap = command.transition_velocity;
452 }
453
454 const std::int64_t old_remaining = joint_window_.active_ ? window_remaining_cycles() : -1;
455
456 bool converted = false;
457 if(!joint_window_.active_) {
458 if(!convert_active_linear_to_window()) {
460 }
461 converted = true;
462 }
463
464 WindowSegment &tail = joint_window_.segments_[joint_window_.segments_.size() - 1];
465 const WindowNode saved_node = tail.node;
466 tail.node = node;
467 if(joint_window_.segments_.size() >= joint_window_.depth_ ||
468 joint_window_.segments_.push_back(fresh) != rt::ErrorCode::ok) {
469 tail.node = saved_node;
470 if(converted) {
471 joint_window_.segments_.clear();
472 }
474 }
475
476 bool late = false;
477 if(!window_rebuild(late)) {
478 joint_window_.segments_.pop_back();
479 WindowSegment &restore = joint_window_.segments_[joint_window_.segments_.size() - 1];
480 restore.node = saved_node;
481 if(converted) {
482 joint_window_.segments_.clear();
483 } else if(!window_rebuild(late)) {
484 window_reset();
485 clear_axes_synchronized();
486 status_ = GroupStatus::standby;
487 abort_motion();
488 }
489 return degrade_blend(command);
490 }
491
492 if(old_remaining >= 0) {
493 const rt::Result<otg::Profile1D> standalone = otg::plan_time_optimal(
494 {0.0, 0.0, 0.0}, {fresh.full_length, 0.0, 0.0}, fresh.limits);
495 if(standalone &&
496 window_remaining_cycles() >=
497 old_remaining + standalone.value().duration_cycles()) {
498 joint_window_.segments_.pop_back();
499 WindowSegment &restore = joint_window_.segments_[joint_window_.segments_.size() - 1];
500 restore.node = saved_node;
501 if(!window_rebuild(late)) {
502 window_reset();
503 clear_axes_synchronized();
504 status_ = GroupStatus::standby;
505 abort_motion();
506 }
507 return degrade_blend(command);
508 }
509 }
510
511 if(converted) {
512 active_ = false;
513 joint_window_.active_ = true;
514 }
515 status_ = GroupStatus::moving;
517}
518
519// Trapezoid-level bidirectional scan + per-segment profile planning over
520// the not-yet-started part of the window. Returns false when any segment
521// profile is infeasible (caller degrades).
522inline bool AxisGroup::window_rebuild(bool &late)
523{
524 late = false;
525 const std::size_t count = joint_window_.segments_.size();
526 if(count == 0 || joint_window_.index_ >= count) {
527 return false;
528 }
529
530 // Live state along the current line piece (euclidean, local coords).
531 double s_live = 0.0;
532 double v_live = 0.0;
533 double a_live = 0.0;
534 std::size_t first = joint_window_.index_;
535 if(joint_window_.active_) {
536 if(joint_window_.in_curve_) {
537 // The current curve is committed; rebuild from the next line.
538 const WindowNode &cur = joint_window_.segments_[joint_window_.index_].node;
539 double s_curve = cur.curve_velocity * static_cast<double>(joint_window_.tick_);
540 if(s_curve > cur.curve_length) {
541 s_curve = cur.curve_length;
542 }
543 (void)s_curve;
544 first = joint_window_.index_ + 1;
545 if(first >= count) {
546 return false;
547 }
548 s_live = 0.0;
549 v_live = cur.curve_velocity;
550 a_live = 0.0;
551 } else {
552 const otg::State1D raw = otg::sample(
553 joint_window_.segments_[joint_window_.index_].profile, rt::CycleTick::from_cycles(joint_window_.tick_));
554 s_live = raw.position;
555 v_live = raw.velocity;
556 a_live = raw.acceleration;
557 }
558 } else {
559 // Fresh conversion: seed state captured in submit_blend.
560 s_live = joint_window_.seed_state_.position;
561 v_live = joint_window_.seed_state_.velocity;
562 a_live = joint_window_.seed_state_.acceleration;
563 }
564
565 // Late submission: already inside (or past) the tail transition region.
566 const WindowSegment &first_seg = joint_window_.segments_[first];
567 if(first == count - 2 || count == 2) {
568 // The newly trimmed segment is the live one: check the room.
569 }
570 if(first < count && joint_window_.segments_[first].line_length() <= 0.0 && first + 1 < count) {
571 // Fully consumed line between two corners is allowed only when
572 // both node velocities agree; v1 degrades instead.
573 return false;
574 }
575 if(!joint_window_.in_curve_ && s_live >= joint_window_.segments_[first].line_length()) {
576 late = true;
577 return false;
578 }
579 (void)first_seg;
580
581 // Forward pass (accelerating limit), then backward pass (braking).
582 std::array<double, WindowCapacity> node_v{};
583 double forward = v_live;
584 for(std::size_t i = first; i < count; ++i) {
585 const double length = i == first && !joint_window_.in_curve_
586 ? joint_window_.segments_[i].line_length() - s_live
587 : joint_window_.segments_[i].line_length();
588 const double usable = length > 0.0 ? length : 0.0;
589 // Approved look-ahead v2: exact jerk-limited reachability
590 // replaces the trapezoid estimate (declared change, KB-039).
591 double reachable = plan::jerk_reachable_speed(
592 forward, usable, joint_window_.segments_[i].limits.max_acceleration,
593 joint_window_.segments_[i].limits.max_jerk);
594 if(i + 1 < count) {
595 const double cap = joint_window_.segments_[i].node.corner_cap;
596 if(reachable > cap) {
597 reachable = cap;
598 }
599 node_v[i] = reachable;
600 forward = reachable;
601 } else {
602 node_v[i] = 0.0; // terminal rest
603 }
604 }
605 double backward = 0.0;
606 for(std::size_t r = count; r > first; --r) {
607 const std::size_t i = r - 1;
608 const double length = i == first && !joint_window_.in_curve_
609 ? joint_window_.segments_[i].line_length() - s_live
610 : joint_window_.segments_[i].line_length();
611 const double usable = length > 0.0 ? length : 0.0;
612 if(i + 1 < count && backward < node_v[i]) {
613 node_v[i] = backward;
614 }
615 backward = plan::jerk_reachable_speed(node_v[i], usable,
616 joint_window_.segments_[i].limits.max_deceleration,
617 joint_window_.segments_[i].limits.max_jerk);
618 if(i + 1 < count) {
619 backward = backward; // entry allowance of segment i
620 }
621 }
622
623 // Quantize curve velocities and plan the per-segment profiles.
624 double entry_velocity = v_live;
625 double entry_acceleration = a_live;
626 double entry_position = joint_window_.in_curve_ ? 0.0 : s_live;
627 for(std::size_t i = first; i < count; ++i) {
628 WindowSegment &seg = joint_window_.segments_[i];
629 double exit_velocity = 0.0;
630 if(i + 1 < count) {
631 if(seg.node.has_curve) {
632 double v = node_v[i];
633 if(v <= 1e-12) {
634 return false; // corner requires rest: degrade
635 }
636 std::int64_t cycles = static_cast<std::int64_t>(
637 std::ceil(seg.node.curve_length / v));
638 if(cycles < 1) {
639 cycles = 1;
640 }
641 seg.node.curve_velocity =
642 seg.node.curve_length / static_cast<double>(cycles);
643 seg.node.curve_cycles = cycles;
644 exit_velocity = seg.node.curve_velocity;
645 } else {
646 exit_velocity = node_v[i];
647 }
648 }
649 const rt::Result<otg::Profile1D> profile = otg::plan_time_optimal(
650 {entry_position, entry_velocity, entry_acceleration},
651 {seg.line_length(), exit_velocity, 0.0}, seg.limits);
652 if(!profile) {
653 return false;
654 }
655 seg.entry_velocity = entry_velocity;
656 seg.exit_velocity = exit_velocity;
657 seg.profile = profile.value();
658 entry_velocity = exit_velocity;
659 entry_acceleration = 0.0;
660 entry_position = 0.0;
661 }
662 if(!joint_window_.in_curve_) {
663 // The live line profile was replanned from the live state; its
664 // tick restarts. A committed curve keeps its own progress.
665 joint_window_.tick_ = 0;
666 }
667 return true;
668}
669
670inline std::int64_t AxisGroup::window_remaining_cycles() const
671{
672 std::int64_t total = 0;
673 for(std::size_t i = joint_window_.index_; i < joint_window_.segments_.size(); ++i) {
674 if(!(i == joint_window_.index_ && joint_window_.in_curve_)) {
675 total += joint_window_.segments_[i].profile.duration_cycles();
676 }
677 if(i + 1 < joint_window_.segments_.size() && joint_window_.segments_[i].node.has_curve) {
678 total += joint_window_.segments_[i].node.curve_cycles;
679 }
680 }
681 return total;
682}
683
684inline void AxisGroup::window_cycle()
685{
686 if(joint_window_.override_paused_ && !joint_window_.stopping_) {
687 return;
688 }
689 ++joint_window_.tick_;
690 if(joint_window_.stopping_) {
691 const otg::State1D st = otg::sample(joint_window_.stop_profile_,
692 rt::CycleTick::from_cycles(joint_window_.tick_));
693 sample_window_arclength(joint_window_.stop_origin_ + st.position);
694 if(joint_window_.tick_ >= joint_window_.stop_profile_.duration_cycles()) {
695 if(joint_window_.override_paused_) {
696 interrupting_ = false;
697 joint_window_.stopping_ = false;
698 status_ = GroupStatus::moving;
699 } else if(interrupting_) {
700 interrupting_ = false;
701 joint_window_.stopping_ = false;
702 status_ = GroupStatus::interrupted;
703 } else {
704 window_reset();
705 clear_axes_synchronized();
706 status_ = GroupStatus::standby;
707 start_next_queued();
708 }
709 }
710 return;
711 }
712
713 WindowSegment &seg = joint_window_.segments_[joint_window_.index_];
714 if(joint_window_.in_curve_) {
715 double s = seg.node.curve_velocity * static_cast<double>(joint_window_.tick_);
716 if(s > seg.node.curve_length) {
717 s = seg.node.curve_length;
718 }
719 sample_window_curve(seg.node, s);
720 if(joint_window_.tick_ >= seg.node.curve_cycles) {
721 if(seg.kind == WindowKind::direct_line) complete_direct(seg.command_id);
722 ++joint_window_.index_;
723 joint_window_.in_curve_ = false;
724 joint_window_.tick_ = 0;
725 }
726 return;
727 }
728
729 const otg::State1D st =
730 otg::sample(seg.profile, rt::CycleTick::from_cycles(joint_window_.tick_));
731 // No clamping: a terminal profile may legally overshoot the target by
732 // a hair inside its envelope and come back; clamping would turn that
733 // into a hard stop (acceleration step). The final sample lands on the
734 // exact segment end by the profile's endpoint contract.
735 const double s = st.position;
736 sample_window_segment(seg, s);
737 if(joint_window_.tick_ >= seg.profile.duration_cycles()) {
738 if(joint_window_.index_ + 1 < joint_window_.segments_.size()) {
739 if(seg.node.has_curve) {
740 joint_window_.in_curve_ = true;
741 joint_window_.tick_ = 0;
742 } else {
743 if(seg.kind == WindowKind::direct_line) complete_direct(seg.command_id);
744 ++joint_window_.index_;
745 joint_window_.tick_ = 0;
746 }
747 } else {
748 if(seg.kind == WindowKind::direct_line) complete_direct(seg.command_id);
749 window_reset();
750 clear_axes_synchronized();
751 status_ = GroupStatus::standby;
752 start_next_queued();
753 }
754 }
755}
756
757inline void AxisGroup::sample_window_segment(const WindowSegment &seg, double arclength)
758{
759 if(seg.kind == WindowKind::arc) {
760 double s = arclength;
761 if(s < 0.0) {
762 s = 0.0;
763 }
764 if(s > seg.full_length) {
765 s = seg.full_length;
766 }
767 const geom::Vec3 point = geom::sample(seg.arc_geom, s);
768 axes_[0]->set_synchronized_position(point.x);
769 axes_[1]->set_synchronized_position(point.y);
770 const double ratio = seg.full_length > 0.0 ? s / seg.full_length : 1.0;
771 for(std::size_t i = 2; i < axes_.size(); ++i) {
772 axes_[i]->set_synchronized_position(
773 seg.entry[i] + (seg.target[i] - seg.entry[i]) * ratio);
774 }
775 return;
776 }
777 for(std::size_t i = 0; i < axes_.size(); ++i) {
778 axes_[i]->set_synchronized_position(seg.entry[i] + seg.dir[i] * arclength);
779 }
780}
781
782inline void AxisGroup::sample_window_curve(const WindowNode &node, double arclength)
783{
784 double target = arclength;
785 if(target < 0.0) {
786 target = 0.0;
787 }
788 if(target > node.curve_length) {
789 target = node.curve_length;
790 }
791 std::size_t low = 0;
792 for(std::size_t i = 1; i < BlendTableSize; ++i) {
793 if(node.cumulative[i] >= target) {
794 low = i - 1;
795 break;
796 }
797 low = i - 1;
798 }
799 const double segment = node.cumulative[low + 1] - node.cumulative[low];
800 const double fraction =
801 segment > 0.0 ? (target - node.cumulative[low]) / segment : 0.0;
802 const double u = (static_cast<double>(low) + fraction) /
803 static_cast<double>(BlendTableSize - 1);
804 std::array<double, MaxAxes> point{};
805 blend_point(node.ctrl, u, point);
806 for(std::size_t i = 0; i < axes_.size(); ++i) {
807 axes_[i]->set_synchronized_position(point[i]);
808 }
809}
810
811// Composite arc length measured from the live piece at stop time: walks
812// the remaining pieces (bounded by the window capacity, simple compares).
813inline void AxisGroup::sample_window_arclength(double arclength)
814{
815 double remaining = arclength;
816 bool in_curve = joint_window_.in_curve_;
817 std::size_t index = joint_window_.index_;
818 while(index < joint_window_.segments_.size()) {
819 const WindowSegment &seg = joint_window_.segments_[index];
820 if(!in_curve) {
821 const double length = seg.line_length();
822 if(remaining <= length || index + 1 >= joint_window_.segments_.size()) {
823 const double s =
824 remaining < 0.0 ? 0.0 : (remaining > length ? length : remaining);
825 sample_window_segment(seg, s);
826 return;
827 }
828 remaining -= length;
829 if(seg.node.has_curve) {
830 in_curve = true;
831 } else {
832 ++index;
833 }
834 } else {
835 if(remaining <= seg.node.curve_length) {
836 sample_window_curve(seg.node, remaining);
837 return;
838 }
839 remaining -= seg.node.curve_length;
840 in_curve = false;
841 ++index;
842 }
843 }
844}
845
846inline void AxisGroup::window_live_state(double &s_live, double &v_live, double &a_live) const
847{
848 if(joint_window_.in_curve_) {
849 const WindowNode &node = joint_window_.segments_[joint_window_.index_].node;
850 double s = node.curve_velocity * static_cast<double>(joint_window_.tick_);
851 if(s > node.curve_length) {
852 s = node.curve_length;
853 }
854 s_live = s;
855 v_live = node.curve_velocity;
856 a_live = 0.0;
857 return;
858 }
859 const otg::State1D raw = otg::sample(joint_window_.segments_[joint_window_.index_].profile,
860 rt::CycleTick::from_cycles(joint_window_.tick_));
861 const double length = joint_window_.segments_[joint_window_.index_].line_length();
862 s_live = raw.position < 0.0 ? 0.0 : (raw.position > length ? length : raw.position);
863 v_live = raw.velocity;
864 a_live = raw.acceleration;
865}
866
867inline void AxisGroup::window_reset()
868{
869 joint_window_.active_ = false;
870 joint_window_.stopping_ = false;
871 joint_window_.override_paused_ = false;
872 joint_window_.in_curve_ = false;
873 joint_window_.index_ = 0;
874 joint_window_.tick_ = 0;
875 joint_window_.segments_.clear();
876}
877
878
879inline rt::Result<std::uint32_t> AxisGroup::degrade_blend(GroupCommand command)
880{
881 last_blend_degraded_id_ = command.command_id;
882 command.buffer_mode = BufferMode::buffered;
883 command.transition_mode = TransitionMode::none;
884 command.transition_velocity = 0.0;
885 command.transition_parameter = 0.0;
886 const rt::ErrorCode queued = queue_.push_back(command);
889 }
891}
892
893inline void AxisGroup::blend_point(const std::array<std::array<double, MaxAxes>, 6> &control,
894 double u,
895 std::array<double, MaxAxes> &out) const
896{
897 const double v = 1.0 - u;
898 const double v2 = v * v;
899 const double u2 = u * u;
900 const double w0 = v2 * v2 * v;
901 const double w1 = 5.0 * v2 * v2 * u;
902 const double w2 = 10.0 * v2 * v * u2;
903 const double w3 = 10.0 * v2 * u2 * u;
904 const double w4 = 5.0 * v * u2 * u2;
905 const double w5 = u2 * u2 * u;
906 for(std::size_t i = 0; i < axes_.size(); ++i) {
907 out[i] = control[0][i] * w0 + control[1][i] * w1 + control[2][i] * w2 +
908 control[3][i] * w3 + control[4][i] * w4 + control[5][i] * w5;
909 }
910}
911
912inline double AxisGroup::blend_curvature(const std::array<std::array<double, MaxAxes>, 6> &control,
913 double u) const
914{
915 const double v = 1.0 - u;
916 const double v2 = v * v;
917 const double u2 = u * u;
918 const double d1w0 = 5.0 * v2 * v2;
919 const double d1w1 = 20.0 * v2 * v * u;
920 const double d1w2 = 30.0 * v2 * u2;
921 const double d1w3 = 20.0 * v * u2 * u;
922 const double d1w4 = 5.0 * u2 * u2;
923 const double d2w0 = 20.0 * v * v * v;
924 const double d2w1 = 60.0 * v * v * u;
925 const double d2w2 = 60.0 * v * u * u;
926 const double d2w3 = 20.0 * u * u * u;
927 double norm1 = 0.0;
928 double norm2 = 0.0;
929 double dot12 = 0.0;
930 for(std::size_t i = 0; i < axes_.size(); ++i) {
931 const double e0 = control[1][i] - control[0][i];
932 const double e1 = control[2][i] - control[1][i];
933 const double e2 = control[3][i] - control[2][i];
934 const double e3 = control[4][i] - control[3][i];
935 const double e4 = control[5][i] - control[4][i];
936 const double first = e0 * d1w0 + e1 * d1w1 + e2 * d1w2 + e3 * d1w3 + e4 * d1w4;
937 const double f0 = e1 - e0;
938 const double f1 = e2 - e1;
939 const double f2 = e3 - e2;
940 const double f3 = e4 - e3;
941 const double second = f0 * d2w0 + f1 * d2w1 + f2 * d2w2 + f3 * d2w3;
942 norm1 += first * first;
943 norm2 += second * second;
944 dot12 += first * second;
945 }
946 if(norm1 <= 1e-24) {
947 return 0.0;
948 }
949 const double area = norm1 * norm2 - dot12 * dot12;
950 if(area <= 0.0) {
951 return 0.0;
952 }
953 return std::sqrt(area) / (norm1 * std::sqrt(norm1));
954}
955
956} // namespace plcopen::core::axis
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
Vec3 sample(LineSegment line, double arclength)
Vec3 tangent(LineSegment line)
double norm(Vec3 value)
rt::Result< Profile1D > plan_time_optimal(State1D from, Target1D to, Limits1D limits)
State1D sample(const Profile1D &profile, rt::CycleTick tick)
double jerk_reachable_speed(double entry, double distance, double bound, double jerk)
定义 path.h:20
std::string trim(std::string_view text)
定义 l2b.h:100