pub struct MitControlCommand {
pub joint_index: u8,
pub pos_ref: f32,
pub vel_ref: f32,
pub kp: f32,
pub kd: f32,
pub t_ref: f32,
}Expand description
MIT 控制指令 (0x15A~0x15F)
用于控制机械臂关节的 MIT 模式(主从模式)。 包含位置参考、速度参考、比例增益、微分增益、力矩参考。
注意:此指令使用复杂的跨字节位域打包,需要仔细处理。
CRC 计算优化(v2.1):
- CRC 属于衍生属性,不在结构体中存储
- 在
to_frame时即时计算,确保数据一致性 - 避免了 “Stale CRC” 问题(修改字段后 CRC 过期)
Fields§
§joint_index: u8§pos_ref: f32§vel_ref: f32§kp: f32§kd: f32§t_ref: f32Implementations§
Source§impl MitControlCommand
impl MitControlCommand
Sourcepub fn uint_to_float(x_int: u32, x_min: f32, x_max: f32, bits: u32) -> f32
pub fn uint_to_float(x_int: u32, x_min: f32, x_max: f32, bits: u32) -> f32
协议解码辅助函数(预留用于解析 MIT 控制反馈)
将无符号整数转换为浮点数(根据协议公式)
§公式
x_float = x_int * (x_max - x_min) / ((1 << bits) - 1) + x_min§用途
当前状态:此函数主要用于测试,保留作为公共 API。
未来用途:如果将来需要从电机反馈帧中解析 MIT 控制参数(如 kp, kd, t_ref),
可以使用此函数进行解码。例如,电机可能会发送 MIT 控制状态的反馈帧,
其中包含当前的阻抗控制参数。
§参数
x_int: 编码后的整数值(从 CAN 帧中读取)x_min: 物理量最小值(如位置的最小弧度)x_max: 物理量最大值(如位置的最大弧度)bits: 编码位数(如 12 位或 16 位)
§示例
ⓘ
// 解析位置反馈(假设使用 16 位编码,范围 -12.5 ~ 12.5 弧度)
let position_raw = 0x8000u32; // 原始值
let position_rad = uint_to_float(position_raw, -12.5, 12.5, 16);Sourcepub fn new(
joint_index: u8,
pos_ref: f32,
vel_ref: f32,
kp: f32,
kd: f32,
t_ref: f32,
) -> Self
pub fn new( joint_index: u8, pos_ref: f32, vel_ref: f32, kp: f32, kd: f32, t_ref: f32, ) -> Self
创建 MIT 控制指令
参数范围(根据官方 SDK,固定值,不要更改):
- pos_ref: -12.5 ~ 12.5(弧度)
- vel_ref: -45.0 ~ 45.0 rad/s
- kp: 0.0 ~ 500.0
- kd: -5.0 ~ 5.0
- t_ref: -18.0 ~ 18.0 N·m
§Arguments
joint_index- 关节序号 [1, 6]pos_ref- 设定期望的目标位置vel_ref- 设定电机运动的速度kp- 比例增益,控制位置误差对输出力矩的影响kd- 微分增益,控制速度误差对输出力矩的影响t_ref- 目标力矩参考值,用于控制电机施加的力矩或扭矩
§版本变更
v2.1:移除了 crc 参数,CRC 在 to_frame 时即时计算。
这样可以避免“Stale CRC“问题(修改字段后 CRC 过期)。
Sourcepub fn to_frame(self) -> PiperFrame
pub fn to_frame(self) -> PiperFrame
转换为 CAN 帧
职责:负责校验(Checksum)和封装(Packet)。 在序列化时即时计算 CRC,确保数据一致性。
协议位域布局:
- Byte 0-1: Pos_ref (16位)
- Byte 2: Vel_ref [bit11~bit4] (8位)
- Byte 3: Vel_ref [bit3~bit0] | Kp [bit11~bit8] (跨字节打包)
- Byte 4: Kp [bit7~bit0] (8位)
- Byte 5: Kd [bit11~bit4] (8位)
- Byte 6: Kd [bit3~bit0] | T_ref [bit7~bit4] (跨字节打包)
- Byte 7: T_ref [bit3~bit0] | CRC [bit3~bit0] (跨字节打包)
参数范围(根据官方 SDK):
- Pos_ref: -12.5 ~ 12.5 (16位)
- Vel_ref: -45.0 ~ 45.0 rad/s (12位)
- Kp: 0.0 ~ 500.0 (12位)
- Kd: -5.0 ~ 5.0 (12位)
- T_ref: -18.0 ~ 18.0 N·m (8位)
版本变更
v2.1:
- 使用
encode_to_bytes获取完整 8 字节数据(CRC 位预留为 0) - 基于前 7 字节计算 CRC
- 将 CRC 填入第 8 字节的低 4 位
- 避免
T_ref的双重计算,性能优化
Trait Implementations§
Source§impl Clone for MitControlCommand
impl Clone for MitControlCommand
Source§fn clone(&self) -> MitControlCommand
fn clone(&self) -> MitControlCommand
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for MitControlCommand
impl Debug for MitControlCommand
impl Copy for MitControlCommand
Auto Trait Implementations§
impl Freeze for MitControlCommand
impl RefUnwindSafe for MitControlCommand
impl Send for MitControlCommand
impl Sync for MitControlCommand
impl Unpin for MitControlCommand
impl UnwindSafe for MitControlCommand
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more