plcopen 0.22.0
载入中...
搜索中...
未找到
profile.h
浏览该文件的文档.
1#pragma once
2
3#include <array>
4#include <cmath>
5#include <cstdint>
6
7#include "axis/state.h"
8#include "fb/motion.h"
9#include "rt/error.h"
10
11namespace plcopen::core::fb
12{
13
14// Profile-table facades (MC_Position/Velocity/AccelerationProfile). The table
15// is a caller-owned fixed array of axis::ProfileSegment; external profile-table
16// import/parsing stays outside the runtime (KB-010 carried over). Segment
17// durations are cycle counts; time_scale multiplies them.
19{
20public:
23 std::size_t segment_count = 0;
24 double time_scale = 1.0;
25 bool continuous_update = false;
27 bool execute = false;
29
30protected:
32 {
33 const bool rising = execute && !last_execute_;
34 const bool falling = !execute && last_execute_;
35 last_execute_ = execute;
36 if(rising) {
38 first_id_ = 0;
39 last_id_ = 0;
40 continuous_update_enabled_ = continuous_update;
41 terminal_low_cycle_ = false;
42 } else if(!execute && terminal_low_cycle_) {
44 first_id_ = 0;
45 last_id_ = 0;
46 terminal_low_cycle_ = false;
47 } else if(falling &&
48 (outputs.done || outputs.command_aborted || outputs.error)) {
50 first_id_ = 0;
51 last_id_ = 0;
52 }
53 return rising;
54 }
55
56 bool inputs_valid() const
57 {
58 return axis_ref != nullptr && segments != nullptr && segment_count >= 1 &&
60 time_scale > 0.0;
61 }
62
64 {
66 outputs.error = true;
67 outputs.error_id = code;
68 first_id_ = 0;
69 last_id_ = 0;
70 }
71
72 std::int64_t scaled_duration(std::int64_t duration_cycles) const
73 {
74 if(duration_cycles <= 0) {
75 return 0;
76 }
77 const double scaled = static_cast<double>(duration_cycles) * time_scale;
78 return static_cast<std::int64_t>(std::llround(scaled));
79 }
80
81 void track(std::uint32_t first_id, std::uint32_t last_id)
82 {
83 first_id_ = first_id;
84 last_id_ = last_id;
86 outputs.command_id = first_id;
87 outputs.command_accepted = true;
88 outputs.busy = true;
89 outputs.active = true;
90 }
91
92 bool tracked_active(std::uint32_t active_id) const
93 {
94 return active_id >= first_id_ && active_id <= last_id_;
95 }
96
98 {
99 for(std::uint64_t id = first_id_; id <= last_id_; ++id) {
100 if(axis.command_pending(static_cast<std::uint32_t>(id))) {
101 return true;
102 }
103 }
104 return false;
105 }
106
107 bool scaled_duration_valid(std::int64_t duration_cycles) const
108 {
109 if(duration_cycles <= 0) {
110 return false;
111 }
112 const double scaled = static_cast<double>(duration_cycles) * time_scale;
113 if(!std::isfinite(scaled)) {
114 return false;
115 }
116 const double rounded = std::round(scaled);
117 return rounded >= 1.0 && rounded < std::ldexp(1.0, 63);
118 }
119
120 std::uint32_t first_id_ = 0;
121 std::uint32_t last_id_ = 0;
122
124 {
125 return continuous_update_enabled_;
126 }
127
129 {
130 terminal_low_cycle_ = !execute;
131 }
132
133private:
134 bool last_execute_ = false;
135 bool continuous_update_enabled_ = false;
136 bool terminal_low_cycle_ = false;
137};
138
140{
141public:
142 double position_scale = 1.0;
143 double position_offset = 0.0;
144
145 void call()
146 {
147 if(rising_edge()) {
148 submit();
149 } else {
150 update();
151 }
152 observe();
153 }
154
155private:
156 void submit()
157 {
158 if(!inputs_valid() || !std::isfinite(position_scale) || !std::isfinite(position_offset)) {
160 return;
161 }
162 for(std::size_t i = 0; i < segment_count; ++i) {
163 const std::int64_t duration = segments[i].duration_cycles;
164 if(duration < 0 || (duration > 0 && !scaled_duration_valid(duration))) {
166 return;
167 }
168 }
169
170 std::array<axis::AxisCommand, axis::AxisModel::QueueCapacity> commands{};
171 for(std::size_t i = 0; i < segment_count; ++i) {
172 const axis::ProfileSegment &segment = segments[i];
173 axis::AxisCommand &command = commands[i];
174 command.kind = segment.relative ? axis::CommandKind::move_relative
176 command.value = segment.relative
177 ? segment.target * position_scale
178 : segment.target * position_scale + position_offset;
179 command.velocity = segment.velocity;
180 command.acceleration = segment.acceleration;
181 command.deceleration = segment.deceleration;
182 command.jerk = segment.jerk;
183 command.min_duration_cycles = scaled_duration(segment.duration_cycles);
184 command.buffer_mode =
186 }
187 const rt::ErrorCode preflight =
188 axis_ref->preflight_position_sequence(commands.data(), segment_count);
189 if(preflight != rt::ErrorCode::ok) {
190 fail(preflight);
191 return;
192 }
193
194 last_target_ = segments[0].target;
195 start_position_ = axis_ref->snapshot().command_position;
196
197 std::uint32_t first_id = 0;
198 std::uint32_t last_id = 0;
199 for(std::size_t i = 0; i < segment_count; ++i) {
200 const rt::Result<std::uint32_t> accepted = axis_ref->submit(commands[i]);
201 if(!accepted) {
202 fail(accepted.error());
203 return;
204 }
205 if(i == 0) {
206 first_id = accepted.value();
207 }
208 last_id = accepted.value();
209 }
210 track(first_id, last_id);
211 }
212
213 void update()
214 {
215 // ContinuousUpdate retargets single-segment profiles only; the linked
216 // multi-segment case has no defined retarget point in the rewrite core.
217 if(!execute || !continuous_update_allowed() || first_id_ == 0 || axis_ref == nullptr ||
218 segment_count != 1 || segments == nullptr || segments[0].target == last_target_) {
219 return;
220 }
221 last_target_ = segments[0].target;
222 // Relative retargets re-resolve from the original command start.
223 const double target = segments[0].relative
224 ? start_position_ + segments[0].target * position_scale
226 const rt::ErrorCode updated = axis_ref->update_active_target(first_id_, target);
227 if(updated != rt::ErrorCode::ok) {
228 outputs.error = true;
229 outputs.error_id = updated;
230 }
231 }
232
233 void observe()
234 {
235 if(first_id_ == 0 || axis_ref == nullptr || outputs.done) {
236 return;
237 }
238 const axis::AxisSnapshot &snapshot = axis_ref->snapshot();
239 for(std::uint64_t id = first_id_; id <= last_id_; ++id) {
240 const rt::ErrorCode command_error =
241 axis_ref->command_error(static_cast<std::uint32_t>(id));
242 if(command_error != rt::ErrorCode::ok) {
243 outputs.error = true;
244 outputs.error_id = command_error;
245 outputs.command_aborted = false;
246 outputs.done = false;
247 outputs.busy = false;
248 outputs.active = false;
250 return;
251 }
252 }
253 if(snapshot.last_completed_command_id == last_id_) {
254 outputs.done = true;
255 outputs.busy = false;
256 outputs.active = false;
258 return;
259 }
260 if(tracked_active(snapshot.active_command_id) || tracked_pending(*axis_ref)) {
261 outputs.busy = true;
262 outputs.active = true;
263 return;
264 }
265 outputs.command_aborted = true;
266 outputs.done = false;
267 outputs.busy = false;
268 outputs.active = false;
270 first_id_ = 0;
271 last_id_ = 0;
272 }
273
274 double last_target_ = 0.0;
275 double start_position_ = 0.0;
276};
277
278// Shared implementation for the velocity-driving profiles. done means "final
279// segment velocity is being held" — an ongoing state, not a completed command.
281{
282protected:
283 // scale/offset semantics differ per block: the velocity profile transforms
284 // the target velocity, the acceleration profile transforms the
285 // acceleration/deceleration limits.
286 void submit_segments(double velocity_scale,
287 double velocity_offset,
288 double acceleration_scale,
289 double acceleration_offset)
290 {
291 if(!inputs_valid() || !std::isfinite(velocity_scale) || !std::isfinite(velocity_offset) ||
292 !std::isfinite(acceleration_scale) || !std::isfinite(acceleration_offset)) {
294 return;
295 }
296 last_target_ = segments[0].target;
297
298 for(std::size_t i = 0; i < segment_count; ++i) {
299 const axis::ProfileSegment &segment = segments[i];
300 const double target_velocity = segment.target * velocity_scale + velocity_offset;
301 const double acceleration =
302 segment.acceleration * acceleration_scale + acceleration_offset;
303 const double deceleration =
304 segment.deceleration * acceleration_scale + acceleration_offset;
305 const bool final_segment = i + 1 == segment_count;
306 if(!std::isfinite(target_velocity) || target_velocity == 0.0 ||
307 !std::isfinite(acceleration) || acceleration <= 0.0 ||
308 !std::isfinite(deceleration) || deceleration <= 0.0 ||
309 !std::isfinite(segment.jerk) || segment.jerk <= 0.0 ||
310 (!final_segment && !scaled_duration_valid(segment.duration_cycles))) {
312 return;
313 }
314 }
315
316 std::uint32_t first_id = 0;
317 std::uint32_t last_id = 0;
318 for(std::size_t i = 0; i < segment_count; ++i) {
319 const axis::ProfileSegment &segment = segments[i];
320 const double target_velocity = segment.target * velocity_scale + velocity_offset;
321 const double acceleration =
322 segment.acceleration * acceleration_scale + acceleration_offset;
323 const double deceleration =
324 segment.deceleration * acceleration_scale + acceleration_offset;
325 const bool final_segment = i + 1 == segment_count;
326 axis::AxisCommand command{};
328 command.value = target_velocity;
329 command.velocity = std::fabs(target_velocity);
330 command.acceleration = acceleration;
331 command.deceleration = deceleration;
332 command.jerk = segment.jerk;
333 // The final segment holds its velocity; earlier segments finish
334 // after their scaled duration.
335 command.min_duration_cycles =
336 final_segment ? 0 : scaled_duration(segment.duration_cycles);
337 command.buffer_mode =
339 const rt::Result<std::uint32_t> accepted = axis_ref->submit(command);
340 if(!accepted) {
341 fail(accepted.error());
342 return;
343 }
344 if(i == 0) {
345 first_id = accepted.value();
346 }
347 last_id = accepted.value();
348 }
349 track(first_id, last_id);
350 }
351
352 void update_segments(double velocity_scale,
353 double velocity_offset,
354 double acceleration_scale,
355 double acceleration_offset)
356 {
357 if(!execute || !continuous_update_allowed() || first_id_ == 0 || axis_ref == nullptr ||
358 segment_count != 1 || segments == nullptr || segments[0].target == last_target_) {
359 return;
360 }
361 // Velocity applies within one cycle in the rewrite core, so retarget
362 // is a plain aborting re-submission with a fresh command id.
363 submit_segments(velocity_scale, velocity_offset, acceleration_scale,
364 acceleration_offset);
365 }
366
367 void observe()
368 {
369 if(first_id_ == 0 || axis_ref == nullptr) {
370 return;
371 }
372 const axis::AxisSnapshot &snapshot = axis_ref->snapshot();
373 for(std::uint64_t id = first_id_; id <= last_id_; ++id) {
374 const rt::ErrorCode command_error =
375 axis_ref->command_error(static_cast<std::uint32_t>(id));
376 if(command_error != rt::ErrorCode::ok) {
377 outputs.error = true;
378 outputs.error_id = command_error;
379 outputs.command_aborted = false;
380 outputs.done = false;
381 outputs.busy = false;
382 outputs.active = false;
384 return;
385 }
386 }
387 if(tracked_active(snapshot.active_command_id)) {
388 outputs.busy = true;
389 outputs.active = true;
390 outputs.done = snapshot.active_command_id == last_id_;
391 return;
392 }
393 outputs.command_aborted = true;
394 outputs.done = false;
395 outputs.busy = false;
396 outputs.active = false;
398 first_id_ = 0;
399 last_id_ = 0;
400 }
401
402private:
403 double last_target_ = 0.0;
404};
405
407{
408public:
409 double velocity_scale = 1.0;
410 double velocity_offset = 0.0;
411
412 void call()
413 {
414 if(rising_edge()) {
416 } else {
418 }
419 observe();
420 }
421};
422
424{
425public:
426 double acceleration_scale = 1.0;
428
429 void call()
430 {
431 if(rising_edge()) {
432 if(!inputs_valid()) {
434 } else {
435 const rt::Result<std::uint32_t> accepted =
436 axis_ref->submit_acceleration_profile(
439 if(!accepted) {
440 fail(accepted.error());
441 } else {
442 track(accepted.value(), accepted.value());
443 }
444 }
445 }
446 if(first_id_ == 0 || axis_ref == nullptr) {
447 return;
448 }
449 const rt::ErrorCode command_error = axis_ref->command_error(first_id_);
450 if(command_error != rt::ErrorCode::ok) {
451 outputs.error = true;
452 outputs.error_id = command_error;
453 outputs.command_aborted = false;
454 outputs.busy = false;
455 outputs.active = false;
457 return;
458 }
459 const axis::AxisSnapshot &snapshot = axis_ref->snapshot();
460 if(snapshot.active_command_id == first_id_) {
461 outputs.busy = true;
462 outputs.active = true;
464 return;
465 }
466 outputs.command_aborted = true;
467 outputs.done = false;
468 outputs.busy = false;
469 outputs.active = false;
471 }
472};
473
474} // namespace plcopen::core::fb
static constexpr std::size_t QueueCapacity
const axis::ProfileSegment * segments
bool tracked_pending(const axis::AxisModel &axis) const
bool tracked_active(std::uint32_t active_id) const
bool scaled_duration_valid(std::int64_t duration_cycles) const
void track(std::uint32_t first_id, std::uint32_t last_id)
void fail(rt::ErrorCode code)
std::int64_t scaled_duration(std::int64_t duration_cycles) const
void update_segments(double velocity_scale, double velocity_offset, double acceleration_scale, double acceleration_offset)
void submit_segments(double velocity_scale, double velocity_offset, double acceleration_scale, double acceleration_offset)
void clear(MotionOutputs &outputs)