plcopen 0.22.0
载入中...
搜索中...
未找到
state.h
浏览该文件的文档.
1#pragma once
2
3#include <array>
4#include <cmath>
5#include <cstddef>
6#include <cstdint>
7
8#include "exec/sync.h"
9#include "otg/profile1d.h"
10#include "otg/time_optimal.h"
11#include "rt/error.h"
12#include "rt/static_vector.h"
13#include "rt/units.h"
14#include "stream/filter.h"
15
16namespace plcopen::core::axis
17{
18
30
40
48
49enum class CommandKind
50{
53 // move_additive resolves against the previous commanded endpoint even when
54 // it aborts the active command (MC_MoveAdditive boundary).
57 // move_continuous_* reach the target with a non-zero end velocity and then
58 // hold it (MC_MoveContinuousAbsolute/Relative).
66};
67
75
83
93
95{
96 std::size_t input = static_cast<std::size_t>(-1);
97};
98
100{
101 double max_velocity = 1.0;
102 double max_acceleration = 1.0;
103 double max_deceleration = 1.0;
104 double max_jerk = 1.0;
105 double min_position = 0.0;
106 double max_position = 0.0;
109};
110
112{
113 double max_velocity = 0.0;
114 double max_acceleration = 0.0;
115 double max_deceleration = 0.0;
116 double max_jerk = 0.0;
117 double min_position = 0.0;
118 double max_position = 0.0;
121};
122
123// Supported parameter registry (MC_Read/WriteParameter). Unsupported entries
124// return rt::ErrorCode::unsupported instead of guessing (KB-006 carried over).
125// Position-lag monitoring is not modeled in the rewrite core; its parameters
126// stay in the enum so callers get the explicit unsupported error.
147
155
165
167{
169 double value = 0.0;
170 double velocity = 1.0;
171 double acceleration = 1.0;
172 double deceleration = 1.0;
173 double jerk = 1.0;
174 // Optional drive-side torque/force ceiling. Zero means no limit.
175 double torque_limit = 0.0;
176 // MC_TorqueControl load-domain rate converted to a per-cycle maximum
177 // setpoint delta. Zero applies the target immediately.
178 double torque_ramp = 0.0;
180 // Only used by the move_continuous_* kinds; it is a signed terminal
181 // velocity. Zero remains explicitly unsupported.
182 double end_velocity = 0.0;
183 // Minimum command time in cycles (profile-table segments). A position
184 // command holds at its target until the duration elapses; a velocity
185 // command finishes after it (0 keeps the plain unlimited hold).
186 std::int64_t min_duration_cycles = 0;
188 bool continuous_update = false;
189 bool lock_stopping = false;
190 // Part 5 step commands keep the axis in Homing across their internal
191 // velocity, halt, and positioning phases.
192 bool homing = false;
193 std::uint32_t command_id = 0;
194};
195
196// One profile-table segment (MC_Position/Velocity/AccelerationProfile).
197// target is a position for position profiles and a signed velocity for
198// velocity/acceleration profiles.
200{
201 double target = 0.0;
202 double velocity = 1.0;
203 double acceleration = 1.0;
204 double deceleration = 1.0;
205 double jerk = 1.0;
206 std::int64_t duration_cycles = 0;
207 bool relative = false;
208};
209
215
222
231
240
247
248enum class CombineMode
249{
252};
253
254class AxisModel;
255class AxisGroup;
256
258{
259 const AxisModel *master = nullptr;
260 double ratio_numerator = 1.0;
261 double ratio_denominator = 1.0;
264 double acceleration = 0.0;
265 double deceleration = 0.0;
266 double jerk = 0.0;
267 // position_sync selects MC_GearInPos semantics: wait for the master sync
268 // window, approach the slave sync position, then follow with aligned phase.
269 bool position_sync = false;
274 // Maximum approach velocity. Zero selects the axis motion limit.
275 double approach_velocity = 0.0;
276};
277
279{
280 double phase_shift = 0.0;
281 double velocity = 0.0;
282 double acceleration = 0.0;
283 double deceleration = 0.0;
284 double jerk = 0.0;
286 bool relative = false;
287 std::uint32_t command_id = 0;
288};
289
298
305
307{
308 const AxisModel *master = nullptr;
310 // Approved cam matrix (decision #2): linear is the byte-identical C0
311 // compatibility default; spline reconstructs a C2 cubic at engage.
313 double master_offset = 0.0;
314 double master_scaling = 1.0;
315 double slave_offset = 0.0;
316 double slave_scaling = 1.0;
319 // master_start_distance > 0 arms the approach window that ends at
320 // master_sync_position; 0 engages immediately.
323 double approach_velocity = 0.0;
324};
325
327{
328 std::uint32_t id = 0;
329 const AxisModel *master = nullptr;
331 bool master_absolute = true;
332 bool slave_absolute = true;
333 double master_origin = 0.0;
334 double slave_origin = 0.0;
335};
336
350
352{
354 double command_position = 0.0;
355 double command_velocity = 0.0;
357 double actual_position = 0.0;
358 double actual_velocity = 0.0;
360 double command_torque = 0.0;
361 double actual_torque = 0.0;
366 double torque_jerk_limit = 0.0;
368 bool torque_mode = false;
369 bool powered = false;
370 bool homed = false;
371 bool error = false;
373 // Set while a move_continuous_* command holds its end velocity after
374 // reaching the target position.
376 std::uint32_t active_command_id = 0;
377 // Id of the most recently *completed* (not aborted) command, so facades
378 // can latch Done even when a queued successor starts in the same cycle.
379 std::uint32_t last_completed_command_id = 0;
380};
381
383{
384 return std::isfinite(command.value) && std::isfinite(command.velocity) &&
385 std::isfinite(command.acceleration) && std::isfinite(command.deceleration) &&
386 std::isfinite(command.jerk) && std::isfinite(command.torque_limit) &&
387 command.torque_limit >= 0.0 && std::isfinite(command.torque_ramp) &&
388 command.torque_ramp >= 0.0 && std::isfinite(command.end_velocity);
389}
390
391inline bool is_valid_direction(Direction direction)
392{
393 return direction == Direction::current || direction == Direction::positive ||
394 direction == Direction::negative || direction == Direction::shortest_way;
395}
396
398{
399 return mode == BufferMode::aborting || mode == BufferMode::buffered ||
401}
402
408
410{
411 public:
412 static constexpr std::size_t QueueCapacity = 8;
413
414 explicit AxisModel(int domain_id = 0) : domain_id_(domain_id) {}
415
416 AxisModel(const AxisModel &) = delete;
417 AxisModel &operator=(const AxisModel &) = delete;
418 AxisModel(AxisModel &&) = delete;
420
421 int domain_id() const { return domain_id_; }
422
423 AxisStatus status() const { return snapshot_.status; }
424
425 const AxisSnapshot &snapshot() const { return snapshot_; }
426
427 bool powered() const { return snapshot_.powered; }
428
429 const MotionLimits &motion_limits() const { return limits_; }
430
432 {
433 if(cycle.period_ns() <= 0) {
435 }
436 AxisSiConfig result{};
437 result.max_velocity = cycle.velocity_to_si(limits_.max_velocity);
438 result.max_acceleration = cycle.acceleration_to_si(limits_.max_acceleration);
439 result.max_deceleration = cycle.acceleration_to_si(limits_.max_deceleration);
440 result.max_jerk = cycle.jerk_to_si(limits_.max_jerk);
441 result.min_position = limits_.min_position;
442 result.max_position = limits_.max_position;
443 result.min_position_enabled = limits_.min_position_enabled;
444 result.max_position_enabled = limits_.max_position_enabled;
446 }
447
448 // KB-068: a standby group may claim this member only when every
449 // standalone writer (base, sync, stream, and superimposed) is idle.
451 {
452 return active_ || !queue_.empty() || sync_kind_ != SyncKind::none || superimposed_active_ ||
453 stream_active_;
454 }
455
456 void *group_owner() const { return group_owner_; }
457
459 {
460 if (snapshot_.status == AxisStatus::errorstop || snapshot_.error ||
461 (group_owner_ != nullptr &&
462 (group_status_ == nullptr || *group_status_ != GroupStatus::standby)))
463 {
465 }
466 return rt::ErrorCode::ok;
467 }
468
470 {
471 if (group_owner_ != nullptr)
473 if (snapshot_.powered || !std::isfinite(limits.max_velocity) ||
474 !std::isfinite(limits.max_acceleration) || !std::isfinite(limits.max_deceleration) ||
475 !std::isfinite(limits.max_jerk) || limits.max_velocity <= 0.0 ||
476 limits.max_acceleration <= 0.0 || limits.max_deceleration <= 0.0 ||
477 limits.max_jerk <= 0.0)
478 {
480 }
481 limits_ = limits;
482 return rt::ErrorCode::ok;
483 }
484
486 {
487 const rt::Result<MotionLimits> limits = si_to_limits(cycle, config, limits_);
488 if(!limits) return limits.error();
489 return configure_limits(limits.value());
490 }
491
492 rt::ErrorCode set_power(bool enabled, bool enable_positive = true,
493 bool enable_negative = true)
494 {
495 if (snapshot_.status == AxisStatus::errorstop && enabled)
496 {
498 }
499 if (enabled && !power_feedback_)
500 {
502 }
503 const bool positive_changed = positive_enabled_ != (enabled && enable_positive);
504 const bool negative_changed = negative_enabled_ != (enabled && enable_negative);
505 positive_enabled_ = enabled && enable_positive;
506 negative_enabled_ = enabled && enable_negative;
507
508 // MC_Power is level-controlled and called every scan cycle. A power
509 // transition always aborts motion; changing a feed enable only aborts
510 // a command that would continue into the newly disabled direction.
511 if (enabled == snapshot_.powered)
512 {
513 if ((positive_changed || negative_changed) &&
514 !active_direction_enabled())
515 {
516 abort_motion();
517 snapshot_.status = AxisStatus::standstill;
518 }
519 return rt::ErrorCode::ok;
520 }
521
522 abort_motion();
523 snapshot_.powered = enabled;
524 snapshot_.status = enabled ? AxisStatus::standstill : AxisStatus::disabled;
525 return rt::ErrorCode::ok;
526 }
527
528 // External power-stage feedback is separate from MC_Power's normal
529 // level-controlled disable. Losing feedback while enabled is an axis
530 // fault, not a user abort.
532 {
533 power_feedback_ = available;
534 if (available || !snapshot_.powered)
535 {
536 return rt::ErrorCode::ok;
537 }
538 snapshot_.powered = false;
540 }
541
543 {
544 if (!std::isfinite(position) || is_moving() || !target_inside_limits(position, true))
545 {
547 }
548 snapshot_.command_position = position;
549 snapshot_.actual_position = position;
550 snapshot_.command_velocity = 0.0;
551 snapshot_.actual_velocity = 0.0;
552 return rt::ErrorCode::ok;
553 }
554
556 {
557 double value = 0.0;
558 switch (parameter)
559 {
561 value = snapshot_.command_position;
562 break;
564 value = limits_.max_position;
565 break;
567 value = limits_.min_position;
568 break;
570 value = limits_.max_position_enabled ? 1.0 : 0.0;
571 break;
573 value = limits_.min_position_enabled ? 1.0 : 0.0;
574 break;
577 value = limits_.max_velocity;
578 break;
580 value = snapshot_.actual_velocity;
581 break;
583 value = snapshot_.command_velocity;
584 break;
587 value = limits_.max_acceleration;
588 break;
591 value = limits_.max_deceleration;
592 break;
595 value = limits_.max_jerk;
596 break;
597 default:
599 }
600 return rt::Result<double>::success(value);
601 }
602
604 {
605 // MC_SetPosition remaps the complete absolute coordinate domain,
606 // including active and queued absolute targets.
607 const bool has_position_profile = active_ &&
608 active_command_.kind != CommandKind::move_velocity &&
609 active_command_.kind != CommandKind::torque &&
610 active_command_.kind != CommandKind::acceleration_profile;
611 if (!std::isfinite(delta) || sync_kind_ != SyncKind::none || stream_active_ ||
612 superimposed_active_ || group_blocks_standalone_motion())
613 {
615 }
616 if (!std::isfinite(snapshot_.command_position + delta) ||
617 !std::isfinite(snapshot_.actual_position + delta) ||
618 !target_inside_limits(snapshot_.command_position + delta, true) ||
619 !target_inside_limits(snapshot_.actual_position + delta, true))
620 {
622 }
623 if (has_position_profile && (!std::isfinite(active_target_ + delta) ||
624 !target_inside_limits(active_target_ + delta, true)))
625 {
627 }
628 for (std::size_t i = 0; i < queue_.size(); ++i)
629 {
630 if ((queue_[i].kind == CommandKind::move_absolute ||
631 queue_[i].kind == CommandKind::move_continuous_absolute) &&
632 (!std::isfinite(queue_[i].value + delta) ||
633 !target_inside_limits(queue_[i].value + delta, true)))
634 {
636 }
637 }
638 if (has_position_profile && active_profile_.translate(delta) != rt::ErrorCode::ok)
639 {
641 }
642 snapshot_.command_position += delta;
643 snapshot_.actual_position += delta;
644 if (active_)
645 {
646 if (has_position_profile)
647 {
648 active_target_ += delta;
649 active_last_sample_ += delta;
650 }
651 if (active_command_.kind == CommandKind::move_absolute ||
652 active_command_.kind == CommandKind::move_continuous_absolute)
653 {
654 active_command_.value += delta;
655 }
656 }
657 for (std::size_t i = 0; i < queue_.size(); ++i)
658 {
659 if (queue_[i].kind == CommandKind::move_absolute ||
661 {
662 queue_[i].value += delta;
663 }
664 }
665 for (ProbeSlot &probe : probes_)
666 {
667 if (probe.captured)
668 probe.recorded_position += delta;
669 if (probe.window_only)
670 {
671 probe.first_position += delta;
672 probe.last_position += delta;
673 }
674 }
675 return rt::ErrorCode::ok;
676 }
677
678 // Part 5 flying reference: remap the current coordinate while preserving
679 // the numeric absolute targets of the active and queued motion commands.
681 {
682 if (!active_ || !std::isfinite(delta) || sync_kind_ != SyncKind::none || stream_active_ ||
683 superimposed_active_ || group_blocks_standalone_motion())
684 {
686 }
687
688 const double position = snapshot_.command_position + delta;
689 const double actual_position = snapshot_.actual_position + delta;
690 if (!std::isfinite(position) || !std::isfinite(actual_position) ||
691 !target_inside_limits(position, true) || !target_inside_limits(actual_position, true))
692 {
694 }
695
696 const bool profile_driven = active_command_.kind != CommandKind::move_velocity &&
697 active_command_.kind != CommandKind::torque &&
698 active_command_.kind != CommandKind::acceleration_profile &&
699 !continuous_holding_ && !override_paused_;
700 otg::Profile1D updated_profile{};
701 if (profile_driven)
702 {
703 if (override_braking_)
704 {
705 updated_profile = active_profile_;
706 if (updated_profile.translate(delta) != rt::ErrorCode::ok)
707 {
709 }
710 }
711 else
712 {
713 const double end_velocity = is_continuous_kind(active_command_.kind)
714 ? active_command_.end_velocity * override_
715 : 0.0;
716 const otg::Limits1D limits = scaled_limits(active_command_);
718 {position, snapshot_.command_velocity, snapshot_.command_acceleration},
719 {active_target_, end_velocity, 0.0}, limits);
720 if (!replanned)
721 {
722 return replanned.error();
723 }
724 updated_profile = replanned.value();
725 }
726 }
727
728 snapshot_.command_position = position;
729 snapshot_.actual_position = actual_position;
730 if (profile_driven)
731 {
732 active_profile_ = updated_profile;
733 if (override_braking_)
734 {
735 active_last_sample_ += delta;
736 }
737 else
738 {
739 active_tick_ = 0;
740 active_last_sample_ = position;
741 snapshot_.active_command_reached_target = false;
742 }
743 }
744 for (ProbeSlot &probe : probes_)
745 {
746 if (probe.captured)
747 probe.recorded_position += delta;
748 if (probe.window_only)
749 {
750 probe.first_position += delta;
751 probe.last_position += delta;
752 }
753 }
754 return rt::ErrorCode::ok;
755 }
756
758 {
759 bool value = false;
760 switch (parameter)
761 {
763 value = limits_.max_position_enabled;
764 break;
766 value = limits_.min_position_enabled;
767 break;
768 default:
770 }
771 return rt::Result<bool>::success(value);
772 }
773
775 {
776 if (!std::isfinite(value))
777 {
779 }
780 switch (parameter)
781 {
783 if (limits_.min_position_enabled && limits_.max_position_enabled &&
784 value < limits_.min_position)
785 {
787 }
788 limits_.max_position = value;
789 return rt::ErrorCode::ok;
791 if (limits_.min_position_enabled && limits_.max_position_enabled &&
792 value > limits_.max_position)
793 {
795 }
796 limits_.min_position = value;
797 return rt::ErrorCode::ok;
800 if (value <= 0.0)
801 {
803 }
804 limits_.max_velocity = value;
805 return rt::ErrorCode::ok;
808 if (value <= 0.0)
809 {
811 }
812 limits_.max_acceleration = value;
813 return rt::ErrorCode::ok;
816 if (value <= 0.0)
817 {
819 }
820 limits_.max_deceleration = value;
821 return rt::ErrorCode::ok;
824 if (value <= 0.0)
825 {
827 }
828 limits_.max_jerk = value;
829 return rt::ErrorCode::ok;
830 default:
832 }
833 }
834
836 {
837 switch (parameter)
838 {
840 limits_.max_position_enabled = value;
841 return rt::ErrorCode::ok;
843 limits_.min_position_enabled = value;
844 return rt::ErrorCode::ok;
845 default:
847 }
848 }
849
851 bool queued)
852 {
855 command.value = position;
856 command.flag = relative;
857 return submit_management(command, queued);
858 }
859
861 double value, bool queued)
862 {
865 command.parameter = parameter;
866 command.value = value;
867 return submit_management(command, queued);
868 }
869
871 bool value, bool queued)
872 {
875 command.parameter = parameter;
876 command.flag = value;
877 return submit_management(command, queued);
878 }
879
881 bool value, bool queued)
882 {
885 command.channel = channel;
886 command.flag = value;
887 return submit_management(command, queued);
888 }
889
890 // MC_SetOverride carries the KB-003 contract: newly planned commands scale
891 // by the override, velocity commands respond per cycle through
892 // signed_velocity(), and an active profile-driven command re-plans from
893 // its current state under the re-scaled velocity limit (an override drop
894 // below the current velocity plans a deceleration entry). A failed replan
895 // keeps the previous override and the running profile untouched.
897 {
898 return set_override(factor, acceleration_override_, jerk_override_);
899 }
900
901 rt::ErrorCode set_override(double velocity_factor,
902 double acceleration_factor,
903 double jerk_factor)
904 {
905 if (group_blocks_standalone_motion())
906 {
908 }
909 if (!std::isfinite(velocity_factor) || velocity_factor < 0.0 ||
910 velocity_factor > 1.0 || !std::isfinite(acceleration_factor) ||
911 acceleration_factor <= 0.0 || acceleration_factor > 1.0 ||
912 !std::isfinite(jerk_factor) || jerk_factor <= 0.0 ||
913 jerk_factor > 1.0)
914 {
916 }
917 const double previous_velocity = override_;
918 const double previous_acceleration = acceleration_override_;
919 const double previous_jerk = jerk_override_;
920 override_ = velocity_factor;
921 acceleration_override_ = acceleration_factor;
922 jerk_override_ = jerk_factor;
923 if (previous_velocity == velocity_factor &&
924 previous_acceleration == acceleration_factor &&
925 previous_jerk == jerk_factor)
926 {
927 return rt::ErrorCode::ok;
928 }
929
930 const auto restore_previous = [&]() {
931 override_ = previous_velocity;
932 acceleration_override_ = previous_acceleration;
933 jerk_override_ = previous_jerk;
934 };
935
936 if (velocity_factor == 0.0 && active_ && active_command_.kind != CommandKind::halt &&
937 active_command_.kind != CommandKind::stop)
938 {
939 const double v0 = snapshot_.command_velocity;
940 const double a0 = snapshot_.command_acceleration;
941 const double scaled_acceleration =
942 active_command_.acceleration * acceleration_override_;
943 const double scaled_deceleration =
944 active_command_.deceleration * acceleration_override_;
945 const double scaled_jerk = active_command_.jerk * jerk_override_;
946 const otg::Limits1D halt_limits{std::fabs(v0) + active_command_.velocity + 1e-9,
947 std::fmax(std::fabs(a0), scaled_acceleration),
948 scaled_deceleration, scaled_jerk};
949 double brake_velocity = v0;
950 double brake_shift = 0.0;
951 if (a0 != 0.0)
952 {
953 const double zero_cycles = std::ceil(std::fabs(a0) / scaled_jerk);
954 brake_velocity += 0.5 * a0 * zero_cycles;
955 brake_shift += v0 * zero_cycles + a0 * zero_cycles * zero_cycles / 3.0;
956 }
957 const double stop_position =
958 snapshot_.command_position + brake_shift +
959 otg::detail::ramp_between(brake_velocity, 0.0, halt_limits).distance;
961 {snapshot_.command_position, v0, a0}, {stop_position, 0.0, 0.0}, halt_limits);
962 if (!profile)
963 {
964 restore_previous();
965 return profile.error();
966 }
967 active_profile_ = profile.value();
968 active_last_sample_ = snapshot_.command_position;
969 active_tick_ = 0;
970 override_braking_ = true;
971 override_paused_ = false;
972 return rt::ErrorCode::ok;
973 }
974
975 if (previous_velocity == 0.0 && velocity_factor > 0.0 && override_paused_)
976 {
977 override_paused_ = false;
978 if (active_command_.kind == CommandKind::move_velocity || continuous_holding_)
979 {
980 return rt::ErrorCode::ok;
981 }
982 const otg::Limits1D limits = scaled_limits(active_command_);
983 const double direction = active_target_ >= snapshot_.command_position ? 1.0 : -1.0;
984 const double end_velocity = is_continuous_kind(active_command_.kind)
985 ? direction * active_command_.end_velocity * velocity_factor
986 : 0.0;
987 const rt::Result<otg::Profile1D> profile =
988 otg::plan_time_optimal({snapshot_.command_position, 0.0, 0.0},
989 {active_target_, end_velocity, 0.0}, limits);
990 if (!profile)
991 {
992 restore_previous();
993 override_paused_ = true;
994 return profile.error();
995 }
996 active_profile_ = profile.value();
997 active_last_sample_ = snapshot_.command_position;
998 active_tick_ = 0;
999 return rt::ErrorCode::ok;
1000 }
1001
1002 const bool profile_driven = active_ && active_command_.kind != CommandKind::move_velocity &&
1003 active_command_.kind != CommandKind::halt &&
1004 active_command_.kind != CommandKind::stop;
1005 if (!profile_driven)
1006 {
1007 return rt::ErrorCode::ok;
1008 }
1009
1010 const double end_velocity = is_continuous_kind(active_command_.kind)
1011 ? active_command_.end_velocity * override_
1012 : 0.0;
1013 if (continuous_holding_)
1014 {
1015 continuous_hold_velocity_ = end_velocity;
1016 return rt::ErrorCode::ok;
1017 }
1018
1019 const otg::Limits1D limits = scaled_limits(active_command_);
1020 const rt::Result<otg::Profile1D> profile =
1021 otg::plan_time_optimal({snapshot_.command_position, snapshot_.command_velocity,
1022 snapshot_.command_acceleration},
1023 {active_target_, end_velocity, 0.0}, limits);
1024 if (!profile)
1025 {
1026 restore_previous();
1027 return profile.error();
1028 }
1029 active_profile_ = profile.value();
1030 active_tick_ = 0;
1031 active_last_sample_ = snapshot_.command_position;
1032 continuous_hold_velocity_ = end_velocity;
1033 snapshot_.active_command_reached_target = false;
1034 return rt::ErrorCode::ok;
1035 }
1036
1038 {
1039 if (snapshot_.active_command_id != 0)
1040 {
1041 record_command_error(snapshot_.active_command_id, code);
1042 }
1043 for (std::size_t i = 0; i < queue_.size(); ++i)
1044 {
1045 record_command_error(queue_[i].command_id, code);
1046 }
1047 if (sync_id_ != 0)
1048 record_command_error(sync_id_, code);
1049 if (superimposed_id_ != 0)
1050 record_command_error(superimposed_id_, code);
1051 if (stream_id_ != 0)
1052 record_command_error(stream_id_, code);
1053 for (std::size_t i = 0; i < management_queue_.size(); ++i)
1054 {
1055 remember_management_result(management_queue_[i].command_id, code);
1056 }
1057 management_queue_.clear();
1058 abort_motion();
1059 snapshot_.error = true;
1060 snapshot_.error_id = code;
1061 snapshot_.status = AxisStatus::errorstop;
1062 return rt::ErrorCode::ok;
1063 }
1064
1066 {
1067 if (snapshot_.status != AxisStatus::errorstop)
1068 {
1070 }
1071 snapshot_.error = false;
1072 snapshot_.error_id = rt::ErrorCode::ok;
1073 snapshot_.status = snapshot_.powered ? AxisStatus::standstill : AxisStatus::disabled;
1074 return rt::ErrorCode::ok;
1075 }
1076
1078 {
1079 if (group_blocks_standalone_motion())
1080 {
1082 }
1083 return submit_impl(command);
1084 }
1085
1087 std::size_t segment_count,
1088 double acceleration_scale,
1089 double acceleration_offset,
1090 double time_scale)
1091 {
1092 if (group_blocks_standalone_motion() || !snapshot_.powered ||
1093 snapshot_.status == AxisStatus::errorstop || segments == nullptr ||
1094 segment_count == 0 || segment_count > QueueCapacity ||
1095 !std::isfinite(acceleration_scale) || !std::isfinite(acceleration_offset) ||
1096 !std::isfinite(time_scale) || time_scale <= 0.0)
1097 {
1099 }
1100 for (std::size_t i = 0; i < segment_count; ++i)
1101 {
1102 const double acceleration =
1103 segments[i].target * acceleration_scale + acceleration_offset;
1104 const double duration = static_cast<double>(segments[i].duration_cycles) * time_scale;
1105 if (!std::isfinite(acceleration) || segments[i].relative ||
1106 segments[i].duration_cycles <= 0 || !std::isfinite(duration) ||
1107 std::round(duration) < 1.0 || std::round(duration) >= std::ldexp(1.0, 63))
1108 {
1110 }
1111 }
1112
1113 abort_motion();
1114 acceleration_segment_count_ = segment_count;
1115 acceleration_segment_index_ = 0;
1116 acceleration_segment_tick_ = 0;
1117 acceleration_profile_completed_ = false;
1118 for (std::size_t i = 0; i < segment_count; ++i)
1119 {
1120 AxisCommand storage{};
1122 storage.value = segments[i].target * acceleration_scale + acceleration_offset;
1123 storage.min_duration_cycles = static_cast<std::int64_t>(
1124 std::llround(static_cast<double>(segments[i].duration_cycles) * time_scale));
1125 queue_.push_back(storage);
1126 }
1127 active_ = true;
1128 active_command_ = {};
1129 active_command_.kind = CommandKind::acceleration_profile;
1130 active_command_.command_id = next_command_id_++;
1131 snapshot_.active_command_id = active_command_.command_id;
1132 snapshot_.active_command_reached_target = false;
1133 snapshot_.status = AxisStatus::continuous_motion;
1134 return rt::Result<std::uint32_t>::success(active_command_.command_id);
1135 }
1136
1137 // Validate an aborting position command followed by buffered position
1138 // commands without changing axis state. Profile-table and homing facades
1139 // use this to reject the whole sequence before the first command starts.
1141 std::size_t command_count,
1142 bool enforce_soft_limits = false) const
1143 {
1144 if (commands == nullptr || command_count == 0 || command_count > QueueCapacity ||
1145 group_blocks_standalone_motion() || !snapshot_.powered ||
1146 snapshot_.status == AxisStatus::errorstop || snapshot_.error)
1147 {
1149 }
1150
1151 const BufferMode first_mode = commands[0].buffer_mode;
1152 if(!is_valid_buffer_mode(first_mode))
1153 {
1155 }
1156 if(active_ && first_mode != BufferMode::aborting &&
1157 queue_.size() + command_count > QueueCapacity)
1158 {
1160 }
1161 double from_position = first_mode == BufferMode::aborting
1162 ? snapshot_.command_position
1163 : queued_endpoint();
1164 double from_velocity = first_mode == BufferMode::aborting
1165 ? snapshot_.command_velocity
1166 : 0.0;
1167 double from_acceleration = first_mode == BufferMode::aborting
1168 ? snapshot_.command_acceleration
1169 : 0.0;
1170 for (std::size_t i = 0; i < command_count; ++i)
1171 {
1172 const AxisCommand &command = commands[i];
1173 const BufferMode expected_mode = i == 0 ? first_mode : BufferMode::buffered;
1174 if (command.buffer_mode != expected_mode ||
1178 !is_valid_direction(command.direction)) ||
1179 !is_finite_command(command) || command.velocity <= 0.0 ||
1180 command.acceleration <= 0.0 || command.deceleration <= 0.0 || command.jerk <= 0.0 ||
1181 command.min_duration_cycles < 0)
1182 {
1184 }
1185
1186 const double target = command.kind == CommandKind::move_relative
1187 ? from_position + command.value
1188 : command.value;
1189 if (!std::isfinite(target))
1190 {
1192 }
1193 if (!target_inside_limits(target, enforce_soft_limits))
1194 {
1196 }
1197
1198 const otg::Limits1D limits = scaled_limits(command);
1200 {from_position, from_velocity, from_acceleration}, {target, 0.0, 0.0}, limits);
1201 if (!profile)
1202 {
1203 return profile.error();
1204 }
1205
1206 from_position = target;
1207 from_velocity = 0.0;
1208 from_acceleration = 0.0;
1209 }
1210 return rt::ErrorCode::ok;
1211 }
1212
1213 private:
1214 friend class AxisGroup;
1215
1216 static rt::Result<MotionLimits> si_to_limits(const rt::CycleConfig &cycle,
1217 const AxisSiConfig &config,
1218 MotionLimits base)
1219 {
1220 if(cycle.period_ns() <= 0) {
1222 }
1223 base.max_velocity = cycle.velocity_to_cycle(config.max_velocity);
1224 base.max_acceleration = cycle.acceleration_to_cycle(config.max_acceleration);
1225 base.max_deceleration = cycle.acceleration_to_cycle(config.max_deceleration);
1226 base.max_jerk = cycle.jerk_to_cycle(config.max_jerk);
1227 base.min_position = config.min_position;
1228 base.max_position = config.max_position;
1229 base.min_position_enabled = config.min_position_enabled;
1230 base.max_position_enabled = config.max_position_enabled;
1231 if(!std::isfinite(base.max_velocity) || !std::isfinite(base.max_acceleration) ||
1232 !std::isfinite(base.max_deceleration) || !std::isfinite(base.max_jerk) ||
1233 (config.min_position_enabled && !std::isfinite(config.min_position)) ||
1234 (config.max_position_enabled && !std::isfinite(config.max_position)) ||
1235 (config.min_position_enabled && config.max_position_enabled &&
1236 config.min_position > config.max_position) ||
1237 base.max_velocity <= 0.0 || base.max_acceleration <= 0.0 ||
1238 base.max_deceleration <= 0.0 || base.max_jerk <= 0.0) {
1240 }
1242 }
1243
1244 rt::ErrorCode set_group_owner(void *owner, const GroupStatus *status = nullptr)
1245 {
1246 group_owner_ = owner;
1247 group_status_ = owner == nullptr ? nullptr : status;
1248 return rt::ErrorCode::ok;
1249 }
1250
1251 rt::Result<std::uint32_t> submit_group_owned(AxisCommand command)
1252 {
1253 return submit_impl(command);
1254 }
1255
1256 rt::ErrorCode preflight_group_owned(AxisCommand command) const
1257 {
1258 if (!snapshot_.powered || snapshot_.status == AxisStatus::errorstop ||
1259 !is_valid_buffer_mode(command.buffer_mode) ||
1260 !is_finite_command(command) || command.velocity <= 0.0 || command.acceleration <= 0.0 ||
1261 command.deceleration <= 0.0 || command.jerk <= 0.0 ||
1264 {
1266 }
1267
1268 command = normalize(command, queued_endpoint());
1269 if (!target_inside_limits(command.value))
1270 {
1272 }
1273 const otg::Limits1D limits = scaled_limits(command);
1274 const rt::Result<otg::Profile1D> profile =
1275 otg::plan_time_optimal({snapshot_.command_position, snapshot_.command_velocity,
1276 snapshot_.command_acceleration},
1277 {command.value, 0.0, 0.0}, limits);
1278 return profile ? rt::ErrorCode::ok : profile.error();
1279 }
1280
1281 void abort_group_owned_motion()
1282 {
1283 abort_motion();
1284 if (snapshot_.status != AxisStatus::errorstop)
1285 {
1286 snapshot_.status = snapshot_.powered ? AxisStatus::standstill : AxisStatus::disabled;
1287 }
1288 }
1289
1290 // KB-068: once the owning group leaves standby, public member motion
1291 // entry points must not become a second writer.
1292 bool group_blocks_standalone_motion() const
1293 {
1294 return group_owner_ != nullptr &&
1295 (group_status_ == nullptr || *group_status_ != GroupStatus::standby);
1296 }
1297
1298 // Pure admissibility prefix of submit_impl(): every rejection that only
1299 // reads state, in strict first-match order — each branch returns a
1300 // different code, so reordering them is an observable behavior change.
1301 //
1302 // The seam sits exactly at the first mutation in submit_impl(), the
1303 // command-id allocation. The two later rejections (the acceleration
1304 // profile takeover rule and the soft-limit check) deliberately stay
1305 // behind that allocation: a command rejected there has already consumed
1306 // a command id, and hoisting them would shift the observable command-id
1307 // sequence, which the golden replay baseline pins.
1308 rt::ErrorCode submit_admissibility(const AxisCommand &command) const
1309 {
1310 const bool torque = command.kind == CommandKind::torque;
1311 if (!snapshot_.powered || snapshot_.status == AxisStatus::errorstop ||
1312 !is_valid_buffer_mode(command.buffer_mode) ||
1314 (torque ? (command.velocity < 0.0 || command.acceleration < 0.0 ||
1315 command.deceleration < 0.0 || command.jerk < 0.0 ||
1316 !is_valid_direction(command.direction) ||
1317 command.direction == Direction::shortest_way)
1318 : (command.velocity <= 0.0 || command.acceleration <= 0.0 ||
1319 command.deceleration <= 0.0 || command.jerk <= 0.0)))
1320 {
1322 }
1323 if(torque && command.buffer_mode != BufferMode::aborting) {
1324 // A torque command has no natural completion point. Matching the
1325 // Beckhoff CST contract, only an aborting takeover is defined.
1327 }
1328 if (stop_lock_id_ != 0 && command.kind != CommandKind::stop)
1329 {
1331 }
1332 if ((command.kind == CommandKind::move_absolute ||
1334 !is_valid_direction(command.direction))
1335 {
1337 }
1338
1339 // Queueing a motion command behind an engaged synchronization or a
1340 // stream session has no defined completion point; only aborting
1341 // commands may take over.
1342 if ((sync_kind_ != SyncKind::none || stream_active_) &&
1343 command.buffer_mode != BufferMode::aborting)
1344 {
1346 }
1347 if (is_continuous_kind(command.kind) && command.end_velocity == 0.0)
1348 {
1350 }
1351 const double direction_reference =
1352 command.buffer_mode == BufferMode::aborting
1353 ? snapshot_.command_position
1354 : queued_endpoint();
1355 if (!command_direction_enabled(command, direction_reference))
1356 {
1358 }
1359 return rt::ErrorCode::ok;
1360 }
1361
1362 rt::Result<std::uint32_t> submit_impl(AxisCommand command)
1363 {
1364 const rt::ErrorCode admissible = submit_admissibility(command);
1365 if (admissible != rt::ErrorCode::ok)
1366 {
1367 return rt::Result<std::uint32_t>::failure(admissible);
1368 }
1369
1370 if (command.command_id == 0)
1371 {
1372 command.command_id = next_command_id_++;
1373 }
1374
1375 if (command.kind == CommandKind::torque)
1376 {
1377 const double prior_torque = snapshot_.command_torque;
1378 abort_motion();
1379 active_ = true;
1380 active_command_ = command;
1381 torque_command_id_ = command.command_id;
1382 snapshot_.active_command_id = command.command_id;
1383 snapshot_.command_torque = command.torque_ramp == 0.0
1384 ? command.value
1385 : prior_torque;
1386 snapshot_.torque_velocity_limit = command.velocity;
1387 snapshot_.torque_acceleration_limit = command.acceleration;
1388 snapshot_.torque_deceleration_limit = command.deceleration;
1389 snapshot_.torque_jerk_limit = command.jerk;
1390 snapshot_.torque_direction = command.direction;
1391 snapshot_.torque_mode = true;
1392 snapshot_.status = AxisStatus::continuous_motion;
1394 }
1395 if (active_ && active_command_.kind == CommandKind::acceleration_profile &&
1396 command.buffer_mode != BufferMode::aborting)
1397 {
1399 }
1400
1401 // The additive target resolves against the endpoint that was committed
1402 // before an aborting takeover discards it.
1403 const double takeover_endpoint = queued_endpoint();
1404 const AxisStatus takeover_status = snapshot_.status;
1405 // Aborting takeovers keep kinematic continuity: abort_motion() zeroes
1406 // the command velocity/acceleration, but the new command must plan
1407 // from the state the axis was actually in (KB-026).
1408 const double takeover_velocity = snapshot_.command_velocity;
1409 const double takeover_acceleration = snapshot_.command_acceleration;
1410 if (command.buffer_mode == BufferMode::aborting)
1411 {
1412 abort_motion();
1413 snapshot_.command_velocity = takeover_velocity;
1414 snapshot_.command_acceleration = takeover_acceleration;
1415 snapshot_.actual_velocity = takeover_velocity;
1416 snapshot_.actual_acceleration = takeover_acceleration;
1417 }
1418 if (command.buffer_mode == BufferMode::aborting)
1419 {
1420 const double relative_base = snapshot_.command_position;
1421 const double additive_base = takeover_status == AxisStatus::discrete_motion
1422 ? takeover_endpoint
1423 : snapshot_.command_position;
1424 command =
1425 normalize(command, command.kind == CommandKind::move_additive ? additive_base
1426 : relative_base);
1427 }
1428 if ((command.kind == CommandKind::move_absolute ||
1430 command.kind == CommandKind::home) &&
1431 !target_inside_limits(command.value))
1432 {
1434 }
1435
1436 if (command.buffer_mode == BufferMode::aborting || !active_)
1437 {
1438 const rt::ErrorCode started = start(command);
1439 if (started != rt::ErrorCode::ok)
1440 {
1441 return rt::Result<std::uint32_t>::failure(started);
1442 }
1444 }
1445
1446 const rt::ErrorCode queued = queue_.push_back(command);
1448 {
1450 }
1452 }
1453
1454 public:
1456 exec::CamTableView table,
1457 bool periodic,
1458 bool master_absolute,
1459 bool slave_absolute,
1460 std::uint32_t replace_id = 0)
1461 {
1462 table.periodic = periodic;
1463 if(master == nullptr || master == this || !table.valid() ||
1464 (!master_absolute && !table.periodic &&
1465 (table.points[0].master > 0.0 || table.points[table.size - 1].master < 0.0))) {
1467 }
1468
1469 CamTableSelection *slot = nullptr;
1470 if(replace_id != 0) {
1471 for(CamTableSelection &selection : cam_table_selections_) {
1472 if(selection.id == replace_id) {
1473 slot = &selection;
1474 break;
1475 }
1476 }
1477 }
1478 if(slot == nullptr) {
1479 for(CamTableSelection &selection : cam_table_selections_) {
1480 if(selection.id == 0) {
1481 slot = &selection;
1482 break;
1483 }
1484 }
1485 }
1486 if(slot == nullptr) {
1488 }
1489
1490 const std::uint32_t id = slot->id == 0 ? next_command_id_++ : slot->id;
1491 *slot = {id,
1492 master,
1493 table,
1494 master_absolute,
1495 slave_absolute,
1496 master->snapshot().command_position,
1497 snapshot_.command_position};
1499 }
1500
1502 {
1503 if(id == 0) {
1505 }
1506 for(const CamTableSelection &selection : cam_table_selections_) {
1507 if(selection.id == id) {
1509 }
1510 }
1512 }
1513
1515 {
1516 if (command.master == nullptr || command.master == this ||
1517 !std::isfinite(command.ratio_numerator) || !std::isfinite(command.ratio_denominator) ||
1518 command.ratio_denominator == 0.0 || !std::isfinite(command.master_sync_position) ||
1519 !std::isfinite(command.slave_sync_position) ||
1520 !std::isfinite(command.master_start_distance) ||
1521 !std::isfinite(command.approach_velocity) || command.approach_velocity < 0.0 ||
1522 !std::isfinite(command.acceleration) || command.acceleration < 0.0 ||
1523 !std::isfinite(command.deceleration) || command.deceleration < 0.0 ||
1524 !std::isfinite(command.jerk) || command.jerk < 0.0)
1525 {
1527 }
1528 if (command.sync_mode != SyncMode::shortest &&
1529 command.sync_mode != SyncMode::catch_up &&
1530 command.sync_mode != SyncMode::slow_down)
1531 {
1533 }
1534 if (command.position_sync && command.sync_mode != SyncMode::shortest)
1535 {
1537 }
1538
1539 const rt::Result<std::uint32_t> begun = begin_sync(SyncKind::gear, command.buffer_mode);
1540 if (!begun)
1541 {
1542 return begun;
1543 }
1544 sync_gear_ = command;
1545 phase_offset_ = 0.0;
1546 abort_phasing_commands();
1547 sync_profile_ready_ = false;
1548 sync_entry_phase_ = command.position_sync ? SyncPhase::waiting_window : SyncPhase::approaching;
1549 if (sync_phase_ != SyncPhase::queued)
1550 {
1551 sync_phase_ = sync_entry_phase_;
1552 }
1553 return begun;
1554 }
1555
1557 {
1558 if (command.master == nullptr || command.master == this || !command.table.valid() ||
1559 !std::isfinite(command.master_offset) || !std::isfinite(command.master_scaling) ||
1560 command.master_scaling == 0.0 || !std::isfinite(command.slave_offset) ||
1561 !std::isfinite(command.slave_scaling) || !std::isfinite(command.master_sync_position) ||
1562 !std::isfinite(command.master_start_distance) || command.master_start_distance < 0.0 ||
1563 !std::isfinite(command.approach_velocity) || command.approach_velocity < 0.0)
1564 {
1566 }
1567
1568 // Approved cam matrix: the spline reconstruction happens at engage
1569 // in the planning domain; a failed build rejects before any state
1570 // changes.
1571 if (command.interpolation == exec::CamInterpolation::spline)
1572 {
1573 const rt::ErrorCode built = cam_spline_.build(command.table);
1574 if (built != rt::ErrorCode::ok)
1575 {
1577 }
1578 }
1579
1580 const rt::Result<std::uint32_t> begun = begin_sync(SyncKind::cam, command.buffer_mode);
1581 if (!begun)
1582 {
1583 return begun;
1584 }
1585 sync_cam_ = command;
1586 sync_entry_phase_ =
1587 command.master_start_distance > 0.0 ? SyncPhase::waiting_window : SyncPhase::engaged;
1588 if (sync_phase_ != SyncPhase::queued)
1589 {
1590 sync_phase_ = sync_entry_phase_;
1591 }
1592 return begun;
1593 }
1594
1596 {
1597 if (command.master1 == nullptr || command.master2 == nullptr || command.master1 == this ||
1598 command.master2 == this || !std::isfinite(command.ratio_numerator_m1) ||
1599 !std::isfinite(command.ratio_denominator_m1) || command.ratio_denominator_m1 == 0.0 ||
1600 !std::isfinite(command.ratio_numerator_m2) ||
1601 !std::isfinite(command.ratio_denominator_m2) || command.ratio_denominator_m2 == 0.0)
1602 {
1604 }
1605
1606 const rt::Result<std::uint32_t> begun = begin_sync(SyncKind::combine, command.buffer_mode);
1607 if (!begun)
1608 {
1609 return begun;
1610 }
1611 sync_combine_ = command;
1612 sync_entry_phase_ = SyncPhase::engaged;
1613 if (sync_phase_ != SyncPhase::queued)
1614 {
1615 sync_phase_ = sync_entry_phase_;
1616 }
1617 return begun;
1618 }
1619
1620 // Approved cam matrix (decision #5): online table switch on an engaged
1621 // cam. The new geometry must agree with the current slave position at
1622 // the current master input (within tolerance); velocity/acceleration may
1623 // jump within the declared boundary — the slave rides the new table from
1624 // the next cycle. The engagement phase, sync id, and master stay.
1626 {
1627 if (sync_kind_ != SyncKind::cam || sync_phase_ != SyncPhase::engaged ||
1628 command.master != sync_cam_.master || !command.table.valid() ||
1629 !std::isfinite(tolerance) || tolerance < 0.0 || command.master_start_distance != 0.0 ||
1630 !std::isfinite(command.master_offset) || !std::isfinite(command.master_scaling) ||
1631 command.master_scaling == 0.0 || !std::isfinite(command.slave_offset) ||
1632 !std::isfinite(command.slave_scaling))
1633 {
1635 }
1636
1637 exec::CamSpline replacement{};
1638 if (command.interpolation == exec::CamInterpolation::spline)
1639 {
1640 const rt::ErrorCode built = replacement.build(command.table);
1641 if (built != rt::ErrorCode::ok)
1642 {
1643 return built;
1644 }
1645 }
1646
1647 const double master_position = master_value(*sync_cam_.master, sync_cam_.source);
1648 const double table_input =
1649 (master_position - command.master_offset) / command.master_scaling;
1650 const rt::Result<double> sampled = command.interpolation == exec::CamInterpolation::spline
1651 ? replacement.sample(table_input)
1652 : command.table.sample(table_input);
1653 if (!sampled)
1654 {
1655 return sampled.error();
1656 }
1657 const double replacement_slave =
1658 command.slave_offset + command.slave_scaling * sampled.value();
1659 const rt::Result<double> current = cam_slave_value(master_position);
1660 if (!current)
1661 {
1662 return current.error();
1663 }
1664 if (std::fabs(replacement_slave - current.value()) > tolerance)
1665 {
1667 }
1668
1669 sync_cam_.table = command.table;
1670 sync_cam_.interpolation = command.interpolation;
1671 sync_cam_.master_offset = command.master_offset;
1672 sync_cam_.master_scaling = command.master_scaling;
1673 sync_cam_.slave_offset = command.slave_offset;
1674 sync_cam_.slave_scaling = command.slave_scaling;
1675 if (command.interpolation == exec::CamInterpolation::spline)
1676 {
1677 cam_spline_ = replacement;
1678 }
1679 return rt::ErrorCode::ok;
1680 }
1681
1682 rt::ErrorCode gear_update(double ratio_numerator, double ratio_denominator)
1683 {
1684 if (sync_kind_ != SyncKind::gear || !std::isfinite(ratio_numerator) ||
1685 !std::isfinite(ratio_denominator) || ratio_denominator == 0.0)
1686 {
1688 }
1689 sync_gear_.ratio_numerator = ratio_numerator;
1690 sync_gear_.ratio_denominator = ratio_denominator;
1691 return rt::ErrorCode::ok;
1692 }
1693
1694 rt::ErrorCode cam_update(double master_offset, double master_scaling, double slave_offset,
1695 double slave_scaling)
1696 {
1697 if (sync_kind_ != SyncKind::cam || !std::isfinite(master_offset) ||
1698 !std::isfinite(master_scaling) || master_scaling == 0.0 ||
1699 !std::isfinite(slave_offset) || !std::isfinite(slave_scaling))
1700 {
1702 }
1703 sync_cam_.master_offset = master_offset;
1704 sync_cam_.master_scaling = master_scaling;
1705 sync_cam_.slave_offset = slave_offset;
1706 sync_cam_.slave_scaling = slave_scaling;
1707 return rt::ErrorCode::ok;
1708 }
1709
1710 rt::ErrorCode combine_update(CombineMode mode, double ratio_numerator_m1,
1711 double ratio_denominator_m1, double ratio_numerator_m2,
1712 double ratio_denominator_m2)
1713 {
1714 if (sync_kind_ != SyncKind::combine || !std::isfinite(ratio_numerator_m1) ||
1715 !std::isfinite(ratio_denominator_m1) || ratio_denominator_m1 == 0.0 ||
1716 !std::isfinite(ratio_numerator_m2) || !std::isfinite(ratio_denominator_m2) ||
1717 ratio_denominator_m2 == 0.0)
1718 {
1720 }
1721 sync_combine_.mode = mode;
1722 sync_combine_.ratio_numerator_m1 = ratio_numerator_m1;
1723 sync_combine_.ratio_denominator_m1 = ratio_denominator_m1;
1724 sync_combine_.ratio_numerator_m2 = ratio_numerator_m2;
1725 sync_combine_.ratio_denominator_m2 = ratio_denominator_m2;
1726 return rt::ErrorCode::ok;
1727 }
1728
1730 {
1731 if (sync_kind_ == SyncKind::none)
1732 {
1734 }
1735 const double velocity = snapshot_.command_velocity;
1736 reset_sync();
1737 snapshot_.active_command_id = 0;
1738 if (velocity != 0.0 && snapshot_.powered)
1739 {
1740 AxisCommand hold{};
1742 hold.value = velocity < 0.0 ? -1.0 : 1.0;
1743 hold.velocity = std::fabs(velocity);
1744 hold.command_id = next_command_id_++;
1745 start(hold);
1746 }
1747 else
1748 {
1749 snapshot_.status = snapshot_.powered ? AxisStatus::standstill : AxisStatus::disabled;
1750 snapshot_.command_velocity = 0.0;
1751 snapshot_.actual_velocity = 0.0;
1752 }
1753 return rt::ErrorCode::ok;
1754 }
1755
1756 // B9 stream session (approved trajectory-stream matrix, decisions #9/#10).
1757 // Engaging is an aborting-class takeover: the filter starts from the
1758 // current kinematic state (no jump; a moving entry arms the filter's
1759 // controlled-stop ladder until the first target). Undefined combinations
1760 // are explicit errors: no engage while unpowered, in errorstop, gear/cam/
1761 // combine-synchronized, group-owned, or already streaming; axis software
1762 // position limits are not auto-applied — wire them through the filter
1763 // envelope in the config.
1765 {
1766 if (!snapshot_.powered || snapshot_.status == AxisStatus::errorstop ||
1767 sync_kind_ != SyncKind::none || stream_active_ || group_owner_ != nullptr)
1768 {
1770 }
1771 const rt::ErrorCode configured = stream_filter_.configure(config);
1772 if (configured != rt::ErrorCode::ok)
1773 {
1774 return rt::Result<std::uint32_t>::failure(configured);
1775 }
1776
1777 // Aborting takeovers keep kinematic continuity (KB-026 pattern):
1778 // abort_motion() zeroes the command velocity/acceleration, but the
1779 // filter must start from the state the axis was actually in.
1780 const double takeover_velocity = snapshot_.command_velocity;
1781 const double takeover_acceleration = snapshot_.command_acceleration;
1782 abort_motion();
1783 snapshot_.command_velocity = takeover_velocity;
1784 snapshot_.actual_velocity = takeover_velocity;
1785 snapshot_.command_acceleration = takeover_acceleration;
1786 snapshot_.actual_acceleration = takeover_acceleration;
1787
1788 const rt::ErrorCode reset = stream_filter_.reset(
1789 {snapshot_.command_position, takeover_velocity, takeover_acceleration});
1790 if (reset != rt::ErrorCode::ok)
1791 {
1792 snapshot_.command_velocity = 0.0;
1793 snapshot_.actual_velocity = 0.0;
1794 snapshot_.command_acceleration = 0.0;
1795 snapshot_.actual_acceleration = 0.0;
1797 }
1798 stream_active_ = true;
1799 stream_id_ = next_command_id_++;
1800 snapshot_.status = AxisStatus::synchronized_motion;
1801 return rt::Result<std::uint32_t>::success(stream_id_);
1802 }
1803
1805 {
1806 if (!stream_active_)
1807 {
1809 }
1810 return stream_filter_.push_target(target);
1811 }
1812
1813 // Graceful exit is only defined at rest; a moving session exits through
1814 // a standard aborting command (MC_Halt/MC_Stop/motion takeover).
1816 {
1817 if (!stream_active_)
1818 {
1820 }
1821 if (snapshot_.command_velocity != 0.0 || snapshot_.command_acceleration != 0.0)
1822 {
1824 }
1825 stream_filter_.end_session();
1826 stream_active_ = false;
1827 stream_id_ = 0;
1828 snapshot_.status = snapshot_.powered ? AxisStatus::standstill : AxisStatus::disabled;
1829 return rt::ErrorCode::ok;
1830 }
1831
1832 std::uint32_t stream_session_id() const { return stream_id_; }
1833
1834 // Read-only session introspection (mode, counters, clamped flag); only
1835 // meaningful while the session is engaged.
1836 const stream::StreamFilter1D &stream_filter() const { return stream_filter_; }
1837
1838 SyncPhase sync_phase() const { return sync_phase_; }
1839
1840 std::uint32_t sync_command_id() const { return sync_id_; }
1841
1842 double gear_phase_offset() const { return phase_offset_; }
1843
1844 bool phasing_active() const { return phasing_active_; }
1845
1846 PhasingCommandState phasing_command_state(std::uint32_t command_id) const
1847 {
1848 for (std::size_t i = 0; i < phasing_result_count_; ++i)
1849 {
1850 const std::size_t index =
1851 (phasing_result_cursor_ + PhasingResultCapacity - 1 - i) % PhasingResultCapacity;
1852 if (phasing_results_[index].command_id == command_id)
1853 {
1854 return phasing_results_[index].state;
1855 }
1856 }
1858 }
1859
1860 double phasing_covered_shift(std::uint32_t command_id) const
1861 {
1862 for (std::size_t i = 0; i < phasing_result_count_; ++i)
1863 {
1864 const std::size_t index =
1865 (phasing_result_cursor_ + PhasingResultCapacity - 1 - i) % PhasingResultCapacity;
1866 if (phasing_results_[index].command_id == command_id)
1867 {
1868 return phasing_results_[index].covered_shift;
1869 }
1870 }
1871 return 0.0;
1872 }
1873
1874 bool gear_engaged_with(const AxisModel *master) const
1875 {
1876 return master != nullptr && sync_kind_ == SyncKind::gear &&
1877 sync_phase_ == SyncPhase::engaged && sync_gear_.master == master;
1878 }
1879
1881 {
1882 if (sync_kind_ != SyncKind::gear || sync_phase_ != SyncPhase::engaged ||
1883 !std::isfinite(command.phase_shift) || !std::isfinite(command.velocity) ||
1884 !std::isfinite(command.acceleration) || !std::isfinite(command.deceleration) ||
1885 !std::isfinite(command.jerk) || command.velocity < 0.0 ||
1886 command.acceleration < 0.0 || command.deceleration < 0.0 || command.jerk < 0.0)
1887 {
1889 }
1890 if (command.buffer_mode != BufferMode::aborting &&
1891 command.buffer_mode != BufferMode::buffered)
1892 {
1894 }
1895 const bool direct = command.velocity == 0.0;
1896 if ((direct && (command.acceleration != 0.0 || command.deceleration != 0.0 ||
1897 command.jerk != 0.0)) ||
1898 (!direct && (command.acceleration <= 0.0 || command.deceleration <= 0.0 ||
1899 command.jerk <= 0.0)))
1900 {
1902 }
1903 if (command.buffer_mode == BufferMode::buffered &&
1904 phasing_active_ && phasing_queue_.size() >= QueueCapacity)
1905 {
1907 }
1908 if (command.buffer_mode == BufferMode::aborting && !direct)
1909 {
1910 const double target = command.relative ? phase_offset_ + command.phase_shift
1911 : command.phase_shift;
1912 if (target != phase_offset_)
1913 {
1914 const otg::Limits1D limits{command.velocity, command.acceleration,
1915 command.deceleration, command.jerk};
1917 {phase_offset_, phasing_velocity_, phasing_acceleration_},
1918 {target, 0.0, 0.0}, limits);
1919 if (!preflight)
1920 {
1921 return rt::Result<std::uint32_t>::failure(preflight.error());
1922 }
1923 }
1924 }
1925
1926 command.command_id = next_command_id_++;
1927 remember_phasing(command.command_id, PhasingCommandState::queued, 0.0);
1928 if (command.buffer_mode == BufferMode::aborting)
1929 {
1930 abort_phasing_commands(true);
1931 remember_phasing(command.command_id, PhasingCommandState::queued, 0.0);
1932 }
1933 if (phasing_active_)
1934 {
1935 const rt::ErrorCode queued = phasing_queue_.push_back(command);
1937 {
1939 }
1940 }
1941 else
1942 {
1943 const rt::ErrorCode started = start_phasing(command);
1944 if (started != rt::ErrorCode::ok)
1945 {
1946 remember_phasing(command.command_id, PhasingCommandState::aborted, 0.0);
1947 return rt::Result<std::uint32_t>::failure(started);
1948 }
1949 }
1951 }
1952
1953 // Adapter hook: inject measured feedback without touching command state.
1954 rt::ErrorCode set_actual_feedback(double position, double velocity, double acceleration = 0.0,
1955 double torque = 0.0)
1956 {
1957 if (!std::isfinite(position) || !std::isfinite(velocity) || !std::isfinite(acceleration) ||
1958 !std::isfinite(torque))
1959 {
1961 }
1962 snapshot_.actual_position = position;
1963 snapshot_.actual_velocity = velocity;
1964 snapshot_.actual_acceleration = acceleration;
1965 snapshot_.actual_torque = torque;
1966 return rt::ErrorCode::ok;
1967 }
1968
1969 // MC_Home direct mode: remap the coordinate and mark the axis homed.
1971 {
1972 if (!std::isfinite(position) || homing_step_precondition() != rt::ErrorCode::ok)
1973 {
1975 }
1976 abort_motion();
1977 snapshot_.status = snapshot_.powered ? AxisStatus::standstill : AxisStatus::disabled;
1978 const rt::ErrorCode set = set_position(position);
1979 if (set != rt::ErrorCode::ok)
1980 {
1981 return set;
1982 }
1983 snapshot_.homed = true;
1984 homing_limits_suspended_ = false;
1985 return rt::ErrorCode::ok;
1986 }
1987
1988 // Part 5 homed lifecycle: accepted Step FBs clear, FinishHoming sets.
1990 {
1991 snapshot_.homed = false;
1992 homing_limits_suspended_ = true;
1993 }
1994
1996 {
1997 snapshot_.homed = true;
1998 homing_limits_suspended_ = false;
1999 }
2000
2002 {
2003 set_homed();
2004 if (!has_standalone_motion() && snapshot_.status == AxisStatus::homing)
2005 {
2006 snapshot_.status = snapshot_.powered ? AxisStatus::standstill : AxisStatus::disabled;
2007 }
2008 }
2009
2011 {
2013 {
2015 }
2016 abort_motion();
2017 set_homed();
2018 snapshot_.status = snapshot_.powered ? AxisStatus::standstill : AxisStatus::disabled;
2019 return rt::ErrorCode::ok;
2020 }
2021
2022 // Digital IO banks. Adapters feed input levels through set_digital_input
2023 // and consume outputs written by the IO function blocks. The touch-probe
2024 // trigger channels are these digital inputs (v0.x Servo extension channel
2025 // semantics); probes capture on the rising edge evaluated inside cycle().
2026 // An input that is already high when the probe arms does not capture until
2027 // a fresh edge.
2028 static constexpr std::size_t DigitalInputCount = 4;
2029 static constexpr std::size_t DigitalOutputCount = 4;
2030
2031 rt::ErrorCode set_digital_input(std::size_t input, bool level)
2032 {
2033 if (input >= DigitalInputCount)
2034 {
2036 }
2037 digital_input_[input] = level;
2038 return rt::ErrorCode::ok;
2039 }
2040
2041 rt::Result<bool> digital_input(std::size_t input) const
2042 {
2043 if (input >= DigitalInputCount)
2044 {
2046 }
2047 return rt::Result<bool>::success(digital_input_[input]);
2048 }
2049
2050 rt::ErrorCode set_digital_output(std::size_t output, bool level)
2051 {
2052 if (output >= DigitalOutputCount)
2053 {
2055 }
2056 digital_output_[output] = level;
2057 return rt::ErrorCode::ok;
2058 }
2059
2060 rt::Result<bool> digital_output(std::size_t output) const
2061 {
2062 if (output >= DigitalOutputCount)
2063 {
2065 }
2066 return rt::Result<bool>::success(digital_output_[output]);
2067 }
2068
2069 // Diagnostic info bits for MC_ReadAxisInfo. Defaults describe the built-in
2070 // simulation (ready, no switches, no warning); adapters override them.
2072 {
2075 bool home_abs_switch = false;
2076 bool limit_switch_pos = false;
2077 bool limit_switch_neg = false;
2078 bool warning = false;
2079 };
2080
2081 void set_axis_info_inputs(const AxisInfoInputs &inputs) { axis_info_ = inputs; }
2082
2083 const AxisInfoInputs &axis_info_inputs() const { return axis_info_; }
2084
2085 rt::Result<std::uint32_t> arm_touch_probe(std::size_t input, bool window_only,
2086 double first_position, double last_position)
2087 {
2088 if (input >= DigitalInputCount)
2089 {
2091 }
2092 if (window_only && (!std::isfinite(first_position) || !std::isfinite(last_position) ||
2093 first_position > last_position))
2094 {
2096 }
2097 ProbeSlot &slot = probes_[input];
2098 slot.armed = true;
2099 slot.captured = false;
2100 slot.window_only = window_only;
2101 slot.first_position = first_position;
2102 slot.last_position = last_position;
2103 slot.recorded_position = 0.0;
2104 slot.last_level = digital_input_[input];
2105 slot.command_id = next_command_id_++;
2106 return rt::Result<std::uint32_t>::success(slot.command_id);
2107 }
2108
2109 // Disarming an idle input is not an error (matches the v0.x boundary).
2110 rt::ErrorCode abort_trigger(std::size_t input)
2111 {
2112 if (input >= DigitalInputCount)
2113 {
2115 }
2116 probes_[input].armed = false;
2117 probes_[input].captured = false;
2118 probes_[input].command_id = 0;
2119 return rt::ErrorCode::ok;
2120 }
2121
2122 std::uint32_t probe_command_id(std::size_t input) const
2123 {
2124 return input < DigitalInputCount ? probes_[input].command_id : 0;
2125 }
2126
2128 {
2129 if (!active_ || input >= DigitalInputCount || sync_kind_ != SyncKind::none ||
2130 stream_active_ || superimposed_active_ || group_blocks_standalone_motion())
2131 {
2133 }
2134 if (passive_homing_id_ != 0)
2135 {
2136 passive_homing_aborted_id_ = passive_homing_id_;
2137 abort_trigger(passive_homing_input_);
2138 }
2139 const rt::Result<std::uint32_t> armed = arm_touch_probe(input, false, 0.0, 0.0);
2140 if (!armed)
2141 return armed;
2142 passive_homing_id_ = armed.value();
2143 passive_homing_input_ = input;
2144 return armed;
2145 }
2146
2148 {
2149 if (owner == 0 || passive_homing_id_ != owner)
2150 {
2152 }
2153 passive_homing_id_ = 0;
2154 return rt::ErrorCode::ok;
2155 }
2156
2158 {
2159 if (passive_homing_id_ == 0)
2160 {
2162 }
2163 const std::uint32_t owner = passive_homing_id_;
2164 abort_trigger(passive_homing_input_);
2165 passive_homing_aborted_id_ = owner;
2166 passive_homing_id_ = 0;
2168 }
2169
2170 std::uint32_t passive_homing_id() const { return passive_homing_id_; }
2171 std::uint32_t passive_homing_aborted_id() const { return passive_homing_aborted_id_; }
2172
2173 bool probe_captured(std::size_t input) const
2174 {
2175 return input < DigitalInputCount && probes_[input].captured;
2176 }
2177
2178 double probe_recorded_position(std::size_t input) const
2179 {
2180 return input < DigitalInputCount ? probes_[input].recorded_position : 0.0;
2181 }
2182
2183 void cycle()
2184 {
2185 if (snapshot_.status == AxisStatus::errorstop)
2186 {
2187 return;
2188 }
2190 {
2191 cycle_management();
2192 }
2193 if (stream_cycle())
2194 {
2195 cycle_probes();
2196 return;
2197 }
2198 if (sync_cycle())
2199 {
2200 cycle_probes();
2201 return;
2202 }
2203 cycle_base_motion();
2204 cycle_superimposed();
2205 cycle_probes();
2206 }
2207
2208 // Independent offset profile on top of the base motion (MC_MoveSuperimposed).
2209 rt::Result<std::uint32_t> submit_superimposed(double distance, double velocity,
2210 double acceleration, double deceleration,
2211 double jerk)
2212 {
2213 if (group_blocks_standalone_motion() || !snapshot_.powered ||
2214 snapshot_.status == AxisStatus::errorstop || sync_kind_ != SyncKind::none ||
2215 stream_active_ || !std::isfinite(distance) || !std::isfinite(velocity) ||
2216 velocity <= 0.0 || !std::isfinite(acceleration) || acceleration <= 0.0 ||
2217 !std::isfinite(deceleration) || deceleration <= 0.0 || !std::isfinite(jerk) ||
2218 jerk <= 0.0 || !direction_enabled(distance))
2219 {
2221 }
2222
2223 const otg::Limits1D limits =
2224 scaled_limits(velocity, acceleration, deceleration, jerk);
2225 const rt::Result<otg::Profile1D> profile =
2226 otg::plan_time_optimal({0.0, 0.0, 0.0}, {distance, 0.0, 0.0}, limits);
2227 if (!profile)
2228 {
2229 return rt::Result<std::uint32_t>::failure(profile.error());
2230 }
2231
2232 superimposed_profile_ = profile.value();
2233 superimposed_tick_ = 0;
2234 superimposed_last_ = 0.0;
2235 superimposed_active_ = true;
2236 superimposed_halting_ = false;
2237 superimposed_id_ = next_command_id_++;
2238 superimposed_completed_id_ = 0;
2239 return rt::Result<std::uint32_t>::success(superimposed_id_);
2240 }
2241
2242 // Stops only the superimposed offset; the base command keeps running. The
2243 // contribution accumulated so far persists in the command position.
2244 rt::ErrorCode halt_superimposed(double deceleration, double jerk)
2245 {
2246 if (!snapshot_.powered || snapshot_.status == AxisStatus::errorstop ||
2247 !std::isfinite(deceleration) || deceleration <= 0.0 ||
2248 !std::isfinite(jerk) || jerk <= 0.0)
2249 {
2251 }
2252 if (!superimposed_active_)
2253 {
2254 return rt::ErrorCode::ok;
2255 }
2256
2258 superimposed_profile_, rt::CycleTick::from_cycles(superimposed_tick_));
2259 if (current.velocity == 0.0 && current.acceleration == 0.0)
2260 {
2261 superimposed_active_ = false;
2262 superimposed_id_ = 0;
2263 superimposed_halting_ = false;
2264 superimposed_completed_id_ = 0;
2265 return rt::ErrorCode::ok;
2266 }
2267
2268 double brake_velocity = current.velocity;
2269 double brake_shift = 0.0;
2270 if (current.acceleration != 0.0)
2271 {
2272 const double zero_cycles =
2273 std::ceil(std::fabs(current.acceleration) / jerk);
2274 brake_velocity += 0.5 * current.acceleration * zero_cycles;
2275 brake_shift += current.velocity * zero_cycles +
2276 current.acceleration * zero_cycles * zero_cycles / 3.0;
2277 }
2278 const otg::Limits1D halt_limits{
2279 std::fabs(current.velocity) + std::fabs(brake_velocity) + 1e-9,
2280 std::fmax(std::fabs(current.acceleration), deceleration),
2281 deceleration, jerk};
2282 const double stop_position = current.position + brake_shift +
2283 otg::detail::ramp_between(brake_velocity, 0.0, halt_limits).distance;
2285 current, {stop_position, 0.0, 0.0}, halt_limits);
2286 if (!halt)
2287 {
2288 return halt.error();
2289 }
2290 superimposed_profile_ = halt.value();
2291 superimposed_tick_ = 0;
2292 superimposed_last_ = current.position;
2293 superimposed_halting_ = true;
2294 superimposed_completed_id_ = 0;
2295 return rt::ErrorCode::ok;
2296 }
2297
2299 {
2300 superimposed_active_ = false;
2301 superimposed_id_ = 0;
2302 superimposed_completed_id_ = 0;
2303 superimposed_halting_ = false;
2304 }
2305
2306 bool superimposed_active() const { return superimposed_active_; }
2307
2308 std::uint32_t superimposed_command_id() const { return superimposed_id_; }
2309
2310 std::uint32_t superimposed_completed_id() const { return superimposed_completed_id_; }
2311
2312 double superimposed_distance() const { return superimposed_last_; }
2313
2315 double distance,
2316 double velocity,
2317 double acceleration,
2318 double deceleration,
2319 double jerk)
2320 {
2321 if(!superimposed_active_ || superimposed_halting_ || command_id == 0 ||
2322 command_id != superimposed_id_ || !std::isfinite(distance) ||
2323 !std::isfinite(velocity) || velocity <= 0.0 ||
2324 !std::isfinite(acceleration) || acceleration <= 0.0 ||
2325 !std::isfinite(deceleration) || deceleration <= 0.0 ||
2326 !std::isfinite(jerk) || jerk <= 0.0 ||
2327 !direction_enabled(distance - superimposed_last_))
2328 {
2330 }
2332 superimposed_profile_, rt::CycleTick::from_cycles(superimposed_tick_));
2334 current, {distance, 0.0, 0.0},
2335 scaled_limits(velocity, acceleration, deceleration, jerk));
2336 if(!profile)
2337 {
2338 return profile.error();
2339 }
2340 superimposed_profile_ = profile.value();
2341 superimposed_tick_ = 0;
2342 superimposed_last_ = current.position;
2343 return rt::ErrorCode::ok;
2344 }
2345
2346 // True while the command sits in the buffered queue (facades report Busy
2347 // for queued successors instead of misreading them as aborted).
2348 bool command_pending(std::uint32_t command_id) const
2349 {
2350 if (command_id == 0)
2351 {
2352 return false;
2353 }
2354 for (std::size_t i = 0; i < queue_.size(); ++i)
2355 {
2356 if (queue_[i].command_id == command_id)
2357 {
2358 return true;
2359 }
2360 }
2361 return false;
2362 }
2363
2364 bool management_command_pending(std::uint32_t command_id) const
2365 {
2366 if(command_id == 0) return false;
2367 for(std::size_t i = 0; i < management_queue_.size(); ++i) {
2368 if(management_queue_[i].command_id == command_id) return true;
2369 }
2370 return false;
2371 }
2372
2373 bool management_command_active(std::uint32_t command_id) const
2374 {
2375 return command_id != 0 && active_management_id_ == command_id;
2376 }
2377
2378 bool management_command_done(std::uint32_t command_id) const
2379 {
2380 for(std::size_t i = 0; i < management_results_.size(); ++i) {
2381 if(management_results_[i].command_id == command_id) {
2382 return management_results_[i].error == rt::ErrorCode::ok;
2383 }
2384 }
2385 return false;
2386 }
2387
2388 rt::ErrorCode management_command_error(std::uint32_t command_id) const
2389 {
2390 for(std::size_t i = 0; i < management_results_.size(); ++i) {
2391 if(management_results_[i].command_id == command_id) {
2392 return management_results_[i].error;
2393 }
2394 }
2395 return rt::ErrorCode::ok;
2396 }
2397
2398 rt::ErrorCode command_error(std::uint32_t command_id) const
2399 {
2400 if (command_id == 0)
2401 {
2402 return rt::ErrorCode::ok;
2403 }
2404 for (std::size_t offset = 0; offset < command_error_count_; ++offset)
2405 {
2406 const std::size_t index =
2407 (command_error_cursor_ + CommandErrorCapacity - 1 - offset) % CommandErrorCapacity;
2408 if (command_errors_[index].command_id == command_id)
2409 {
2410 return command_errors_[index].error;
2411 }
2412 }
2413 return rt::ErrorCode::ok;
2414 }
2415
2416 // A function-block-local runtime failure terminates only the owning
2417 // command. The first buffered successor takes over from the live state.
2418 rt::ErrorCode fail_command(std::uint32_t command_id, rt::ErrorCode code)
2419 {
2420 if (command_id == 0 || code == rt::ErrorCode::ok || !active_ ||
2421 snapshot_.active_command_id != command_id)
2422 {
2424 }
2425 record_command_error(command_id, code);
2426 active_ = false;
2427 continuous_holding_ = false;
2428 override_braking_ = false;
2429 override_paused_ = false;
2430 blend_armed_ = false;
2431 snapshot_.active_command_id = 0;
2432 snapshot_.active_command_reached_target = false;
2433 snapshot_.command_acceleration = 0.0;
2434 snapshot_.actual_acceleration = 0.0;
2435 snapshot_.status = active_command_.homing ? AxisStatus::homing
2436 : (snapshot_.powered ? AxisStatus::standstill
2438 start_next_queued();
2439 if (!active_)
2440 {
2441 base_velocity_ = 0.0;
2442 snapshot_.command_velocity = 0.0;
2443 snapshot_.actual_velocity = 0.0;
2444 }
2445 return rt::ErrorCode::ok;
2446 }
2447
2448 std::uint32_t torque_command_id() const { return torque_command_id_; }
2449
2450 bool command_in_velocity(std::uint32_t command_id) const
2451 {
2452 return command_id != 0 && active_ && snapshot_.active_command_id == command_id &&
2453 active_command_.kind == CommandKind::move_velocity &&
2454 std::fabs(snapshot_.command_velocity - signed_velocity(active_command_)) <= 1e-12;
2455 }
2456
2457 bool command_in_end_velocity(std::uint32_t command_id) const
2458 {
2459 return command_id != 0 && active_ && snapshot_.active_command_id == command_id &&
2460 is_continuous_kind(active_command_.kind) &&
2461 snapshot_.active_command_reached_target &&
2462 std::fabs(snapshot_.command_velocity - continuous_hold_velocity_) <= 1e-12;
2463 }
2464
2465 double command_torque() const { return snapshot_.command_torque; }
2466
2467 rt::ErrorCode release_stop(std::uint32_t command_id)
2468 {
2469 if (command_id == 0 || stop_lock_id_ != command_id)
2470 {
2472 }
2473 if (active_ && snapshot_.active_command_id == command_id)
2474 {
2475 stop_release_requested_ = true;
2476 return rt::ErrorCode::ok;
2477 }
2478 stop_lock_id_ = 0;
2479 stop_release_requested_ = false;
2480 if (snapshot_.status == AxisStatus::stopping)
2481 {
2482 snapshot_.status = snapshot_.powered ? AxisStatus::standstill : AxisStatus::disabled;
2483 }
2484 return rt::ErrorCode::ok;
2485 }
2486
2487 // ContinuousUpdate for the active velocity command (MC_MoveVelocity,
2488 // KB-009): the new signed direction and magnitude apply from the next
2489 // cycle; the override keeps scaling per cycle.
2490 rt::ErrorCode update_active_velocity(std::uint32_t command_id, double direction_value,
2491 double velocity)
2492 {
2493 if (!active_ || snapshot_.active_command_id != command_id || command_id == 0 ||
2494 active_command_.kind != CommandKind::move_velocity || !std::isfinite(direction_value) ||
2495 !std::isfinite(velocity) || velocity <= 0.0 ||
2496 !direction_enabled(direction_value))
2497 {
2499 }
2500 active_command_.value = direction_value;
2501 active_command_.velocity = velocity;
2502 return rt::ErrorCode::ok;
2503 }
2504
2505 rt::ErrorCode update_active_torque(std::uint32_t command_id,
2506 const AxisCommand &command)
2507 {
2508 if(!active_ || snapshot_.active_command_id != command_id ||
2509 command_id == 0 || active_command_.kind != CommandKind::torque ||
2511 command.velocity < 0.0 || command.acceleration < 0.0 ||
2512 command.deceleration < 0.0 || command.jerk < 0.0 ||
2513 !is_valid_direction(command.direction) ||
2514 command.direction == Direction::shortest_way ||
2515 command.buffer_mode != BufferMode::aborting) {
2517 }
2518 active_command_.value = command.value;
2519 active_command_.torque_ramp = command.torque_ramp;
2520 active_command_.velocity = command.velocity;
2521 active_command_.acceleration = command.acceleration;
2522 active_command_.deceleration = command.deceleration;
2523 active_command_.jerk = command.jerk;
2524 active_command_.direction = command.direction;
2525 snapshot_.torque_velocity_limit = command.velocity;
2526 snapshot_.torque_acceleration_limit = command.acceleration;
2527 snapshot_.torque_deceleration_limit = command.deceleration;
2528 snapshot_.torque_jerk_limit = command.jerk;
2529 snapshot_.torque_direction = command.direction;
2530 return rt::ErrorCode::ok;
2531 }
2532
2533 // Retargets an active position command
2534 // (ContinuousUpdate).
2535 rt::ErrorCode update_active_target(std::uint32_t command_id, double target)
2536 {
2537 if (group_blocks_standalone_motion() || !active_ ||
2538 snapshot_.active_command_id != command_id || command_id == 0 ||
2539 !std::isfinite(target) ||
2540 (!is_continuous_kind(active_command_.kind) &&
2541 active_command_.kind != CommandKind::move_absolute &&
2542 active_command_.kind != CommandKind::move_relative &&
2543 active_command_.kind != CommandKind::move_additive))
2544 {
2546 }
2547 if (!target_inside_limits(target))
2548 {
2550 }
2551 if (!direction_enabled(target - snapshot_.command_position))
2552 {
2554 }
2555
2556 double end_velocity = 0.0;
2557 if (is_continuous_kind(active_command_.kind))
2558 {
2559 end_velocity = active_command_.end_velocity * override_;
2560 }
2561 const otg::Limits1D limits = scaled_limits(active_command_);
2562 const rt::Result<otg::Profile1D> profile =
2563 otg::plan_time_optimal({snapshot_.command_position, snapshot_.command_velocity,
2564 snapshot_.command_acceleration},
2565 {target, end_velocity, 0.0}, limits);
2566 if (!profile)
2567 {
2568 return profile.error();
2569 }
2570
2571 active_profile_ = profile.value();
2572 active_tick_ = 0;
2573 active_last_sample_ = snapshot_.command_position;
2574 active_target_ = target;
2575 active_command_.value = target;
2576 continuous_holding_ = false;
2577 continuous_hold_velocity_ = end_velocity;
2578 snapshot_.active_command_reached_target = false;
2579 return rt::ErrorCode::ok;
2580 }
2581
2582 private:
2583 void set_synchronized_position(double position) { set_synchronized_state(position, 0.0, 0.0); }
2584
2585 void set_synchronized_state(double position, double velocity, double acceleration)
2586 {
2588 snapshot_.command_position = position;
2589 snapshot_.actual_position = position;
2590 snapshot_.command_velocity = velocity;
2591 snapshot_.actual_velocity = velocity;
2592 snapshot_.command_acceleration = acceleration;
2593 snapshot_.actual_acceleration = acceleration;
2594 }
2595
2596 void clear_synchronized()
2597 {
2598 reset_sync();
2599 if (snapshot_.status == AxisStatus::synchronized_motion)
2600 {
2602 }
2603 }
2604
2605 rt::Result<std::uint32_t> begin_sync(SyncKind kind, BufferMode buffer_mode)
2606 {
2607 // Synchronizing a streaming axis is undefined (approved stream
2608 // matrix, decision #10): explicit error, not a takeover.
2609 if (group_blocks_standalone_motion() || !snapshot_.powered ||
2610 snapshot_.status == AxisStatus::errorstop || stream_active_)
2611 {
2613 }
2614 if (buffer_mode == BufferMode::aborting)
2615 {
2616 abort_motion();
2617 }
2618 else if (sync_kind_ != SyncKind::none)
2619 {
2620 // Re-synchronizing a synchronized axis is only defined as a takeover.
2622 }
2623 // Synchronization owns the axis position; a running superimposed offset
2624 // cannot compose with it.
2625 reset_superimposed();
2626 sync_kind_ = kind;
2627 sync_id_ = next_command_id_++;
2628 sync_phase_ = active_ ? SyncPhase::queued : SyncPhase::idle;
2629 return rt::Result<std::uint32_t>::success(sync_id_);
2630 }
2631
2632 void reset_sync()
2633 {
2634 abort_phasing_commands();
2635 sync_kind_ = SyncKind::none;
2636 sync_phase_ = SyncPhase::idle;
2637 sync_entry_phase_ = SyncPhase::idle;
2638 sync_id_ = 0;
2639 phase_offset_ = 0.0;
2640 sync_profile_ready_ = false;
2641 }
2642
2643 double master_value(const AxisModel &master, MasterValueSource source) const
2644 {
2645 const AxisSnapshot &snapshot = master.snapshot();
2646 return source == MasterValueSource::actual ? snapshot.actual_position
2647 : snapshot.command_position;
2648 }
2649
2650 double master_velocity(const AxisModel &master, MasterValueSource source) const
2651 {
2652 const AxisSnapshot &snapshot = master.snapshot();
2653 return source == MasterValueSource::actual ? snapshot.actual_velocity
2654 : snapshot.command_velocity;
2655 }
2656
2657 double sync_master_sync_position() const
2658 {
2659 return sync_kind_ == SyncKind::gear ? sync_gear_.master_sync_position
2660 : sync_cam_.master_sync_position;
2661 }
2662
2663 double sync_master_start_distance() const
2664 {
2665 return sync_kind_ == SyncKind::gear ? sync_gear_.master_start_distance
2666 : sync_cam_.master_start_distance;
2667 }
2668
2669 double sync_approach_velocity() const
2670 {
2671 return sync_kind_ == SyncKind::gear ? sync_gear_.approach_velocity
2672 : sync_cam_.approach_velocity;
2673 }
2674
2675 // Slave setpoint at the moment the master crosses the sync position.
2676 rt::Result<double> sync_target_at_sync() const
2677 {
2678 if (sync_kind_ == SyncKind::gear)
2679 {
2680 return rt::Result<double>::success(sync_gear_.slave_sync_position);
2681 }
2682 return cam_slave_value(sync_master_sync_position());
2683 }
2684
2685 rt::Result<double> cam_slave_value(double master_position) const
2686 {
2687 const double table_input =
2688 (master_position - sync_cam_.master_offset) / sync_cam_.master_scaling;
2689 const rt::Result<double> sampled = sync_cam_.interpolation == exec::CamInterpolation::spline
2690 ? cam_spline_.sample(table_input)
2691 : sync_cam_.table.sample(table_input);
2692 if (!sampled)
2693 {
2694 return sampled;
2695 }
2696 return rt::Result<double>::success(sync_cam_.slave_offset +
2697 sync_cam_.slave_scaling * sampled.value());
2698 }
2699
2700 void remember_phasing(std::uint32_t command_id, PhasingCommandState state,
2701 double covered_shift)
2702 {
2703 for (std::size_t i = 0; i < phasing_result_count_; ++i)
2704 {
2705 const std::size_t index =
2706 (phasing_result_cursor_ + PhasingResultCapacity - 1 - i) % PhasingResultCapacity;
2707 if (phasing_results_[index].command_id == command_id)
2708 {
2709 phasing_results_[index].state = state;
2710 phasing_results_[index].covered_shift = covered_shift;
2711 return;
2712 }
2713 }
2714 phasing_results_[phasing_result_cursor_] = {command_id, state, covered_shift};
2715 phasing_result_cursor_ = (phasing_result_cursor_ + 1) % PhasingResultCapacity;
2716 if (phasing_result_count_ < PhasingResultCapacity)
2717 {
2718 ++phasing_result_count_;
2719 }
2720 }
2721
2722 void abort_phasing_commands(bool preserve_kinematics = false)
2723 {
2724 if (phasing_active_)
2725 {
2726 remember_phasing(phasing_id_, PhasingCommandState::aborted,
2727 phase_offset_ - phasing_start_offset_);
2728 }
2729 for (std::size_t i = 0; i < phasing_queue_.size(); ++i)
2730 {
2731 remember_phasing(phasing_queue_[i].command_id, PhasingCommandState::aborted, 0.0);
2732 }
2733 phasing_queue_.clear();
2734 phasing_active_ = false;
2735 phasing_id_ = 0;
2736 if (!preserve_kinematics)
2737 {
2738 phasing_velocity_ = 0.0;
2739 phasing_acceleration_ = 0.0;
2740 }
2741 phasing_tick_ = 0;
2742 }
2743
2744 rt::ErrorCode start_phasing(const PhasingCommand &command)
2745 {
2746 const double target = command.relative ? phase_offset_ + command.phase_shift
2747 : command.phase_shift;
2748 phasing_start_offset_ = phase_offset_;
2749 phasing_target_ = target;
2750 phasing_id_ = command.command_id;
2751 if (target == phase_offset_ || command.velocity == 0.0)
2752 {
2753 phase_offset_ = target;
2754 phasing_velocity_ = 0.0;
2755 phasing_acceleration_ = 0.0;
2756 phasing_active_ = false;
2757 remember_phasing(command.command_id, PhasingCommandState::completed,
2758 phase_offset_ - phasing_start_offset_);
2759 phasing_id_ = 0;
2760 return rt::ErrorCode::ok;
2761 }
2762
2763 const otg::Limits1D limits{command.velocity, command.acceleration,
2764 command.deceleration, command.jerk};
2765 const rt::Result<otg::Profile1D> planned = otg::plan_time_optimal(
2766 {phase_offset_, phasing_velocity_, phasing_acceleration_}, {target, 0.0, 0.0}, limits);
2767 if (!planned)
2768 {
2769 phasing_id_ = 0;
2770 return planned.error();
2771 }
2772 phasing_profile_ = planned.value();
2773 phasing_tick_ = 0;
2774 phasing_active_ = true;
2775 remember_phasing(command.command_id, PhasingCommandState::active, 0.0);
2776 return rt::ErrorCode::ok;
2777 }
2778
2779 void start_next_phasing()
2780 {
2781 while (!phasing_active_ && !phasing_queue_.empty())
2782 {
2783 const PhasingCommand next = phasing_queue_[0];
2784 for (std::size_t i = 1; i < phasing_queue_.size(); ++i)
2785 {
2786 phasing_queue_[i - 1] = phasing_queue_[i];
2787 }
2788 phasing_queue_.pop_back();
2789 const rt::ErrorCode started = start_phasing(next);
2790 if (started != rt::ErrorCode::ok)
2791 {
2792 remember_phasing(next.command_id, PhasingCommandState::aborted, 0.0);
2793 }
2794 }
2795 }
2796
2797 void advance_phasing()
2798 {
2799 if (!phasing_active_)
2800 {
2801 start_next_phasing();
2802 return;
2803 }
2804 ++phasing_tick_;
2805 const otg::State1D state =
2806 otg::sample(phasing_profile_, rt::CycleTick::from_cycles(phasing_tick_));
2807 phase_offset_ = state.position;
2808 phasing_velocity_ = state.velocity;
2809 phasing_acceleration_ = state.acceleration;
2810 remember_phasing(phasing_id_, PhasingCommandState::active,
2811 phase_offset_ - phasing_start_offset_);
2812 if (phasing_tick_ >= phasing_profile_.duration_cycles())
2813 {
2814 phase_offset_ = phasing_target_;
2815 phasing_velocity_ = 0.0;
2816 phasing_acceleration_ = 0.0;
2817 remember_phasing(phasing_id_, PhasingCommandState::completed,
2818 phase_offset_ - phasing_start_offset_);
2819 phasing_active_ = false;
2820 phasing_id_ = 0;
2821 start_next_phasing();
2822 }
2823 }
2824
2825 rt::Result<double> sync_engaged_position()
2826 {
2827 if (sync_kind_ == SyncKind::gear)
2828 {
2829 advance_phasing();
2830 const double master = master_value(*sync_gear_.master, sync_gear_.source);
2831 const double ratio = sync_gear_.ratio_numerator / sync_gear_.ratio_denominator;
2832 return rt::Result<double>::success(master * ratio + phase_offset_);
2833 }
2834 if (sync_kind_ == SyncKind::cam)
2835 {
2836 return cam_slave_value(master_value(*sync_cam_.master, sync_cam_.source));
2837 }
2838 const double master1 =
2839 master_value(*sync_combine_.master1, sync_combine_.source_m1) *
2840 (sync_combine_.ratio_numerator_m1 / sync_combine_.ratio_denominator_m1);
2841 const double master2 =
2842 master_value(*sync_combine_.master2, sync_combine_.source_m2) *
2843 (sync_combine_.ratio_numerator_m2 / sync_combine_.ratio_denominator_m2);
2845 sync_combine_.mode == CombineMode::sub_axes ? master1 - master2 : master1 + master2);
2846 }
2847
2848 double sync_engaged_velocity(double next_position) const
2849 {
2850 if (sync_kind_ == SyncKind::gear)
2851 {
2852 return master_velocity(*sync_gear_.master, sync_gear_.source) *
2853 (sync_gear_.ratio_numerator / sync_gear_.ratio_denominator);
2854 }
2855 if (sync_kind_ == SyncKind::combine)
2856 {
2857 const double master1 =
2858 master_velocity(*sync_combine_.master1, sync_combine_.source_m1) *
2859 (sync_combine_.ratio_numerator_m1 / sync_combine_.ratio_denominator_m1);
2860 const double master2 =
2861 master_velocity(*sync_combine_.master2, sync_combine_.source_m2) *
2862 (sync_combine_.ratio_numerator_m2 / sync_combine_.ratio_denominator_m2);
2863 return sync_combine_.mode == CombineMode::sub_axes ? master1 - master2
2864 : master1 + master2;
2865 }
2866 return next_position - snapshot_.command_position;
2867 }
2868
2869 otg::Limits1D gear_limits() const
2870 {
2871 const double ratio = sync_gear_.ratio_numerator / sync_gear_.ratio_denominator;
2872 const double target_velocity =
2873 master_velocity(*sync_gear_.master, sync_gear_.source) * ratio;
2874 const double velocity = sync_gear_.position_sync
2875 ? (sync_gear_.approach_velocity > 0.0
2876 ? sync_gear_.approach_velocity
2877 : limits_.max_velocity)
2878 : std::fmax(std::fabs(snapshot_.command_velocity),
2879 std::fabs(target_velocity)) + 1e-9;
2880 return {velocity,
2881 sync_gear_.acceleration > 0.0 ? sync_gear_.acceleration
2882 : limits_.max_acceleration,
2883 sync_gear_.deceleration > 0.0 ? sync_gear_.deceleration
2884 : limits_.max_deceleration,
2885 sync_gear_.jerk > 0.0 ? sync_gear_.jerk : limits_.max_jerk};
2886 }
2887
2888 rt::ErrorCode plan_gear_velocity_transition()
2889 {
2890 if (sync_gear_.acceleration == 0.0 && sync_gear_.deceleration == 0.0 &&
2891 sync_gear_.jerk == 0.0)
2892 {
2893 // The three optional dynamics inputs form one contract: all zero
2894 // selects unprofiled velocity locking, any nonzero value enables
2895 // the fully constrained engagement planner.
2896 sync_profile_ready_ = false;
2897 return rt::ErrorCode::ok;
2898 }
2899 const double ratio = sync_gear_.ratio_numerator / sync_gear_.ratio_denominator;
2900 const double target_velocity =
2901 master_velocity(*sync_gear_.master, sync_gear_.source) * ratio;
2902 if (std::fabs(snapshot_.command_velocity - target_velocity) <= 1e-12 &&
2903 std::fabs(snapshot_.command_acceleration) <= 1e-12)
2904 {
2905 sync_profile_ready_ = false;
2906 return rt::ErrorCode::ok;
2907 }
2908 const otg::Limits1D limits = gear_limits();
2909 const double ramp_distance =
2910 otg::detail::ramp_between(snapshot_.command_velocity, target_velocity, limits).distance;
2911 const rt::Result<otg::Profile1D> planned = otg::plan_time_optimal(
2912 {snapshot_.command_position, snapshot_.command_velocity,
2913 snapshot_.command_acceleration},
2914 {snapshot_.command_position + ramp_distance, target_velocity, 0.0}, limits);
2915 if (!planned)
2916 {
2917 return planned.error();
2918 }
2919 sync_profile_ = planned.value();
2920 sync_profile_tick_ = 0;
2921 sync_profile_target_velocity_ = target_velocity;
2922 sync_profile_ready_ = true;
2923 return rt::ErrorCode::ok;
2924 }
2925
2926 int sync_window_direction() const
2927 {
2928 const double distance = sync_master_start_distance();
2929 if (distance > 0.0)
2930 return 1;
2931 if (distance < 0.0)
2932 return -1;
2933 const double velocity = sync_kind_ == SyncKind::gear
2934 ? master_velocity(*sync_gear_.master, sync_gear_.source)
2935 : master_velocity(*sync_cam_.master, sync_cam_.source);
2936 if (velocity > 0.0)
2937 return 1;
2938 if (velocity < 0.0)
2939 return -1;
2940 return sync_window_master_value() <= sync_master_sync_position() ? 1 : -1;
2941 }
2942
2943 bool sync_reached(double master, double boundary) const
2944 {
2945 return sync_window_direction() > 0 ? master >= boundary : master <= boundary;
2946 }
2947
2948 rt::ErrorCode plan_position_sync_profile()
2949 {
2950 const double master = sync_window_master_value();
2951 const double master_speed = sync_kind_ == SyncKind::gear
2952 ? master_velocity(*sync_gear_.master, sync_gear_.source)
2953 : master_velocity(*sync_cam_.master, sync_cam_.source);
2954 const double remaining = sync_master_sync_position() - master;
2955 if (remaining * master_speed <= 0.0)
2956 {
2958 }
2959 // The master setpoint is sampled before the slave in the same task.
2960 // One guard cycle keeps the fixed-time slave endpoint aligned with
2961 // the first cycle that observes the master crossing the sync point.
2962 const std::int64_t cycles = static_cast<std::int64_t>(
2963 std::ceil(std::fabs(remaining / master_speed))) + 1;
2964 const rt::Result<double> target = sync_target_at_sync();
2965 if (!target)
2966 {
2967 return target.error();
2968 }
2969 const double ratio = sync_gear_.ratio_numerator / sync_gear_.ratio_denominator;
2970 const double target_velocity = master_speed * ratio;
2971 const rt::Result<otg::Profile1D> planned = otg::solve_fixed_time(
2972 {snapshot_.command_position, snapshot_.command_velocity,
2973 snapshot_.command_acceleration},
2974 {target.value(), target_velocity, 0.0}, gear_limits(), cycles > 0 ? cycles : 1);
2975 if (!planned)
2976 {
2977 return planned.error();
2978 }
2979 sync_profile_ = planned.value();
2980 sync_profile_tick_ = 0;
2981 sync_profile_ready_ = true;
2982 approach_window_begin_ = sync_master_sync_position() - sync_master_start_distance();
2983 approach_start_slave_ = snapshot_.command_position;
2984 return rt::ErrorCode::ok;
2985 }
2986
2987 void fail_sync_command(rt::ErrorCode code)
2988 {
2989 const std::uint32_t failed_id = sync_id_;
2990 reset_sync();
2991 record_command_error(failed_id, code);
2992 snapshot_.active_command_id = 0;
2993 snapshot_.command_velocity = 0.0;
2994 snapshot_.actual_velocity = 0.0;
2995 snapshot_.command_acceleration = 0.0;
2996 snapshot_.actual_acceleration = 0.0;
2997 snapshot_.status = snapshot_.powered ? AxisStatus::standstill : AxisStatus::disabled;
2998 }
2999
3000 void enter_engaged()
3001 {
3002 if (sync_kind_ == SyncKind::gear)
3003 {
3004 const double ratio = sync_gear_.ratio_numerator / sync_gear_.ratio_denominator;
3005 phase_offset_ = sync_gear_.position_sync
3006 ? sync_gear_.slave_sync_position -
3007 sync_gear_.master_sync_position * ratio
3008 : snapshot_.command_position -
3009 master_value(*sync_gear_.master, sync_gear_.source) * ratio;
3010 }
3011 sync_profile_ready_ = false;
3012 sync_phase_ = SyncPhase::engaged;
3013 snapshot_.active_command_id = sync_id_;
3014 snapshot_.status = AxisStatus::synchronized_motion;
3015 }
3016
3017 // Returns true when a stream session owns this cycle: the filter output
3018 // is the axis setpoint (the filter starts from the takeover state, so
3019 // the position domain is continuous with the pre-engage motion).
3020 bool stream_cycle()
3021 {
3022 if (!stream_active_)
3023 {
3024 return false;
3025 }
3026 const otg::State1D state = stream_filter_.cycle();
3027 snapshot_.command_position = state.position;
3028 snapshot_.actual_position = state.position;
3029 snapshot_.command_velocity = state.velocity;
3030 snapshot_.actual_velocity = state.velocity;
3031 snapshot_.command_acceleration = state.acceleration;
3032 snapshot_.actual_acceleration = state.acceleration;
3033 return true;
3034 }
3035
3036 // Returns true when synchronization owns this cycle (idle motion excluded).
3037 bool sync_cycle()
3038 {
3039 if (sync_kind_ == SyncKind::none)
3040 {
3041 return false;
3042 }
3043 if (sync_kind_ == SyncKind::group_path)
3044 {
3045 return true;
3046 }
3047 if (sync_phase_ == SyncPhase::queued)
3048 {
3049 if (active_)
3050 {
3051 return false;
3052 }
3053 sync_phase_ = sync_entry_phase_;
3054 }
3055
3056 if (sync_phase_ == SyncPhase::waiting_window)
3057 {
3058 const double master = sync_window_master_value();
3059 const double window_begin = sync_master_sync_position() - sync_master_start_distance();
3060 if (!sync_reached(master, window_begin))
3061 {
3062 return true;
3063 }
3064 if (!sync_reached(master, sync_master_sync_position()))
3065 {
3066 if (sync_kind_ == SyncKind::gear)
3067 {
3068 const rt::ErrorCode planned = plan_position_sync_profile();
3069 if (planned != rt::ErrorCode::ok)
3070 {
3071 fail_sync_command(planned);
3072 return true;
3073 }
3074 }
3075 else
3076 {
3077 approach_window_begin_ = window_begin;
3078 approach_start_slave_ = snapshot_.command_position;
3079 }
3080 sync_phase_ = SyncPhase::approaching;
3081 }
3082 else
3083 {
3084 enter_engaged();
3085 }
3086 }
3087
3088 if (sync_phase_ == SyncPhase::approaching)
3089 {
3090 if (sync_kind_ == SyncKind::gear && !sync_gear_.position_sync)
3091 {
3092 if (!sync_profile_ready_)
3093 {
3094 const rt::ErrorCode planned = plan_gear_velocity_transition();
3095 if (planned != rt::ErrorCode::ok)
3096 {
3097 fail_sync_command(planned);
3098 return true;
3099 }
3100 if (!sync_profile_ready_)
3101 {
3102 enter_engaged();
3103 return true;
3104 }
3105 }
3106 ++sync_profile_tick_;
3107 const otg::State1D state =
3108 otg::sample(sync_profile_, rt::CycleTick::from_cycles(sync_profile_tick_));
3109 snapshot_.active_command_id = sync_id_;
3110 set_synchronized_state(state.position, state.velocity, state.acceleration);
3111 if (sync_profile_tick_ >= sync_profile_.duration_cycles())
3112 {
3113 const double live_target =
3114 master_velocity(*sync_gear_.master, sync_gear_.source) *
3115 (sync_gear_.ratio_numerator / sync_gear_.ratio_denominator);
3116 sync_profile_ready_ = false;
3117 if (std::fabs(live_target - sync_profile_target_velocity_) <= 1e-12)
3118 {
3119 enter_engaged();
3120 }
3121 }
3122 return true;
3123 }
3124
3125 const double master = sync_window_master_value();
3126 if (sync_kind_ == SyncKind::gear && sync_gear_.position_sync)
3127 {
3128 if (sync_profile_tick_ < sync_profile_.duration_cycles())
3129 {
3130 ++sync_profile_tick_;
3131 const otg::State1D state = otg::sample(
3132 sync_profile_, rt::CycleTick::from_cycles(sync_profile_tick_));
3133 snapshot_.active_command_id = sync_id_;
3134 set_synchronized_state(state.position, state.velocity, state.acceleration);
3135 if (sync_profile_tick_ >= sync_profile_.duration_cycles())
3136 {
3137 if (!sync_reached(master, sync_master_sync_position()))
3138 {
3139 fail_sync_command(rt::ErrorCode::infeasible);
3140 }
3141 else
3142 {
3143 enter_engaged();
3144 }
3145 }
3146 return true;
3147 }
3148 if (!sync_reached(master, sync_master_sync_position()))
3149 {
3150 fail_sync_command(rt::ErrorCode::infeasible);
3151 return true;
3152 }
3153 enter_engaged();
3154 }
3155 else if (sync_reached(master, sync_master_sync_position()))
3156 {
3157 enter_engaged();
3158 }
3159 else
3160 {
3161 const rt::Result<double> target_at_sync = sync_target_at_sync();
3162 if (!target_at_sync)
3163 {
3164 trigger_error();
3165 return true;
3166 }
3167 const double span = sync_master_sync_position() - approach_window_begin_;
3168 double progress = span > 0.0 ? (master - approach_window_begin_) / span : 1.0;
3169 progress = progress < 0.0 ? 0.0 : (progress > 1.0 ? 1.0 : progress);
3170 double target = approach_start_slave_ +
3171 (target_at_sync.value() - approach_start_slave_) * progress;
3172 target = cap_approach_step(target);
3173 snapshot_.active_command_id = sync_id_;
3174 set_synchronized_position(target);
3175 return true;
3176 }
3177 }
3178
3179 if (sync_phase_ == SyncPhase::engaged)
3180 {
3181 const rt::Result<double> position = sync_engaged_position();
3182 if (!position)
3183 {
3184 trigger_error();
3185 return true;
3186 }
3187 snapshot_.active_command_id = sync_id_;
3188 const double velocity = sync_engaged_velocity(position.value());
3189 set_synchronized_state(position.value(), velocity,
3190 velocity - snapshot_.command_velocity);
3191 return true;
3192 }
3193 return true;
3194 }
3195
3196 double sync_window_master_value() const
3197 {
3198 return sync_kind_ == SyncKind::gear ? master_value(*sync_gear_.master, sync_gear_.source)
3199 : master_value(*sync_cam_.master, sync_cam_.source);
3200 }
3201
3202 double cap_approach_step(double target) const
3203 {
3204 const double cap = sync_approach_velocity();
3205 if (cap <= 0.0)
3206 {
3207 return target;
3208 }
3209 const double step = target - snapshot_.command_position;
3210 if (step > cap)
3211 {
3212 return snapshot_.command_position + cap;
3213 }
3214 if (step < -cap)
3215 {
3216 return snapshot_.command_position - cap;
3217 }
3218 return target;
3219 }
3220
3221 bool is_moving() const
3222 {
3223 return snapshot_.status == AxisStatus::discrete_motion ||
3224 snapshot_.status == AxisStatus::continuous_motion ||
3225 snapshot_.status == AxisStatus::synchronized_motion ||
3226 snapshot_.status == AxisStatus::stopping;
3227 }
3228
3229 double queued_endpoint() const
3230 {
3231 double endpoint = active_ ? active_target_ : snapshot_.command_position;
3232 for (std::size_t i = 0; i < queue_.size(); ++i)
3233 {
3234 const AxisCommand &queued = queue_[i];
3235 if (queued.kind == CommandKind::move_absolute ||
3237 queued.kind == CommandKind::home)
3238 {
3239 endpoint = queued.value;
3240 }
3241 }
3242 return endpoint;
3243 }
3244
3245 AxisCommand normalize(AxisCommand command, double takeover_endpoint) const
3246 {
3247 if (command.kind == CommandKind::move_relative ||
3249 {
3250 command.value = takeover_endpoint + command.value;
3252 }
3254 {
3255 command.value = takeover_endpoint + command.value;
3257 }
3258 return command;
3259 }
3260
3261 double signed_velocity(AxisCommand command) const
3262 {
3263 const double scaled = command.velocity * override_;
3264 return command.value < 0.0 ? -scaled : scaled;
3265 }
3266
3267 otg::Limits1D scaled_limits(double velocity, double acceleration,
3268 double deceleration, double jerk) const
3269 {
3270 return {velocity * override_, acceleration * acceleration_override_,
3271 deceleration * acceleration_override_, jerk * jerk_override_};
3272 }
3273
3274 otg::Limits1D scaled_limits(const AxisCommand &command) const
3275 {
3276 return scaled_limits(command.velocity, command.acceleration,
3277 command.deceleration, command.jerk);
3278 }
3279
3280 bool direction_enabled(double delta) const
3281 {
3282 return delta == 0.0 || (delta > 0.0 ? positive_enabled_ : negative_enabled_);
3283 }
3284
3285 bool command_direction_enabled(const AxisCommand &command,
3286 double reference_position) const
3287 {
3288 switch (command.kind)
3289 {
3295 return direction_enabled(command.value);
3298 case CommandKind::home:
3299 return direction_enabled(command.value - reference_position);
3300 default:
3301 return true;
3302 }
3303 }
3304
3305 bool active_direction_enabled() const
3306 {
3307 if (!active_ && sync_kind_ == SyncKind::none && !stream_active_)
3308 {
3309 return true;
3310 }
3311 if (snapshot_.command_velocity != 0.0)
3312 {
3313 return direction_enabled(snapshot_.command_velocity);
3314 }
3315 return !active_ || command_direction_enabled(active_command_,
3316 snapshot_.command_position);
3317 }
3318
3319 bool target_inside_limits(double target, bool enforce_soft_limits = false) const
3320 {
3321 if (homing_limits_suspended_ && !enforce_soft_limits)
3322 {
3323 return true;
3324 }
3325 if (limits_.min_position_enabled && target < limits_.min_position)
3326 {
3327 return false;
3328 }
3329 if (limits_.max_position_enabled && target > limits_.max_position)
3330 {
3331 return false;
3332 }
3333 return true;
3334 }
3335
3336 rt::ErrorCode start(AxisCommand command)
3337 {
3338 if (command.kind == CommandKind::move_relative ||
3341 {
3342 command = normalize(command, snapshot_.command_position);
3343 }
3344 active_command_ = command;
3345 active_tick_ = 0;
3346 continuous_holding_ = false;
3347 blend_armed_ = false;
3348 snapshot_.active_command_reached_target = false;
3349 snapshot_.active_command_id = command.command_id;
3350 snapshot_.command_torque_limit = command.torque_limit;
3351
3353 {
3354 active_ = true;
3355 snapshot_.status = command.homing ? AxisStatus::homing : AxisStatus::continuous_motion;
3356 return rt::ErrorCode::ok;
3357 }
3358
3359 if (command.kind == CommandKind::halt || command.kind == CommandKind::stop)
3360 {
3361 if (command.kind == CommandKind::stop && command.lock_stopping)
3362 {
3363 stop_lock_id_ = command.command_id;
3364 stop_release_requested_ = false;
3365 }
3366 // Controlled stop (MC_Halt/MC_Stop carry over the v0.x contract):
3367 // decelerate from the current state with the commanded
3368 // deceleration/jerk. The braking target is exempt from the
3369 // software position limits — the axis must be allowed to come to
3370 // rest. A resting axis still finishes within one cycle.
3371 halt_profiled_ = false;
3372 const double v0 = snapshot_.command_velocity;
3373 const double a0 = snapshot_.command_acceleration;
3374 if (v0 != 0.0 || a0 != 0.0)
3375 {
3376 double brake_velocity = v0;
3377 double brake_shift = 0.0;
3378 if (a0 != 0.0)
3379 {
3380 const double zero_cycles = std::ceil(std::fabs(a0) / command.jerk);
3381 brake_velocity += 0.5 * a0 * zero_cycles;
3382 brake_shift += v0 * zero_cycles + a0 * zero_cycles * zero_cycles / 3.0;
3383 }
3384 const otg::Limits1D halt_limits{
3385 std::fabs(v0) + std::fabs(brake_velocity) + 1e-9,
3386 std::fabs(a0) > command.acceleration ? std::fabs(a0) : command.acceleration,
3387 command.deceleration, command.jerk};
3388 const double stop_position =
3389 snapshot_.command_position + brake_shift +
3390 otg::detail::ramp_between(brake_velocity, 0.0, halt_limits).distance;
3391 const rt::Result<otg::Profile1D> halt = otg::plan_time_optimal(
3392 {snapshot_.command_position, v0, a0}, {stop_position, 0.0, 0.0}, halt_limits);
3393 if (halt)
3394 {
3395 active_profile_ = halt.value();
3396 active_last_sample_ = snapshot_.command_position;
3397 active_target_ = stop_position;
3398 halt_profiled_ = true;
3399 }
3400 }
3401 active_ = true;
3402 snapshot_.status = command.homing ? AxisStatus::homing : AxisStatus::stopping;
3403 return rt::ErrorCode::ok;
3404 }
3405
3406 const double target = command.value;
3407 if (!target_inside_limits(target))
3408 {
3410 }
3411
3412 double target_velocity = 0.0;
3413 if (is_continuous_kind(command.kind))
3414 {
3415 target_velocity = command.end_velocity * override_;
3416 }
3417
3418 active_target_ = target;
3419 const otg::Limits1D limits = scaled_limits(command);
3420 const rt::Result<otg::Profile1D> profile =
3421 otg::plan_time_optimal({snapshot_.command_position, snapshot_.command_velocity,
3422 snapshot_.command_acceleration},
3423 {target, target_velocity, 0.0}, limits);
3424 if (!profile)
3425 {
3426 return profile.error();
3427 }
3428
3429 active_profile_ = profile.value();
3430 active_last_sample_ = snapshot_.command_position;
3431 continuous_hold_velocity_ = target_velocity;
3432 active_ = true;
3433 snapshot_.status = command.homing
3437 return rt::ErrorCode::ok;
3438 }
3439
3440 void cycle_base_motion()
3441 {
3442 if (!active_)
3443 {
3444 return;
3445 }
3446
3447 if (override_paused_)
3448 {
3449 return;
3450 }
3451
3452 if (active_command_.kind == CommandKind::acceleration_profile)
3453 {
3454 if (!acceleration_profile_completed_)
3455 {
3456 const AxisCommand &segment = queue_[acceleration_segment_index_];
3457 const double acceleration = segment.value;
3458 snapshot_.command_acceleration = acceleration;
3459 snapshot_.actual_acceleration = acceleration;
3460 snapshot_.command_velocity += acceleration;
3461 snapshot_.actual_velocity = snapshot_.command_velocity;
3462 snapshot_.command_position += snapshot_.command_velocity;
3463 snapshot_.actual_position = snapshot_.command_position;
3464 base_velocity_ = snapshot_.command_velocity;
3465 ++acceleration_segment_tick_;
3466 if (acceleration_segment_tick_ >= segment.min_duration_cycles)
3467 {
3468 acceleration_segment_tick_ = 0;
3469 ++acceleration_segment_index_;
3470 if (acceleration_segment_index_ >= acceleration_segment_count_)
3471 {
3472 acceleration_profile_completed_ = true;
3473 snapshot_.active_command_reached_target = true;
3474 snapshot_.command_acceleration = 0.0;
3475 snapshot_.actual_acceleration = 0.0;
3476 }
3477 }
3478 }
3479 else
3480 {
3481 snapshot_.command_position += snapshot_.command_velocity;
3482 snapshot_.actual_position = snapshot_.command_position;
3483 }
3484 return;
3485 }
3486
3487 if (!override_braking_ &&
3488 (active_command_.kind == CommandKind::move_velocity ||
3489 active_command_.kind == CommandKind::torque || continuous_holding_))
3490 {
3491 if (active_command_.kind == CommandKind::torque)
3492 {
3493 const double delta =
3494 active_command_.value - snapshot_.command_torque;
3495 if(active_command_.torque_ramp == 0.0 ||
3496 std::fabs(delta) <= active_command_.torque_ramp) {
3497 snapshot_.command_torque = active_command_.value;
3498 } else {
3499 snapshot_.command_torque +=
3500 delta < 0.0 ? -active_command_.torque_ramp
3501 : active_command_.torque_ramp;
3502 }
3503 snapshot_.command_velocity = 0.0;
3504 snapshot_.command_acceleration = 0.0;
3505 return;
3506 }
3507 const double velocity =
3508 continuous_holding_ ? continuous_hold_velocity_ : signed_velocity(active_command_);
3509 base_velocity_ = velocity;
3510 snapshot_.command_velocity = velocity;
3511 snapshot_.actual_velocity = velocity;
3512 snapshot_.command_acceleration = 0.0;
3513 snapshot_.actual_acceleration = 0.0;
3514 snapshot_.command_position += velocity;
3515 snapshot_.actual_position = snapshot_.command_position;
3516 if (!continuous_holding_ && active_command_.min_duration_cycles > 0)
3517 {
3518 ++active_tick_;
3519 if (active_tick_ >= active_command_.min_duration_cycles)
3520 {
3521 finish_active();
3522 }
3523 }
3524 return;
3525 }
3526
3527 if ((active_command_.kind == CommandKind::halt ||
3528 active_command_.kind == CommandKind::stop) &&
3529 !halt_profiled_)
3530 {
3531 finish_active();
3532 return;
3533 }
3534
3535 ++active_tick_;
3536 const otg::State1D state =
3537 otg::sample(active_profile_, rt::CycleTick::from_cycles(active_tick_));
3538 // Incremental application lets the superimposed offset compose with the
3539 // base profile without a second position bookkeeping domain.
3540 const double step = state.position - active_last_sample_;
3541 active_last_sample_ = state.position;
3542 snapshot_.command_position += step;
3543 snapshot_.command_velocity = state.velocity;
3544 snapshot_.command_acceleration = state.acceleration;
3545 snapshot_.actual_position = snapshot_.command_position;
3546 snapshot_.actual_velocity = state.velocity;
3547 snapshot_.actual_acceleration = state.acceleration;
3548 base_velocity_ = state.velocity;
3549
3550 // KB-001 velocity-threshold blending: arm once the speed exceeds the
3551 // successor's threshold of the nominal command velocity, hand over
3552 // once it falls back below. Short moves that never arm degrade to
3553 // BUFFERED; homing, halt/stop, and continuous holds do not blend.
3554 const bool blend_eligible = active_command_.kind == CommandKind::move_absolute &&
3555 !queue_.empty() &&
3556 (queue_[0].buffer_mode == BufferMode::blending_low ||
3557 queue_[0].buffer_mode == BufferMode::blending_high);
3558 if (blend_eligible)
3559 {
3560 const double nominal = active_command_.velocity * override_;
3561 const double threshold =
3562 (queue_[0].buffer_mode == BufferMode::blending_low ? 0.3 : 0.7) * nominal;
3563 const double speed = std::fabs(state.velocity);
3564 if (speed > threshold)
3565 {
3566 blend_armed_ = true;
3567 }
3568 else if (blend_armed_)
3569 {
3570 blend_into_next();
3571 return;
3572 }
3573 }
3574 else
3575 {
3576 blend_armed_ = false;
3577 }
3578
3579 if (active_tick_ >= active_profile_.duration_cycles())
3580 {
3581 if (override_braking_)
3582 {
3583 override_braking_ = false;
3584 override_paused_ = true;
3585 snapshot_.command_velocity = 0.0;
3586 snapshot_.actual_velocity = 0.0;
3587 snapshot_.command_acceleration = 0.0;
3588 snapshot_.actual_acceleration = 0.0;
3589 return;
3590 }
3591 // Timed profile segments hold at the target until their minimum
3592 // duration elapses (otg::sample keeps returning the finish state).
3593 if (active_tick_ < active_command_.min_duration_cycles)
3594 {
3595 return;
3596 }
3597 if (active_command_.kind == CommandKind::home)
3598 {
3599 snapshot_.homed = true;
3600 }
3601 if (is_continuous_kind(active_command_.kind))
3602 {
3603 snapshot_.active_command_reached_target = true;
3604 continuous_holding_ = true;
3605 snapshot_.status = AxisStatus::continuous_motion;
3606 return;
3607 }
3608 finish_active();
3609 }
3610 }
3611
3612 void cycle_superimposed()
3613 {
3614 if (!superimposed_active_)
3615 {
3616 return;
3617 }
3618 if (!active_ && snapshot_.status == AxisStatus::standstill)
3619 {
3620 snapshot_.status = AxisStatus::discrete_motion;
3621 }
3622
3623 ++superimposed_tick_;
3624 const otg::State1D state =
3625 otg::sample(superimposed_profile_, rt::CycleTick::from_cycles(superimposed_tick_));
3626 const double step = state.position - superimposed_last_;
3627 superimposed_last_ = state.position;
3628 snapshot_.command_position += step;
3629 snapshot_.actual_position = snapshot_.command_position;
3630 snapshot_.command_velocity = base_velocity_ + state.velocity;
3631 snapshot_.actual_velocity = snapshot_.command_velocity;
3632
3633 if (superimposed_tick_ >= superimposed_profile_.duration_cycles())
3634 {
3635 superimposed_active_ = false;
3636 superimposed_completed_id_ =
3637 superimposed_halting_ ? 0 : superimposed_id_;
3638 superimposed_id_ = 0;
3639 superimposed_halting_ = false;
3640 snapshot_.command_velocity = base_velocity_;
3641 snapshot_.actual_velocity = base_velocity_;
3642 if (!active_ && snapshot_.status == AxisStatus::discrete_motion)
3643 {
3644 snapshot_.status = AxisStatus::standstill;
3645 }
3646 }
3647 }
3648
3649 void finish_active()
3650 {
3651 const bool locked_stop =
3652 active_command_.kind == CommandKind::stop && active_command_.lock_stopping &&
3653 stop_lock_id_ == snapshot_.active_command_id && !stop_release_requested_;
3654 active_ = false;
3655 continuous_holding_ = false;
3656 base_velocity_ = 0.0;
3657 snapshot_.last_completed_command_id = snapshot_.active_command_id;
3658 snapshot_.active_command_id = 0;
3659 snapshot_.active_command_reached_target = false;
3660 snapshot_.command_velocity = 0.0;
3661 snapshot_.actual_velocity = 0.0;
3662 snapshot_.command_acceleration = 0.0;
3663 snapshot_.actual_acceleration = 0.0;
3664 snapshot_.command_torque_limit = 0.0;
3665 snapshot_.status =
3666 locked_stop
3668 : (active_command_.homing
3670 : (snapshot_.powered ? AxisStatus::standstill : AxisStatus::disabled));
3671 if (active_command_.kind == CommandKind::stop && active_command_.lock_stopping &&
3672 !locked_stop)
3673 {
3674 stop_lock_id_ = 0;
3675 stop_release_requested_ = false;
3676 }
3677 if (locked_stop)
3678 {
3679 return;
3680 }
3681 start_next_queued();
3682 }
3683
3684 // KB-001 velocity-threshold blending: the queued successor takes over
3685 // while the active profile is still decelerating, planning from the live
3686 // state (the predecessor counts as completed at the handover point).
3687 void blend_into_next()
3688 {
3689 snapshot_.last_completed_command_id = snapshot_.active_command_id;
3690 blend_armed_ = false;
3691 start_next_queued();
3692 }
3693
3694 void start_next_queued()
3695 {
3696 if (queue_.empty())
3697 {
3698 return;
3699 }
3700
3701 const AxisCommand next = queue_[0];
3702 for (std::size_t i = 1; i < queue_.size(); ++i)
3703 {
3704 queue_[i - 1] = queue_[i];
3705 }
3706 queue_.pop_back();
3707 start(next);
3708 }
3709
3710 void abort_motion()
3711 {
3712 active_ = false;
3713 active_tick_ = 0;
3714 continuous_holding_ = false;
3715 acceleration_profile_completed_ = false;
3716 acceleration_segment_count_ = 0;
3717 acceleration_segment_index_ = 0;
3718 acceleration_segment_tick_ = 0;
3719 override_braking_ = false;
3720 override_paused_ = false;
3721 base_velocity_ = 0.0;
3722 queue_.clear();
3723 reset_sync();
3724 reset_superimposed();
3725 if (stream_active_)
3726 {
3727 stream_filter_.end_session();
3728 }
3729 stream_active_ = false;
3730 stream_id_ = 0;
3731 torque_command_id_ = 0;
3732 stop_lock_id_ = 0;
3733 stop_release_requested_ = false;
3734 snapshot_.command_torque = 0.0;
3735 snapshot_.command_torque_limit = 0.0;
3736 snapshot_.torque_velocity_limit = 0.0;
3737 snapshot_.torque_acceleration_limit = 0.0;
3738 snapshot_.torque_deceleration_limit = 0.0;
3739 snapshot_.torque_jerk_limit = 0.0;
3740 snapshot_.torque_direction = Direction::current;
3741 snapshot_.torque_mode = false;
3742 if (snapshot_.status == AxisStatus::synchronized_motion)
3743 {
3744 snapshot_.status = snapshot_.powered ? AxisStatus::standstill : AxisStatus::disabled;
3745 }
3746 snapshot_.active_command_id = 0;
3747 snapshot_.active_command_reached_target = false;
3748 snapshot_.command_velocity = 0.0;
3749 snapshot_.actual_velocity = 0.0;
3750 snapshot_.command_acceleration = 0.0;
3751 snapshot_.actual_acceleration = 0.0;
3752 }
3753
3754 void reset_superimposed()
3755 {
3756 superimposed_active_ = false;
3757 superimposed_id_ = 0;
3758 superimposed_completed_id_ = 0;
3759 superimposed_tick_ = 0;
3760 superimposed_last_ = 0.0;
3761 superimposed_halting_ = false;
3762 }
3763
3764 void cycle_probes()
3765 {
3766 for (std::size_t input = 0; input < DigitalInputCount; ++input)
3767 {
3768 ProbeSlot &slot = probes_[input];
3769 const bool level = digital_input_[input];
3770 const bool rising = level && !slot.last_level;
3771 slot.last_level = level;
3772 if (!slot.armed || !rising)
3773 {
3774 continue;
3775 }
3776 const double position = snapshot_.actual_position;
3777 if (slot.window_only &&
3778 (position < slot.first_position || position > slot.last_position))
3779 {
3780 continue;
3781 }
3782 slot.armed = false;
3783 slot.captured = true;
3784 slot.recorded_position = position;
3785 }
3786 }
3787
3788 struct ProbeSlot
3789 {
3790 bool armed = false;
3791 bool captured = false;
3792 bool window_only = false;
3793 bool last_level = false;
3794 double first_position = 0.0;
3795 double last_position = 0.0;
3796 double recorded_position = 0.0;
3797 std::uint32_t command_id = 0;
3798 };
3799
3800 struct CommandError
3801 {
3802 std::uint32_t command_id = 0;
3804 };
3805
3806 struct AxisManagementResult
3807 {
3808 std::uint32_t command_id = 0;
3810 };
3811
3812 rt::Result<std::uint32_t> submit_management(AxisManagementCommand command,
3813 bool queued)
3814 {
3815 if(queued) {
3816 command.command_id = next_command_id_++;
3817 const rt::ErrorCode added = management_queue_.push_back(command);
3818 return added == rt::ErrorCode::ok
3820 : rt::Result<std::uint32_t>::failure(added);
3821 }
3822 const rt::ErrorCode result = execute_management(command);
3823 if(result != rt::ErrorCode::ok) {
3825 }
3826 return rt::Result<std::uint32_t>::success(last_management_command_id_);
3827 }
3828
3829 rt::ErrorCode execute_management(AxisManagementCommand command)
3830 {
3831 if(command.command_id == 0) command.command_id = next_command_id_++;
3832 active_management_id_ = command.command_id;
3834 switch(command.kind) {
3836 const AxisSnapshot before = snapshot_;
3837 const double delta = command.flag ? command.value
3838 : command.value - before.actual_position;
3839 const bool moving = before.status == AxisStatus::discrete_motion ||
3840 before.status == AxisStatus::continuous_motion ||
3841 before.status == AxisStatus::stopping;
3842 result = moving ? shift_coordinates(delta)
3843 : set_position(before.actual_position + delta);
3844 break;
3845 }
3847 result = write_parameter(command.parameter, command.value);
3848 break;
3850 result = write_bool_parameter(command.parameter, command.flag);
3851 break;
3853 result = set_digital_output(command.channel, command.flag);
3854 break;
3855 }
3856 remember_management_result(command.command_id, result);
3857 last_management_command_id_ = command.command_id;
3858 active_management_id_ = 0;
3859 return result;
3860 }
3861
3862 void cycle_management()
3863 {
3864 if(management_queue_.empty()) return;
3865 const AxisManagementCommand command = management_queue_[0];
3866 for(std::size_t i = 1; i < management_queue_.size(); ++i) {
3867 management_queue_[i - 1] = management_queue_[i];
3868 }
3869 management_queue_.pop_back();
3870 execute_management(command);
3871 }
3872
3873 void remember_management_result(std::uint32_t command_id, rt::ErrorCode error)
3874 {
3875 if(management_results_.size() == QueueCapacity) {
3876 for(std::size_t i = 1; i < management_results_.size(); ++i) {
3877 management_results_[i - 1] = management_results_[i];
3878 }
3879 management_results_.pop_back();
3880 }
3881 management_results_.push_back({command_id, error});
3882 }
3883
3884 // Active + full buffered queue + the independent sync/superimposed/stream
3885 // owners must all retain axis-error attribution in the same cycle.
3886 static constexpr std::size_t CommandErrorCapacity = QueueCapacity + 4;
3887
3888 void record_command_error(std::uint32_t command_id, rt::ErrorCode code)
3889 {
3890 if (command_id == 0 || code == rt::ErrorCode::ok)
3891 {
3892 return;
3893 }
3894 command_errors_[command_error_cursor_] = {command_id, code};
3895 command_error_cursor_ = (command_error_cursor_ + 1) % CommandErrorCapacity;
3896 if (command_error_count_ < CommandErrorCapacity)
3897 {
3898 ++command_error_count_;
3899 }
3900 }
3901
3902 void *group_owner_ = nullptr;
3903 const GroupStatus *group_status_ = nullptr;
3904 double active_target_ = 0.0;
3905 double active_last_sample_ = 0.0;
3906 double base_velocity_ = 0.0;
3907 double continuous_hold_velocity_ = 0.0;
3908 double override_ = 1.0;
3909 double acceleration_override_ = 1.0;
3910 double jerk_override_ = 1.0;
3911 std::int64_t active_tick_ = 0;
3912 std::int64_t superimposed_tick_ = 0;
3913 double superimposed_last_ = 0.0;
3914 static constexpr std::size_t PhasingResultCapacity = 16;
3915 double phase_offset_ = 0.0;
3916 double phasing_target_ = 0.0;
3917 double phasing_start_offset_ = 0.0;
3918 double phasing_velocity_ = 0.0;
3919 double phasing_acceleration_ = 0.0;
3920 double approach_window_begin_ = 0.0;
3921 double approach_start_slave_ = 0.0;
3922 double sync_profile_target_velocity_ = 0.0;
3923 std::int64_t sync_profile_tick_ = 0;
3924 std::int64_t phasing_tick_ = 0;
3925 std::size_t command_error_cursor_ = 0;
3926 std::size_t command_error_count_ = 0;
3927 std::size_t acceleration_segment_count_ = 0;
3928 std::size_t acceleration_segment_index_ = 0;
3929 std::int64_t acceleration_segment_tick_ = 0;
3930 std::size_t passive_homing_input_ = 0;
3931 MotionLimits limits_{};
3932 GearInCommand sync_gear_{};
3933 CombineAxesCommand sync_combine_{};
3934 AxisSnapshot snapshot_{};
3935 AxisCommand active_command_{};
3936 CamInCommand sync_cam_{};
3937 std::array<CamTableSelection, QueueCapacity> cam_table_selections_{};
3938 std::array<ProbeSlot, DigitalInputCount> probes_{};
3939 rt::StaticVector<AxisCommand, QueueCapacity> queue_{};
3940 rt::StaticVector<AxisManagementCommand, QueueCapacity> management_queue_{};
3941 rt::StaticVector<AxisManagementResult, QueueCapacity> management_results_{};
3942 rt::StaticVector<PhasingCommand, QueueCapacity> phasing_queue_{};
3943 exec::CamSpline cam_spline_{};
3944 otg::Profile1D active_profile_{};
3945 otg::Profile1D superimposed_profile_{};
3946 otg::Profile1D sync_profile_{};
3947 otg::Profile1D phasing_profile_{};
3948 stream::StreamFilter1D stream_filter_{};
3949 int domain_id_ = 0;
3950 std::uint32_t next_command_id_ = 1;
3951 std::uint32_t active_management_id_ = 0;
3952 std::uint32_t last_management_command_id_ = 0;
3953 std::uint32_t superimposed_id_ = 0;
3954 std::uint32_t superimposed_completed_id_ = 0;
3955 SyncKind sync_kind_ = SyncKind::none;
3956 SyncPhase sync_phase_ = SyncPhase::idle;
3957 SyncPhase sync_entry_phase_ = SyncPhase::idle;
3958 std::uint32_t sync_id_ = 0;
3959 std::uint32_t phasing_id_ = 0;
3960 std::uint32_t stream_id_ = 0;
3961 std::uint32_t torque_command_id_ = 0;
3962 std::uint32_t stop_lock_id_ = 0;
3963 std::uint32_t passive_homing_id_ = 0;
3964 std::uint32_t passive_homing_aborted_id_ = 0;
3965 std::array<CommandError, CommandErrorCapacity> command_errors_{};
3966 std::array<PhasingCommandResult, PhasingResultCapacity> phasing_results_{};
3967 std::size_t phasing_result_cursor_ = 0;
3968 std::size_t phasing_result_count_ = 0;
3969 bool active_ = false;
3970 bool continuous_holding_ = false;
3971 bool override_braking_ = false;
3972 bool override_paused_ = false;
3973 bool halt_profiled_ = false;
3974 bool blend_armed_ = false;
3975 bool superimposed_active_ = false;
3976 bool superimposed_halting_ = false;
3977 bool phasing_active_ = false;
3978 bool sync_profile_ready_ = false;
3979 bool stream_active_ = false;
3980 bool homing_limits_suspended_ = false;
3981 bool stop_release_requested_ = false;
3982 bool power_feedback_ = true;
3983 bool positive_enabled_ = false;
3984 bool negative_enabled_ = false;
3985 bool acceleration_profile_completed_ = false;
3986 std::array<bool, DigitalInputCount> digital_input_{};
3987 std::array<bool, DigitalOutputCount> digital_output_{};
3988 AxisInfoInputs axis_info_{};
3989};
3990
3991} // namespace plcopen::core::axis
rt::ErrorCode stream_push(const stream::StreamTarget &target)
rt::ErrorCode command_error(std::uint32_t command_id) const
double phasing_covered_shift(std::uint32_t command_id) const
rt::ErrorCode home_direct(double position)
rt::ErrorCode set_override(double velocity_factor, double acceleration_factor, double jerk_factor)
void set_axis_info_inputs(const AxisInfoInputs &inputs)
bool gear_engaged_with(const AxisModel *master) const
rt::Result< std::uint32_t > abort_passive_homing()
std::uint32_t passive_homing_aborted_id() const
rt::Result< std::uint32_t > gear_in(const GearInCommand &command)
rt::ErrorCode shift_coordinates(double delta)
rt::ErrorCode finish_passive_homing(std::uint32_t owner)
rt::ErrorCode cam_switch(const CamInCommand &command, double tolerance)
static constexpr std::size_t DigitalInputCount
rt::Result< bool > read_bool_parameter(AxisParameter parameter) const
rt::ErrorCode combine_update(CombineMode mode, double ratio_numerator_m1, double ratio_denominator_m1, double ratio_numerator_m2, double ratio_denominator_m2)
const stream::StreamFilter1D & stream_filter() const
bool management_command_done(std::uint32_t command_id) const
rt::Result< std::uint32_t > submit_set_position(double position, bool relative, bool queued)
rt::Result< CamTableSelection > cam_table_selection(std::uint32_t id) const
std::uint32_t sync_command_id() const
rt::Result< std::uint32_t > select_cam_table(const AxisModel *master, exec::CamTableView table, bool periodic, bool master_absolute, bool slave_absolute, std::uint32_t replace_id=0)
rt::ErrorCode trigger_error(rt::ErrorCode code=rt::ErrorCode::precondition_failed)
bool command_pending(std::uint32_t command_id) const
AxisModel(const AxisModel &)=delete
rt::ErrorCode release_stop(std::uint32_t command_id)
rt::Result< std::uint32_t > cam_in(const CamInCommand &command)
PhasingCommandState phasing_command_state(std::uint32_t command_id) const
rt::ErrorCode update_active_velocity(std::uint32_t command_id, double direction_value, double velocity)
rt::Result< AxisSiConfig > si_config(const rt::CycleConfig &cycle) const
rt::ErrorCode set_position(double position)
rt::ErrorCode write_bool_parameter(AxisParameter parameter, bool value)
rt::ErrorCode write_parameter(AxisParameter parameter, double value)
rt::Result< std::uint32_t > stream_engage(const stream::StreamFilterConfig &config)
rt::Result< std::uint32_t > begin_passive_homing(std::size_t input)
rt::Result< double > read_parameter(AxisParameter parameter) const
rt::ErrorCode set_digital_input(std::size_t input, bool level)
rt::Result< std::uint32_t > combine_in(const CombineAxesCommand &command)
AxisModel & operator=(AxisModel &&)=delete
rt::Result< std::uint32_t > arm_touch_probe(std::size_t input, bool window_only, double first_position, double last_position)
rt::ErrorCode cam_update(double master_offset, double master_scaling, double slave_offset, double slave_scaling)
rt::ErrorCode fail_command(std::uint32_t command_id, rt::ErrorCode code)
rt::Result< bool > digital_output(std::size_t output) const
rt::ErrorCode update_superimposed_target(std::uint32_t command_id, double distance, double velocity, double acceleration, double deceleration, double jerk)
const AxisInfoInputs & axis_info_inputs() const
rt::ErrorCode configure_si(const rt::CycleConfig &cycle, const AxisSiConfig &config)
rt::ErrorCode management_command_error(std::uint32_t command_id) const
rt::ErrorCode set_digital_output(std::size_t output, bool level)
rt::ErrorCode set_power(bool enabled, bool enable_positive=true, bool enable_negative=true)
double probe_recorded_position(std::size_t input) const
const MotionLimits & motion_limits() const
rt::Result< std::uint32_t > submit_acceleration_profile(const ProfileSegment *segments, std::size_t segment_count, double acceleration_scale, double acceleration_offset, double time_scale)
rt::ErrorCode halt_superimposed(double deceleration, double jerk)
const AxisSnapshot & snapshot() const
bool management_command_active(std::uint32_t command_id) const
bool management_command_pending(std::uint32_t command_id) const
std::uint32_t passive_homing_id() const
rt::Result< bool > digital_input(std::size_t input) const
rt::Result< std::uint32_t > submit_superimposed(double distance, double velocity, double acceleration, double deceleration, double jerk)
rt::Result< std::uint32_t > submit_write_parameter(AxisParameter parameter, double value, bool queued)
rt::ErrorCode gear_update(double ratio_numerator, double ratio_denominator)
bool command_in_velocity(std::uint32_t command_id) const
static constexpr std::size_t QueueCapacity
rt::Result< std::uint32_t > submit_write_digital_output(std::size_t channel, bool value, bool queued)
rt::Result< std::uint32_t > submit_write_bool_parameter(AxisParameter parameter, bool value, bool queued)
rt::Result< std::uint32_t > submit_phasing(PhasingCommand command)
rt::ErrorCode set_power_feedback(bool available)
rt::ErrorCode homing_step_precondition() const
std::uint32_t superimposed_completed_id() const
rt::ErrorCode abort_trigger(std::size_t input)
rt::ErrorCode set_override(double factor)
rt::ErrorCode update_active_target(std::uint32_t command_id, double target)
AxisModel & operator=(const AxisModel &)=delete
std::uint32_t stream_session_id() const
std::uint32_t superimposed_command_id() const
rt::ErrorCode update_active_torque(std::uint32_t command_id, const AxisCommand &command)
rt::Result< std::uint32_t > submit(AxisCommand command)
rt::ErrorCode set_actual_feedback(double position, double velocity, double acceleration=0.0, double torque=0.0)
rt::ErrorCode set_position_on_the_fly(double delta)
static constexpr std::size_t DigitalOutputCount
std::uint32_t torque_command_id() const
rt::ErrorCode preflight_position_sequence(const AxisCommand *commands, std::size_t command_count, bool enforce_soft_limits=false) const
bool command_in_end_velocity(std::uint32_t command_id) const
rt::ErrorCode configure_limits(MotionLimits limits)
AxisModel(AxisModel &&)=delete
bool probe_captured(std::size_t input) const
std::uint32_t probe_command_id(std::size_t input) const
rt::Result< double > sample(double master_position) const
定义 sync.h:351
rt::ErrorCode build(const CamTableView &table)
定义 sync.h:213
rt::ErrorCode translate(double delta)
static constexpr CycleTick from_cycles(std::int64_t cycles)
定义 cycle.h:42
constexpr const T & value() const
定义 error.h:49
constexpr ErrorCode error() const
定义 error.h:44
static constexpr Result success(const T &value)
定义 error.h:24
static constexpr Result failure(ErrorCode error)
定义 error.h:29
bool is_continuous_kind(CommandKind kind)
bool is_valid_buffer_mode(BufferMode mode)
bool is_valid_direction(Direction direction)
bool is_finite_command(const AxisCommand &command)
RampTiming ramp_between(double va, double vb, const Limits1D &limits)
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)
exec::CamInterpolation interpolation