41 std::size_t begin, std::size_t end)
44 for(std::size_t i = begin; i < end; ++i) sum += bytes[i];
45 return static_cast<std::uint8_t
>(~sum);
50 const std::uint8_t *parameters,
51 std::size_t parameter_count,
54 if(
id == 0xFF || parameter_count > 253 ||
55 (parameter_count != 0 && parameters ==
nullptr)) {
60 packet.
size = parameter_count + 6;
61 packet.
bytes[0] = 0xFF;
62 packet.
bytes[1] = 0xFF;
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];
74 const std::uint8_t *parameters,
75 std::size_t parameter_count,
78 if(
id == 0xFF || parameter_count > 253 ||
79 (parameter_count != 0 && parameters ==
nullptr)) {
84 packet.
size = parameter_count + 6;
85 packet.
bytes[0] = 0xFF;
86 packet.
bytes[1] = 0xFF;
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];
104 if(
byte == 0xFF) buffer_[size_++] = byte;
108 if(
byte == 0xFF) buffer_[size_++] = byte;
116 buffer_[size_++] = byte;
118 expected_ =
static_cast<std::size_t
>(buffer_[3]) + 4;
124 if(expected_ == 0 || size_ < expected_)
return;
126 frame.
id = buffer_[2];
127 frame.
error = buffer_[4];
139 void reset_with(std::uint8_t
byte)
143 if(
byte == 0xFF) buffer_[size_++] = byte;
147 std::size_t size_ = 0;
148 std::size_t expected_ = 0;
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)));
161 const int magnitude =
static_cast<int>(value & (sign_mask - 1u));
162 return (value & sign_mask) != 0 ? -magnitude : magnitude;
167 static constexpr double Pi = 3.14159265358979323846;
200 for(std::size_t i = 0; i <
count; ++i) {
201 if(ids[i] == 0 || ids[i] == 0xFE || ids[i] == 0xFF) {
204 for(std::size_t j = 0; j < i; ++j) {
209 for(std::size_t i = 0; i < count_; ++i) {
212 feedback_[i].info.communication_ready =
false;
213 stale_cycles_[i] = 0;
214 received_[i] =
false;
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(
233 const int velocity_units =
static_cast<int>(wrap_units(
234 std::fabs(setpoints[i].velocity) *
235 config_.velocity_units_per_radian_second,
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,
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;
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);
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];
273 const std::uint8_t parameters[] = {33, 0};
275 parameters, 2, packet);
280 if(index >= count_)
return;
281 setpoints_[index] = setpoints;
282 setpoints_[index].
torque = 0.0;
287 for(std::size_t i = 0; i < count_; ++i) received_[i] =
false;
294 for(std::size_t i = 0; i < packet.
size; ++i) parser.
push(packet.
bytes[i], frame);
302 parser_.push(
byte, frame);
306 std::size_t
count()
const {
return count_; }
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));
331 raw_status_[index] = frame.
error;
332 received_[index] =
true;
333 stale_cycles_[index] = 0;
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;
351 feedback = index < count_ ? feedback_[index] :
ServoFeedback{};
357 return index < count_ && stale_cycles_[index] != 0;
362 return index < count_ ? raw_status_[index] : 0;
367 return 16 + 23 *
count;
371 std::size_t frequency_hz)
378 static std::uint16_t wrap_units(
double value,
unsigned int modulus)
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);
386 std::size_t find(std::uint8_t
id)
const
388 for(std::size_t i = 0; i < count_; ++i) {
389 if(ids_[i] ==
id)
return i;
394 FeetechConfig config_{};
401 FeetechParser parser_{};
402 std::size_t count_ = 0;
417 setpoints_.torque = 0.0;
418 bus_->latch(index_, setpoints_);
423 bus_->read_feedback(index_, feedback);
430 std::size_t index_ = 0;
455 for(std::size_t i = 0; i < count_; ++i) ids_[i] = ids[i];
462 std::size_t response_capacity)
468 for(std::size_t i = 0; i <
sync_write.size; ++i) {
471 for(std::size_t i = 0; i <
sync_read.size; ++i) {
474 if(responses ==
nullptr || response_capacity < count_ || !write_frame.
ready ||
475 !read_frame.
ready || write_frame.
id != 0xFE || read_frame.
id != 0xFE ||
484 if(entries != count_ || write_frame.
parameter_count != 2 + entries * 8) {
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);
492 acceleration_[index] = write_frame.
parameters[cursor++];
493 position_[index] =
static_cast<std::uint16_t
>(
495 (
static_cast<std::uint16_t
>(write_frame.
parameters[cursor + 1]) << 8));
497 velocity_[index] =
static_cast<std::uint16_t
>(
499 (
static_cast<std::uint16_t
>(write_frame.
parameters[cursor + 1]) << 8));
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),
515 std::size_t find(std::uint8_t
id)
const
517 for(std::size_t i = 0; i < count_; ++i) {
518 if(ids_[i] ==
id)
return i;
523 FeetechConfig config_{};
528 std::size_t count_ = 0;
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)
void end_feedback_cycle()
void begin_feedback_cycle()
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
std::size_t count() 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)
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 Pi
double velocity_units_per_radian_second
double acceleration_units_per_radian_second2
static constexpr double RadiansPerCount
std::size_t stale_limit_cycles
std::uint8_t parameters[253]
std::size_t parameter_count
std::uint8_t bytes[Capacity]
static constexpr std::size_t Capacity
axis::AxisModel::AxisInfoInputs info