MotorStatusArray
数据结构
| 字节索引 | 类型 | 字段含义 |
|---|---|---|
| 0-1 | uint16 | 电机数量 |
| 2-3 | uint16 | 保留字段,必须为 0 |
| 4- | MotorStatus 数组 | 电机状态数组,N 为电机数量 |
单个 MotorStatus 固定为 32 字节,因此总长度为 字节
示例
C++ 示例
struct MotorStatus {
float angle;
float angular_vel;
float torque;
float current;
float voltage;
float temp;
uint32_t fault_code;
uint32_t state;
};
struct MotorStatusArrayHeader {
uint16_t motor_count;
uint16_t reserved;
};
// payload = MotorStatusArrayHeader + motor_count * MotorStatus
Rust 示例
#[repr(C)]
#[derive(Clone, Copy, Debug, Default)]
pub struct MotorStatus {
pub angle: f32,
pub angular_vel: f32,
pub torque: f32,
pub current: f32,
pub voltage: f32,
pub temp: f32,
pub fault_code: u32,
pub state: u32,
}
#[repr(C)]
#[derive(Clone, Copy, Debug, Default)]
pub struct MotorStatusArrayHeader {
pub motor_count: u16,
pub reserved: u16,
}
const _: () = {
assert!(core::mem::size_of::<MotorStatus>() == 32);
assert!(core::mem::size_of::<MotorStatusArrayHeader>() == 4);
};
Dart 示例
import 'dart:typed_data';
final class MotorStatusArrayView {
static const int headerLength = 4;
static const int motorStatusLength = 32;
final ByteBuffer buffer;
final ByteData header;
final int offsetInBytes;
MotorStatusArrayView(this.buffer, [this.offsetInBytes = 0])
: header = ByteData.view(buffer, offsetInBytes, headerLength);
int get motorCount => header.getUint16(0, Endian.little);
ByteData motorStatusData(int index) {
return ByteData.view(
buffer,
offsetInBytes + headerLength + index * motorStatusLength,
motorStatusLength,
);
}
}
Java 示例
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
public final class MotorStatusArrayView {
public static final int HEADER_LENGTH = 4;
public static final int MOTOR_STATUS_LENGTH = 32;
private final ByteBuffer buffer;
private final int offset;
public MotorStatusArrayView(ByteBuffer buffer, int offset) {
this.buffer = buffer.order(ByteOrder.LITTLE_ENDIAN);
this.offset = offset;
}
public int motorCount() { return Short.toUnsignedInt(buffer.getShort(offset)); }
public int motorStatusOffset(int index) {
return offset + HEADER_LENGTH + index * MOTOR_STATUS_LENGTH;
}
}