plcopen 0.22.0
载入中...
搜索中...
未找到
feedback_guard.h
浏览该文件的文档.
1#pragma once
2
3// T28 (KB-105): deterministic, allocation-free feedback toxicity guard for the
4// cycle boundary (doc/compliance/feedback-toxicity-semantics.md). Rejects
5// non-finite values, per-tick position jumps, and velocity/acceleration/torque
6// envelope violations before any sample reaches AxisModel::set_actual_feedback.
7
8#include <cmath>
9#include <cstdint>
10
11#include "adapters/servo.h"
12#include "axis/state.h"
13#include "rt/error.h"
14
16{
17
28
36
38{
39 // Non-negative thresholds; 0.0 disables checking for that channel.
40 double max_position_step = 0.0;
41 double max_velocity_abs = 0.0;
43 double max_torque_abs = 0.0;
44 // Consecutive rejected frames required to latch ErrorStop (1..16).
45 std::uint32_t max_consecutive_rejects = 1;
46};
47
49{
50public:
51 static constexpr std::uint32_t MaxConsecutiveRejectLimit = 16;
52
54 {
55 if(!valid_threshold(config.max_position_step) ||
56 !valid_threshold(config.max_velocity_abs) ||
57 !valid_threshold(config.max_acceleration_abs) ||
58 !valid_threshold(config.max_torque_abs) ||
59 config.max_consecutive_rejects == 0U ||
60 config.max_consecutive_rejects > MaxConsecutiveRejectLimit) {
62 }
63 config_ = config;
64 return rt::ErrorCode::ok;
65 }
66
67 const FeedbackGuardConfig &config() const { return config_; }
68
69 void reset()
70 {
71 primed_ = false;
72 tripped_ = false;
73 last_valid_position_ = 0.0;
74 consecutive_rejects_ = 0U;
75 last_fault_ = FeedbackFaultKind::none;
76 last_outcome_ = FeedbackGuardOutcome::none;
77 }
78
79 // Re-anchors the position reference after an intentional coordinate remap
80 // (such as homing or MC_SetPosition) so the next frame is not flagged.
81 rt::ErrorCode reanchor(double position)
82 {
83 if(tripped_) {
85 }
86 if(!std::isfinite(position)) {
88 }
89 primed_ = true;
90 last_valid_position_ = position;
91 consecutive_rejects_ = 0U;
92 return rt::ErrorCode::ok;
93 }
94
95 // Applies feedback through toxicity checks.
96 // Returns:
97 // - ok with last_outcome() == applied: frame accepted and bridged.
98 // - ok with last_outcome() == rejected_held: frame dropped below K;
99 // axis actuals/IO and last_valid_position_ remain anchored at last
100 // clean sample.
101 // - precondition_failed with last_outcome() == tripped: reached K
102 // consecutive rejects (or already latched); calls
103 // axis.set_power_feedback(false), which drives a powered axis into
104 // ErrorStop (an already-disabled axis remains disabled).
106 {
107 if(tripped_) {
109 last_outcome_ = FeedbackGuardOutcome::tripped;
111 }
112
113 const FeedbackFaultKind fault = classify_fault(feedback);
115 return record_rejection(axis, fault, rt::ErrorCode::ok);
116 }
117
118 const rt::ErrorCode bridged = bridge_feedback(axis, feedback);
119 if(bridged != rt::ErrorCode::ok) {
120 return record_rejection(axis, FeedbackFaultKind::non_finite, bridged);
121 }
122
123 primed_ = true;
124 last_valid_position_ = feedback.position;
125 consecutive_rejects_ = 0U;
126 last_outcome_ = FeedbackGuardOutcome::applied;
127 return rt::ErrorCode::ok;
128 }
129
130 bool primed() const { return primed_; }
131 bool tripped() const { return tripped_; }
132 double last_valid_position() const { return last_valid_position_; }
133 std::uint32_t consecutive_rejects() const { return consecutive_rejects_; }
134 std::uint64_t total_rejects() const { return total_rejects_; }
135 FeedbackFaultKind last_fault() const { return last_fault_; }
136 FeedbackGuardOutcome last_outcome() const { return last_outcome_; }
137
138private:
139 rt::ErrorCode record_rejection(axis::AxisModel &axis,
141 rt::ErrorCode hold_code)
142 {
143 last_fault_ = fault;
144 ++total_rejects_;
145 ++consecutive_rejects_;
146 if(consecutive_rejects_ >= config_.max_consecutive_rejects) {
147 tripped_ = true;
148 last_outcome_ = FeedbackGuardOutcome::tripped;
149 (void)axis.set_power_feedback(false);
151 }
153 return hold_code;
154 }
155 static bool valid_threshold(double value)
156 {
157 return std::isfinite(value) && value >= 0.0;
158 }
159
160 FeedbackFaultKind classify_fault(const ServoFeedback &feedback) const
161 {
162 if(!std::isfinite(feedback.position) || !std::isfinite(feedback.velocity) ||
163 !std::isfinite(feedback.acceleration) || !std::isfinite(feedback.torque)) {
165 }
166 if(primed_ && config_.max_position_step > 0.0 &&
167 std::fabs(feedback.position - last_valid_position_) > config_.max_position_step) {
169 }
170 if(config_.max_velocity_abs > 0.0 &&
171 std::fabs(feedback.velocity) > config_.max_velocity_abs) {
173 }
174 if(config_.max_acceleration_abs > 0.0 &&
175 std::fabs(feedback.acceleration) > config_.max_acceleration_abs) {
177 }
178 if(config_.max_torque_abs > 0.0 &&
179 std::fabs(feedback.torque) > config_.max_torque_abs) {
181 }
183 }
184
185 FeedbackGuardConfig config_{};
186 bool primed_ = false;
187 bool tripped_ = false;
188 double last_valid_position_ = 0.0;
189 std::uint32_t consecutive_rejects_ = 0U;
190 std::uint64_t total_rejects_ = 0U;
193};
194
195} // namespace plcopen::core::adapters
const FeedbackGuardConfig & config() const
rt::ErrorCode configure(const FeedbackGuardConfig &config)
static constexpr std::uint32_t MaxConsecutiveRejectLimit
rt::ErrorCode reanchor(double position)
FeedbackGuardOutcome last_outcome() const
rt::ErrorCode apply(axis::AxisModel &axis, const ServoFeedback &feedback)
rt::ErrorCode bridge_feedback(axis::AxisModel &axis, const ServoFeedback &feedback)
定义 servo.h:83