plcopen 0.22.0
载入中...
搜索中...
未找到
sync.h
浏览该文件的文档.
1#pragma once
2
3#include <cmath>
4#include <cstdint>
5
6#include "axis/group.h"
7#include "axis/state.h"
8#include "exec/sync.h"
9#include "fb/motion.h"
10#include "rt/error.h"
11
12namespace plcopen::core::fb
13{
14
15// MC_GearIn / MC_GearInPos / MC_CamIn require master and slave to be members
16// of the same enabled group (KB boundary carried over from the v0.x tests).
17inline bool same_enabled_group(const axis::AxisModel &master, const axis::AxisModel &slave)
18{
19 void *owner = slave.group_owner();
20 if(owner == nullptr || owner != master.group_owner()) {
21 return false;
22 }
23 const auto *group = static_cast<const axis::AxisGroup *>(owner);
24 return group->status() != axis::GroupStatus::disabled;
25}
26
27// Shared facade state for master/slave synchronization commands. in_sync maps
28// to the v0.x InGear/InSync outputs; start_sync pulses one cycle at approach
29// start and at sync entry.
31{
32public:
35 bool execute = false;
36 bool continuous_update = false;
38 bool in_sync = false;
39 bool start_sync = false;
40
41protected:
43 {
44 const bool rising = execute && !last_execute_;
45 const bool falling = !execute && last_execute_;
46 last_execute_ = execute;
47 if(rising) {
49 in_sync = false;
50 start_sync = false;
52 last_phase_ = axis::SyncPhase::idle;
53 continuous_update_enabled_ = continuous_update;
54 terminal_low_cycle_ = false;
55 } else if(!execute && terminal_low_cycle_) {
58 terminal_low_cycle_ = false;
59 } else if(falling &&
60 (outputs.done || outputs.command_aborted || outputs.error)) {
63 }
64 return rising;
65 }
66
68 {
69 outputs.command_accepted = false;
70 outputs.command_aborted = false;
71 outputs.error = false;
72 outputs.error_id = rt::ErrorCode::ok;
73 in_sync = false;
74 start_sync = false;
75 last_phase_ = axis::SyncPhase::idle;
76 if(!accepted) {
77 outputs.error = true;
78 outputs.error_id = accepted.error();
79 outputs.busy = false;
80 outputs.active = false;
82 return;
83 }
84 tracked_command_id_ = accepted.value();
85 outputs.command_id = tracked_command_id_;
86 outputs.command_accepted = true;
87 outputs.busy = true;
88 outputs.active = true;
89 }
90
91 void observe_sync(bool pulse_approach = true)
92 {
93 start_sync = false;
94 if(tracked_command_id_ == 0 || slave_ref == nullptr) {
95 return;
96 }
97 const rt::ErrorCode command_error = slave_ref->command_error(tracked_command_id_);
98 if(command_error != rt::ErrorCode::ok) {
99 outputs.error = true;
100 outputs.error_id = command_error;
101 outputs.command_aborted = false;
102 outputs.busy = false;
103 outputs.active = false;
104 in_sync = false;
105 terminal_low_cycle_ = !execute;
106 return;
107 }
108 if(slave_ref->sync_command_id() != tracked_command_id_) {
109 outputs.command_aborted = true;
110 outputs.busy = false;
111 outputs.active = false;
112 in_sync = false;
113 terminal_low_cycle_ = !execute;
114 return;
115 }
116
117 const axis::SyncPhase phase = slave_ref->sync_phase();
118 outputs.busy = true;
119 outputs.active = phase != axis::SyncPhase::queued;
121 start_sync = phase != last_phase_ &&
122 (phase == axis::SyncPhase::engaged ||
123 (pulse_approach && phase == axis::SyncPhase::approaching));
124 last_phase_ = phase;
125 }
126
127 std::uint32_t tracked_command_id_ = 0;
128
130 {
131 return continuous_update_enabled_;
132 }
133
134private:
135 bool last_execute_ = false;
136 bool continuous_update_enabled_ = false;
137 bool terminal_low_cycle_ = false;
139};
140
142{
143public:
145 double ratio_numerator = 1.0;
146 double ratio_denominator = 1.0;
147 double acceleration = 0.0;
148 double deceleration = 0.0;
149 double jerk = 0.0;
151
152 void call()
153 {
154 if(rising_edge()) {
155 if(group_ref == nullptr || slave_ref == nullptr) {
157 } else {
158 accept(group_ref->sync_axis_to_group(
161 }
162 }
163 observe_sync();
164 }
165};
166
168{
169public:
170 double ratio_numerator = 1.0;
171 double ratio_denominator = 1.0;
173 double acceleration = 0.0;
174 double deceleration = 0.0;
175 double jerk = 0.0;
177 bool in_gear = false;
178
179 void call()
180 {
181 if(rising_edge()) {
183 } else {
185 }
186 observe_sync(false);
188 }
189
190protected:
191 void submit_gear(const axis::GearInCommand &command)
192 {
193 if(master_ref == nullptr || slave_ref == nullptr) {
195 return;
196 }
199 return;
200 }
201 accept(slave_ref->gear_in(command));
202 }
203
205 {
207 slave_ref == nullptr) {
208 return;
209 }
210 const rt::ErrorCode updated = slave_ref->gear_update(ratio_numerator, ratio_denominator);
211 if(updated != rt::ErrorCode::ok) {
212 outputs.error = true;
213 outputs.error_id = updated;
214 }
215 }
216
218 {
219 axis::GearInCommand command{};
220 command.master = master_ref;
221 command.ratio_numerator = ratio_numerator;
222 command.ratio_denominator = ratio_denominator;
223 command.source = master_value_source;
224 command.acceleration = acceleration;
225 command.deceleration = deceleration;
226 command.jerk = jerk;
227 command.buffer_mode = buffer_mode;
228 return command;
229 }
230};
231
232class FbGearInPos : public FbGearIn
233{
234public:
239 double velocity = 0.0;
240
241 void call()
242 {
243 if(rising_edge()) {
245 command.position_sync = true;
246 command.master_sync_position = master_sync_position;
247 command.slave_sync_position = slave_sync_position;
248 command.master_start_distance = master_start_distance;
249 command.sync_mode = sync_mode;
250 command.approach_velocity = velocity;
251 submit_gear(command);
252 } else {
254 }
255 observe_sync();
257 }
258};
259
261{
262public:
264 bool execute = false;
265 bool done = false;
266 bool error = false;
268
269 void call()
270 {
271 if(!rising_edge()) {
272 return;
273 }
274 if(axis_ref == nullptr) {
276 return;
277 }
278 const rt::ErrorCode detached = axis_ref->sync_out();
279 if(detached != rt::ErrorCode::ok) {
280 fail(detached);
281 return;
282 }
283 done = true;
284 outputs.done = true;
285 }
286
287private:
288 bool rising_edge()
289 {
290 const bool rising = execute && !last_execute_;
291 last_execute_ = execute;
292 if(!execute) {
293 clear(outputs);
294 done = false;
295 error = false;
296 }
297 return rising;
298 }
299
300 void fail(rt::ErrorCode code)
301 {
302 error = true;
303 outputs.error = true;
304 outputs.error_id = code;
305 done = false;
306 outputs.done = false;
307 }
308
309 bool last_execute_ = false;
310};
311
313
315{
316public:
320 bool periodic = false;
321 bool master_absolute = true;
322 bool slave_absolute = true;
324 bool execute = false;
325 bool done = false;
326 bool busy = false;
327 bool error = false;
329 std::uint32_t cam_table_id = 0;
331
332 void call()
333 {
334 const bool rising = execute && !last_execute_;
335 last_execute_ = execute;
336 if(!execute) {
337 done = false;
338 busy = false;
339 error = false;
341 return;
342 }
343 if(!rising) {
344 return;
345 }
346 busy = false;
347 if(master_ref == nullptr || slave_ref == nullptr || !cam_table.valid()) {
348 error = true;
350 done = false;
352 return;
353 }
355 error = true;
357 done = false;
359 return;
360 }
362 error = true;
364 done = false;
366 return;
367 }
368 const rt::Result<std::uint32_t> selected =
369 slave_ref->select_cam_table(master_ref, cam_table, periodic,
372 if(!selected) {
373 error = true;
374 error_id = selected.error();
375 done = false;
377 return;
378 }
380 cam_table_selected.periodic = periodic;
381 cam_table_id = selected.value();
382 done = true;
383 error = false;
385 }
386
387private:
388 bool last_execute_ = false;
389};
390
391class FbCamIn : public SyncExecuteFb
392{
393public:
395 double master_offset = 0.0;
396 double master_scaling = 1.0;
397 double slave_offset = 0.0;
398 double slave_scaling = 1.0;
401 double velocity = 0.0;
404 std::uint32_t cam_table_id = 0;
406 bool end_of_profile = false;
407
408 void call()
409 {
410 if(rising_edge()) {
411 reset_profile_tracking();
412 submit();
414 slave_ref != nullptr) {
415 const double effective_master_offset = master_offset + selected_master_origin_;
416 const double effective_slave_offset =
417 slave_offset + selected_slave_origin_ + relative_slave_bias_;
418 const rt::ErrorCode updated = slave_ref->cam_update(effective_master_offset,
420 effective_slave_offset,
422 if(updated != rt::ErrorCode::ok) {
423 outputs.error = true;
424 outputs.error_id = updated;
425 } else {
426 profile_master_offset_ = effective_master_offset;
427 profile_master_scaling_ = master_scaling;
428 }
429 }
430 observe_sync();
431 update_end_of_profile();
432 }
433
434private:
435 void submit()
436 {
437 if(master_ref == nullptr || slave_ref == nullptr) {
439 return;
440 }
443 return;
444 }
447 return;
448 }
449 axis::CamInCommand command{};
450 command.master = master_ref;
451 command.table = cam_table;
452 command.master_offset = master_offset;
453 command.master_scaling = master_scaling;
454 command.slave_offset = slave_offset;
455 command.slave_scaling = slave_scaling;
456 selected_master_origin_ = 0.0;
457 selected_slave_origin_ = 0.0;
458 relative_slave_bias_ = 0.0;
459 if(cam_table_id != 0) {
460 const rt::Result<axis::CamTableSelection> selected =
461 slave_ref->cam_table_selection(cam_table_id);
462 if(!selected || selected.value().master != master_ref) {
464 return;
465 }
466 const axis::CamTableSelection selection = selected.value();
467 command.table = selection.table;
468 if(!selection.master_absolute) {
469 selected_master_origin_ = selection.master_origin;
470 command.master_offset += selected_master_origin_;
471 }
472 if(!selection.slave_absolute) {
473 selected_slave_origin_ = selection.slave_origin;
474 command.slave_offset += selected_slave_origin_;
475 }
476 }
478 if(!command.table.valid() || !std::isfinite(command.master_scaling) ||
479 command.master_scaling == 0.0 || !std::isfinite(command.slave_scaling)) {
481 return;
482 }
483 const axis::AxisSnapshot &master_snapshot = master_ref->snapshot();
484 const double master_position =
486 ? master_snapshot.actual_position
487 : master_snapshot.command_position;
488 const double table_input =
489 (master_position - command.master_offset) / command.master_scaling;
490 const rt::Result<double> sampled = command.table.sample(table_input);
491 if(!sampled) {
493 return;
494 }
495 command.slave_offset = slave_ref->snapshot().command_position -
496 command.slave_scaling * sampled.value();
497 relative_slave_bias_ =
498 command.slave_offset - slave_offset - selected_slave_origin_;
499 }
501 command.buffer_mode = buffer_mode;
502 command.master_sync_position = master_sync_position;
503 command.master_start_distance = master_start_distance;
504 command.approach_velocity = velocity;
505 const rt::Result<std::uint32_t> accepted = slave_ref->cam_in(command);
506 if(accepted) {
507 profile_table_ = command.table;
508 profile_master_offset_ = command.master_offset;
509 profile_master_scaling_ = command.master_scaling;
510 profile_source_ = command.source;
511 }
512 accept(accepted);
513 }
514
515 void reset_profile_tracking()
516 {
517 end_of_profile = false;
518 profile_period_valid_ = false;
519 profile_table_ = {};
520 }
521
522 void update_end_of_profile()
523 {
524 end_of_profile = false;
525 if(!in_sync || master_ref == nullptr || !profile_table_.valid() ||
526 profile_master_scaling_ == 0.0) {
527 profile_period_valid_ = false;
528 return;
529 }
530 const axis::AxisSnapshot &snapshot = master_ref->snapshot();
531 const double master_position = profile_source_ == axis::MasterValueSource::actual
532 ? snapshot.actual_position
533 : snapshot.command_position;
534 const double input =
535 (master_position - profile_master_offset_) / profile_master_scaling_;
536 const double first = profile_table_.points[0].master;
537 const double last = profile_table_.points[profile_table_.size - 1].master;
538 if(!profile_table_.periodic) {
539 end_of_profile = input < first || input > last;
540 return;
541 }
542 const double span = last - first;
543 const std::int64_t period = static_cast<std::int64_t>(std::floor((input - first) / span));
544 if(profile_period_valid_) {
545 end_of_profile = period != profile_period_;
546 }
547 profile_period_ = period;
548 profile_period_valid_ = true;
549 }
550
551 exec::CamTableView profile_table_{};
552 double profile_master_offset_ = 0.0;
553 double profile_master_scaling_ = 1.0;
555 double selected_master_origin_ = 0.0;
556 double selected_slave_origin_ = 0.0;
557 double relative_slave_bias_ = 0.0;
558 std::int64_t profile_period_ = 0;
559 bool profile_period_valid_ = false;
560};
561
563{
564public:
568 double ratio_numerator_m1 = 1.0;
570 double ratio_numerator_m2 = 1.0;
575
576 void call()
577 {
578 if(rising_edge()) {
579 submit();
581 slave_ref != nullptr) {
582 const rt::ErrorCode updated = slave_ref->combine_update(combine_mode,
587 if(updated != rt::ErrorCode::ok) {
588 outputs.error = true;
589 outputs.error_id = updated;
590 }
591 }
592 observe_sync();
593 }
594
595private:
596 void submit()
597 {
598 if(master1_ref == nullptr || master2_ref == nullptr || slave_ref == nullptr) {
600 return;
601 }
602 axis::CombineAxesCommand command{};
603 command.master1 = master1_ref;
604 command.master2 = master2_ref;
605 command.mode = combine_mode;
606 command.ratio_numerator_m1 = ratio_numerator_m1;
607 command.ratio_denominator_m1 = ratio_denominator_m1;
608 command.ratio_numerator_m2 = ratio_numerator_m2;
609 command.ratio_denominator_m2 = ratio_denominator_m2;
610 command.source_m1 = master_value_source_m1;
611 command.source_m2 = master_value_source_m2;
612 command.buffer_mode = buffer_mode;
613 accept(slave_ref->combine_in(command));
614 }
615};
616
617// Phase inputs are latched on the rising edge; changing them while the
618// transition runs has no effect (v0.x latching boundary).
620{
621public:
624 double phase_shift = 0.0;
625 double velocity = 0.0;
626 double acceleration = 0.0;
627 double deceleration = 0.0;
628 double jerk = 0.0;
630 bool execute = false;
634
635protected:
636 explicit PhasingFb(bool relative)
637 : relative_(relative)
638 {
639 }
640
641 void step()
642 {
643 const bool rising = execute && !last_execute_;
644 const bool falling = !execute && last_execute_;
645 last_execute_ = execute;
646 if(rising) {
647 clear(outputs);
648 started_ = false;
649 tracked_command_id_ = 0;
652 terminal_low_cycle_ = false;
653 } else if(!execute && terminal_low_cycle_) {
654 clear(outputs);
655 started_ = false;
656 tracked_command_id_ = 0;
657 terminal_low_cycle_ = false;
658 return;
659 } else if(falling &&
660 (outputs.done || outputs.command_aborted || outputs.error)) {
661 clear(outputs);
662 started_ = false;
663 tracked_command_id_ = 0;
664 }
665 if(rising) {
666 start();
667 return;
668 }
669 if(started_ && slave_ref != nullptr) {
670 absolute_phase_shift = slave_ref->gear_phase_offset();
671 covered_phase_shift = slave_ref->phasing_covered_shift(tracked_command_id_);
672 const axis::PhasingCommandState state =
673 slave_ref->phasing_command_state(tracked_command_id_);
678 outputs.done = true;
679 outputs.busy = false;
680 outputs.active = false;
681 terminal_low_cycle_ = !execute;
682 } else if(state == axis::PhasingCommandState::aborted) {
683 outputs.command_aborted = true;
684 outputs.busy = false;
685 outputs.active = false;
686 terminal_low_cycle_ = !execute;
687 }
688 }
689 }
690
691private:
692 void start()
693 {
694 clear(outputs);
695 started_ = false;
696 if(master_ref == nullptr || slave_ref == nullptr || master_ref == slave_ref ||
698 outputs.error = true;
700 return;
701 }
702 axis::PhasingCommand command{};
703 command.phase_shift = phase_shift;
704 command.velocity = velocity;
705 command.acceleration = acceleration;
706 command.deceleration = deceleration;
707 command.jerk = jerk;
708 command.buffer_mode = buffer_mode;
709 command.relative = relative_;
710 const rt::Result<std::uint32_t> requested = slave_ref->submit_phasing(command);
711 if(!requested) {
712 outputs.error = true;
713 outputs.error_id = requested.error();
714 return;
715 }
716 started_ = true;
717 tracked_command_id_ = requested.value();
718 outputs.command_id = tracked_command_id_;
722 const axis::PhasingCommandState state =
723 slave_ref->phasing_command_state(tracked_command_id_);
728 }
729
730 bool relative_ = false;
731 bool last_execute_ = false;
732 bool started_ = false;
733 bool terminal_low_cycle_ = false;
734 std::uint32_t tracked_command_id_ = 0;
735};
736
738{
739public:
741 : PhasingFb(false)
742 {
743 }
744
745 void call()
746 {
747 step();
748 }
749};
750
752{
753public:
755 : PhasingFb(true)
756 {
757 }
758
759 void call()
760 {
761 step();
762 }
763};
764
765} // namespace plcopen::core::fb
double phasing_covered_shift(std::uint32_t command_id) const
bool gear_engaged_with(const AxisModel *master) const
PhasingCommandState phasing_command_state(std::uint32_t command_id) const
rt::Result< std::uint32_t > combine_in(const CombineAxesCommand &command)
rt::Result< std::uint32_t > submit_phasing(PhasingCommand command)
exec::CamTableView cam_table
定义 sync.h:394
axis::CamStartMode start_mode
定义 sync.h:402
axis::MasterValueSource master_value_source
定义 sync.h:403
axis::BufferMode buffer_mode
定义 sync.h:405
exec::CamTableView cam_table_selected
定义 sync.h:330
axis::ExecutionMode execution_mode
定义 sync.h:323
axis::MasterValueSource master_value_source_m2
定义 sync.h:573
axis::MasterValueSource master_value_source_m1
定义 sync.h:572
void submit_gear(const axis::GearInCommand &command)
定义 sync.h:191
axis::GearInCommand make_command() const
定义 sync.h:217
axis::BufferMode buffer_mode
定义 sync.h:176
axis::MasterValueSource master_value_source
定义 sync.h:172
axis::AxisModel * axis_ref
定义 sync.h:263
axis::AxisModel * master_ref
定义 sync.h:622
axis::AxisModel * slave_ref
定义 sync.h:623
axis::BufferMode buffer_mode
定义 sync.h:629
void accept(rt::Result< std::uint32_t > accepted)
定义 sync.h:67
void observe_sync(bool pulse_approach=true)
定义 sync.h:91
static constexpr Result failure(ErrorCode error)
定义 error.h:29
bool same_enabled_group(const axis::AxisModel &master, const axis::AxisModel &slave)
定义 sync.h:17
void clear(MotionOutputs &outputs)
FbGearOut FbCamOut
定义 sync.h:312