use haply::{
compute_total_force,
device_model::{DeviceType, *},
Cube, HaplyDevice, Sphere,
};
use log::debug;
use tokio;
#[tokio::main]
async fn main() {
let http_base = "http://localhost:10000"; let ws_url = "ws://localhost:10001";
let mut device = HaplyDevice::new(http_base, ws_url).await.unwrap();
let configs = device.list_devices().await.unwrap_or_else(|e| {
debug!("Error listing device configs: {}", e);
vec![]
});
let mut device_infos: Vec<DeviceInfo> = Vec::new();
for cfg in configs {
match cfg {
Config::DeviceConfig(dc) => device_infos.push(dc.device_info.clone()),
Config::VGConfig(vgc) => device_infos.push(DeviceInfo {
id: vgc.id.clone(),
major_version: 0,
minor_version: 0,
device_type: DeviceType::VerseGrip,
uuid: String::new(),
}),
Config::WVGConfig(wvgc) => device_infos.push(DeviceInfo {
id: wvgc.id.clone(),
major_version: wvgc.major_version,
minor_version: wvgc.minor_version,
device_type: DeviceType::WirelessVerseGrip,
uuid: String::new(),
}),
}
}
if device_infos.is_empty() {
debug!("No devices found. Exiting.");
return;
}
let devices_info = DevicesInfo {
devices: device_infos.clone(),
};
debug!("Using device infos: {:?}", devices_info);
let initial_info = devices_info.devices[0].clone();
let sphere = Sphere {
center: [0.05, -0.15, 0.16],
radius: 0.1,
stiffness: 1000.0, magnetism: 0.0, damping: 0.1, };
let cube = Cube {
center: [3.0, 0.0, 0.0],
size: 4.0,
stiffness: 0.8,
magnetism: 1.5,
damping: 0.1,
};
let current_state = device.read_state().await.unwrap();
debug!("Current device state: {:?}", current_state);
let force = Force {
x: 0.0,
y: 0.0,
z: 0.0,
};
let id = initial_info.id.clone();
let force_in = ForceInput {
device_id: id.clone(),
forces: force,
};
let force_in_vec = vec![force_in];
if let Err(e) = device
.update_force(force_in_vec, Some(false), Some(true))
.await
{
debug!("Error updating force: {}", e);
}
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
let current_state = device.read_state().await.unwrap();
debug!("Current device state: {:?}", current_state);
let objects: Vec<Box<dyn haply::physics_model::PhysicalObject>> =
vec![Box::new(sphere), Box::new(cube)];
loop {
let current_state = device.read_state().await.unwrap();
let position = current_state.inverse3[0]
.state
.cursor_position
.clone()
.unwrap_or_default();
debug!("Current cursor position: {:?}", position);
let orientation = current_state.wireless_verse_grip[0]
.state
.orientation
.clone()
.unwrap_or_default();
debug!("Current orientation: {:?}", orientation);
let total_force = compute_total_force(position.into(), &objects);
debug!("Computed total force: {:?}", total_force);
let force = Force {
x: total_force[0],
y: total_force[1],
z: total_force[2],
};
let id = current_state.inverse3[0].device_id.clone();
let force_in = ForceInput {
device_id: id.clone(),
forces: force,
};
let force_in_vec = vec![force_in];
if let Err(e) = device
.update_force(force_in_vec, Some(true), Some(true))
.await
{
debug!("Error updating force: {}", e);
}
tokio::time::sleep(std::time::Duration::from_millis(1)).await;
}
}