plcopen 0.22.0
载入中...
搜索中...
未找到
homing.h
浏览该文件的文档.
1#pragma once
2
3#include <cmath>
4#include <cstddef>
5#include <cstdint>
6
7#include "axis/state.h"
8#include "fb/motion.h"
9#include "rt/error.h"
10
11namespace plcopen::core::fb
12{
13
14// Part 5 composable homing step FBs (approved matrix 2026-07-07).
15// Each successfully started Step FB clears homed; only MC_FinishHoming sets it.
16// Soft-limit monitoring is suspended during homing steps and restored by
17// FinishHoming (Part 5 contract: limits are meaningless before homing).
18
19// MC_HomeDirect: abort the current command and set axis position without motion.
21{
22 public:
24 double set_position = 0.0;
26 bool execute = false;
28
29 void call()
30 {
31 const bool rising = execute && !last_execute_;
32 last_execute_ = execute;
33 if (!execute)
34 {
36 return;
37 }
38 if (!rising)
39 {
40 return;
41 }
43 if (axis_ref == nullptr || !std::isfinite(set_position))
44 {
45 outputs.error = true;
47 return;
48 }
50 {
51 outputs.error = true;
53 return;
54 }
55 const rt::ErrorCode result = axis_ref->home_direct(set_position);
56 if (result != rt::ErrorCode::ok)
57 {
58 outputs.error = true;
59 outputs.error_id = result;
60 return;
61 }
62 outputs.done = true;
63 }
64
65 private:
66 bool last_execute_ = false;
67};
68
69// MC_FinishHoming: set homed flag + optional park move.
71{
72 public:
73 double distance = 0.0;
75 double velocity = 1.0;
76 double acceleration = 1.0;
77 double deceleration = 1.0;
78 double jerk = 1.0;
79
80 void call()
81 {
82 if (rising_edge())
83 {
84 homed_applied_ = false;
85 if (axis_ref == nullptr)
86 {
88 return;
89 }
92 {
94 return;
95 }
96 if (!std::isfinite(distance) || !std::isfinite(velocity) || velocity <= 0.0 ||
97 !std::isfinite(acceleration) || acceleration <= 0.0 ||
98 !std::isfinite(deceleration) || deceleration <= 0.0 || !std::isfinite(jerk) ||
99 jerk <= 0.0)
100 {
102 return;
103 }
104 if (distance != 0.0)
105 {
106 axis::AxisCommand cmd{};
108 cmd.value = distance;
109 cmd.velocity = velocity;
112 cmd.jerk = jerk;
115 {
116 const rt::ErrorCode preflight =
117 axis_ref->preflight_position_sequence(&cmd, 1, true);
118 if (preflight != rt::ErrorCode::ok)
119 {
121 return;
122 }
123 }
124 const rt::Result<std::uint32_t> submitted = axis_ref->submit(cmd);
125 accept(submitted);
126 }
127 else
128 {
129 const rt::ErrorCode finished = axis_ref->finish_homing_now();
130 if (finished != rt::ErrorCode::ok)
131 {
133 return;
134 }
136 outputs.done = true;
137 outputs.busy = false;
138 outputs.active = false;
139 homed_applied_ = true;
140 }
141 }
142 observe_axis();
143 if (axis_ref != nullptr && !homed_applied_ && (outputs.active || outputs.done))
144 {
145 axis_ref->finish_homing();
146 homed_applied_ = true;
147 }
148 }
149
150 private:
151 bool homed_applied_ = false;
152};
153
154// Common base for switch/pulse homing search steps. The lifecycle is:
155// escaping (optional) -> searching -> halting -> positioning -> done
157{
158 public:
160 bool execute = false;
161 double velocity = 1.0;
162 double set_position = 0.0;
165 double torque_limit = 0.0;
167 std::int64_t time_limit = 0;
168 double distance_limit = 0.0;
170
171 protected:
182
189
191
193 {
194 const bool falling = !execute && last_execute_;
195 const bool rising = execute && !last_execute_;
196 last_execute_ = execute;
197 if (falling && phase_ == Phase::idle &&
198 (outputs.done || outputs.command_aborted || outputs.error))
199 {
200 clear(outputs);
201 }
202 if (!execute && terminal_low_cycle_)
203 {
204 clear(outputs);
205 terminal_low_cycle_ = false;
206 }
207 if (rising)
208 {
209 clear(outputs);
210 terminal_low_cycle_ = false;
211 }
212 return rising;
213 }
214
215 bool validate_inputs(bool require_reference) const
216 {
217 return axis_ref != nullptr && std::isfinite(velocity) && velocity > 0.0 &&
218 std::isfinite(set_position) && std::isfinite(torque_limit) && torque_limit >= 0.0 &&
221 (!require_reference || signal_input() < axis::AxisModel::DigitalInputCount);
222 }
223
225 {
226 const rt::ErrorCode precondition = axis_ref->homing_step_precondition();
227 if (precondition != rt::ErrorCode::ok)
228 {
229 return precondition;
230 }
231 const rt::ErrorCode vel = submit_velocity(-direction_sign_);
232 if (vel != rt::ErrorCode::ok)
233 {
234 return vel;
235 }
236 if (axis_ref->command_pending(phase_command_id_))
237 {
238 outputs.active = false;
240 }
241 else
242 {
244 }
245 return rt::ErrorCode::ok;
246 }
247
249 {
250 const rt::ErrorCode precondition = axis_ref->homing_step_precondition();
251 if (precondition != rt::ErrorCode::ok)
252 {
253 return precondition;
254 }
255 const rt::ErrorCode vel = submit_velocity(direction_sign_);
256 if (vel != rt::ErrorCode::ok)
257 {
258 return vel;
259 }
260 if (axis_ref->command_pending(phase_command_id_))
261 {
262 outputs.active = false;
264 return rt::ErrorCode::ok;
265 }
266 return activate_search();
267 }
268
270 {
271 axis_ref->clear_homed();
273 {
274 start_position_ = axis_ref->snapshot().command_position;
275 search_cycles_ = 0;
276 }
277 outputs.active = true;
279 }
280
282 {
283 if (phase_ != Phase::escaping)
284 {
285 axis_ref->clear_homed();
286 start_position_ = axis_ref->snapshot().command_position;
287 search_cycles_ = 0;
288 }
289 outputs.active = true;
290 const rt::Result<bool> level = read_signal();
291 if (!level)
292 {
293 return level.error();
294 }
295 last_signal_level_ = level.value();
296 if (!use_probe_)
297 {
299 return rt::ErrorCode::ok;
300 }
301 const rt::Result<std::uint32_t> armed =
302 axis_ref->arm_touch_probe(signal_input(), false, 0.0, 0.0);
303 if (!armed)
304 {
305 submit_halt();
306 phase_command_id_ = 0;
307 return armed.error();
308 }
309 probe_command_id_ = armed.value();
311 return rt::ErrorCode::ok;
312 }
313
314 void observe()
315 {
316 if (axis_ref == nullptr || phase_ == Phase::idle)
317 {
318 return;
319 }
320 const axis::AxisSnapshot &snap = axis_ref->snapshot();
322 {
324 return;
325 }
326
327 switch (phase_)
328 {
331 if (axis_ref->command_pending(phase_command_id_))
332 {
333 break;
334 }
335 if (snap.active_command_id != phase_command_id_)
336 {
337 command_aborted();
338 break;
339 }
341 {
343 }
344 else
345 {
346 const rt::ErrorCode started = activate_search();
347 if (started != rt::ErrorCode::ok)
348 {
349 fail(started);
350 }
351 }
352 break;
353 case Phase::escaping:
354 {
355 if (snap.active_command_id != phase_command_id_)
356 {
357 command_aborted();
358 return;
359 }
360 if (limit_exceeded(snap))
361 {
362 return;
363 }
364 const rt::Result<bool> level = read_signal();
365 if (level && level.value() != escape_level_)
366 {
367 const rt::ErrorCode started = start_search();
368 if (started != rt::ErrorCode::ok)
369 {
370 fail(started);
371 }
372 }
373 break;
374 }
375 case Phase::searching:
376 if (snap.active_command_id != phase_command_id_ ||
377 (use_probe_ && axis_ref->probe_command_id(signal_input()) != probe_command_id_))
378 {
379 command_aborted();
380 return;
381 }
382 if (limit_exceeded(snap))
383 {
384 return;
385 }
386 if (reverse_on_limit_ && (axis_ref->axis_info_inputs().limit_switch_pos ||
387 axis_ref->axis_info_inputs().limit_switch_neg))
388 {
389 const rt::Result<bool> level = read_signal();
390 if (!level)
391 {
392 fail(level.error());
393 return;
394 }
395 set_escape_level(level.value());
396 const rt::ErrorCode started = start_escape();
397 if (started != rt::ErrorCode::ok)
398 {
399 fail(started);
400 }
401 return;
402 }
403 if (search_condition_met())
404 {
405 captured_position_ = use_probe_ ? axis_ref->probe_recorded_position(signal_input())
406 : snap.command_position;
407 release_owned_probe();
408 const rt::ErrorCode halted = submit_halt();
409 if (halted != rt::ErrorCode::ok)
410 {
411 fail(halted);
412 return;
413 }
415 }
416 break;
417 case Phase::halting:
418 if (snap.active_command_id == phase_command_id_)
419 {
420 break;
421 }
422 if (snap.active_command_id != 0 || snap.last_completed_command_id != phase_command_id_)
423 {
424 command_aborted();
425 return;
426 }
427 {
428 axis::AxisCommand cmd{};
430 cmd.value = captured_position_;
431 cmd.velocity = velocity;
432 cmd.acceleration = axis_ref->motion_limits().max_acceleration;
433 cmd.deceleration = axis_ref->motion_limits().max_deceleration;
434 cmd.jerk = axis_ref->motion_limits().max_jerk;
435 cmd.homing = true;
436 const rt::Result<std::uint32_t> moved = axis_ref->submit(cmd);
437 if (!moved)
438 {
439 fail(moved.error());
440 return;
441 }
442 phase_command_id_ = moved.value();
444 }
445 break;
447 if (snap.active_command_id == phase_command_id_)
448 {
449 break;
450 }
451 if (snap.active_command_id != 0 || snap.last_completed_command_id != phase_command_id_)
452 {
453 command_aborted();
454 return;
455 }
456 {
458 {
459 const rt::ErrorCode set = axis_ref->set_position(set_position);
460 if (set != rt::ErrorCode::ok)
461 {
462 fail(set);
463 return;
464 }
465 }
466 outputs.done = true;
467 outputs.busy = false;
468 outputs.active = false;
470 phase_command_id_ = 0;
471 terminal_low_cycle_ = !execute;
472 }
473 break;
474 default:
475 break;
476 }
477 }
478
479 private:
480 void fail(rt::ErrorCode code)
481 {
482 release_owned_probe();
483 outputs.done = false;
484 outputs.command_aborted = false;
485 outputs.error = true;
486 outputs.error_id = code;
487 outputs.busy = false;
488 outputs.active = false;
490 phase_command_id_ = 0;
491 terminal_low_cycle_ = !execute;
492 }
493
494 void command_aborted()
495 {
496 release_owned_probe();
497 if (phase_ != Phase::halting && phase_command_id_ != 0 &&
498 axis_ref->snapshot().active_command_id == phase_command_id_)
499 {
500 submit_halt();
501 }
502 outputs.done = false;
503 outputs.busy = false;
504 outputs.active = false;
506 outputs.error = false;
509 phase_command_id_ = 0;
510 terminal_low_cycle_ = !execute;
511 }
512
513 void release_owned_probe()
514 {
515 if (axis_ref != nullptr && probe_command_id_ != 0 &&
516 axis_ref->probe_command_id(signal_input()) == probe_command_id_)
517 {
519 }
520 probe_command_id_ = 0;
521 }
522
523 bool search_condition_met()
524 {
525 if (use_probe_)
526 {
527 return axis_ref->probe_captured(signal_input());
528 }
529 const rt::Result<bool> level = read_signal();
530 if (!level)
531 {
532 return false;
533 }
534 const bool current = level.value();
535 const bool rising = !last_signal_level_ && current;
536 const bool falling = last_signal_level_ && !current;
537 last_signal_level_ = current;
538 switch (switch_mode_)
539 {
541 return current;
543 return !current;
545 return rising;
547 return falling;
549 return direction_sign_ > 0.0 ? rising : falling;
551 return direction_sign_ < 0.0 ? rising : falling;
552 }
553 return false;
554 }
555
556 rt::ErrorCode submit_velocity(double dir)
557 {
558 axis::AxisCommand cmd{};
560 cmd.value = dir;
561 cmd.velocity = velocity;
562 cmd.acceleration = axis_ref->motion_limits().max_acceleration;
563 cmd.deceleration = axis_ref->motion_limits().max_deceleration;
564 cmd.jerk = axis_ref->motion_limits().max_jerk;
565 cmd.torque_limit = torque_limit;
567 cmd.homing = true;
568 const rt::Result<std::uint32_t> result = axis_ref->submit(cmd);
569 if (result)
570 {
571 phase_command_id_ = result.value();
572 }
573 return result ? rt::ErrorCode::ok : result.error();
574 }
575
576 protected:
577 std::size_t signal_input() const { return reference_signal_.input; }
578
580 {
581 if (axis_ref == nullptr)
582 {
584 }
585 if (signal_source_ == SignalSource::positive_limit)
586 {
587 return rt::Result<bool>::success(axis_ref->axis_info_inputs().limit_switch_pos);
588 }
589 if (signal_source_ == SignalSource::negative_limit)
590 {
591 return rt::Result<bool>::success(axis_ref->axis_info_inputs().limit_switch_neg);
592 }
593 return axis_ref->digital_input(signal_input());
594 }
595
597 bool use_probe)
598 {
599 reference_signal_ = reference;
600 switch_mode_ = mode;
601 signal_source_ = SignalSource::reference;
602 use_probe_ = use_probe;
603 }
604
606 {
607 switch_mode_ = mode;
610 use_probe_ = false;
611 }
612
613 bool valid_switch_mode() const
614 {
615 switch (switch_mode_)
616 {
623 return true;
624 }
625 return false;
626 }
627
628 void set_escape_level(bool level) { escape_level_ = level; }
629
630 void set_reverse_on_limit(bool enabled) { reverse_on_limit_ = enabled; }
631
632 bool set_direction_sign(bool initial_level, bool allow_switch_modes)
633 {
634 switch (direction)
635 {
637 direction_sign_ = 1.0;
638 return true;
640 direction_sign_ = -1.0;
641 return true;
643 if (!allow_switch_modes)
644 return false;
645 direction_sign_ = initial_level ? -1.0 : 1.0;
646 return true;
648 if (!allow_switch_modes)
649 return false;
650 direction_sign_ = initial_level ? 1.0 : -1.0;
651 return true;
652 }
653 return false;
654 }
655
656 private:
657 bool limit_exceeded(const axis::AxisSnapshot &snapshot)
658 {
659 ++search_cycles_;
660 if ((time_limit > 0 && search_cycles_ > time_limit) ||
661 (distance_limit > 0.0 &&
662 std::fabs(snapshot.command_position - start_position_) > distance_limit))
663 {
666 return true;
667 }
668 return false;
669 }
670
671 rt::ErrorCode submit_halt()
672 {
673 axis::AxisCommand cmd{};
674 cmd.kind = axis::CommandKind::halt;
675 cmd.velocity = velocity;
676 cmd.acceleration = axis_ref->motion_limits().max_acceleration;
677 cmd.deceleration = axis_ref->motion_limits().max_deceleration;
678 cmd.jerk = axis_ref->motion_limits().max_jerk;
679 cmd.homing = true;
680 const rt::Result<std::uint32_t> result = axis_ref->submit(cmd);
681 if (!result)
682 {
683 return result.error();
684 }
685 phase_command_id_ = result.value();
686 return rt::ErrorCode::ok;
687 }
688
689 double start_position_ = 0.0;
690 double captured_position_ = 0.0;
691 std::int64_t search_cycles_ = 0;
692 std::uint32_t phase_command_id_ = 0;
693 std::uint32_t probe_command_id_ = 0;
694 bool last_execute_ = false;
695 bool terminal_low_cycle_ = false;
696 double direction_sign_ = 1.0;
697 axis::ReferenceSignalRef reference_signal_{};
699 SignalSource signal_source_ = SignalSource::reference;
700 bool use_probe_ = true;
701 bool last_signal_level_ = false;
702 bool escape_level_ = false;
703 bool reverse_on_limit_ = false;
704};
705
706// MC_StepAbsoluteSwitch: search for absolute switch edge. If the switch is
707// already triggered, escapes in the opposite direction first.
709{
710 public:
713
714 void call()
715 {
716 if (rising_edge())
717 {
722 {
723 clear(outputs);
724 outputs.error = true;
726 return;
727 }
728 if (!validate_inputs(true) || !valid_switch_mode())
729 {
730 outputs.error = true;
732 return;
733 }
734 outputs.busy = true;
735 outputs.active = true;
736 const rt::Result<bool> level = axis_ref->digital_input(signal_input());
737 if (!level || !set_direction_sign(level.value(), true))
738 {
739 clear(outputs);
740 outputs.error = true;
742 return;
743 }
744 const rt::ErrorCode e = start_search();
745 if (e != rt::ErrorCode::ok)
746 {
747 clear(outputs);
748 outputs.error = true;
749 outputs.error_id = e;
750 }
751 }
752 observe();
753 }
754};
755
756// MC_StepLimitSwitch: search for limit switch edge. The switch must not
757// already be triggered (approach direction must go from off to on).
759{
760 public:
762
763 void call()
764 {
765 if (rising_edge())
766 {
769 {
770 clear(outputs);
771 outputs.error = true;
773 return;
774 }
777 {
778 outputs.error = true;
780 return;
781 }
787 {
788 outputs.error = true;
790 return;
791 }
792 const rt::Result<bool> level = read_signal();
793 if (!level || !set_direction_sign(level.value(), false))
794 {
795 outputs.error = true;
797 return;
798 }
799 outputs.busy = true;
800 outputs.active = true;
801 set_escape_level(true);
802 const rt::ErrorCode e = level.value() ? start_escape() : start_search();
803 if (e != rt::ErrorCode::ok)
804 {
805 clear(outputs);
806 outputs.error = true;
807 outputs.error_id = e;
808 }
809 }
810 observe();
811 }
812};
813
814// MC_StepReferencePulse: search for encoder reference pulse (Z-phase).
816{
817 public:
819
820 void call()
821 {
822 if (rising_edge())
823 {
827 {
828 clear(outputs);
829 outputs.error = true;
831 return;
832 }
833 if (!validate_inputs(true))
834 {
835 outputs.error = true;
837 return;
838 }
839 const rt::Result<bool> level = axis_ref->digital_input(signal_input());
840 if (!level || !set_direction_sign(level.value(), false))
841 {
842 outputs.error = true;
844 return;
845 }
846 outputs.busy = true;
847 outputs.active = true;
848 const bool on_limit = axis_ref->axis_info_inputs().limit_switch_pos ||
849 axis_ref->axis_info_inputs().limit_switch_neg;
850 if (on_limit)
851 {
852 set_escape_level(level.value());
853 }
854 const rt::ErrorCode e = on_limit ? start_escape() : start_search();
855 if (e != rt::ErrorCode::ok)
856 {
857 clear(outputs);
858 outputs.error = true;
859 outputs.error_id = e;
860 }
861 }
862 observe();
863 }
864};
865
867{
868 public:
869 // KB-072: actual feedback is the only block-detection source.
871 bool execute = false;
873 double velocity = 1.0;
874 double set_position = 0.0;
877 std::int64_t detection_velocity_time = 0;
878 double torque_limit = 0.0;
879 std::int64_t time_limit = 0;
880 double distance_limit = 0.0;
883
884 void call()
885 {
886 const bool falling = !execute && last_execute_;
887 const bool rising = execute && !last_execute_;
888 last_execute_ = execute;
889 if (falling && phase_ == Phase::idle &&
890 (outputs.done || outputs.command_aborted || outputs.error))
891 {
892 clear(outputs);
893 }
894 if (!execute && terminal_low_cycle_)
895 {
896 clear(outputs);
897 terminal_low_cycle_ = false;
898 }
899 if (rising)
900 start();
901 observe();
902 }
903
904 private:
905 enum class Phase
906 {
907 idle,
908 queued,
909 searching,
910 halting,
911 positioning
912 };
913
914 void start()
915 {
916 clear(outputs);
917 terminal_low_cycle_ = false;
919 {
921 return;
922 }
923 if (axis_ref == nullptr || !std::isfinite(velocity) || velocity <= 0.0 ||
924 !std::isfinite(set_position) || !std::isfinite(detection_velocity_limit) ||
926 !std::isfinite(torque_limit) || torque_limit < 0.0 || time_limit < 0 ||
927 !std::isfinite(distance_limit) || distance_limit < 0.0 ||
931 {
933 return;
934 }
935 axis::AxisCommand command{};
937 command.value = direction == axis::HomeDirection::positive ? 1.0 : -1.0;
938 command.velocity = velocity;
939 command.acceleration = axis_ref->motion_limits().max_acceleration;
940 command.deceleration = axis_ref->motion_limits().max_deceleration;
941 command.jerk = axis_ref->motion_limits().max_jerk;
942 command.torque_limit = torque_limit;
943 command.buffer_mode = buffer_mode;
944 command.homing = true;
945 const rt::Result<std::uint32_t> submitted = axis_ref->submit(command);
946 if (!submitted)
947 {
948 fail(submitted.error());
949 return;
950 }
951 command_id_ = submitted.value();
952 elapsed_cycles_ = 0;
953 detected_cycles_ = 0;
954 outputs.busy = true;
955 if (axis_ref->command_pending(command_id_))
956 {
957 outputs.active = false;
958 phase_ = Phase::queued;
959 }
960 else
961 {
962 activate_search();
963 }
964 }
965
966 void observe()
967 {
968 if (axis_ref == nullptr || phase_ == Phase::idle)
969 return;
970 const axis::AxisSnapshot &snapshot = axis_ref->snapshot();
971 if (phase_ == Phase::queued)
972 {
973 if (axis_ref->command_pending(command_id_))
974 {
975 return;
976 }
977 if (snapshot.active_command_id == command_id_)
978 {
979 activate_search();
980 return;
981 }
982 const rt::ErrorCode error = axis_ref->command_error(command_id_);
984 {
985 fail(error);
986 }
987 else
988 {
989 abort();
990 }
991 return;
992 }
993 if (phase_ == Phase::searching)
994 {
995 if (snapshot.active_command_id != command_id_)
996 {
997 abort();
998 return;
999 }
1000 ++elapsed_cycles_;
1001 if ((time_limit > 0 && elapsed_cycles_ > time_limit) ||
1002 (distance_limit > 0.0 &&
1003 std::fabs(snapshot.command_position - start_position_) > distance_limit))
1004 {
1005 axis_ref->trigger_error();
1007 return;
1008 }
1009 const bool torque_reached =
1010 torque_limit == 0.0 || std::fabs(snapshot.actual_torque) >= torque_limit;
1011 const bool velocity_reached =
1012 std::fabs(snapshot.actual_velocity) <= detection_velocity_limit;
1013 detected_cycles_ = torque_reached && velocity_reached ? detected_cycles_ + 1 : 0;
1014 const std::int64_t required =
1016 if (detected_cycles_ < required)
1017 return;
1018 axis::AxisCommand halt{};
1020 halt.velocity = velocity;
1021 halt.acceleration = axis_ref->motion_limits().max_acceleration;
1022 halt.deceleration = axis_ref->motion_limits().max_deceleration;
1023 halt.jerk = axis_ref->motion_limits().max_jerk;
1024 halt.homing = true;
1025 const rt::Result<std::uint32_t> submitted = axis_ref->submit(halt);
1026 if (!submitted)
1027 {
1028 fail(submitted.error());
1029 return;
1030 }
1031 command_id_ = submitted.value();
1032 phase_ = Phase::halting;
1033 return;
1034 }
1035 if (snapshot.active_command_id == command_id_)
1036 return;
1037 if (snapshot.active_command_id != 0 || snapshot.last_completed_command_id != command_id_)
1038 {
1039 abort();
1040 return;
1041 }
1042 if (phase_ == Phase::halting && set_position_enabled)
1043 {
1044 if (axis_ref->set_position(set_position) != rt::ErrorCode::ok)
1045 {
1047 return;
1048 }
1049 phase_ = Phase::positioning;
1050 }
1051 outputs.done = true;
1052 outputs.busy = false;
1053 outputs.active = false;
1054 phase_ = Phase::idle;
1055 terminal_low_cycle_ = !execute;
1056 }
1057
1058 void activate_search()
1059 {
1060 axis_ref->clear_homed();
1061 start_position_ = axis_ref->snapshot().command_position;
1062 elapsed_cycles_ = 0;
1063 detected_cycles_ = 0;
1064 outputs.active = true;
1065 phase_ = Phase::searching;
1066 }
1067
1068 void fail(rt::ErrorCode error)
1069 {
1070 clear(outputs);
1071 outputs.error = true;
1072 outputs.error_id = error;
1073 phase_ = Phase::idle;
1074 terminal_low_cycle_ = !execute;
1075 }
1076
1077 void abort()
1078 {
1079 clear(outputs);
1080 outputs.command_aborted = true;
1081 phase_ = Phase::idle;
1082 terminal_low_cycle_ = !execute;
1083 }
1084
1085 Phase phase_ = Phase::idle;
1086 bool last_execute_ = false;
1087 bool terminal_low_cycle_ = false;
1088 std::uint32_t command_id_ = 0;
1089 double start_position_ = 0.0;
1090 std::int64_t elapsed_cycles_ = 0;
1091 std::int64_t detected_cycles_ = 0;
1092};
1093
1095{
1096 double signed_distance = 0.0;
1098};
1099
1101{
1102 static constexpr std::size_t Capacity = 32;
1104 std::size_t count = 0;
1105 double tolerance = 0.0;
1106};
1107
1109{
1110 public:
1112 bool execute = false;
1114 double velocity = 1.0;
1115 double torque_limit = 0.0;
1116 std::int64_t time_limit = 0;
1117 double distance_limit = 0.0;
1120
1122 {
1123 if (outputs.busy)
1125 if (map == nullptr)
1127 code_map_ = map;
1128 return rt::ErrorCode::ok;
1129 }
1130
1132 {
1133 if (outputs.busy)
1136 {
1138 }
1139 reference_signal_ = reference;
1140 return rt::ErrorCode::ok;
1141 }
1142
1143 void call()
1144 {
1145 const bool falling = !execute && last_execute_;
1146 const bool rising = execute && !last_execute_;
1147 last_execute_ = execute;
1148 if (falling && phase_ == Phase::idle &&
1149 (outputs.done || outputs.command_aborted || outputs.error))
1150 {
1151 clear(outputs);
1152 }
1153 if (!execute && terminal_low_cycle_)
1154 {
1155 clear(outputs);
1156 terminal_low_cycle_ = false;
1157 }
1158 if (rising)
1159 start();
1160 observe();
1161 }
1162
1163 private:
1164 enum class Phase
1165 {
1166 idle,
1167 queued,
1168 first_mark,
1169 second_mark,
1170 halting
1171 };
1172
1173 bool valid_map() const
1174 {
1175 if (code_map_ == nullptr || code_map_->count == 0 ||
1176 code_map_->count > DistanceCodeMap::Capacity || !std::isfinite(code_map_->tolerance) ||
1177 code_map_->tolerance < 0.0)
1178 {
1179 return false;
1180 }
1181 for (std::size_t i = 0; i < code_map_->count; ++i)
1182 {
1183 const DistanceCodeEntry &entry = code_map_->entries[i];
1184 if (!std::isfinite(entry.signed_distance) ||
1185 !std::isfinite(entry.second_mark_position) || entry.signed_distance == 0.0)
1186 return false;
1187 for (std::size_t j = i + 1; j < code_map_->count; ++j)
1188 {
1189 if (std::fabs(entry.signed_distance - code_map_->entries[j].signed_distance) <=
1190 code_map_->tolerance)
1191 return false;
1192 }
1193 }
1194 return true;
1195 }
1196
1197 void start()
1198 {
1199 clear(outputs);
1200 terminal_low_cycle_ = false;
1202 {
1204 return;
1205 }
1206 if (axis_ref == nullptr || !valid_map() || !std::isfinite(velocity) || velocity <= 0.0 ||
1207 !std::isfinite(torque_limit) || torque_limit < 0.0 || time_limit < 0 ||
1208 !std::isfinite(distance_limit) || distance_limit < 0.0 ||
1211 reference_signal_.input >= axis::AxisModel::DigitalInputCount ||
1212 axis_ref->homing_step_precondition() != rt::ErrorCode::ok)
1213 {
1215 return;
1216 }
1217 axis::AxisCommand command{};
1219 command.value = direction == axis::HomeDirection::positive ? 1.0 : -1.0;
1220 command.velocity = velocity;
1221 command.acceleration = axis_ref->motion_limits().max_acceleration;
1222 command.deceleration = axis_ref->motion_limits().max_deceleration;
1223 command.jerk = axis_ref->motion_limits().max_jerk;
1224 command.torque_limit = torque_limit;
1225 command.buffer_mode = buffer_mode;
1226 command.homing = true;
1227 const rt::Result<std::uint32_t> submitted = axis_ref->submit(command);
1228 if (!submitted)
1229 {
1230 fail(submitted.error());
1231 return;
1232 }
1233 command_id_ = submitted.value();
1234 elapsed_cycles_ = 0;
1235 outputs.busy = true;
1236 if (axis_ref->command_pending(command_id_))
1237 {
1238 outputs.active = false;
1239 phase_ = Phase::queued;
1240 }
1241 else
1242 {
1243 activate();
1244 }
1245 }
1246
1247 void observe()
1248 {
1249 if (axis_ref == nullptr || phase_ == Phase::idle)
1250 return;
1251 const axis::AxisSnapshot &snapshot = axis_ref->snapshot();
1252 if (phase_ == Phase::queued)
1253 {
1254 if (axis_ref->command_pending(command_id_))
1255 return;
1256 if (snapshot.active_command_id == command_id_)
1257 {
1258 activate();
1259 }
1260 else
1261 {
1262 abort();
1263 }
1264 return;
1265 }
1266 if (phase_ == Phase::first_mark || phase_ == Phase::second_mark)
1267 {
1268 if (snapshot.active_command_id != command_id_)
1269 {
1270 abort();
1271 return;
1272 }
1273 ++elapsed_cycles_;
1274 if ((time_limit > 0 && elapsed_cycles_ > time_limit) ||
1275 (distance_limit > 0.0 &&
1276 std::fabs(snapshot.command_position - start_position_) > distance_limit))
1277 {
1278 axis_ref->trigger_error();
1280 return;
1281 }
1282 if (!axis_ref->probe_captured(reference_signal_.input))
1283 return;
1284 const double mark = axis_ref->probe_recorded_position(reference_signal_.input);
1285 release_probe();
1286 if (phase_ == Phase::first_mark)
1287 {
1288 first_mark_ = mark;
1289 const rt::Result<std::uint32_t> armed =
1290 axis_ref->arm_touch_probe(reference_signal_.input, false, 0.0, 0.0);
1291 if (!armed)
1292 {
1293 fail(armed.error());
1294 return;
1295 }
1296 probe_id_ = armed.value();
1297 phase_ = Phase::second_mark;
1298 return;
1299 }
1300 const double distance = mark - first_mark_;
1301 std::size_t matches = 0;
1302 double position = 0.0;
1303 for (std::size_t i = 0; i < code_map_->count; ++i)
1304 {
1305 if (std::fabs(distance - code_map_->entries[i].signed_distance) <=
1306 code_map_->tolerance)
1307 {
1308 ++matches;
1309 position = code_map_->entries[i].second_mark_position;
1310 }
1311 }
1312 if (matches != 1)
1313 {
1315 return;
1316 }
1317 resolved_position_ = position;
1318 axis::AxisCommand halt{};
1320 halt.velocity = velocity;
1321 halt.acceleration = axis_ref->motion_limits().max_acceleration;
1322 halt.deceleration = axis_ref->motion_limits().max_deceleration;
1323 halt.jerk = axis_ref->motion_limits().max_jerk;
1324 halt.homing = true;
1325 const rt::Result<std::uint32_t> submitted = axis_ref->submit(halt);
1326 if (!submitted)
1327 {
1328 fail(submitted.error());
1329 return;
1330 }
1331 command_id_ = submitted.value();
1332 phase_ = Phase::halting;
1333 return;
1334 }
1335 if (snapshot.active_command_id == command_id_)
1336 return;
1337 if (snapshot.active_command_id != 0 || snapshot.last_completed_command_id != command_id_)
1338 {
1339 abort();
1340 return;
1341 }
1342 if (axis_ref->set_position(resolved_position_) != rt::ErrorCode::ok)
1343 {
1345 return;
1346 }
1347 outputs.done = true;
1348 outputs.busy = false;
1349 outputs.active = false;
1350 phase_ = Phase::idle;
1351 terminal_low_cycle_ = !execute;
1352 }
1353
1354 void activate()
1355 {
1356 const rt::Result<std::uint32_t> armed =
1357 axis_ref->arm_touch_probe(reference_signal_.input, false, 0.0, 0.0);
1358 if (!armed)
1359 {
1360 fail(armed.error());
1361 return;
1362 }
1363 axis_ref->clear_homed();
1364 probe_id_ = armed.value();
1365 start_position_ = axis_ref->snapshot().command_position;
1366 elapsed_cycles_ = 0;
1367 outputs.active = true;
1368 phase_ = Phase::first_mark;
1369 }
1370
1371 void release_probe()
1372 {
1373 if (axis_ref != nullptr && probe_id_ != 0 &&
1374 axis_ref->probe_command_id(reference_signal_.input) == probe_id_)
1375 {
1376 axis_ref->abort_trigger(reference_signal_.input);
1377 }
1378 probe_id_ = 0;
1379 }
1380
1381 void fail(rt::ErrorCode error)
1382 {
1383 release_probe();
1384 clear(outputs);
1385 outputs.error = true;
1386 outputs.error_id = error;
1387 phase_ = Phase::idle;
1388 terminal_low_cycle_ = !execute;
1389 }
1390
1391 void abort()
1392 {
1393 release_probe();
1394 clear(outputs);
1395 outputs.command_aborted = true;
1396 phase_ = Phase::idle;
1397 terminal_low_cycle_ = !execute;
1398 }
1399
1400 Phase phase_ = Phase::idle;
1401 bool last_execute_ = false;
1402 bool terminal_low_cycle_ = false;
1403 std::uint32_t command_id_ = 0;
1404 std::uint32_t probe_id_ = 0;
1405 double start_position_ = 0.0;
1406 double first_mark_ = 0.0;
1407 double resolved_position_ = 0.0;
1408 std::int64_t elapsed_cycles_ = 0;
1409 const DistanceCodeMap *code_map_ = nullptr;
1410 axis::ReferenceSignalRef reference_signal_{0};
1411};
1412
1414{
1415 public:
1418 bool execute = false;
1420
1421 rt::ErrorCode bind_source(const double *source)
1422 {
1423 if (started_)
1425 if (source == nullptr)
1427 source_ = source;
1428 return rt::ErrorCode::ok;
1429 }
1430
1431 void call()
1432 {
1433 const bool rising = execute && !last_execute_;
1434 last_execute_ = execute;
1435 if (!execute)
1436 {
1437 clear(outputs);
1438 return;
1439 }
1440 if (!rising)
1441 return;
1442 started_ = true;
1443 clear(outputs);
1444 if (axis_ref == nullptr || source_ == nullptr || !std::isfinite(*source_))
1445 {
1446 outputs.error = true;
1448 return;
1449 }
1451 {
1452 outputs.error = true;
1454 return;
1455 }
1456 const rt::ErrorCode result = axis_ref->home_direct(*source_);
1457 if (result != rt::ErrorCode::ok)
1458 {
1459 outputs.error = true;
1460 outputs.error_id = result;
1461 return;
1462 }
1463 outputs.done = true;
1464 }
1465
1466 private:
1467 const double *source_ = nullptr;
1468 bool last_execute_ = false;
1469 bool started_ = false;
1470};
1471
1473{
1474 public:
1475 // KB-072: one passive owner observes motion without replacing it.
1477 bool execute = false;
1480 double set_position = 0.0;
1481 std::int64_t time_limit = 0;
1482 double distance_limit = 0.0;
1484
1485 protected:
1487 {
1488 const bool falling = !execute && last_execute_;
1489 const bool rising = execute && !last_execute_;
1490 last_execute_ = execute;
1491 if (falling && owner_id_ == 0 && (outputs.done || outputs.command_aborted || outputs.error))
1492 {
1493 clear(outputs);
1494 }
1495 if (!execute && terminal_low_cycle_)
1496 {
1497 clear(outputs);
1498 terminal_low_cycle_ = false;
1499 }
1500 if (rising)
1501 {
1502 clear(outputs);
1503 terminal_low_cycle_ = false;
1504 }
1505 return rising;
1506 }
1507
1508 void start()
1509 {
1510 clear(outputs);
1511 terminal_low_cycle_ = false;
1512 if (axis_ref == nullptr || !std::isfinite(set_position) || time_limit < 0 ||
1513 !std::isfinite(distance_limit) || distance_limit < 0.0 ||
1516 {
1519 return;
1520 }
1521 const rt::Result<std::uint32_t> begun =
1522 axis_ref->begin_passive_homing(reference_signal.input);
1523 if (!begun)
1524 {
1525 fail(begun.error());
1526 return;
1527 }
1528 owner_id_ = begun.value();
1529 start_position_ = axis_ref->snapshot().actual_position;
1530 elapsed_cycles_ = 0;
1531 outputs.busy = true;
1532 outputs.active = true;
1533 }
1534
1535 void observe(bool condition, bool use_current_position = false)
1536 {
1537 if (axis_ref == nullptr || owner_id_ == 0)
1538 return;
1539 if (axis_ref->passive_homing_aborted_id() == owner_id_)
1540 {
1541 clear(outputs);
1542 outputs.command_aborted = true;
1543 owner_id_ = 0;
1544 terminal_low_cycle_ = !execute;
1545 return;
1546 }
1547 ++elapsed_cycles_;
1548 const axis::AxisSnapshot &snapshot = axis_ref->snapshot();
1549 if ((time_limit > 0 && elapsed_cycles_ > time_limit) ||
1550 (distance_limit > 0.0 &&
1551 std::fabs(snapshot.actual_position - start_position_) > distance_limit))
1552 {
1554 axis_ref->abort_passive_homing();
1555 owner_id_ = 0;
1556 return;
1557 }
1558 if (!condition ||
1559 (!use_current_position && !axis_ref->probe_captured(reference_signal.input)))
1560 return;
1561 const double captured = use_current_position
1562 ? snapshot.actual_position
1563 : axis_ref->probe_recorded_position(reference_signal.input);
1564 const double delta = set_position - captured;
1565 const rt::ErrorCode shifted = axis_ref->set_position_on_the_fly(delta);
1566 if (shifted != rt::ErrorCode::ok ||
1567 axis_ref->finish_passive_homing(owner_id_) != rt::ErrorCode::ok)
1568 {
1569 if (shifted != rt::ErrorCode::ok)
1570 axis_ref->trigger_error();
1572 owner_id_ = 0;
1573 return;
1574 }
1575 outputs.done = true;
1576 outputs.busy = false;
1577 outputs.active = false;
1578 owner_id_ = 0;
1579 terminal_low_cycle_ = !execute;
1580 }
1581
1583 {
1584 clear(outputs);
1585 outputs.error = true;
1586 outputs.error_id = error;
1587 terminal_low_cycle_ = !execute;
1588 }
1589
1590 std::uint32_t owner_id_ = 0;
1591
1592 private:
1593 bool last_execute_ = false;
1594 bool terminal_low_cycle_ = false;
1595 double start_position_ = 0.0;
1596 std::int64_t elapsed_cycles_ = 0;
1597};
1598
1600{
1601 public:
1603
1604 void call()
1605 {
1606 if (rising_edge())
1607 {
1608 if (!valid_switch_mode())
1609 {
1611 return;
1612 }
1613 const rt::Result<bool> level =
1615 : axis_ref->digital_input(reference_signal.input);
1616 if (!level)
1617 {
1618 fail(level.error());
1619 return;
1620 }
1621 last_level_ = level.value();
1622 start();
1623 }
1624 if (axis_ref == nullptr || owner_id_ == 0)
1625 return;
1626 const rt::Result<bool> level = axis_ref->digital_input(reference_signal.input);
1627 if (!level)
1628 {
1629 fail(level.error());
1630 return;
1631 }
1632 const bool rising = level.value() && !last_level_;
1633 const bool falling = !level.value() && last_level_;
1634 last_level_ = level.value();
1635 const double actual_velocity = axis_ref->snapshot().actual_velocity;
1638 actual_velocity == 0.0)
1639 {
1640 axis_ref->abort_passive_homing();
1641 owner_id_ = 0;
1643 return;
1644 }
1645 bool condition = false;
1646 switch (switch_mode)
1647 {
1649 condition = level.value();
1650 break;
1652 condition = !level.value();
1653 break;
1655 condition = rising;
1656 break;
1658 condition = falling;
1659 break;
1661 condition = actual_velocity > 0.0 ? rising : falling;
1662 break;
1664 condition = actual_velocity < 0.0 ? rising : falling;
1665 break;
1666 }
1667 observe(condition, true);
1668 }
1669
1670 private:
1671 bool valid_switch_mode() const
1672 {
1673 switch (switch_mode)
1674 {
1681 return true;
1682 }
1683 return false;
1684 }
1685
1686 bool last_level_ = false;
1687};
1688
1690{
1691 public:
1692 void call()
1693 {
1694 if (rising_edge())
1695 start();
1696 observe(true);
1697 }
1698};
1699
1701{
1702 public:
1704 bool execute = false;
1706
1707 void call()
1708 {
1709 const bool rising = execute && !last_execute_;
1710 last_execute_ = execute;
1711 if (!execute)
1712 {
1713 clear(outputs);
1714 return;
1715 }
1716 if (!rising)
1717 return;
1718 clear(outputs);
1719 if (axis_ref == nullptr)
1720 {
1721 outputs.error = true;
1723 return;
1724 }
1725 const rt::Result<std::uint32_t> aborted = axis_ref->abort_passive_homing();
1726 if (!aborted)
1727 {
1728 outputs.error = true;
1729 outputs.error_id = aborted.error();
1730 return;
1731 }
1732 outputs.done = true;
1733 }
1734
1735 private:
1736 bool last_execute_ = false;
1737};
1738
1739} // namespace plcopen::core::fb
static constexpr std::size_t DigitalInputCount
rt::ErrorCode trigger_error(rt::ErrorCode code=rt::ErrorCode::precondition_failed)
const MotionLimits & motion_limits() const
const AxisSnapshot & snapshot() const
rt::ErrorCode homing_step_precondition() const
rt::ErrorCode abort_trigger(std::size_t input)
rt::Result< std::uint32_t > submit(AxisCommand command)
std::uint32_t probe_command_id(std::size_t input) const
bool rising_edge(bool retain_active_on_falling=false)
void accept(rt::Result< std::uint32_t > accepted)
rt::ErrorCode bind_source(const double *source)
axis::ReferenceSignalRef reference_signal
axis::HomeDirection direction
rt::ErrorCode bind_reference_signal(axis::ReferenceSignalRef reference)
rt::ErrorCode bind_distance_code_map(const DistanceCodeMap *map)
axis::ReferenceSignalRef reference_signal
rt::Result< bool > read_signal() const
bool validate_inputs(bool require_reference) const
void configure_reference(axis::ReferenceSignalRef reference, axis::SwitchMode mode, bool use_probe)
bool set_direction_sign(bool initial_level, bool allow_switch_modes)
void set_reverse_on_limit(bool enabled)
void configure_limit(axis::SwitchMode mode)
void fail(rt::ErrorCode error)
void observe(bool condition, bool use_current_position=false)
axis::ReferenceSignalRef reference_signal
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
void clear(MotionOutputs &outputs)
DistanceCodeEntry entries[Capacity]
static constexpr std::size_t Capacity