use moteus_protocol::command::{
BrakeCommand, CurrentCommand, CurrentFormat, PositionCommand, PositionFormat, StopCommand,
};
use moteus_protocol::query::{QueryFormat, QueryResult};
use moteus_protocol::{calculate_arbitration_id, parse_arbitration_id, CanFdFrame, Resolution};
fn main() {
println!("=== moteus-protocol Example ===\n");
println!("1. Creating a stop command for servo ID 1:");
let mut stop_frame = CanFdFrame::new();
stop_frame.arbitration_id = calculate_arbitration_id(0, 1, 0, true);
StopCommand::serialize(&mut stop_frame);
print_frame(&stop_frame);
println!("\n2. Creating a position command:");
let mut pos_frame = CanFdFrame::new();
pos_frame.arbitration_id = calculate_arbitration_id(0, 1, 0, true);
let pos_cmd = PositionCommand::new()
.position(0.5) .velocity(1.0) .kp_scale(1.0)
.kd_scale(1.0);
let pos_format = PositionFormat::default();
pos_cmd.serialize(&mut pos_frame, &pos_format);
print_frame(&pos_frame);
println!("\n3. Creating a query-only command:");
let mut query_frame = CanFdFrame::new();
query_frame.arbitration_id = calculate_arbitration_id(0, 1, 0, true);
let mut custom_query = QueryFormat::default();
custom_query.position = Resolution::Float; custom_query.velocity = Resolution::Float;
custom_query.torque = Resolution::Float;
let reply_size = custom_query.serialize(&mut query_frame);
print_frame(&query_frame);
println!(" Expected reply size: {} bytes", reply_size);
println!("\n4. Parsing a simulated response:");
let mut response = CanFdFrame::new();
response.arbitration_id = calculate_arbitration_id(1, 0, 0, false);
response.data[0] = 0x21; response.data[1] = 0x00; response.data[2] = 0x0A; response.data[3] = 0x2D; response.data[4] = 0x01; response.data[5] = 0x00; response.data[6] = 0x00;
response.data[7] = 0x00;
response.data[8] = 0x3F;
response.size = 9;
let result = QueryResult::parse(&response);
println!(" Parsed QueryResult:");
println!(" Mode: {:?}", result.mode);
println!(" Position: {:.4} rev", result.position);
println!(" Velocity: {:.4} rev/s", result.velocity);
println!(" Torque: {:.4} Nm", result.torque);
println!("\n5. Creating a current command:");
let mut current_frame = CanFdFrame::new();
current_frame.arbitration_id = calculate_arbitration_id(0, 1, 0, true);
let current_cmd = CurrentCommand::new().d_current(0.0).q_current(0.5);
current_cmd.serialize(&mut current_frame, &CurrentFormat::default());
print_frame(¤t_frame);
println!("\n6. Creating a brake command:");
let mut brake_frame = CanFdFrame::new();
brake_frame.arbitration_id = calculate_arbitration_id(0, 1, 0, true);
BrakeCommand::serialize(&mut brake_frame);
print_frame(&brake_frame);
println!(" Mode byte: {} (Brake=15)", brake_frame.data[2]);
println!("\n=== Done ===");
}
fn print_frame(frame: &CanFdFrame) {
let (source, destination, _prefix) = parse_arbitration_id(frame.arbitration_id);
let reply_required = frame.arbitration_id & 0x8000 != 0;
print!(" Arbitration ID: 0x{:04X}", frame.arbitration_id);
println!(" (dest={}, source={})", destination, source);
print!(" Data ({} bytes): ", frame.size);
for i in 0..frame.size as usize {
print!("{:02X} ", frame.data[i]);
}
println!();
println!(" Reply required: {}", reply_required);
}