haply 1.3.1

Haply Robotics Client Library for the Inverse Service
Documentation
//! Damping configuration -- sets damping and streams position, velocity, orientation.
//!
//! Usage:
//!   cargo run --example damping_demo
//!
//! Toggle `USE_HTTP` to switch between HTTP and WS configuration.

use haply::{
    device_model::{ DampingConfig, Force, ForceInput, Inverse3Configure },
    http::InverseHttpClient,
    HaplyDevice,
};
use std::io::Write;
use std::time::Duration;
use tokio::time::{ interval, sleep };

const USE_HTTP: bool = false;
const HTTP_BASE: &str = "http://localhost:10001";
const WS_URL: &str = "ws://localhost:10001/";
/// Session selector for HTTP session-scoped endpoints.
const SESSION: &str = ":-1";

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
    let mut device = HaplyDevice::new(HTTP_BASE, WS_URL).await?;
    let http = InverseHttpClient::new(HTTP_BASE);

    sleep(Duration::from_millis(300)).await;
    let state = device.read_state().await?;

    let inv = state.inverse3.first().ok_or("No Inverse3 device connected")?;
    let id = inv.device_id.clone();
    println!("Using Inverse3 device: {}", id);

    let damping = DampingConfig {
        scalar: Some(5.0),
        vector: None,
    };

    if USE_HTTP {
        println!("Setting damping via HTTP (scalar=5.0)...");
        http.set_filters_damping("inverse3", &id, Some(SESSION), &damping).await?;
    } else {
        println!("Setting damping via WS (scalar=5.0)...");
        device.configure_inverse3(
            &id,
            Inverse3Configure {
                damping: Some(damping),
                ..Default::default()
            },
            Some(true)
        ).await?;
    }

    println!("Damping active -- streaming state (Ctrl+C to stop)");
    let mut ticker = interval(Duration::from_millis(10));
    loop {
        ticker.tick().await;

        // Send zero force to engage the control loop (damping filter applies on top)
        device.update_force(
            vec![ForceInput {
                device_id: id.clone(),
                forces: Force { x: 0.0, y: 0.0, z: 0.0 },
            }],
            Some(true),
            Some(true)
        ).await?;

        let state = device.read_state().await?;

        if let Some(inv) = state.inverse3.first() {
            let pos = inv.state.cursor_position.unwrap_or_default();
            let vel = inv.state.cursor_velocity.unwrap_or_default();
            let ori = inv.state.body_orientation.unwrap_or_default();

            print!(
                "\rPos: ({:.3},{:.3},{:.3}) Vel: ({:.3},{:.3},{:.3}) Ori: ({:.3},{:.3},{:.3},{:.3})   ",
                pos.x,
                pos.y,
                pos.z,
                vel.x,
                vel.y,
                vel.z,
                ori.x,
                ori.y,
                ori.z,
                ori.w
            );
            std::io::stdout().flush().unwrap();
        }
    }
}