use eyre::Result;
use robstride::robstride03::{RobStride03, RobStride03Command};
use robstride::SocketCanTransport;
use robstride::{Actuator, ControlCommand, Protocol, Transport, TxCommand, TypedCommandData};
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::mpsc;
use tracing::{error, info, trace};
use tracing_subscriber::{fmt, EnvFilter};
#[tokio::main]
async fn main() -> Result<()> {
let subscriber = fmt()
.with_env_filter(EnvFilter::from_default_env())
.finish();
tracing::subscriber::set_global_default(subscriber)?;
let (tx, mut rx) = mpsc::channel(32);
let transport = SocketCanTransport::new("can1".to_string()).await?;
let mut transport_clone = transport.clone();
let _transport_task = tokio::spawn(async move {
info!("Starting transport handling task");
loop {
tokio::select! {
Some(cmd) = rx.recv() => {
trace!("Processing outgoing command: {:?}", cmd);
match cmd {
TxCommand::Send { id, data } => {
if let Err(e) = transport_clone.send(id, &data).await {
error!("Transport sender error: {}", e);
}
}
}
}
}
}
});
let motor = RobStride03::new(33, 0, tx);
println!("Enabling motor...");
motor.enable().await?;
tokio::time::sleep(Duration::from_millis(500)).await;
println!("Setting limits...");
motor.set_max_torque(20.0).await?; motor.set_max_velocity(5.0).await?;
let target_position = 0.0;
println!("Moving to position: {} radians", target_position);
let command = RobStride03Command {
target_angle_rad: target_position,
target_velocity_rads: 0.0,
kp: 25.0, kd: 5.0, torque_nm: 0.0,
}
.to_control_command();
motor.control(command).await?;
tokio::time::sleep(Duration::from_secs(2)).await;
motor.get_feedback().await?;
println!("Disabling motor...");
motor.disable(false).await?;
tokio::time::sleep(Duration::from_millis(100)).await;
Ok(())
}