plcopen 0.22.0
载入中...
搜索中...
未找到
io.h
浏览该文件的文档.
1#pragma once
2
3#include <cmath>
4#include <cstddef>
5#include <cstdint>
6
7#include "axis/state.h"
8#include "fb/base.h"
9#include "fb/motion.h"
10#include "rt/error.h"
11#include "rt/static_vector.h"
12
13namespace plcopen::core::fb
14{
15
16// Digital IO facades over the fixed AxisModel IO banks (the v0.x Servo
17// extension channels). Unsupported channels report rt::ErrorCode::unsupported.
18
20{
21public:
23 std::size_t input_number = 0;
24 bool value = false;
25
26 void call()
27 {
28 if(!begin_enable()) {
29 if(!enable) value = false;
30 return;
31 }
32 if(axis_ref == nullptr) {
33 value = false;
35 return;
36 }
37 const rt::Result<bool> read = axis_ref->digital_input(input_number);
38 if(!read) {
39 value = false;
40 fail_enable(read.error());
41 return;
42 }
43 value = read.value();
45 }
46};
47
49{
50public:
52 std::size_t output_number = 0;
53 bool value = false;
54
55 void call()
56 {
57 if(!begin_enable()) {
58 if(!enable) value = false;
59 return;
60 }
61 if(axis_ref == nullptr) {
62 value = false;
64 return;
65 }
66 const rt::Result<bool> read = axis_ref->digital_output(output_number);
67 if(!read) {
68 value = false;
69 fail_enable(read.error());
70 return;
71 }
72 value = read.value();
74 }
75};
76
103
105{
106 std::size_t track_number = 1;
107 double on_position = 0.0;
108 double off_position = 0.0;
109 double period = 0.0;
121 std::int64_t duration_ns = 0;
122};
123
125{
126 std::int64_t on_compensation_ns = 0;
127 std::int64_t off_compensation_ns = 0;
128};
129
131{
132 const CamTrackOption *data = nullptr;
133 std::size_t size = 0;
134};
135
137{
138 bool *data = nullptr;
139 std::size_t size = 0;
140};
141
143{
144 const CamSwitchAction *data = nullptr;
145 std::size_t size = 0;
146};
147
148template <std::size_t Capacity> class CamSwitchTable
149{
150public:
152 {
153 return actions_.push_back(action);
154 }
155
156 void clear()
157 {
158 actions_.clear();
159 }
160
162 {
163 return {actions_.data(), actions_.size()};
164 }
165
166private:
168};
169
171{
172public:
177 bool enable = false;
178 std::uint32_t enable_mask = 0xFFFFFFFFU;
180 bool in_operation = false;
181 bool busy = false;
182 bool error = false;
184
185 void call(std::int64_t task_period_ns = 1000000)
186 {
187 busy = false;
188 if(!enable) {
189 release_outputs();
190 reset_actions();
191 in_operation = false;
192 error = false;
194 return;
195 }
196 if(axis_ref == nullptr || switches.data == nullptr || switches.size == 0 ||
197 switches.size > MaxActions || task_period_ns <= 0 ||
201 return;
202 }
203
206 const axis::AxisSnapshot &snapshot = axis_ref->snapshot();
207 const double position = value_source == axis::MasterValueSource::actual
208 ? snapshot.actual_position
209 : snapshot.command_position;
210 const double velocity = value_source == axis::MasterValueSource::actual
211 ? snapshot.actual_velocity
212 : snapshot.command_velocity;
213 const bool new_table = controlled_axis_ != axis_ref || active_table_ != switches.data ||
214 active_table_size_ != switches.size;
215 if(new_table) {
216 reset_actions();
217 }
218 for(std::size_t index = 0; index < switches.size; ++index) {
219 const CamSwitchAction &action = switches.data[index];
220 if(action.track_number == 0 ||
221 action.track_number > axis::AxisModel::DigitalOutputCount) {
223 return;
224 }
225 if(!std::isfinite(action.on_position) || !std::isfinite(action.off_position) ||
226 !std::isfinite(action.period) || action.period < 0.0 ||
227 (action.axis_direction != CamSwitchAction::AxisDirection::both &&
228 action.axis_direction != CamSwitchAction::AxisDirection::positive &&
229 action.axis_direction != CamSwitchAction::AxisDirection::negative) ||
230 (action.cam_switch_mode != CamSwitchAction::Mode::position &&
231 action.cam_switch_mode != CamSwitchAction::Mode::time) ||
232 (action.cam_switch_mode == CamSwitchAction::Mode::position &&
233 action.period == 0.0 && action.on_position > action.off_position) ||
234 (action.cam_switch_mode == CamSwitchAction::Mode::time &&
235 action.duration_ns <= 0)) {
237 return;
238 }
239 const std::size_t track = action.track_number - 1;
240 if((outputs.data != nullptr && outputs.size <= track) ||
241 (track_options.data != nullptr && track_options.size <= track)) {
243 return;
244 }
245 tracks[track] = true;
246 }
247
248 for(std::size_t index = 0; index < switches.size; ++index) {
249 const CamSwitchAction &action = switches.data[index];
250 const std::size_t track = action.track_number - 1;
251 const bool enabled = (enable_mask & (std::uint32_t{1} << track)) != 0;
252 const CamTrackOption options_for_track =
253 track_options.data == nullptr ? CamTrackOption{} : track_options.data[track];
254 const bool direction_matches =
255 action.axis_direction == CamSwitchAction::AxisDirection::both ||
256 (action.axis_direction == CamSwitchAction::AxisDirection::positive &&
257 velocity > 0.0) ||
258 (action.axis_direction == CamSwitchAction::AxisDirection::negative &&
259 velocity < 0.0);
260 bool level = false;
261 if(enabled && direction_matches) {
262 if(action.cam_switch_mode == CamSwitchAction::Mode::position) {
263 const std::int64_t compensation = action_active_[index]
264 ? options_for_track.off_compensation_ns
265 : options_for_track.on_compensation_ns;
266 const double compensated_position =
267 position - velocity * static_cast<double>(compensation) / 1.0e9;
268 action_active_[index] = inside_window(compensated_position, action);
269 } else {
270 const double compensated_position =
271 position - velocity *
272 static_cast<double>(options_for_track.on_compensation_ns) /
273 1.0e9;
274 if(previous_valid_[index] && crossed_trigger(previous_position_[index],
275 compensated_position,
276 velocity,
277 action)) {
278 const std::uint64_t duration =
279 static_cast<std::uint64_t>(action.duration_ns);
280 remaining_cycles_[index] =
281 (duration + static_cast<std::uint64_t>(task_period_ns) - 1U) /
282 static_cast<std::uint64_t>(task_period_ns);
283 }
284 action_active_[index] = remaining_cycles_[index] != 0;
285 if(remaining_cycles_[index] != 0) {
286 --remaining_cycles_[index];
287 }
288 previous_position_[index] = compensated_position;
289 previous_valid_[index] = true;
290 }
291 level = action_active_[index];
292 } else {
293 action_active_[index] = false;
294 remaining_cycles_[index] = 0;
295 previous_valid_[index] = false;
296 }
297 levels[track] = levels[track] || level;
298 }
299
300 release_outputs();
301 for(std::size_t track = 0; track < axis::AxisModel::DigitalOutputCount; ++track) {
302 if(tracks[track]) {
303 axis_ref->set_digital_output(track, levels[track]);
304 if(outputs.data != nullptr) {
305 outputs.data[track] = levels[track];
306 }
307 }
308 controlled_tracks_[track] = tracks[track];
309 }
310 controlled_axis_ = axis_ref;
311 controlled_outputs_ = outputs.data;
312 controlled_outputs_size_ = outputs.size;
313 active_table_ = switches.data;
314 active_table_size_ = switches.size;
315 in_operation = true;
316 error = false;
318 }
319
320private:
321 static constexpr std::size_t MaxActions = 8;
322
323 static bool inside_window(double position, const CamSwitchAction &action)
324 {
325 if(action.period > 0.0) {
326 double wrapped = std::fmod(position, action.period);
327 if(wrapped < 0.0) {
328 wrapped += action.period;
329 }
330 if(action.on_position <= action.off_position) {
331 return wrapped >= action.on_position && wrapped <= action.off_position;
332 }
333 return wrapped >= action.on_position || wrapped <= action.off_position;
334 }
335 return position >= action.on_position && position <= action.off_position;
336 }
337
338 static bool crossed_trigger(double previous, double current, double velocity,
339 const CamSwitchAction &action)
340 {
341 if(velocity == 0.0) {
342 return false;
343 }
344 if(action.period <= 0.0) {
345 return velocity > 0.0 ? previous < action.on_position && current >= action.on_position
346 : previous > action.on_position && current <= action.on_position;
347 }
348 const auto wrap = [&](double value) {
349 double wrapped = std::fmod(value, action.period);
350 return wrapped < 0.0 ? wrapped + action.period : wrapped;
351 };
352 const double previous_wrapped = wrap(previous);
353 const double current_wrapped = wrap(current);
354 const double trigger = wrap(action.on_position);
355 if(velocity > 0.0) {
356 return previous_wrapped <= current_wrapped
357 ? previous_wrapped < trigger && current_wrapped >= trigger
358 : trigger > previous_wrapped || trigger <= current_wrapped;
359 }
360 return previous_wrapped >= current_wrapped
361 ? previous_wrapped > trigger && current_wrapped <= trigger
362 : trigger < previous_wrapped || trigger >= current_wrapped;
363 }
364
365 void reset_actions()
366 {
367 for(std::size_t index = 0; index < MaxActions; ++index) {
368 action_active_[index] = false;
369 remaining_cycles_[index] = 0;
370 previous_position_[index] = 0.0;
371 previous_valid_[index] = false;
372 }
373 active_table_ = nullptr;
374 active_table_size_ = 0;
375 }
376
377 void release_outputs()
378 {
379 if(controlled_axis_ != nullptr) {
380 for(std::size_t track = 0; track < axis::AxisModel::DigitalOutputCount; ++track) {
381 if(controlled_tracks_[track]) {
382 controlled_axis_->set_digital_output(track, false);
383 if(controlled_outputs_ != nullptr && track < controlled_outputs_size_) {
384 controlled_outputs_[track] = false;
385 }
386 }
387 controlled_tracks_[track] = false;
388 }
389 }
390 controlled_axis_ = nullptr;
391 controlled_outputs_ = nullptr;
392 controlled_outputs_size_ = 0;
393 }
394
395 void fail(rt::ErrorCode code)
396 {
397 release_outputs();
398 in_operation = false;
399 error = true;
400 error_id = code;
401 }
402
403 axis::AxisModel *controlled_axis_ = nullptr;
404 bool *controlled_outputs_ = nullptr;
405 std::size_t controlled_outputs_size_ = 0;
406 const CamSwitchAction *active_table_ = nullptr;
407 std::size_t active_table_size_ = 0;
408 bool controlled_tracks_[axis::AxisModel::DigitalOutputCount]{};
409 bool action_active_[MaxActions]{};
410 std::uint64_t remaining_cycles_[MaxActions]{};
411 double previous_position_[MaxActions]{};
412 bool previous_valid_[MaxActions]{};
413};
414
415// MC_ReadAxisInfo: diagnostic snapshot. The rewrite core is a simulation until
416// a hardware adapter feeds set_axis_info_inputs; limit switches fold together
417// the adapter bits and the software-limit trip state.
419{
420public:
422 bool simulation = false;
424 bool ready_for_power_on = false;
425 bool power_on = false;
426 bool is_homed = false;
427 bool home_abs_switch = false;
428 bool limit_switch_pos = false;
429 bool limit_switch_neg = false;
430 bool axis_warning = false;
431
432 void call()
433 {
434 if(!begin_enable()) {
435 if(!enable) clear_values();
436 return;
437 }
438 if(axis_ref == nullptr) {
439 clear_values();
441 return;
442 }
443 const axis::AxisSnapshot &snapshot = axis_ref->snapshot();
444 const axis::AxisModel::AxisInfoInputs &info = axis_ref->axis_info_inputs();
445 simulation = true;
448 power_on = snapshot.powered;
449 is_homed = snapshot.homed;
451 limit_switch_pos = info.limit_switch_pos || sw_limit_tripped(true);
452 limit_switch_neg = info.limit_switch_neg || sw_limit_tripped(false);
453 axis_warning = info.warning;
455 }
456
457private:
458 bool sw_limit_tripped(bool positive) const
459 {
460 const axis::AxisParameter enable_parameter = positive
463 const rt::Result<bool> enabled = axis_ref->read_bool_parameter(enable_parameter);
464 if(!enabled || !enabled.value()) {
465 return false;
466 }
467 const axis::AxisParameter limit_parameter =
469 const rt::Result<double> limit = axis_ref->read_parameter(limit_parameter);
470 if(!limit) {
471 return false;
472 }
473 const double position = axis_ref->snapshot().command_position;
474 return positive ? position > limit.value() : position < limit.value();
475 }
476
477 void clear_values()
478 {
479 simulation = false;
480 communication_ready = false;
481 ready_for_power_on = false;
482 power_on = false;
483 is_homed = false;
484 home_abs_switch = false;
485 limit_switch_pos = false;
486 limit_switch_neg = false;
487 axis_warning = false;
488 }
489};
490
491// MC_ReadMotionState: direction and motion phase derived from the selected
492// value source. The typed source enum makes the v0.x invalid-source error
493// unrepresentable.
495{
496public:
499 bool direction_positive = false;
500 bool direction_negative = false;
501 bool accelerating = false;
502 bool constant_velocity = false;
503 bool decelerating = false;
504
505 void call()
506 {
507 if(!begin_enable()) {
508 if(!enable) clear_values();
509 return;
510 }
511 if(axis_ref == nullptr) {
512 clear_values();
514 return;
515 }
516 const axis::AxisSnapshot &snapshot = axis_ref->snapshot();
517 const double velocity = source == axis::MasterValueSource::actual
518 ? snapshot.actual_velocity
519 : snapshot.command_velocity;
520 const double acceleration = snapshot.command_acceleration;
521 direction_positive = velocity > 0.0;
522 direction_negative = velocity < 0.0;
523 constant_velocity = velocity != 0.0 && acceleration == 0.0;
524 accelerating = acceleration != 0.0 && velocity * acceleration > 0.0;
525 decelerating = acceleration != 0.0 && velocity * acceleration < 0.0;
527 }
528
529private:
530 void clear_values()
531 {
532 direction_positive = false;
533 direction_negative = false;
534 accelerating = false;
535 constant_velocity = false;
536 decelerating = false;
537 }
538};
539
540} // namespace plcopen::core::fb
rt::Result< bool > read_bool_parameter(AxisParameter parameter) const
rt::Result< double > read_parameter(AxisParameter parameter) const
const AxisSnapshot & snapshot() const
static constexpr std::size_t DigitalOutputCount
void accept_management(rt::Result< std::uint32_t > accepted)
CamSwitchTableView view() const
定义 io.h:161
rt::ErrorCode push(const CamSwitchAction &action)
定义 io.h:151
void fail_enable(rt::ErrorCode code)
定义 base.h:142
void call(std::int64_t task_period_ns=1000000)
定义 io.h:185
axis::MasterValueSource value_source
定义 io.h:179
CamTrackOptionsView track_options
定义 io.h:176
axis::MasterValueSource source
定义 io.h:498
static constexpr Result failure(ErrorCode error)
定义 error.h:29
enum plcopen::core::fb::CamSwitchAction::Mode cam_switch_mode
enum plcopen::core::fb::CamSwitchAction::AxisDirection axis_direction
const CamSwitchAction * data
定义 io.h:144