Skip to main content

MitControlCommand

Struct MitControlCommand 

Source
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: f32

Implementations§

Source§

impl MitControlCommand

Source

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);
Source

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 过期)。

Source

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

Source§

fn clone(&self) -> MitControlCommand

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for MitControlCommand

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Copy for MitControlCommand

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.