plcopen 0.22.0
载入中...
搜索中...
未找到
probe.h
浏览该文件的文档.
1#pragma once
2
3#include <cstddef>
4#include <cstdint>
5
6#include "axis/state.h"
7#include "fb/motion.h"
8#include "rt/error.h"
9
10namespace plcopen::core::fb
11{
12
13// MC_TouchProbe: arms one trigger input and reports the position captured on
14// its rising edge. window_only gates the capture to [first, last]. The trigger
15// levels are fed by adapters through AxisModel::set_digital_input.
17{
18public:
20 std::size_t trigger_input = 0;
21 bool window_only = false;
22 double first_position = 0.0;
23 double last_position = 0.0;
24 bool execute = false;
26 double recorded_position = 0.0;
27
28 void call()
29 {
30 const bool rising = execute && !last_execute_;
31 const bool falling = !execute && last_execute_;
32 last_execute_ = execute;
33 if(rising) {
36 tracked_command_id_ = 0;
37 terminal_low_cycle_ = false;
38 } else if(!execute && terminal_low_cycle_) {
41 tracked_command_id_ = 0;
42 terminal_low_cycle_ = false;
43 return;
44 } else if(falling &&
45 (outputs.done || outputs.command_aborted || outputs.error)) {
48 tracked_command_id_ = 0;
49 }
50 if(rising) {
51 arm();
52 return;
53 }
54 observe();
55 }
56
57private:
58 void arm()
59 {
62 tracked_command_id_ = 0;
63 if(axis_ref == nullptr) {
64 outputs.error = true;
66 return;
67 }
68 const rt::Result<std::uint32_t> armed =
70 if(!armed) {
71 outputs.error = true;
72 outputs.error_id = armed.error();
73 return;
74 }
75 tracked_command_id_ = armed.value();
76 outputs.command_id = tracked_command_id_;
78 outputs.busy = true;
79 outputs.active = true;
80 }
81
82 void observe()
83 {
84 if(tracked_command_id_ == 0 || axis_ref == nullptr || outputs.done) {
85 return;
86 }
87 if(axis_ref->probe_command_id(trigger_input) != tracked_command_id_) {
88 outputs.command_aborted = true;
89 outputs.busy = false;
90 outputs.active = false;
91 tracked_command_id_ = 0;
92 return;
93 }
94 if(axis_ref->probe_captured(trigger_input)) {
95 recorded_position = axis_ref->probe_recorded_position(trigger_input);
96 outputs.done = true;
97 outputs.busy = false;
98 outputs.active = false;
99 terminal_low_cycle_ = !execute;
100 return;
101 }
102 outputs.busy = true;
103 outputs.active = true;
104 }
105
106 std::uint32_t tracked_command_id_ = 0;
107 bool last_execute_ = false;
108 bool terminal_low_cycle_ = false;
109};
110
111// MC_AbortTrigger: disarms the probe on the matching input; disarming an idle
112// input is not an error.
114{
115public:
117 std::size_t trigger_input = 0;
118 bool execute = false;
120
121 void call()
122 {
123 const bool rising = execute && !last_execute_;
124 last_execute_ = execute;
125 if(!execute) {
126 clear(outputs);
127 return;
128 }
129 if(!rising) {
130 return;
131 }
132 clear(outputs);
133 const rt::ErrorCode aborted = axis_ref == nullptr
135 : axis_ref->abort_trigger(trigger_input);
137 outputs.error = true;
138 outputs.error_id = aborted;
139 return;
140 }
141 outputs.done = true;
142 }
143
144private:
145 bool last_execute_ = false;
146};
147
148// MC_EmergencyStop (project extension): drives the axis into errorstop; the
149// axis recovers through MC_Reset.
151{
152public:
154 bool execute = false;
156
157 void call()
158 {
159 const bool rising = execute && !last_execute_;
160 last_execute_ = execute;
161 if(!execute) {
162 clear(outputs);
163 return;
164 }
165 if(!rising) {
166 return;
167 }
168 clear(outputs);
169 if(axis_ref == nullptr) {
170 outputs.error = true;
172 return;
173 }
174 const rt::ErrorCode stopped = axis_ref->trigger_error();
175 if(stopped != rt::ErrorCode::ok) {
176 outputs.error = true;
177 outputs.error_id = stopped;
178 return;
179 }
180 outputs.done = true;
181 }
182
183private:
184 bool last_execute_ = false;
185};
186
187} // namespace plcopen::core::fb
rt::Result< std::uint32_t > arm_touch_probe(std::size_t input, bool window_only, double first_position, double last_position)
constexpr const T & value() const
定义 error.h:49
constexpr ErrorCode error() const
定义 error.h:44
void clear(MotionOutputs &outputs)