plcopen 0.22.0
载入中...
搜索中...
未找到
feetech.h
浏览该文件的文档.
1#pragma once
2
3#include <cmath>
4#include <cstddef>
5#include <cstdint>
6
7#include "adapters/servo.h"
8#include "rt/error.h"
9
11{
12
13class FeetechBus;
14
15enum class FeetechInstruction : std::uint8_t
16{
17 ping = 0x01,
18 read = 0x02,
19 write = 0x03,
20 sync_read = 0x82,
21 sync_write = 0x83,
22};
23
25{
26 static constexpr std::size_t Capacity = 272;
27 std::uint8_t bytes[Capacity] = {};
28 std::size_t size = 0;
29};
30
32{
33 std::uint8_t id = 0;
34 std::uint8_t error = 0;
35 std::uint8_t parameters[253] = {};
36 std::size_t parameter_count = 0;
37 bool ready = false;
38};
39
40inline std::uint8_t feetech_checksum(const std::uint8_t *bytes,
41 std::size_t begin, std::size_t end)
42{
43 unsigned int sum = 0;
44 for(std::size_t i = begin; i < end; ++i) sum += bytes[i];
45 return static_cast<std::uint8_t>(~sum);
46}
47
48inline rt::ErrorCode encode_instruction(std::uint8_t id,
49 FeetechInstruction instruction,
50 const std::uint8_t *parameters,
51 std::size_t parameter_count,
52 FeetechPacket &packet)
53{
54 if(id == 0xFF || parameter_count > 253 ||
55 (parameter_count != 0 && parameters == nullptr)) {
56 packet = {};
58 }
59 packet = {};
60 packet.size = parameter_count + 6;
61 packet.bytes[0] = 0xFF;
62 packet.bytes[1] = 0xFF;
63 packet.bytes[2] = id;
64 packet.bytes[3] = static_cast<std::uint8_t>(parameter_count + 2);
65 packet.bytes[4] = static_cast<std::uint8_t>(instruction);
66 for(std::size_t i = 0; i < parameter_count; ++i) {
67 packet.bytes[5 + i] = parameters[i];
68 }
69 packet.bytes[packet.size - 1] = feetech_checksum(packet.bytes, 2, packet.size - 1);
70 return rt::ErrorCode::ok;
71}
72
73inline rt::ErrorCode encode_status(std::uint8_t id, std::uint8_t error,
74 const std::uint8_t *parameters,
75 std::size_t parameter_count,
76 FeetechPacket &packet)
77{
78 if(id == 0xFF || parameter_count > 253 ||
79 (parameter_count != 0 && parameters == nullptr)) {
80 packet = {};
82 }
83 packet = {};
84 packet.size = parameter_count + 6;
85 packet.bytes[0] = 0xFF;
86 packet.bytes[1] = 0xFF;
87 packet.bytes[2] = id;
88 packet.bytes[3] = static_cast<std::uint8_t>(parameter_count + 2);
89 packet.bytes[4] = error;
90 for(std::size_t i = 0; i < parameter_count; ++i) {
91 packet.bytes[5 + i] = parameters[i];
92 }
93 packet.bytes[packet.size - 1] = feetech_checksum(packet.bytes, 2, packet.size - 1);
94 return rt::ErrorCode::ok;
95}
96
98{
99public:
100 void push(std::uint8_t byte, FeetechFrame &frame)
101 {
102 frame.ready = false;
103 if(size_ == 0) {
104 if(byte == 0xFF) buffer_[size_++] = byte;
105 return;
106 }
107 if(size_ == 1) {
108 if(byte == 0xFF) buffer_[size_++] = byte;
109 else size_ = 0;
110 return;
111 }
112 if(size_ >= FeetechPacket::Capacity) {
113 reset_with(byte);
114 return;
115 }
116 buffer_[size_++] = byte;
117 if(size_ == 4) {
118 expected_ = static_cast<std::size_t>(buffer_[3]) + 4;
119 if(buffer_[3] < 2 || expected_ > FeetechPacket::Capacity) {
120 reset_with(byte);
121 }
122 return;
123 }
124 if(expected_ == 0 || size_ < expected_) return;
125 if(feetech_checksum(buffer_, 2, expected_ - 1) == buffer_[expected_ - 1]) {
126 frame.id = buffer_[2];
127 frame.error = buffer_[4];
128 frame.parameter_count = static_cast<std::size_t>(buffer_[3]) - 2;
129 for(std::size_t i = 0; i < frame.parameter_count; ++i) {
130 frame.parameters[i] = buffer_[5 + i];
131 }
132 frame.ready = true;
133 }
134 size_ = 0;
135 expected_ = 0;
136 }
137
138private:
139 void reset_with(std::uint8_t byte)
140 {
141 size_ = 0;
142 expected_ = 0;
143 if(byte == 0xFF) buffer_[size_++] = byte;
144 }
145
146 std::uint8_t buffer_[FeetechPacket::Capacity] = {};
147 std::size_t size_ = 0;
148 std::size_t expected_ = 0;
149};
150
151inline std::uint16_t encode_sign_magnitude(int value, std::uint16_t sign_mask)
152{
153 const std::int64_t wide = value;
154 const unsigned int magnitude = static_cast<unsigned int>(wide < 0 ? -wide : wide);
155 return static_cast<std::uint16_t>((value < 0 ? sign_mask : 0u) |
156 (magnitude & (sign_mask - 1u)));
157}
158
159inline int decode_sign_magnitude(std::uint16_t value, std::uint16_t sign_mask)
160{
161 const int magnitude = static_cast<int>(value & (sign_mask - 1u));
162 return (value & sign_mask) != 0 ? -magnitude : magnitude;
163}
164
166{
167 static constexpr double Pi = 3.14159265358979323846;
168 static constexpr double RadiansPerCount = 2.0 * Pi / 4096.0;
169
172 std::size_t stale_limit_cycles = 5;
173};
174
176{
177public:
178 static constexpr std::size_t StorageCapacity = 32;
179 static constexpr std::size_t ProtocolCycleCapacity = 31;
180
182 {
183 if(!std::isfinite(config.velocity_units_per_radian_second) ||
184 config.velocity_units_per_radian_second <= 0.0 ||
185 !std::isfinite(config.acceleration_units_per_radian_second2) ||
187 config.stale_limit_cycles == 0) {
189 }
190 config_ = config;
191 return rt::ErrorCode::ok;
192 }
193
194 rt::ErrorCode bind(const std::uint8_t *ids, std::size_t count)
195 {
196 if(count == 0 || ids == nullptr) return rt::ErrorCode::invalid_argument;
199 }
200 for(std::size_t i = 0; i < count; ++i) {
201 if(ids[i] == 0 || ids[i] == 0xFE || ids[i] == 0xFF) {
203 }
204 for(std::size_t j = 0; j < i; ++j) {
205 if(ids[i] == ids[j]) return rt::ErrorCode::invalid_argument;
206 }
207 }
208 count_ = count;
209 for(std::size_t i = 0; i < count_; ++i) {
210 ids_[i] = ids[i];
211 feedback_[i] = {};
212 feedback_[i].info.communication_ready = false;
213 stale_cycles_[i] = 0;
214 received_[i] = false;
215 raw_status_[i] = 0;
216 }
217 return rt::ErrorCode::ok;
218 }
219
220 rt::ErrorCode build_cycle(const ServoSetpoints *setpoints, std::size_t count,
223 {
224 if(count != count_ || setpoints == nullptr) return rt::ErrorCode::invalid_argument;
225 std::uint8_t write_parameters[2 + ProtocolCycleCapacity * 8] = {};
226 write_parameters[0] = 41;
227 write_parameters[1] = 7;
228 std::size_t cursor = 2;
229 for(std::size_t i = 0; i < count_; ++i) {
230 const std::uint16_t position_counts = wrap_units(
231 setpoints[i].position / FeetechConfig::RadiansPerCount + 2048.0,
232 65536u);
233 const int velocity_units = static_cast<int>(wrap_units(
234 std::fabs(setpoints[i].velocity) *
235 config_.velocity_units_per_radian_second,
236 32768u));
237 const std::uint8_t acceleration_units = static_cast<std::uint8_t>(
238 wrap_units(std::fabs(setpoints[i].acceleration) *
239 config_.acceleration_units_per_radian_second2,
240 256u));
241 write_parameters[cursor++] = ids_[i];
242 write_parameters[cursor++] = static_cast<std::uint8_t>(acceleration_units);
243 write_parameters[cursor++] = static_cast<std::uint8_t>(position_counts);
244 write_parameters[cursor++] = static_cast<std::uint8_t>(position_counts >> 8);
245 write_parameters[cursor++] = 0;
246 write_parameters[cursor++] = 0;
247 const std::uint16_t velocity = encode_sign_magnitude(
248 setpoints[i].velocity < 0.0 ? -velocity_units : velocity_units, 0x8000u);
249 write_parameters[cursor++] = static_cast<std::uint8_t>(velocity);
250 write_parameters[cursor++] = static_cast<std::uint8_t>(velocity >> 8);
251 }
253 0xFE, FeetechInstruction::sync_write, write_parameters, cursor, sync_write);
254 if(result != rt::ErrorCode::ok) return result;
255
256 std::uint8_t read_parameters[2 + ProtocolCycleCapacity] = {};
257 read_parameters[0] = 56;
258 read_parameters[1] = 8;
259 for(std::size_t i = 0; i < count_; ++i) read_parameters[2 + i] = ids_[i];
261 read_parameters, count_ + 2, sync_read);
262 }
263
269
270 rt::ErrorCode build_initialize(std::size_t index, FeetechPacket &packet) const
271 {
272 if(index >= count_) return rt::ErrorCode::out_of_range;
273 const std::uint8_t parameters[] = {33, 0};
275 parameters, 2, packet);
276 }
277
278 void latch(std::size_t index, const ServoSetpoints &setpoints)
279 {
280 if(index >= count_) return;
281 setpoints_[index] = setpoints;
282 setpoints_[index].torque = 0.0;
283 }
284
286 {
287 for(std::size_t i = 0; i < count_; ++i) received_[i] = false;
288 }
289
290 void consume(const FeetechPacket &packet)
291 {
292 FeetechParser parser;
293 FeetechFrame frame{};
294 for(std::size_t i = 0; i < packet.size; ++i) parser.push(packet.bytes[i], frame);
295 if(!frame.ready || frame.parameter_count != 8) return;
296 consume(frame);
297 }
298
299 void push(std::uint8_t byte)
300 {
301 FeetechFrame frame{};
302 parser_.push(byte, frame);
303 if(frame.ready) consume(frame);
304 }
305
306 std::size_t count() const { return count_; }
307
308private:
309 void consume(const FeetechFrame &frame)
310 {
311 if(frame.parameter_count != 8) return;
312 const std::size_t index = find(frame.id);
313 if(index >= count_) return;
314 const std::uint16_t position =
315 static_cast<std::uint16_t>(frame.parameters[0] |
316 (static_cast<std::uint16_t>(frame.parameters[1]) << 8));
317 const std::uint16_t velocity =
318 static_cast<std::uint16_t>(frame.parameters[2] |
319 (static_cast<std::uint16_t>(frame.parameters[3]) << 8));
320 const std::uint16_t load =
321 static_cast<std::uint16_t>(frame.parameters[4] |
322 (static_cast<std::uint16_t>(frame.parameters[5]) << 8));
323 feedback_[index].position =
324 (static_cast<int>(position) - 2048) * FeetechConfig::RadiansPerCount;
325 feedback_[index].velocity =
326 decode_sign_magnitude(velocity, 0x8000u) /
328 feedback_[index].acceleration = 0.0;
329 feedback_[index].torque = decode_sign_magnitude(load, 0x0400u) / 1023.0;
330 feedback_[index].info.communication_ready = true;
331 raw_status_[index] = frame.error;
332 received_[index] = true;
333 stale_cycles_[index] = 0;
334 }
335
336public:
337
339 {
340 for(std::size_t i = 0; i < count_; ++i) {
341 if(received_[i]) continue;
342 if(stale_cycles_[i] < config_.stale_limit_cycles) ++stale_cycles_[i];
343 if(stale_cycles_[i] >= config_.stale_limit_cycles) {
344 feedback_[i].info.communication_ready = false;
345 }
346 }
347 }
348
349 void read_feedback(std::size_t index, ServoFeedback &feedback) const
350 {
351 feedback = index < count_ ? feedback_[index] : ServoFeedback{};
352 if(index >= count_) feedback.info.communication_ready = false;
353 }
354
355 bool stale(std::size_t index) const
356 {
357 return index < count_ && stale_cycles_[index] != 0;
358 }
359
360 std::uint8_t raw_status(std::size_t index) const
361 {
362 return index < count_ ? raw_status_[index] : 0;
363 }
364
365 static constexpr std::size_t cycle_wire_bytes(std::size_t count)
366 {
367 return 16 + 23 * count;
368 }
369
370 static constexpr double utilization_percent(std::size_t count,
371 std::size_t frequency_hz)
372 {
373 return static_cast<double>(cycle_wire_bytes(count) * 10 * frequency_hz) /
374 10000.0;
375 }
376
377private:
378 static std::uint16_t wrap_units(double value, unsigned int modulus)
379 {
380 if(!std::isfinite(value)) return 0;
381 double wrapped = std::fmod(std::round(value), static_cast<double>(modulus));
382 if(wrapped < 0.0) wrapped += static_cast<double>(modulus);
383 return static_cast<std::uint16_t>(wrapped);
384 }
385
386 std::size_t find(std::uint8_t id) const
387 {
388 for(std::size_t i = 0; i < count_; ++i) {
389 if(ids_[i] == id) return i;
390 }
391 return count_;
392 }
393
394 FeetechConfig config_{};
395 std::uint8_t ids_[StorageCapacity] = {};
396 ServoFeedback feedback_[StorageCapacity] = {};
397 std::size_t stale_cycles_[StorageCapacity] = {};
398 bool received_[StorageCapacity] = {};
399 std::uint8_t raw_status_[StorageCapacity] = {};
400 ServoSetpoints setpoints_[StorageCapacity] = {};
401 FeetechParser parser_{};
402 std::size_t count_ = 0;
403};
404
405class FeetechServo final : public Servo
406{
407public:
408 FeetechServo(FeetechBus &bus, std::size_t index)
409 : bus_(&bus)
410 , index_(index)
411 {
412 }
413
415 {
416 setpoints_ = setpoints;
417 setpoints_.torque = 0.0;
418 bus_->latch(index_, setpoints_);
419 }
420
421 void read_feedback(ServoFeedback &feedback) override
422 {
423 bus_->read_feedback(index_, feedback);
424 }
425
426 const ServoSetpoints &setpoints() const { return setpoints_; }
427
428private:
429 FeetechBus *bus_ = nullptr;
430 std::size_t index_ = 0;
431 ServoSetpoints setpoints_{};
432};
433
435{
436public:
438 {
439 if(!std::isfinite(config.velocity_units_per_radian_second) ||
440 config.velocity_units_per_radian_second <= 0.0 ||
441 !std::isfinite(config.acceleration_units_per_radian_second2) ||
444 }
445 config_ = config;
446 return rt::ErrorCode::ok;
447 }
448
449 rt::ErrorCode bind(const std::uint8_t *ids, std::size_t count)
450 {
451 FeetechBus validator;
452 const rt::ErrorCode result = validator.bind(ids, count);
453 if(result != rt::ErrorCode::ok) return result;
454 count_ = count;
455 for(std::size_t i = 0; i < count_; ++i) ids_[i] = ids[i];
456 return rt::ErrorCode::ok;
457 }
458
461 FeetechPacket *responses,
462 std::size_t response_capacity)
463 {
464 FeetechFrame write_frame{};
465 FeetechFrame read_frame{};
466 FeetechParser write_parser;
467 FeetechParser read_parser;
468 for(std::size_t i = 0; i < sync_write.size; ++i) {
469 write_parser.push(sync_write.bytes[i], write_frame);
470 }
471 for(std::size_t i = 0; i < sync_read.size; ++i) {
472 read_parser.push(sync_read.bytes[i], read_frame);
473 }
474 if(responses == nullptr || response_capacity < count_ || !write_frame.ready ||
475 !read_frame.ready || write_frame.id != 0xFE || read_frame.id != 0xFE ||
476 write_frame.error != static_cast<std::uint8_t>(FeetechInstruction::sync_write) ||
477 read_frame.error != static_cast<std::uint8_t>(FeetechInstruction::sync_read) ||
478 write_frame.parameter_count < 2 || read_frame.parameter_count != count_ + 2 ||
479 write_frame.parameters[0] != 41 || write_frame.parameters[1] != 7 ||
480 read_frame.parameters[0] != 56 || read_frame.parameters[1] != 8) {
482 }
483 const std::size_t entries = (write_frame.parameter_count - 2) / 8;
484 if(entries != count_ || write_frame.parameter_count != 2 + entries * 8) {
486 }
487 std::size_t cursor = 2;
488 for(std::size_t entry = 0; entry < entries; ++entry) {
489 const std::uint8_t id = write_frame.parameters[cursor++];
490 const std::size_t index = find(id);
491 if(index >= count_) return rt::ErrorCode::invalid_argument;
492 acceleration_[index] = write_frame.parameters[cursor++];
493 position_[index] = static_cast<std::uint16_t>(
494 write_frame.parameters[cursor] |
495 (static_cast<std::uint16_t>(write_frame.parameters[cursor + 1]) << 8));
496 cursor += 4;
497 velocity_[index] = static_cast<std::uint16_t>(
498 write_frame.parameters[cursor] |
499 (static_cast<std::uint16_t>(write_frame.parameters[cursor + 1]) << 8));
500 cursor += 2;
501 }
502 for(std::size_t i = 0; i < count_; ++i) {
503 std::uint8_t present[8] = {
504 static_cast<std::uint8_t>(position_[i]),
505 static_cast<std::uint8_t>(position_[i] >> 8),
506 static_cast<std::uint8_t>(velocity_[i]),
507 static_cast<std::uint8_t>(velocity_[i] >> 8),
508 0, 0, 120, 25};
509 encode_status(ids_[i], 0, present, 8, responses[i]);
510 }
511 return rt::ErrorCode::ok;
512 }
513
514private:
515 std::size_t find(std::uint8_t id) const
516 {
517 for(std::size_t i = 0; i < count_; ++i) {
518 if(ids_[i] == id) return i;
519 }
520 return count_;
521 }
522
523 FeetechConfig config_{};
524 std::uint8_t ids_[FeetechBus::StorageCapacity] = {};
525 std::uint16_t position_[FeetechBus::StorageCapacity] = {};
526 std::uint16_t velocity_[FeetechBus::StorageCapacity] = {};
527 std::uint8_t acceleration_[FeetechBus::StorageCapacity] = {};
528 std::size_t count_ = 0;
529};
530
531} // namespace plcopen::core::adapters
rt::ErrorCode bind(const std::uint8_t *ids, std::size_t count)
static constexpr std::size_t StorageCapacity
void latch(std::size_t index, const ServoSetpoints &setpoints)
bool stale(std::size_t index) const
rt::ErrorCode build_cycle(const ServoSetpoints *setpoints, std::size_t count, FeetechPacket &sync_write, FeetechPacket &sync_read)
rt::ErrorCode build_initialize(std::size_t index, FeetechPacket &packet) const
void read_feedback(std::size_t index, ServoFeedback &feedback) const
static constexpr std::size_t cycle_wire_bytes(std::size_t count)
static constexpr double utilization_percent(std::size_t count, std::size_t frequency_hz)
std::uint8_t raw_status(std::size_t index) const
rt::ErrorCode build_cycle(FeetechPacket &sync_write, FeetechPacket &sync_read)
rt::ErrorCode configure(const FeetechConfig &config)
static constexpr std::size_t ProtocolCycleCapacity
void consume(const FeetechPacket &packet)
void push(std::uint8_t byte, FeetechFrame &frame)
void write_setpoints(const ServoSetpoints &setpoints) override
void read_feedback(ServoFeedback &feedback) override
const ServoSetpoints & setpoints() const
FeetechServo(FeetechBus &bus, std::size_t index)
rt::ErrorCode bind(const std::uint8_t *ids, std::size_t count)
rt::ErrorCode configure(const FeetechConfig &config)
rt::ErrorCode exchange(const FeetechPacket &sync_write, const FeetechPacket &sync_read, FeetechPacket *responses, std::size_t response_capacity)
std::uint8_t feetech_checksum(const std::uint8_t *bytes, std::size_t begin, std::size_t end)
rt::ErrorCode encode_instruction(std::uint8_t id, FeetechInstruction instruction, const std::uint8_t *parameters, std::size_t parameter_count, FeetechPacket &packet)
rt::ErrorCode encode_status(std::uint8_t id, std::uint8_t error, const std::uint8_t *parameters, std::size_t parameter_count, FeetechPacket &packet)
int decode_sign_magnitude(std::uint16_t value, std::uint16_t sign_mask)
std::uint16_t encode_sign_magnitude(int value, std::uint16_t sign_mask)
static constexpr double RadiansPerCount
static constexpr std::size_t Capacity
axis::AxisModel::AxisInfoInputs info
定义 servo.h:50