pub struct PiperContext {Show 17 fields
pub joint_position: Arc<ArcSwap<JointPositionState>>,
pub end_pose: Arc<ArcSwap<EndPoseState>>,
pub joint_dynamic: Arc<ArcSwap<JointDynamicState>>,
pub robot_control: Arc<ArcSwap<RobotControlState>>,
pub gripper: Arc<ArcSwap<GripperState>>,
pub joint_driver_low_speed: Arc<ArcSwap<JointDriverLowSpeedState>>,
pub collision_protection: Arc<RwLock<CollisionProtectionState>>,
pub joint_limit_config: Arc<RwLock<JointLimitConfigState>>,
pub joint_accel_config: Arc<RwLock<JointAccelConfigState>>,
pub end_limit_config: Arc<RwLock<EndLimitConfigState>>,
pub firmware_version: Arc<RwLock<FirmwareVersionState>>,
pub master_slave_control_mode: Arc<ArcSwap<MasterSlaveControlModeState>>,
pub master_slave_joint_control: Arc<ArcSwap<MasterSlaveJointControlState>>,
pub master_slave_gripper_control: Arc<ArcSwap<MasterSlaveGripperControlState>>,
pub fps_stats: Arc<ArcSwap<FpsStatistics>>,
pub connection_monitor: ConnectionMonitor,
pub hooks: Arc<RwLock<HookManager>>,
}Expand description
Piper 上下文(所有状态的聚合)
Fields§
§joint_position: Arc<ArcSwap<JointPositionState>>关节位置状态(帧组同步:0x2A5-0x2A7)
end_pose: Arc<ArcSwap<EndPoseState>>末端位姿状态(帧组同步:0x2A2-0x2A4)
joint_dynamic: Arc<ArcSwap<JointDynamicState>>关节动态状态(独立帧 + Buffered Commit:关节速度 + 电流)
robot_control: Arc<ArcSwap<RobotControlState>>机器人控制状态(单个CAN帧:0x2A1)
gripper: Arc<ArcSwap<GripperState>>夹爪状态(单个CAN帧:0x2A8)
joint_driver_low_speed: Arc<ArcSwap<JointDriverLowSpeedState>>关节驱动器低速反馈状态(单个CAN帧:0x261-0x266)
collision_protection: Arc<RwLock<CollisionProtectionState>>碰撞保护状态(按需查询:0x47B)
joint_limit_config: Arc<RwLock<JointLimitConfigState>>关节限制配置状态(按需查询:0x473)
joint_accel_config: Arc<RwLock<JointAccelConfigState>>关节加速度限制配置状态(按需查询:0x47C)
end_limit_config: Arc<RwLock<EndLimitConfigState>>末端限制配置状态(按需查询:0x478)
firmware_version: Arc<RwLock<FirmwareVersionState>>固件版本状态(按需查询:0x4AF)
master_slave_control_mode: Arc<ArcSwap<MasterSlaveControlModeState>>主从模式控制模式指令状态(主从模式:0x151)
master_slave_joint_control: Arc<ArcSwap<MasterSlaveJointControlState>>主从模式关节控制指令状态(主从模式:0x155-0x157,帧组同步)
master_slave_gripper_control: Arc<ArcSwap<MasterSlaveGripperControlState>>主从模式夹爪控制指令状态(主从模式:0x159)
fps_stats: Arc<ArcSwap<FpsStatistics>>FPS 统计(各状态的更新频率统计)
使用 ArcSwap 支持在运行中原子性“重置统计窗口“(替换为新的 FpsStatistics),
且不引入每帧的锁开销。
connection_monitor: ConnectionMonitor连接监控(用于检测机器人是否仍在响应)
使用 App Start Relative Time 模式,确保时间单调性。
hooks: Arc<RwLock<HookManager>>钩子管理器(用于运行时回调注册)
使用 RwLock 支持动态添加/移除回调,同时保证线程安全。
§设计理由(v1.2.1)
- Config vs Context 分离:
PipelineConfig保持为 POD(Plain Old Data),PiperContext管理运行时状态和动态组件(如回调) - 动态注册: 运行时可以添加/移除回调,无需重新配置 pipeline
- 线程安全: 使用
RwLock保证并发访问安全
§示例
use piper_driver::recording::AsyncRecordingHook;
use piper_driver::hooks::FrameCallback;
use piper_driver::state::PiperContext;
use std::sync::Arc;
// 创建上下文和录制钩子
let context = PiperContext::new();
let (hook, _rx) = AsyncRecordingHook::new();
// 注册为回调
if let Ok(mut hooks) = context.hooks.write() {
hooks.add_callback(Arc::new(hook) as Arc<dyn FrameCallback>);
}Implementations§
Source§impl PiperContext
impl PiperContext
Sourcepub fn new() -> Self
pub fn new() -> Self
创建新的上下文
初始化所有状态结构,包括:
- 热数据(ArcSwap):
joint_position,end_pose,joint_dynamic - 温数据(ArcSwap):
robot_control,gripper,joint_driver_low_speed - 冷数据(RwLock):
collision_protection,joint_limit_config,joint_accel_config,end_limit_config - FPS 统计:
fps_stats
§Example
use piper_driver::PiperContext;
let ctx = PiperContext::new();
let joint_pos = ctx.joint_position.load();
assert_eq!(joint_pos.hardware_timestamp_us, 0);Sourcepub fn capture_motion_snapshot(&self) -> MotionSnapshot
pub fn capture_motion_snapshot(&self) -> MotionSnapshot
捕获运动状态快照(逻辑原子性)
虽然不能保证物理上的完全同步(因为CAN帧本身就不是同时到的), 但可以保证逻辑上的原子性(在同一时刻读取多个状态)。
注意:返回的状态可能来自不同的CAN传输周期。
性能:返回栈上对象,开销极小(仅包含 Arc 的克隆,不复制实际数据)
§Example
use piper_driver::PiperContext;
let ctx = PiperContext::new();
let snapshot = ctx.capture_motion_snapshot();
println!("Joint positions: {:?}", snapshot.joint_position.joint_pos);
println!("End pose: {:?}", snapshot.end_pose.end_pose);Trait Implementations§
Auto Trait Implementations§
impl !Freeze for PiperContext
impl RefUnwindSafe for PiperContext
impl Send for PiperContext
impl Sync for PiperContext
impl Unpin for PiperContext
impl UnwindSafe for PiperContext
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> 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