main/
main.rs

1use cocube_rs::CoCube;
2use std::thread;
3use std::time::Duration;
4
5fn main() -> Result<(), Box<dyn std::error::Error>> {
6    // 初始化CoCube实例
7    let agent = CoCube::new(
8        1, 
9        "192.168.31.1", 
10        "192.168.31.31", 
11        1,    
12        6000
13    )?;
14
15    // 获取并打印位置信息
16    let pos = agent.get_position();
17    println!("Current position: x={:.2}, y={:.2}, yaw={:.2}rad", pos[0], pos[1], pos[2]);
18
19    // 基础运动控制示例
20    println!("Moving forward...");
21    agent.move_robot("forward", 40);
22    thread::sleep(Duration::from_secs(2));
23    agent.wheels_stop();
24
25    println!("Rotating left...");
26    agent.rotate_robot("left", 30);
27    thread::sleep(Duration::from_secs(2));
28    agent.wheels_stop();
29
30    // 目标点导航示例
31    let target_x = 200.0;
32    let target_y = 200.0;
33    println!("Moving to target ({}, {})", target_x, target_y);
34   // agent.move_to_target(target_x, target_y, 50);
35
36    // 等待到达目标点
37    // loop {
38    //     let curr_pos = agent.get_position();
39    //     let dx = curr_pos[0] - target_x;
40    //     let dy = curr_pos[1] - target_y;
41    //     let distance = (dx.powi(2) + dy.powi(2)).sqrt();
42        
43    //     println!("Current position: ({:.1}, {:.1}), Distance: {:.1} points", 
44    //             curr_pos[0], curr_pos[1], distance);
45        
46    //     if distance < 10.0 {
47    //         println!("Reached target!");
48    //         break;
49    //     }
50    //     thread::sleep(Duration::from_millis(100));
51    // }
52
53    // 显示控制示例
54    agent.set_display_color(0, 255, 0); // 设置绿色
55    if let Some(_) = agent.mb_display(33554431) {
56        // 成功处理显示命令
57    }
58
59    Ok(())
60}