Skip to main content

Observer

Struct Observer 

Source
pub struct Observer { /* private fields */ }
Expand description

状态观察器(只读接口,View 模式)

直接持有 driver::Piper 引用,零拷贝、零延迟地读取底层状态。 不再使用缓存层,避免数据延迟和锁竞争。

Implementations§

Source§

impl Observer

Source

pub fn new(driver: Arc<RobotPiper>) -> Self

创建新的 Observer

注意: 此方法通常不直接调用,Observer 应该通过 Piper 状态机的 observer() 方法获取。 此方法为 pub 以支持内部测试和性能基准测试。

基准测试: 为了支持性能基准测试,此方法在 benches 中也可访问。

Source

pub fn snapshot(&self) -> MotionSnapshot

获取运动快照(推荐用于控制算法)

此方法尽可能快地连续读取多个相关状态,减少时间偏斜。 即使底层是分帧更新的,此方法也能提供逻辑上最一致的数据。

§性能
  • 延迟:~20ns(连续调用 3 次 ArcSwap::load)
  • 无锁竞争(ArcSwap 是 Wait-Free 的)
§推荐使用场景
  • 高频控制算法(>100Hz)
  • 阻抗控制、力矩控制等需要时间一致性的算法
Source

pub fn joint_positions(&self) -> JointArray<Rad>

获取关节位置(独立读取,可能与其他状态有时间偏斜)

§注意

如果需要与其他状态(如速度、力矩)保持时间一致性, 请使用 snapshot() 方法。

Source

pub fn joint_velocities(&self) -> JointArray<RadPerSecond>

获取关节速度(独立读取,可能与其他状态有时间偏斜)

§注意

如果需要与其他状态(如位置、力矩)保持时间一致性, 请使用 snapshot() 方法。

§返回值

返回 JointArray<RadPerSecond>,保持类型安全。

Source

pub fn joint_torques(&self) -> JointArray<NewtonMeter>

获取关节力矩(独立读取,可能与其他状态有时间偏斜)

§注意

如果需要与其他状态(如位置、速度)保持时间一致性, 请使用 snapshot() 方法。

Source

pub fn gripper_state(&self) -> GripperState

获取夹爪状态

Source

pub fn gripper_position(&self) -> f64

获取夹爪位置 (0.0-1.0)

Source

pub fn gripper_effort(&self) -> f64

获取夹爪力度 (0.0-1.0)

Source

pub fn is_gripper_enabled(&self) -> bool

检查夹爪是否使能

Source

pub fn joint_enabled_mask(&self) -> u8

获取使能掩码(Bit 0-5 对应 J1-J6)

Source

pub fn is_joint_enabled(&self, joint_index: usize) -> bool

检查指定关节是否使能

Source

pub fn end_pose(&self) -> EndPoseState

获取末端位姿(独立读取,可能与其他状态有时间偏斜)

§返回值

返回 EndPoseState,包含:

  • end_pose: [X, Y, Z, Rx, Ry, Rz]
    • X, Y, Z: 位置(米)
    • Rx, Ry, Rz: 姿态角(弧度)
§示例
let end_pose = observer.end_pose();
println!("Position: X={:.4}, Y={:.4}, Z={:.4}",
    end_pose.end_pose[0], end_pose.end_pose[1], end_pose.end_pose[2]);
Source

pub fn is_all_enabled(&self) -> bool

检查是否全部使能

Source

pub fn is_all_disabled(&self) -> bool

检查是否全部失能

Source

pub fn is_partially_enabled(&self) -> bool

检查是否部分使能

Source

pub fn is_arm_enabled(&self) -> bool

检查机械臂是否使能(兼容旧 API)

如果所有关节都使能,返回 true

Source

pub fn joint_state(&self, joint: Joint) -> (Rad, RadPerSecond, NewtonMeter)

获取单个关节的状态

返回 (position, velocity, torque) 元组。 注意:此方法独立读取,可能与其他状态有时间偏斜。 如需时间一致性,请使用 snapshot() 方法。

Source

pub fn is_connected(&self) -> bool

检查机器人是否仍在响应

如果在超时窗口内收到反馈,返回 true。 这可用于检测机器人是否断电、CAN 线缆断开或固件崩溃。

§示例
if observer.is_connected() {
    println!("Robot is still responding");
} else {
    println!("Robot connection lost!");
}
Source

pub fn connection_age(&self) -> Duration

获取自上次反馈以来的时间

返回自上次成功处理 CAN 帧以来的时间。 可用于连接质量监控或诊断。

§示例
let age = observer.connection_age();
if age.as_millis() > 100 {
    println!("Connection is degrading: {}ms since last feedback", age.as_millis());
}
Source

pub fn collision_protection_levels(&self) -> [u8; 6]

获取碰撞保护级别

返回6个关节的当前碰撞防护等级(0~8,等级0代表不检测碰撞)。

§返回

返回 [J1, J2, J3, J4, J5, J6] 的碰撞防护等级

§示例
let levels = observer.collision_protection_levels();
println!("J1-J6 碰撞保护级别: {:?}", levels);

// 检查某个关节的保护等级
if levels[0] == 0 {
    println!("J1 未启用碰撞保护");
}

Trait Implementations§

Source§

impl Clone for Observer

Source§

fn clone(&self) -> Observer

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 Send for Observer

Source§

impl Sync for Observer

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