001_basic/
001_basic.rs

1use robotiq_rs::*;
2
3#[tokio::main]
4async fn main() -> Result<(), RobotiqError> {
5    // The serial port path
6    let path = "COM17";
7
8    // create a connection to serial RS485 modbus
9    let mut gripper = RobotiqGripper::from_path(path)?;
10
11    // Reset and Activation of Gripper
12    //
13    // reset gripper, recommended
14    gripper.reset().await?;
15    // activate the gripper, it will try to open and close.
16    gripper.activate().await?.await_activate().await?;
17    println!("finished activation.");
18    std::thread::sleep(std::time::Duration::from_millis(1000));
19
20    // Basic Gripper Command
21    //
22    // set gripper with position, speed and force
23    gripper.go_to(0x08, 0x00, 0x00).await?;
24    // the result of the setting command, return whether or not it have clamp an object
25    let obj_detect_status = gripper.await_go_to().await?;
26    println!("Object Detect Status : {:?}", obj_detect_status);
27    std::thread::sleep(std::time::Duration::from_millis(1000));
28
29    // Chained command
30    //
31    // chained command with builder pattern
32    let obj_detect_status = gripper.go_to(0xFF, 0xFF, 0xFF).await?.await_go_to().await?;
33    println!("Object Detect Status : {:?}", obj_detect_status);
34    std::thread::sleep(std::time::Duration::from_millis(1000));
35
36    // Automatic Release Routine
37    //
38    // set the gripper into motion
39    gripper.go_to(0x08, 0x00, 0x00).await?;
40    std::thread::sleep(std::time::Duration::from_millis(100));
41    gripper.automatic_release(false).await?;
42    // you will need to reset and reactivate the gripper after automatic release routine
43    gripper
44        .reset()
45        .await?
46        .activate()
47        .await?
48        .await_activate()
49        .await?;
50    std::thread::sleep(std::time::Duration::from_millis(1000));
51    gripper.go_to(0x08, 0x00, 0x00).await?;
52    std::thread::sleep(std::time::Duration::from_millis(1000));
53
54    // Gripper Command
55    //
56    // Construct GripperCommand to command gripper
57    //
58    // a null command, all zero, will deactivate and reset the gripper
59    let cmd_null = GripperCommand::new();
60    // command to activate the gripper
61    let cmd_act = GripperCommand::new().act(true);
62    // commands to set the gripper with position requested, speed, and force
63    let cmd_goto_1 = GripperCommand::new()
64        .act(true)
65        .gto(true)
66        .pos_req(0x08)
67        .speed(0x00)
68        .force(0x00);
69    let cmd_goto_2 = GripperCommand::new()
70        .act(true)
71        .gto(true)
72        .pos_req(0xFF)
73        .speed(0xFF)
74        .force(0xFF);
75    // command to perform automatic release routine
76    let cmd_atr = GripperCommand::new().act(true).atr(true).ard(true);
77
78    //
79    //
80    gripper.write_async(cmd_null).await?;
81    gripper.write_async(cmd_act).await?;
82    while gripper.read_async().await?.sta != ActivationStatus::Completed {
83        std::thread::sleep(std::time::Duration::from_millis(100));
84    }
85    std::thread::sleep(std::time::Duration::from_millis(1000));
86
87    //
88    //
89    gripper.write_async(cmd_goto_1).await?;
90    while gripper.read_async().await?.obj == ObjDetectStatus::InMotion {
91        std::thread::sleep(std::time::Duration::from_millis(100));
92    }
93    std::thread::sleep(std::time::Duration::from_millis(1000));
94
95    //
96    //
97    gripper.write_async(cmd_goto_2).await?;
98    std::thread::sleep(std::time::Duration::from_millis(100));
99    gripper.write_async(cmd_atr).await?;
100    while gripper.read_async().await?.fault != GripperFault::AutomaticReleaseCompleted {
101        std::thread::sleep(std::time::Duration::from_millis(100));
102    }
103    std::thread::sleep(std::time::Duration::from_millis(1000));
104
105    gripper
106        .reset()
107        .await?
108        .activate()
109        .await?
110        .await_activate()
111        .await?
112        .go_to(0x00, 0xFF, 0xFF)
113        .await?
114        .await_go_to()
115        .await?;
116
117    Ok(())
118}