Skip to main content

PiperContext

Struct PiperContext 

Source
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

Source

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

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§

Source§

impl Default for PiperContext

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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
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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more