plcopen 0.22.0
载入中...
搜索中...
未找到
joint_group.h
浏览该文件的文档.
1#pragma once
2
3// B9/H1 multi-joint stream aggregation. The legacy API remains a fixed bank
4// of independent filters. Frame sessions add an atomic (whole-frame
5// transactional: a frame is accepted and activated all-or-nothing),
6// keep-latest command surface with synchronized group watchdog timing and
7// fixed-capacity output snapshots (trajectory-stream semantics v2.1).
8// "Atomic" here says nothing about lock-freedom or memory ordering: this
9// type holds no std::atomic and performs no synchronization.
10//
11// THREADING CONTRACT -- single thread (planning domain):
12// push_frame(), cycle(), and read_setpoint_frame() form one single-thread
13// contract and must all be called from the owning planning-domain thread.
14// Concurrent producers are a data race. Cross-thread frame delivery is out
15// of scope (trajectory-stream matrix decision #12): a producer on another
16// thread must hand frames over through its own SPSC queue (rt::SpscQueue)
17// that the planning thread drains before calling push_frame().
18// read_setpoint_frame() returns a reference into live storage that the next
19// cycle() overwrites -- copy the frame before yielding or passing it on.
20//
21// RT-SAFE cycle path: fixed storage only; no heap allocation, locks,
22// exceptions, OS calls, or wall-clock access.
23
24#include <cmath>
25#include <cstddef>
26#include <cstdint>
27
28#include "rt/error.h"
29#include "stream/filter.h"
30
32{
33
34inline constexpr std::size_t JointStreamMaxJoints = 48;
35
37{
40};
41
43{
44 double q_des = 0.0;
45 double dq_des = 0.0;
46 double tau_ff = 0.0;
47 double kp = 0.0;
48 double kd = 0.0;
49};
50
57
59{
60 double position = 0.0;
61 double velocity = 0.0;
62 double acceleration = 0.0;
63 double tau_ff = 0.0;
64 double kp = 0.0;
65 double kd = 0.0;
66};
67
69{
71 std::size_t joint_count = 0;
72 std::int64_t cycle_timestamp = 0;
73 std::int64_t producer_timestamp_cycles = 0;
74 std::uint64_t frame_sequence = 0;
75};
76
78{
80 double max_abs_tau_ff = 0.0;
81 double min_kp = 0.0;
82 double max_kp = 0.0;
83 double min_kd = 0.0;
84 double max_kd = 0.0;
85 double safe_kp = 0.0;
86 double safe_kd = 0.0;
87};
88
96
98{
99public:
100 static constexpr std::size_t MaxJoints = JointStreamMaxJoints;
101 static constexpr std::size_t SlowReplansPerCycle = 10;
102
103 // Applies one shared configuration to all legacy-independent joints.
105 {
108 }
109 const std::size_t preflight_count = joint_count > joint_count_ ? joint_count : joint_count_;
110 for(std::size_t i = 0; i < preflight_count; ++i) {
111 if(!filters_[i].can_configure(config)) {
113 }
114 }
115 for(std::size_t i = 0; i < joint_count; ++i) {
116 const rt::ErrorCode configured = filters_[i].configure(config);
117 if(configured != rt::ErrorCode::ok) {
118 return configured;
119 }
120 }
121 initialize_configuration(SessionKind::legacy_independent, joint_count);
122 return rt::ErrorCode::ok;
123 }
124
125 // Applies the complete per-joint frame policy transactionally. No member
126 // observes the replacement policy unless every member passes preflight.
128 {
129 if(!valid_frame_config(config)) {
131 }
132
133 const std::size_t preflight_count =
134 config.joint_count > joint_count_ ? config.joint_count : joint_count_;
135 for(std::size_t i = 0; i < preflight_count; ++i) {
136 const StreamFilterConfig &filter =
137 i < config.joint_count ? config.joints[i].filter : config.joints[0].filter;
138 if(!filters_[i].can_configure(filter)) {
140 }
141 }
142 for(std::size_t i = 0; i < config.joint_count; ++i) {
143 const rt::ErrorCode configured = filters_[i].configure(config.joints[i].filter);
144 if(configured != rt::ErrorCode::ok) {
145 return configured;
146 }
147 }
148
149 frame_mode_ = config.mode;
150 gain_ramp_cycles_ = config.gain_ramp_cycles;
151 group_timeout_cycles_ = config.joints[0].filter.timeout_cycles;
152 for(std::size_t i = 0; i < config.joint_count; ++i) {
153 frame_configs_[i] = config.joints[i];
154 if(config.joints[i].filter.timeout_cycles < group_timeout_cycles_) {
155 group_timeout_cycles_ = config.joints[i].filter.timeout_cycles;
156 }
157 }
158 initialize_configuration(SessionKind::frame, config.joint_count);
159 return rt::ErrorCode::ok;
160 }
161
163 {
164 for(std::size_t i = 0; i < joint_count_; ++i) {
165 filters_[i].end_session();
166 member_reset_[i] = false;
167 }
168 clear_frame_runtime();
169 }
170
172 {
173 if(joint >= joint_count_ ||
174 (session_kind_ == SessionKind::frame && (have_accepted_frame_ || local_cycle_ != 0))) {
176 }
177 const rt::ErrorCode result = filters_[joint].reset(state);
178 if(result != rt::ErrorCode::ok) {
179 return result;
180 }
181 if(session_kind_ == SessionKind::frame) {
182 member_reset_[joint] = true;
183 direct_states_[joint] = state;
184 mixed_[joint] = {};
185 mixed_[joint].tau_ff = 0.0;
186 mixed_[joint].kp = frame_configs_[joint].safe_kp;
187 mixed_[joint].kd = frame_configs_[joint].safe_kd;
188 mixed_[joint].gain_start_kp = mixed_[joint].kp;
189 mixed_[joint].gain_start_kd = mixed_[joint].kd;
190 mixed_[joint].gain_target_kp = mixed_[joint].kp;
191 mixed_[joint].gain_target_kd = mixed_[joint].kd;
192 snapshot_.joints[joint] = {state.position, state.velocity, state.acceleration, 0.0,
193 mixed_[joint].kp, mixed_[joint].kd};
194 }
195 return rt::ErrorCode::ok;
196 }
197
198 rt::ErrorCode push_target(std::size_t joint, const StreamTarget &target)
199 {
200 if(session_kind_ != SessionKind::legacy_independent || joint >= joint_count_) {
202 }
203 return filters_[joint].push_target(target);
204 }
205
206 // Producer-side frame transaction (whole-frame all-or-nothing, not a
207 // lock-free operation). Producer timestamps order and identify frames
208 // only; local cycle time controls activation and watchdog age. Must be
209 // called from the planning-domain thread that also calls cycle(); an
210 // off-thread producer marshals through its own SPSC first.
212 {
213 if(session_kind_ != SessionKind::frame) {
215 }
216 if(!all_members_reset() || !valid_command_frame(frame) ||
217 (have_accepted_frame_ && frame.timestamp_cycles <= latest_accepted_timestamp_)) {
218 ++rejected_frames_;
220 }
221
222 pending_frame_ = frame;
223 latest_accepted_timestamp_ = frame.timestamp_cycles;
224 have_accepted_frame_ = true;
225 have_pending_frame_ = true;
226 pending_frame_sequence_ = ++accepted_frame_sequence_;
227 return rt::ErrorCode::ok;
228 }
229
230 // RT cycle path. Legacy sessions retain the original independent loop.
231 // Frame sessions latch at most one complete keep-latest frame per cycle.
232 void cycle()
233 {
234 if(session_kind_ == SessionKind::legacy_independent) {
235 for(std::size_t i = 0; i < joint_count_; ++i) {
236 filters_[i].cycle();
237 }
238 return;
239 }
240 if(session_kind_ != SessionKind::frame || !all_members_reset()) {
241 return;
242 }
243 if(local_cycle_ < INT64_MAX) {
244 ++local_cycle_;
245 }
246
247 const bool activated = have_pending_frame_;
248 if(activated) {
249 activate_pending_frame();
250 } else if(frame_mode_ == JointFrameMode::direct && !dropping_) {
251 for(std::size_t i = 0; i < joint_count_; ++i) {
252 direct_states_[i].acceleration = 0.0;
253 }
254 }
255
256 if(have_active_frame_ && !dropping_ &&
257 positive_cycle_delta(local_cycle_, last_activation_cycle_) >
258 static_cast<std::uint64_t>(group_timeout_cycles_)) {
259 begin_group_dropout();
260 }
261
262 if(frame_mode_ == JointFrameMode::upsample) {
263 cycle_upsample_filters();
264 } else if(dropping_) {
265 cycle_direct_dropout();
266 }
267
268 advance_mixed_fields();
269 publish_snapshot();
270 }
271
272 otg::State1D state(std::size_t joint) const
273 {
274 if(joint >= joint_count_) {
275 return otg::State1D{};
276 }
277 if(session_kind_ == SessionKind::frame) {
278 const JointSetpoint &setpoint = snapshot_.joints[joint];
279 return {setpoint.position, setpoint.velocity, setpoint.acceleration};
280 }
281 return filters_[joint].state();
282 }
283
284 const StreamFilter1D &joint(std::size_t index) const { return filters_[index]; }
285
286 std::size_t joint_count() const { return joint_count_; }
287
288 // Reference into live storage: the next cycle() overwrites it, and the
289 // call belongs to the same single planning-domain thread as cycle().
290 // Copy the frame before handing it to another cycle or thread.
291 const JointSetpointFrame &read_setpoint_frame() const { return snapshot_; }
292
293 std::uint32_t rejected_frames() const { return rejected_frames_; }
294
295 std::uint32_t dropout_count() const { return group_dropout_count_; }
296
297 std::size_t pending_replans() const
298 {
299 if(session_kind_ != SessionKind::frame || frame_mode_ != JointFrameMode::upsample) {
300 return 0;
301 }
302 std::size_t count = 0;
303 for(std::size_t i = 0; i < joint_count_; ++i) {
304 if(filters_[i].replan_pending()) {
305 ++count;
306 }
307 }
308 return count;
309 }
310
311 std::uint64_t deferred_replans() const { return deferred_replans_; }
312
313private:
314 enum class SessionKind
315 {
316 unconfigured,
317 legacy_independent,
318 frame,
319 };
320
321 struct MixedState
322 {
323 double tau_ff = 0.0;
324 double tau_start = 0.0;
325 double kp = 0.0;
326 double kd = 0.0;
327 double gain_start_kp = 0.0;
328 double gain_start_kd = 0.0;
329 double gain_target_kp = 0.0;
330 double gain_target_kd = 0.0;
331 std::int64_t gain_tick = 0;
332 std::int64_t gain_duration = 0;
333 };
334
335 static std::uint64_t positive_cycle_delta(std::int64_t later, std::int64_t earlier)
336 {
337 return static_cast<std::uint64_t>(later) - static_cast<std::uint64_t>(earlier);
338 }
339
340 static bool finite_bounds(double minimum, double maximum)
341 {
342 return std::isfinite(minimum) && std::isfinite(maximum) && minimum <= maximum;
343 }
344
345 bool valid_frame_config(const JointStreamGroupConfig &config) const
346 {
347 if(config.joint_count < 1 || config.joint_count > MaxJoints ||
348 config.gain_ramp_cycles < 1 ||
349 (config.mode != JointFrameMode::direct && config.mode != JointFrameMode::upsample)) {
350 return false;
351 }
352 for(std::size_t i = 0; i < config.joint_count; ++i) {
353 const JointStreamConfig &member = config.joints[i];
354 if(!filters_[i].can_configure(member.filter) || !std::isfinite(member.max_abs_tau_ff) ||
355 member.max_abs_tau_ff < 0.0 || !finite_bounds(member.min_kp, member.max_kp) ||
356 !finite_bounds(member.min_kd, member.max_kd) || !std::isfinite(member.safe_kp) ||
357 !std::isfinite(member.safe_kd) || member.safe_kp < member.min_kp ||
358 member.safe_kp > member.max_kp || member.safe_kd < member.min_kd ||
359 member.safe_kd > member.max_kd) {
360 return false;
361 }
362 }
363 return true;
364 }
365
366 bool valid_command_frame(const JointCommandFrame &frame) const
367 {
368 if(frame.joint_count != joint_count_) {
369 return false;
370 }
371 for(std::size_t i = 0; i < joint_count_; ++i) {
372 const JointCommand &command = frame.joints[i];
373 const JointStreamConfig &config = frame_configs_[i];
374 if(!std::isfinite(command.q_des) || !std::isfinite(command.dq_des) ||
375 !std::isfinite(command.tau_ff) || !std::isfinite(command.kp) ||
376 !std::isfinite(command.kd) ||
377 (config.filter.position_envelope_enabled &&
378 (command.q_des < config.filter.min_position ||
379 command.q_des > config.filter.max_position)) ||
380 std::fabs(command.dq_des) > config.filter.limits.max_velocity ||
381 std::fabs(command.tau_ff) > config.max_abs_tau_ff || command.kp < config.min_kp ||
382 command.kp > config.max_kp || command.kd < config.min_kd ||
383 command.kd > config.max_kd) {
384 return false;
385 }
386 }
387 return true;
388 }
389
390 bool all_members_reset() const
391 {
392 for(std::size_t i = 0; i < joint_count_; ++i) {
393 if(!member_reset_[i]) {
394 return false;
395 }
396 }
397 return true;
398 }
399
400 void initialize_configuration(SessionKind kind, std::size_t joint_count)
401 {
402 session_kind_ = kind;
403 joint_count_ = joint_count;
404 for(std::size_t i = 0; i < MaxJoints; ++i) {
405 member_reset_[i] = false;
406 direct_states_[i] = {};
407 mixed_[i] = {};
408 }
409 clear_frame_runtime();
410 snapshot_.joint_count = joint_count;
411 }
412
413 void clear_frame_runtime()
414 {
415 pending_frame_ = {};
416 snapshot_ = {};
417 snapshot_.joint_count = joint_count_;
418 local_cycle_ = 0;
419 latest_accepted_timestamp_ = 0;
420 last_activation_cycle_ = 0;
421 accepted_frame_sequence_ = 0;
422 pending_frame_sequence_ = 0;
423 active_frame_sequence_ = 0;
424 active_producer_timestamp_ = 0;
425 deferred_replans_ = 0;
426 rejected_frames_ = 0;
427 group_dropout_count_ = 0;
428 slow_replan_start_ = 0;
429 have_accepted_frame_ = false;
430 have_pending_frame_ = false;
431 have_active_frame_ = false;
432 dropping_ = false;
433 }
434
435 void start_gain_ramp(std::size_t joint, double kp, double kd, std::int64_t duration)
436 {
437 MixedState &mixed = mixed_[joint];
438 mixed.gain_start_kp = mixed.kp;
439 mixed.gain_start_kd = mixed.kd;
440 mixed.gain_target_kp = kp;
441 mixed.gain_target_kd = kd;
442 mixed.gain_tick = 0;
443 mixed.gain_duration = duration;
444 }
445
446 void activate_pending_frame()
447 {
448 have_pending_frame_ = false;
449 have_active_frame_ = true;
450 dropping_ = false;
451 last_activation_cycle_ = local_cycle_;
452 active_frame_sequence_ = pending_frame_sequence_;
453 active_producer_timestamp_ = pending_frame_.timestamp_cycles;
454
455 for(std::size_t i = 0; i < joint_count_; ++i) {
456 const JointCommand &command = pending_frame_.joints[i];
457 mixed_[i].tau_ff = command.tau_ff;
458 start_gain_ramp(i, command.kp, command.kd, gain_ramp_cycles_);
459 if(frame_mode_ == JointFrameMode::direct) {
460 const double previous_velocity = direct_states_[i].velocity;
461 direct_states_[i] = {
462 command.q_des,
463 command.dq_des,
464 command.dq_des - previous_velocity,
465 };
466 } else {
467 StreamTarget target{};
468 target.position = command.q_des;
469 target.velocity = command.dq_des;
470 target.has_velocity = true;
471 target.timestamp_cycles =
472 filters_[i].now_cycles() < INT64_MAX ? filters_[i].now_cycles() + 1 : INT64_MAX;
473 filters_[i].push_target(target);
474 }
475 }
476 }
477
478 void begin_group_dropout()
479 {
480 dropping_ = true;
481 ++group_dropout_count_;
482 for(std::size_t i = 0; i < joint_count_; ++i) {
483 const otg::State1D from =
484 frame_mode_ == JointFrameMode::direct ? direct_states_[i] : filters_[i].state();
485 filters_[i].begin_dropout_from(from);
486 mixed_[i].tau_start = mixed_[i].tau_ff;
487 start_gain_ramp(i, frame_configs_[i].safe_kp, frame_configs_[i].safe_kd,
488 frame_configs_[i].filter.extrapolation_cycles);
489 }
490 }
491
492 void cycle_upsample_filters()
493 {
494 std::size_t slow_replans = 0;
495 for(std::size_t offset = 0; offset < joint_count_; ++offset) {
496 const std::size_t joint = (slow_replan_start_ + offset) % joint_count_;
498 filters_[joint].cycle_with_replan_budget(slow_replans < SlowReplansPerCycle, result);
500 ++slow_replans;
501 } else if(result == StreamFilter1D::ReplanResult::deferred) {
502 ++deferred_replans_;
503 }
504 }
505 slow_replan_start_ = (slow_replan_start_ + SlowReplansPerCycle) % joint_count_;
506 }
507
508 void cycle_direct_dropout()
509 {
510 for(std::size_t i = 0; i < joint_count_; ++i) {
512 direct_states_[i] = filters_[i].cycle_with_replan_budget(true, result);
513 }
514 }
515
516 void advance_mixed_fields()
517 {
518 for(std::size_t i = 0; i < joint_count_; ++i) {
519 MixedState &mixed = mixed_[i];
520 if(dropping_) {
521 const std::int64_t duration = frame_configs_[i].filter.extrapolation_cycles;
522 if(duration <= 0) {
523 mixed.tau_ff = 0.0;
524 } else if(mixed.gain_tick < duration) {
525 const double remaining = 1.0 - static_cast<double>(mixed.gain_tick + 1) /
526 static_cast<double>(duration);
527 mixed.tau_ff = remaining > 0.0 ? mixed.tau_start * remaining : 0.0;
528 }
529 }
530
531 if(mixed.gain_tick < mixed.gain_duration) {
532 ++mixed.gain_tick;
533 const double fraction =
534 static_cast<double>(mixed.gain_tick) / static_cast<double>(mixed.gain_duration);
535 mixed.kp =
536 mixed.gain_start_kp + (mixed.gain_target_kp - mixed.gain_start_kp) * fraction;
537 mixed.kd =
538 mixed.gain_start_kd + (mixed.gain_target_kd - mixed.gain_start_kd) * fraction;
539 } else {
540 mixed.kp = mixed.gain_target_kp;
541 mixed.kd = mixed.gain_target_kd;
542 }
543 }
544 }
545
546 void publish_snapshot()
547 {
548 snapshot_.joint_count = joint_count_;
549 snapshot_.cycle_timestamp = local_cycle_;
550 snapshot_.producer_timestamp_cycles = active_producer_timestamp_;
551 snapshot_.frame_sequence = active_frame_sequence_;
552 for(std::size_t i = 0; i < joint_count_; ++i) {
553 const otg::State1D state =
554 frame_mode_ == JointFrameMode::direct ? direct_states_[i] : filters_[i].state();
555 snapshot_.joints[i] = {
556 state.position, state.velocity, state.acceleration,
557 mixed_[i].tau_ff, mixed_[i].kp, mixed_[i].kd,
558 };
559 }
560 }
561
562 StreamFilter1D filters_[MaxJoints]{};
563 JointStreamConfig frame_configs_[MaxJoints]{};
564 otg::State1D direct_states_[MaxJoints]{};
565 MixedState mixed_[MaxJoints]{};
566 JointCommandFrame pending_frame_{};
567 JointSetpointFrame snapshot_{};
568 std::size_t joint_count_ = 0;
569 std::size_t slow_replan_start_ = 0;
570 std::int64_t gain_ramp_cycles_ = 1;
571 std::int64_t group_timeout_cycles_ = 1;
572 std::int64_t local_cycle_ = 0;
573 std::int64_t latest_accepted_timestamp_ = 0;
574 std::int64_t last_activation_cycle_ = 0;
575 std::int64_t active_producer_timestamp_ = 0;
576 std::uint64_t accepted_frame_sequence_ = 0;
577 std::uint64_t pending_frame_sequence_ = 0;
578 std::uint64_t active_frame_sequence_ = 0;
579 std::uint64_t deferred_replans_ = 0;
580 std::uint32_t rejected_frames_ = 0;
581 std::uint32_t group_dropout_count_ = 0;
582 SessionKind session_kind_ = SessionKind::unconfigured;
584 bool member_reset_[MaxJoints]{};
585 bool have_accepted_frame_ = false;
586 bool have_pending_frame_ = false;
587 bool have_active_frame_ = false;
588 bool dropping_ = false;
589};
590
591} // namespace plcopen::core::stream
rt::ErrorCode configure(std::size_t joint_count, const StreamFilterConfig &config)
rt::ErrorCode reset(std::size_t joint, otg::State1D state)
const StreamFilter1D & joint(std::size_t index) const
rt::ErrorCode configure_frame(const JointStreamGroupConfig &config)
static constexpr std::size_t SlowReplansPerCycle
static constexpr std::size_t MaxJoints
rt::ErrorCode push_frame(const JointCommandFrame &frame)
const JointSetpointFrame & read_setpoint_frame() const
otg::State1D state(std::size_t joint) const
rt::ErrorCode push_target(std::size_t joint, const StreamTarget &target)
constexpr std::size_t JointStreamMaxJoints
JointCommand joints[JointStreamMaxJoints]
JointSetpoint joints[JointStreamMaxJoints]
JointStreamConfig joints[JointStreamMaxJoints]