plcopen 0.22.0
载入中...
搜索中...
未找到
ipc_transport.h
浏览该文件的文档.
1#pragma once
2
3// X5 / ADR-0006: fixed-layout, OS-free transport contract between an outer
4// executor and a fieldbus process. Named shared-memory creation and process
5// lifecycle stay in the host; this header only defines bounded value
6// conversion and lock-free shared storage.
7
8#include <array>
9#include <atomic>
10#include <cmath>
11#include <cstddef>
12#include <cstdint>
13#include <type_traits>
14
15#include "adapters/servo.h"
16#include "rt/ipc_channel.h"
17
19{
20
21constexpr std::uint32_t TransportMagic = 0x49355850U; // "PX5I", little-endian bytes.
22constexpr std::uint32_t TransportAbiVersion = 1;
23constexpr std::size_t MaxAxes = 8;
24
34
35enum class TransportState : std::uint32_t
36{
44};
45
46enum class TransportError : std::uint32_t
47{
48 none = 0,
53};
54
56{
57 double position = 0.0;
58 double velocity = 0.0;
59 double acceleration = 0.0;
60 double torque = 0.0;
61 double torque_limit = 0.0;
65 double torque_jerk_limit = 0.0;
66 std::int32_t torque_direction = 0;
67 std::uint32_t torque_mode = 0;
68};
69
71{
72 double position = 0.0;
73 double velocity = 0.0;
74 double acceleration = 0.0;
75 double torque = 0.0;
76 std::uint32_t digital_inputs = 0;
77 std::uint32_t axis_info = 0;
78};
79
81{
82 std::uint64_t tick = 0;
83 std::uint32_t axis_count = 0;
84 std::uint32_t reserved = 0;
86};
87
89{
90 std::uint64_t tick = 0;
91 std::uint32_t axis_count = 0;
92 std::uint32_t reserved = 0;
94};
95
96static_assert(sizeof(ServoSetpointWire) == 80, "X5 setpoint wire ABI changed");
97static_assert(sizeof(ServoFeedbackWire) == 40, "X5 feedback wire ABI changed");
98static_assert(sizeof(ServoSetpointFrame) == 656, "X5 setpoint frame ABI changed");
99static_assert(sizeof(ServoFeedbackFrame) == 336, "X5 feedback frame ABI changed");
100static_assert(std::is_trivially_copyable<ServoSetpointFrame>::value,
101 "X5 setpoint frame must be trivially copyable");
102static_assert(std::is_trivially_copyable<ServoFeedbackFrame>::value,
103 "X5 feedback frame must be trivially copyable");
104
105namespace detail
106{
107
108constexpr std::uint32_t CommunicationReady = 1U << 0U;
109constexpr std::uint32_t ReadyForPowerOn = 1U << 1U;
110constexpr std::uint32_t HomeAbsSwitch = 1U << 2U;
111constexpr std::uint32_t LimitSwitchPos = 1U << 3U;
112constexpr std::uint32_t LimitSwitchNeg = 1U << 4U;
113constexpr std::uint32_t Warning = 1U << 5U;
116constexpr std::uint32_t DigitalInputMask = (1U << ServoFeedback::DigitalInputCount) - 1U;
117
118inline bool valid_direction(axis::Direction direction)
119{
120 switch(direction) {
125 return true;
126 }
127 return false;
128}
129
131{
132 return std::isfinite(wire.position) && std::isfinite(wire.velocity) &&
133 std::isfinite(wire.acceleration) && std::isfinite(wire.torque) &&
134 std::isfinite(wire.torque_limit) && std::isfinite(wire.torque_velocity_limit) &&
135 std::isfinite(wire.torque_acceleration_limit) &&
136 std::isfinite(wire.torque_deceleration_limit) && std::isfinite(wire.torque_jerk_limit) &&
137 wire.torque_direction >= 0 && wire.torque_direction <= 3 && wire.torque_mode <= 1U;
138}
139
141{
142 return std::isfinite(wire.position) && std::isfinite(wire.velocity) &&
143 std::isfinite(wire.acceleration) && std::isfinite(wire.torque) &&
144 (wire.digital_inputs & ~DigitalInputMask) == 0U &&
145 (wire.axis_info & ~AxisInfoMask) == 0U;
146}
147
148inline bool valid_setpoint_source(const ServoSetpoints &source)
149{
150 return std::isfinite(source.position) && std::isfinite(source.velocity) &&
151 std::isfinite(source.acceleration) && std::isfinite(source.torque) &&
152 std::isfinite(source.torque_limit) && std::isfinite(source.torque_velocity_limit) &&
153 std::isfinite(source.torque_acceleration_limit) &&
154 std::isfinite(source.torque_deceleration_limit) &&
155 std::isfinite(source.torque_jerk_limit);
156}
157
158inline bool valid_feedback_source(const ServoFeedback &source)
159{
160 return std::isfinite(source.position) && std::isfinite(source.velocity) &&
161 std::isfinite(source.acceleration) && std::isfinite(source.torque);
162}
163
165{
166 ServoSetpointWire wire{};
167 wire.position = source.position;
168 wire.velocity = source.velocity;
169 wire.acceleration = source.acceleration;
170 wire.torque = source.torque;
171 wire.torque_limit = source.torque_limit;
176 switch(source.torque_direction) {
178 wire.torque_direction = 0;
179 break;
181 wire.torque_direction = 1;
182 break;
184 wire.torque_direction = 2;
185 break;
187 wire.torque_direction = 3;
188 break;
189 }
190 wire.torque_mode = source.torque_mode ? 1U : 0U;
191 return wire;
192}
193
194inline void decode_setpoint(const ServoSetpointWire &wire, ServoSetpoints &target)
195{
197 switch(wire.torque_direction) {
198 case 0:
199 direction = axis::Direction::current;
200 break;
201 case 1:
202 direction = axis::Direction::positive;
203 break;
204 case 2:
205 direction = axis::Direction::negative;
206 break;
207 case 3:
209 break;
210 default:
211 break;
212 }
213 target.position = wire.position;
214 target.velocity = wire.velocity;
215 target.acceleration = wire.acceleration;
216 target.torque = wire.torque;
217 target.torque_limit = wire.torque_limit;
222 target.torque_direction = direction;
223 target.torque_mode = wire.torque_mode != 0U;
224}
225
227{
228 ServoFeedbackWire wire{};
229 wire.position = source.position;
230 wire.velocity = source.velocity;
231 wire.acceleration = source.acceleration;
232 wire.torque = source.torque;
233 for(std::size_t index = 0; index < ServoFeedback::DigitalInputCount; ++index) {
234 if(source.digital_inputs[index]) {
235 wire.digital_inputs |= 1U << index;
236 }
237 }
238 if(source.info.communication_ready)
240 if(source.info.ready_for_power_on)
242 if(source.info.home_abs_switch)
243 wire.axis_info |= HomeAbsSwitch;
244 if(source.info.limit_switch_pos)
246 if(source.info.limit_switch_neg)
248 if(source.info.warning)
249 wire.axis_info |= Warning;
250 return wire;
251}
252
254{
255 target.position = wire.position;
256 target.velocity = wire.velocity;
257 target.acceleration = wire.acceleration;
258 target.torque = wire.torque;
259 for(std::size_t index = 0; index < ServoFeedback::DigitalInputCount; ++index) {
260 target.digital_inputs[index] = (wire.digital_inputs & (1U << index)) != 0U;
261 }
263 target.info.ready_for_power_on = (wire.axis_info & ReadyForPowerOn) != 0U;
264 target.info.home_abs_switch = (wire.axis_info & HomeAbsSwitch) != 0U;
265 target.info.limit_switch_pos = (wire.axis_info & LimitSwitchPos) != 0U;
266 target.info.limit_switch_neg = (wire.axis_info & LimitSwitchNeg) != 0U;
267 target.info.warning = (wire.axis_info & Warning) != 0U;
268}
269
270} // namespace detail
271
272inline bool encode_setpoints(std::uint64_t tick, const ServoSetpoints *values, std::size_t count,
273 ServoSetpointFrame &frame)
274{
275 if(values == nullptr || count == 0 || count > MaxAxes)
276 return false;
277 frame = ServoSetpointFrame{};
278 frame.tick = tick;
279 frame.axis_count = static_cast<std::uint32_t>(count);
280 for(std::size_t index = 0; index < count; ++index) {
281 if(!detail::valid_direction(values[index].torque_direction) ||
282 !detail::valid_setpoint_source(values[index])) {
283 return false;
284 }
285 const ServoSetpointWire wire = detail::encode_setpoint(values[index]);
287 return false;
288 frame.axes[index] = wire;
289 }
290 return true;
291}
292
293inline bool decode_setpoints(const ServoSetpointFrame &frame, ServoSetpoints *values,
294 std::size_t count, std::uint64_t &tick)
295{
296 if(values == nullptr || frame.axis_count == 0 || frame.axis_count > MaxAxes ||
297 count != frame.axis_count || frame.reserved != 0U) {
298 return false;
299 }
300 for(std::size_t index = 0; index < count; ++index)
301 if(!detail::valid_setpoint_wire(frame.axes[index]))
302 return false;
303 for(std::size_t index = 0; index < count; ++index)
304 detail::decode_setpoint(frame.axes[index], values[index]);
305 tick = frame.tick;
306 return true;
307}
308
309inline bool encode_feedback(std::uint64_t tick, const ServoFeedback *values, std::size_t count,
310 ServoFeedbackFrame &frame)
311{
312 if(values == nullptr || count == 0 || count > MaxAxes)
313 return false;
314 frame = ServoFeedbackFrame{};
315 frame.tick = tick;
316 frame.axis_count = static_cast<std::uint32_t>(count);
317 for(std::size_t index = 0; index < count; ++index) {
318 if(!detail::valid_feedback_source(values[index]))
319 return false;
320 const ServoFeedbackWire wire = detail::encode_feedback_value(values[index]);
322 return false;
323 frame.axes[index] = wire;
324 }
325 return true;
326}
327
328inline bool decode_feedback(const ServoFeedbackFrame &frame, ServoFeedback *values,
329 std::size_t count, std::uint64_t &tick)
330{
331 if(values == nullptr || frame.axis_count == 0 || frame.axis_count > MaxAxes ||
332 count != frame.axis_count || frame.reserved != 0U) {
333 return false;
334 }
335 for(std::size_t index = 0; index < count; ++index)
336 if(!detail::valid_feedback_wire(frame.axes[index]))
337 return false;
338 for(std::size_t index = 0; index < count; ++index)
339 detail::decode_feedback_value(frame.axes[index], values[index]);
340 tick = frame.tick;
341 return true;
342}
343
345{
346 std::uint64_t generation = 0;
347 std::uint64_t last_setpoint_tick = 0;
348 std::uint64_t last_feedback_tick = 0;
349 std::uint64_t setpoints_published = 0;
350 std::uint64_t setpoints_consumed = 0;
351 std::uint64_t feedback_published = 0;
352 std::uint64_t feedback_consumed = 0;
353 std::uint64_t setpoint_starvation = 0;
354 std::uint64_t feedback_dropped = 0;
355};
356
357struct alignas(64) TransportHeader
358{
359 std::uint32_t magic = 0;
360 std::uint32_t abi_version = 0;
361 std::uint32_t header_bytes = 0;
362 std::uint32_t region_bytes = 0;
363 std::uint32_t max_axes = 0;
364 std::uint32_t axis_count = 0;
365 std::uint32_t transport_depth = 0;
366 std::uint32_t setpoint_frame_bytes = 0;
367 std::uint32_t feedback_frame_bytes = 0;
368 std::uint32_t status_bytes = 0;
369 std::uint32_t reserved0 = 0;
370 std::uint32_t reserved1 = 0;
371 std::uint64_t generation = 0;
372 std::uint64_t period_ns = 0;
373 std::atomic<std::uint32_t> state{0};
374 std::atomic<std::uint32_t> last_error{0};
375 std::atomic<std::uint64_t> executor_heartbeat{0};
376 std::atomic<std::uint64_t> bus_heartbeat{0};
377 std::atomic<std::uint64_t> phase_tick{0};
378};
379
380static_assert(std::atomic<std::uint32_t>::is_always_lock_free,
381 "X5 requires lock-free uint32 atomics");
382static_assert(std::atomic<std::uint64_t>::is_always_lock_free,
383 "X5 requires lock-free uint64 atomics");
384static_assert(std::is_standard_layout<TransportHeader>::value,
385 "X5 transport header must have stable member order");
386static_assert(sizeof(TransportHeader) == 128, "X5 transport header ABI changed");
387static_assert(offsetof(TransportHeader, generation) == 48,
388 "X5 transport header generation offset changed");
389static_assert(offsetof(TransportHeader, state) == 64,
390 "X5 transport header state offset changed");
391static_assert(offsetof(TransportHeader, phase_tick) == 88,
392 "X5 transport header phase offset changed");
393
394template <std::size_t Depth = 8> struct ServoIpcRegion
395{
396 static_assert(Depth > 0, "X5 transport depth must be positive");
397
402
403 bool initialize(std::size_t axis_count, std::uint64_t period_ns, std::uint64_t generation)
404 {
405 if(axis_count == 0 || axis_count > MaxAxes || period_ns == 0 || generation == 0) {
406 return false;
407 }
408 header.state.store(static_cast<std::uint32_t>(TransportState::initializing),
409 std::memory_order_relaxed);
410 header.magic = TransportMagic;
411 header.abi_version = TransportAbiVersion;
412 header.header_bytes = static_cast<std::uint32_t>(sizeof(TransportHeader));
413 header.region_bytes = static_cast<std::uint32_t>(sizeof(*this));
414 header.max_axes = static_cast<std::uint32_t>(MaxAxes);
415 header.axis_count = static_cast<std::uint32_t>(axis_count);
416 header.transport_depth = static_cast<std::uint32_t>(Depth);
417 header.setpoint_frame_bytes = static_cast<std::uint32_t>(sizeof(ServoSetpointFrame));
418 header.feedback_frame_bytes = static_cast<std::uint32_t>(sizeof(ServoFeedbackFrame));
419 header.status_bytes = static_cast<std::uint32_t>(sizeof(TransportStatusSnapshot));
420 header.reserved0 = 0;
421 header.reserved1 = 0;
422 header.generation = generation;
423 header.period_ns = period_ns;
424 header.last_error.store(static_cast<std::uint32_t>(TransportError::none),
425 std::memory_order_relaxed);
426 header.executor_heartbeat.store(0, std::memory_order_relaxed);
427 header.bus_heartbeat.store(0, std::memory_order_relaxed);
428 header.phase_tick.store(0, std::memory_order_relaxed);
429 setpoints.initialize();
430 feedback.initialize();
431 status.initialize();
432 header.state.store(static_cast<std::uint32_t>(TransportState::priming),
433 std::memory_order_release);
434 return true;
435 }
436
438 {
439 const std::uint32_t raw_state = header.state.load(std::memory_order_acquire);
440 if(raw_state == static_cast<std::uint32_t>(TransportState::uninitialized) ||
441 raw_state == static_cast<std::uint32_t>(TransportState::initializing)) {
443 }
444 if(raw_state > static_cast<std::uint32_t>(TransportState::faulted)) {
446 }
447 if(header.magic != TransportMagic)
449 if(header.abi_version != TransportAbiVersion) {
451 }
452 if(header.header_bytes != sizeof(TransportHeader) || header.region_bytes != sizeof(*this) ||
453 header.max_axes != MaxAxes || header.transport_depth != Depth ||
454 header.setpoint_frame_bytes != sizeof(ServoSetpointFrame) ||
455 header.feedback_frame_bytes != sizeof(ServoFeedbackFrame) ||
456 header.status_bytes != sizeof(TransportStatusSnapshot) || header.period_ns == 0 ||
457 header.generation == 0) {
459 }
460 if(header.axis_count == 0 || header.axis_count > MaxAxes) {
462 }
463 return AttachStatus::ok;
464 }
465
467 {
468 return static_cast<TransportState>(header.state.load(std::memory_order_acquire));
469 }
470
472 {
473 if((expected == TransportState::priming && desired == TransportState::running) ||
474 (expected == TransportState::running && desired == TransportState::draining) ||
475 (expected == TransportState::draining && desired == TransportState::stopped)) {
476 std::uint32_t raw = static_cast<std::uint32_t>(expected);
477 return header.state.compare_exchange_strong(raw, static_cast<std::uint32_t>(desired),
478 std::memory_order_acq_rel,
479 std::memory_order_acquire);
480 }
481 return false;
482 }
483
485 {
486 header.last_error.store(static_cast<std::uint32_t>(error), std::memory_order_release);
487 header.state.store(static_cast<std::uint32_t>(TransportState::faulted),
488 std::memory_order_release);
489 }
490};
491
492static_assert(sizeof(ServoIpcRegion<8>) == 9344, "X5 default transport region ABI changed");
493
494} // namespace plcopen::core::adapters::ipc
bool valid_feedback_wire(const ServoFeedbackWire &wire)
void decode_feedback_value(const ServoFeedbackWire &wire, ServoFeedback &target)
void decode_setpoint(const ServoSetpointWire &wire, ServoSetpoints &target)
bool valid_setpoint_wire(const ServoSetpointWire &wire)
bool valid_direction(axis::Direction direction)
bool valid_feedback_source(const ServoFeedback &source)
bool valid_setpoint_source(const ServoSetpoints &source)
ServoSetpointWire encode_setpoint(const ServoSetpoints &source)
ServoFeedbackWire encode_feedback_value(const ServoFeedback &source)
bool encode_setpoints(std::uint64_t tick, const ServoSetpoints *values, std::size_t count, ServoSetpointFrame &frame)
bool decode_feedback(const ServoFeedbackFrame &frame, ServoFeedback *values, std::size_t count, std::uint64_t &tick)
bool encode_feedback(std::uint64_t tick, const ServoFeedback *values, std::size_t count, ServoFeedbackFrame &frame)
constexpr std::uint32_t TransportAbiVersion
constexpr std::uint32_t TransportMagic
bool decode_setpoints(const ServoSetpointFrame &frame, ServoSetpoints *values, std::size_t count, std::uint64_t &tick)
bool digital_inputs[DigitalInputCount]
定义 servo.h:49
static constexpr std::size_t DigitalInputCount
定义 servo.h:43
axis::AxisModel::AxisInfoInputs info
定义 servo.h:50
bool transition(TransportState expected, TransportState desired)
rt::IpcDoubleBuffer< TransportStatusSnapshot > status
bool initialize(std::size_t axis_count, std::uint64_t period_ns, std::uint64_t generation)
rt::IpcSpscRing< ServoSetpointFrame, Depth > setpoints
rt::IpcSpscRing< ServoFeedbackFrame, Depth > feedback