pub struct PiperFrame {
pub id: u32,
pub data: [u8; 8],
pub len: u8,
pub is_extended: bool,
pub timestamp_us: u64,
}Expand description
CAN 2.0 标准帧的统一抽象
§设计目的
PiperFrame 是协议层和硬件层之间的中间抽象,提供:
- 层次解耦:协议层不依赖底层 CAN 实现(SocketCAN/GS-USB)
- 统一接口:上层通过
CanAdaptertrait 使用统一的帧类型 - 类型安全:编译时保证帧格式正确,避免原始字节操作错误
§在架构中的位置
Protocol Layer (piper-protocol)
↓ TryFrom<PiperFrame> 解析 / new_standard() 构建
PiperFrame (此类型)
↓ 转换逻辑在 CAN 层实现
CAN Layer (piper-can)
↓ SocketCAN/GS-USB 适配器
Hardware§设计特性
- Copy trait:零成本复制,适合高频 CAN 场景(~1kHz 帧率)
- 固定 8 字节:避免堆分配,减少内存碎片
- 无生命周期:自包含数据结构,简化 API
- 时间戳支持:
timestamp_us字段支持录制/回放功能
§限制
- 仅支持 CAN 2.0:固定 8 字节数据
- 不支持 CAN FD:最长 64 字节的帧需要使用
PiperFrameFd(未来扩展)
§转换示例
use piper_protocol::PiperFrame;
// 创建标准帧
let frame = PiperFrame::new_standard(0x123, &[1, 2, 3, 4]);
// 创建扩展帧
let frame_ext = PiperFrame::new_extended(0x12345678, &[5, 6, 7, 8]);
// 访问数据
assert_eq!(frame.id(), 0x123);
assert_eq!(frame.data_slice(), &[1, 2, 3, 4]);Fields§
§id: u32CAN ID(标准帧或扩展帧)
data: [u8; 8]帧数据(固定 8 字节,未使用部分为 0)
len: u8有效数据长度 (0-8)
is_extended: bool是否为扩展帧(29-bit ID)
timestamp_us: u64硬件时间戳(微秒),0 表示不可用
Implementations§
Source§impl PiperFrame
impl PiperFrame
Sourcepub fn new_standard(id: u16, data: &[u8]) -> PiperFrame
pub fn new_standard(id: u16, data: &[u8]) -> PiperFrame
创建标准帧
Sourcepub fn new_extended(id: u32, data: &[u8]) -> PiperFrame
pub fn new_extended(id: u32, data: &[u8]) -> PiperFrame
创建扩展帧
Sourcepub fn data_slice(&self) -> &[u8] ⓘ
pub fn data_slice(&self) -> &[u8] ⓘ
获取数据切片(只包含有效数据)
Trait Implementations§
Source§impl Clone for PiperFrame
impl Clone for PiperFrame
Source§fn clone(&self) -> PiperFrame
fn clone(&self) -> PiperFrame
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 PiperFrame
impl Debug for PiperFrame
Source§impl From<&PiperFrame> for TimestampedFrame
impl From<&PiperFrame> for TimestampedFrame
Source§fn from(frame: &PiperFrame) -> TimestampedFrame
fn from(frame: &PiperFrame) -> TimestampedFrame
Converts to this type from the input type.
Source§impl From<PiperCommand> for PiperFrame
impl From<PiperCommand> for PiperFrame
Source§fn from(cmd: PiperCommand) -> PiperFrame
fn from(cmd: PiperCommand) -> PiperFrame
Converts to this type from the input type.
Source§impl From<PiperFrame> for PiperCommand
impl From<PiperFrame> for PiperCommand
Source§fn from(frame: PiperFrame) -> PiperCommand
fn from(frame: PiperFrame) -> PiperCommand
默认转换为可靠命令(向后兼容)
Source§impl PartialEq for PiperFrame
impl PartialEq for PiperFrame
Source§impl TryFrom<PiperFrame> for CollisionProtectionLevelFeedback
impl TryFrom<PiperFrame> for CollisionProtectionLevelFeedback
Source§type Error = ProtocolError
type Error = ProtocolError
The type returned in the event of a conversion error.
Source§fn try_from(
frame: PiperFrame,
) -> Result<CollisionProtectionLevelFeedback, <CollisionProtectionLevelFeedback as TryFrom<PiperFrame>>::Error>
fn try_from( frame: PiperFrame, ) -> Result<CollisionProtectionLevelFeedback, <CollisionProtectionLevelFeedback as TryFrom<PiperFrame>>::Error>
Performs the conversion.
Source§impl TryFrom<PiperFrame> for ControlModeCommandFeedback
impl TryFrom<PiperFrame> for ControlModeCommandFeedback
Source§type Error = ProtocolError
type Error = ProtocolError
The type returned in the event of a conversion error.
Source§fn try_from(
frame: PiperFrame,
) -> Result<ControlModeCommandFeedback, <ControlModeCommandFeedback as TryFrom<PiperFrame>>::Error>
fn try_from( frame: PiperFrame, ) -> Result<ControlModeCommandFeedback, <ControlModeCommandFeedback as TryFrom<PiperFrame>>::Error>
Performs the conversion.
Source§impl TryFrom<PiperFrame> for EndPoseFeedback1
impl TryFrom<PiperFrame> for EndPoseFeedback1
Source§type Error = ProtocolError
type Error = ProtocolError
The type returned in the event of a conversion error.
Source§fn try_from(
frame: PiperFrame,
) -> Result<EndPoseFeedback1, <EndPoseFeedback1 as TryFrom<PiperFrame>>::Error>
fn try_from( frame: PiperFrame, ) -> Result<EndPoseFeedback1, <EndPoseFeedback1 as TryFrom<PiperFrame>>::Error>
Performs the conversion.
Source§impl TryFrom<PiperFrame> for EndPoseFeedback2
impl TryFrom<PiperFrame> for EndPoseFeedback2
Source§type Error = ProtocolError
type Error = ProtocolError
The type returned in the event of a conversion error.
Source§fn try_from(
frame: PiperFrame,
) -> Result<EndPoseFeedback2, <EndPoseFeedback2 as TryFrom<PiperFrame>>::Error>
fn try_from( frame: PiperFrame, ) -> Result<EndPoseFeedback2, <EndPoseFeedback2 as TryFrom<PiperFrame>>::Error>
Performs the conversion.
Source§impl TryFrom<PiperFrame> for EndPoseFeedback3
impl TryFrom<PiperFrame> for EndPoseFeedback3
Source§type Error = ProtocolError
type Error = ProtocolError
The type returned in the event of a conversion error.
Source§fn try_from(
frame: PiperFrame,
) -> Result<EndPoseFeedback3, <EndPoseFeedback3 as TryFrom<PiperFrame>>::Error>
fn try_from( frame: PiperFrame, ) -> Result<EndPoseFeedback3, <EndPoseFeedback3 as TryFrom<PiperFrame>>::Error>
Performs the conversion.
Source§impl TryFrom<PiperFrame> for EndVelocityAccelFeedback
impl TryFrom<PiperFrame> for EndVelocityAccelFeedback
Source§type Error = ProtocolError
type Error = ProtocolError
The type returned in the event of a conversion error.
Source§fn try_from(
frame: PiperFrame,
) -> Result<EndVelocityAccelFeedback, <EndVelocityAccelFeedback as TryFrom<PiperFrame>>::Error>
fn try_from( frame: PiperFrame, ) -> Result<EndVelocityAccelFeedback, <EndVelocityAccelFeedback as TryFrom<PiperFrame>>::Error>
Performs the conversion.
Source§impl TryFrom<PiperFrame> for FirmwareReadFeedback
impl TryFrom<PiperFrame> for FirmwareReadFeedback
Source§type Error = ProtocolError
type Error = ProtocolError
The type returned in the event of a conversion error.
Source§fn try_from(
frame: PiperFrame,
) -> Result<FirmwareReadFeedback, <FirmwareReadFeedback as TryFrom<PiperFrame>>::Error>
fn try_from( frame: PiperFrame, ) -> Result<FirmwareReadFeedback, <FirmwareReadFeedback as TryFrom<PiperFrame>>::Error>
Performs the conversion.
Source§impl TryFrom<PiperFrame> for GripperControlFeedback
impl TryFrom<PiperFrame> for GripperControlFeedback
Source§type Error = ProtocolError
type Error = ProtocolError
The type returned in the event of a conversion error.
Source§fn try_from(
frame: PiperFrame,
) -> Result<GripperControlFeedback, <GripperControlFeedback as TryFrom<PiperFrame>>::Error>
fn try_from( frame: PiperFrame, ) -> Result<GripperControlFeedback, <GripperControlFeedback as TryFrom<PiperFrame>>::Error>
Performs the conversion.
Source§impl TryFrom<PiperFrame> for GripperFeedback
impl TryFrom<PiperFrame> for GripperFeedback
Source§type Error = ProtocolError
type Error = ProtocolError
The type returned in the event of a conversion error.
Source§fn try_from(
frame: PiperFrame,
) -> Result<GripperFeedback, <GripperFeedback as TryFrom<PiperFrame>>::Error>
fn try_from( frame: PiperFrame, ) -> Result<GripperFeedback, <GripperFeedback as TryFrom<PiperFrame>>::Error>
Performs the conversion.
Source§impl TryFrom<PiperFrame> for GripperTeachParamsFeedback
impl TryFrom<PiperFrame> for GripperTeachParamsFeedback
Source§type Error = ProtocolError
type Error = ProtocolError
The type returned in the event of a conversion error.
Source§fn try_from(
frame: PiperFrame,
) -> Result<GripperTeachParamsFeedback, <GripperTeachParamsFeedback as TryFrom<PiperFrame>>::Error>
fn try_from( frame: PiperFrame, ) -> Result<GripperTeachParamsFeedback, <GripperTeachParamsFeedback as TryFrom<PiperFrame>>::Error>
Performs the conversion.
Source§impl TryFrom<PiperFrame> for JointControl12Feedback
impl TryFrom<PiperFrame> for JointControl12Feedback
Source§type Error = ProtocolError
type Error = ProtocolError
The type returned in the event of a conversion error.
Source§fn try_from(
frame: PiperFrame,
) -> Result<JointControl12Feedback, <JointControl12Feedback as TryFrom<PiperFrame>>::Error>
fn try_from( frame: PiperFrame, ) -> Result<JointControl12Feedback, <JointControl12Feedback as TryFrom<PiperFrame>>::Error>
Performs the conversion.
Source§impl TryFrom<PiperFrame> for JointControl34Feedback
impl TryFrom<PiperFrame> for JointControl34Feedback
Source§type Error = ProtocolError
type Error = ProtocolError
The type returned in the event of a conversion error.
Source§fn try_from(
frame: PiperFrame,
) -> Result<JointControl34Feedback, <JointControl34Feedback as TryFrom<PiperFrame>>::Error>
fn try_from( frame: PiperFrame, ) -> Result<JointControl34Feedback, <JointControl34Feedback as TryFrom<PiperFrame>>::Error>
Performs the conversion.
Source§impl TryFrom<PiperFrame> for JointControl56Feedback
impl TryFrom<PiperFrame> for JointControl56Feedback
Source§type Error = ProtocolError
type Error = ProtocolError
The type returned in the event of a conversion error.
Source§fn try_from(
frame: PiperFrame,
) -> Result<JointControl56Feedback, <JointControl56Feedback as TryFrom<PiperFrame>>::Error>
fn try_from( frame: PiperFrame, ) -> Result<JointControl56Feedback, <JointControl56Feedback as TryFrom<PiperFrame>>::Error>
Performs the conversion.
Source§impl TryFrom<PiperFrame> for JointDriverHighSpeedFeedback
impl TryFrom<PiperFrame> for JointDriverHighSpeedFeedback
Source§type Error = ProtocolError
type Error = ProtocolError
The type returned in the event of a conversion error.
Source§fn try_from(
frame: PiperFrame,
) -> Result<JointDriverHighSpeedFeedback, <JointDriverHighSpeedFeedback as TryFrom<PiperFrame>>::Error>
fn try_from( frame: PiperFrame, ) -> Result<JointDriverHighSpeedFeedback, <JointDriverHighSpeedFeedback as TryFrom<PiperFrame>>::Error>
Performs the conversion.
Source§impl TryFrom<PiperFrame> for JointDriverLowSpeedFeedback
impl TryFrom<PiperFrame> for JointDriverLowSpeedFeedback
Source§type Error = ProtocolError
type Error = ProtocolError
The type returned in the event of a conversion error.
Source§fn try_from(
frame: PiperFrame,
) -> Result<JointDriverLowSpeedFeedback, <JointDriverLowSpeedFeedback as TryFrom<PiperFrame>>::Error>
fn try_from( frame: PiperFrame, ) -> Result<JointDriverLowSpeedFeedback, <JointDriverLowSpeedFeedback as TryFrom<PiperFrame>>::Error>
Performs the conversion.
Source§impl TryFrom<PiperFrame> for JointEndVelocityAccelFeedback
impl TryFrom<PiperFrame> for JointEndVelocityAccelFeedback
Source§type Error = ProtocolError
type Error = ProtocolError
The type returned in the event of a conversion error.
Source§fn try_from(
frame: PiperFrame,
) -> Result<JointEndVelocityAccelFeedback, <JointEndVelocityAccelFeedback as TryFrom<PiperFrame>>::Error>
fn try_from( frame: PiperFrame, ) -> Result<JointEndVelocityAccelFeedback, <JointEndVelocityAccelFeedback as TryFrom<PiperFrame>>::Error>
Performs the conversion.
Source§impl TryFrom<PiperFrame> for JointFeedback12
impl TryFrom<PiperFrame> for JointFeedback12
Source§type Error = ProtocolError
type Error = ProtocolError
The type returned in the event of a conversion error.
Source§fn try_from(
frame: PiperFrame,
) -> Result<JointFeedback12, <JointFeedback12 as TryFrom<PiperFrame>>::Error>
fn try_from( frame: PiperFrame, ) -> Result<JointFeedback12, <JointFeedback12 as TryFrom<PiperFrame>>::Error>
Performs the conversion.
Source§impl TryFrom<PiperFrame> for JointFeedback34
impl TryFrom<PiperFrame> for JointFeedback34
Source§type Error = ProtocolError
type Error = ProtocolError
The type returned in the event of a conversion error.
Source§fn try_from(
frame: PiperFrame,
) -> Result<JointFeedback34, <JointFeedback34 as TryFrom<PiperFrame>>::Error>
fn try_from( frame: PiperFrame, ) -> Result<JointFeedback34, <JointFeedback34 as TryFrom<PiperFrame>>::Error>
Performs the conversion.
Source§impl TryFrom<PiperFrame> for JointFeedback56
impl TryFrom<PiperFrame> for JointFeedback56
Source§type Error = ProtocolError
type Error = ProtocolError
The type returned in the event of a conversion error.
Source§fn try_from(
frame: PiperFrame,
) -> Result<JointFeedback56, <JointFeedback56 as TryFrom<PiperFrame>>::Error>
fn try_from( frame: PiperFrame, ) -> Result<JointFeedback56, <JointFeedback56 as TryFrom<PiperFrame>>::Error>
Performs the conversion.
Source§impl TryFrom<PiperFrame> for MotorLimitFeedback
impl TryFrom<PiperFrame> for MotorLimitFeedback
Source§type Error = ProtocolError
type Error = ProtocolError
The type returned in the event of a conversion error.
Source§fn try_from(
frame: PiperFrame,
) -> Result<MotorLimitFeedback, <MotorLimitFeedback as TryFrom<PiperFrame>>::Error>
fn try_from( frame: PiperFrame, ) -> Result<MotorLimitFeedback, <MotorLimitFeedback as TryFrom<PiperFrame>>::Error>
Performs the conversion.
Source§impl TryFrom<PiperFrame> for MotorMaxAccelFeedback
impl TryFrom<PiperFrame> for MotorMaxAccelFeedback
Source§type Error = ProtocolError
type Error = ProtocolError
The type returned in the event of a conversion error.
Source§fn try_from(
frame: PiperFrame,
) -> Result<MotorMaxAccelFeedback, <MotorMaxAccelFeedback as TryFrom<PiperFrame>>::Error>
fn try_from( frame: PiperFrame, ) -> Result<MotorMaxAccelFeedback, <MotorMaxAccelFeedback as TryFrom<PiperFrame>>::Error>
Performs the conversion.
Source§impl TryFrom<PiperFrame> for RobotStatusFeedback
impl TryFrom<PiperFrame> for RobotStatusFeedback
Source§type Error = ProtocolError
type Error = ProtocolError
The type returned in the event of a conversion error.
Source§fn try_from(
frame: PiperFrame,
) -> Result<RobotStatusFeedback, <RobotStatusFeedback as TryFrom<PiperFrame>>::Error>
fn try_from( frame: PiperFrame, ) -> Result<RobotStatusFeedback, <RobotStatusFeedback as TryFrom<PiperFrame>>::Error>
Performs the conversion.
Source§impl TryFrom<PiperFrame> for SettingResponse
impl TryFrom<PiperFrame> for SettingResponse
Source§type Error = ProtocolError
type Error = ProtocolError
The type returned in the event of a conversion error.
Source§fn try_from(
frame: PiperFrame,
) -> Result<SettingResponse, <SettingResponse as TryFrom<PiperFrame>>::Error>
fn try_from( frame: PiperFrame, ) -> Result<SettingResponse, <SettingResponse as TryFrom<PiperFrame>>::Error>
Performs the conversion.
impl Copy for PiperFrame
impl Eq for PiperFrame
impl StructuralPartialEq for PiperFrame
Auto Trait Implementations§
impl Freeze for PiperFrame
impl RefUnwindSafe for PiperFrame
impl Send for PiperFrame
impl Sync for PiperFrame
impl Unpin for PiperFrame
impl UnwindSafe for PiperFrame
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more