use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use crate::ArmState;
#[derive(Debug, Clone, Default)]
pub struct JointStateEntry {
pub position: f64,
pub velocity: Option<f64>,
pub acceleration: Option<f64>,
pub torque: Option<f64>,
}
pub type JointStateMap = Arc<Mutex<HashMap<String, JointStateEntry>>>;
pub fn joint_state_map_from_arm_state<const N: usize>(
names: &[&str; N],
state: &ArmState<N>,
) -> HashMap<String, JointStateEntry> {
let positions = state.measured.joint.unwrap_or([0.0; N]);
let velocities = state.measured.joint_vel;
let accelerations = state.measured.joint_acc;
let torques = state.measured.torque;
names
.iter()
.enumerate()
.map(|(i, name)| {
(
name.to_string(),
JointStateEntry {
position: positions[i],
velocity: velocities.map(|v| v[i]),
acceleration: accelerations.map(|a| a[i]),
torque: torques.map(|t| t[i]),
},
)
})
.collect()
}
pub fn update_joint_state_map<const N: usize>(
map: &JointStateMap,
names: &[&str],
state: &ArmState<N>,
) {
let positions = state.measured.joint.unwrap_or([0.0; N]);
let velocities = state.measured.joint_vel;
let accelerations = state.measured.joint_acc;
let torques = state.measured.torque;
if let Ok(mut guard) = map.lock() {
for (i, name) in names.iter().enumerate() {
let entry = guard
.entry(name.to_string())
.or_insert_with(JointStateEntry::default);
entry.position = positions[i];
entry.velocity = velocities.map(|v| v[i]);
entry.acceleration = accelerations.map(|a| a[i]);
entry.torque = torques.map(|t| t[i]);
}
}
}
pub trait JointStateSync {
fn joint_state_handle(&self) -> JointStateMap;
}