plcopen 0.22.0
载入中...
搜索中...
未找到
servo.h
浏览该文件的文档.
1#pragma once
2
3// ADR-0004 (Accepted 2026-07-05): the stable narrow hardware interface for
4// Phase B5 (plcopen-fieldbus) and B7 (RT reference executor). The core L5
5// never holds a Servo pointer — an outer executor calls the interface at the
6// cycle boundary and bridges feedback into the existing AxisModel hooks and
7// snapshots into write_setpoints. Adapters are outer-ring composition, not a
8// kernel dependency: the core keeps value semantics, deterministic replay,
9// and zero OS contact.
10//
11// Interface contract: no allocation, no exceptions, bounded time. The
12// executor owns threads and clocks. CSP/CSV/CST drive-side mode semantics
13// extend this interface with B5 (real-drive context); the mode manager
14// skeleton lives in mode_manager.h.
15
16#include <cstddef>
17
18#include "axis/state.h"
19#include "rt/error.h"
20
22{
23
24// Full-order feedforward per 6.3-#9: the library generates every order
25// unconditionally; the drive wires what it needs.
27{
28 double position = 0.0;
29 double velocity = 0.0;
30 double acceleration = 0.0;
31 double torque = 0.0;
32 double torque_limit = 0.0;
36 double torque_jerk_limit = 0.0;
38 bool torque_mode = false;
39};
40
42{
43 static constexpr std::size_t DigitalInputCount = 4;
44
45 double position = 0.0;
46 double velocity = 0.0;
47 double acceleration = 0.0;
48 double torque = 0.0;
51};
52
53class Servo
54{
55 public:
56 virtual ~Servo() = default;
57 virtual void write_setpoints(const ServoSetpoints &setpoints) = 0;
58 virtual void read_feedback(ServoFeedback &feedback) = 0;
59};
60
61// Bridge helpers (ADR-0004 decision #2, KB-105 T28): the executor composes
62// these at the cycle boundary; they must not alter semantics — the golden
63// replays run bit-identical through the bridge (verified in the adapters
64// suite). Numeric non-finite fields reject the whole frame atomically so
65// digital inputs and AxisInfoInputs are not partially overwritten.
67{
68 ServoSetpoints setpoints{};
69 setpoints.position = snapshot.command_position;
70 setpoints.velocity = snapshot.command_velocity;
71 setpoints.acceleration = snapshot.command_acceleration;
72 setpoints.torque = snapshot.command_torque;
73 setpoints.torque_limit = snapshot.command_torque_limit;
77 setpoints.torque_jerk_limit = snapshot.torque_jerk_limit;
78 setpoints.torque_direction = snapshot.torque_direction;
79 setpoints.torque_mode = snapshot.torque_mode;
80 return setpoints;
81}
82
84{
85 const rt::ErrorCode status = axis.set_actual_feedback(
86 feedback.position, feedback.velocity, feedback.acceleration, feedback.torque);
87 if(status != rt::ErrorCode::ok) {
88 return status;
89 }
90 for(std::size_t i = 0; i < ServoFeedback::DigitalInputCount; ++i) {
91 (void)axis.set_digital_input(i, feedback.digital_inputs[i]);
92 }
93 axis.set_axis_info_inputs(feedback.info);
94 return rt::ErrorCode::ok;
95}
96
97// Simulation reference implementation (the interface's acceptance vehicle):
98// an ideal drive that tracks setpoints exactly and echoes them as actuals.
99// The same application code runs against real hardware by swapping this
100// object — the zero-modification promise of the adoption funnel.
101class ServoSim final : public Servo
102{
103 public:
104 void write_setpoints(const ServoSetpoints &setpoints) override { state_ = setpoints; }
105
106 void read_feedback(ServoFeedback &feedback) override
107 {
108 feedback.position = state_.position;
109 feedback.velocity = state_.velocity;
110 feedback.acceleration = state_.acceleration;
111 feedback.torque = state_.torque;
112 for (std::size_t i = 0; i < ServoFeedback::DigitalInputCount; ++i)
113 {
114 feedback.digital_inputs[i] = digital_inputs_[i];
115 }
116 feedback.info = info_;
117 }
118
119 // Test-side controls.
120 void set_digital_input(std::size_t input, bool level)
121 {
123 {
124 digital_inputs_[input] = level;
125 }
126 }
127
128 void set_info(const axis::AxisModel::AxisInfoInputs &info) { info_ = info; }
129
130 private:
131 ServoSetpoints state_{};
132 bool digital_inputs_[ServoFeedback::DigitalInputCount] = {};
133 axis::AxisModel::AxisInfoInputs info_{};
134};
135
136} // namespace plcopen::core::adapters
void read_feedback(ServoFeedback &feedback) override
void set_digital_input(std::size_t input, bool level)
void write_setpoints(const ServoSetpoints &setpoints) override
void set_info(const axis::AxisModel::AxisInfoInputs &info)
virtual void write_setpoints(const ServoSetpoints &setpoints)=0
virtual void read_feedback(ServoFeedback &feedback)=0
ServoSetpoints make_setpoints(const axis::AxisSnapshot &snapshot)
定义 servo.h:66
rt::ErrorCode bridge_feedback(axis::AxisModel &axis, const ServoFeedback &feedback)
定义 servo.h:83
bool digital_inputs[DigitalInputCount]
定义 servo.h:49
static constexpr std::size_t DigitalInputCount
定义 servo.h:43
axis::AxisModel::AxisInfoInputs info
定义 servo.h:50