Skip to main content

actuator/
actuator.rs

1use std::time::SystemTime;
2
3//Disclaimer: I had gemini write the mujoco xml. I dont know how to write this and frankly would
4//rather just let onshape-to-robot do the work.
5const EXAMPLE: &str = "<mujoco model=\"simple_rrbot\">
6    <compiler angle=\"degree\" coordinate=\"local\"/>
7
8    <worldbody>
9        <light directional=\"true\" diffuse=\".8 .8 .8\" specular=\".2 .2 .2\" pos=\"0 0 5\" dir=\"0 0 -1\"/>
10        
11        <body name=\"base\" pos=\"0 0 0\">
12            
13            <body name=\"link1\" pos=\"0 0.1 0.4\">
14                <geom name=\"link1_geom\" type=\"capsule\" size=\"0.04\" fromto=\"0 0 0 0 0 0.4\" rgba=\"1 0 0 1\"/>
15                
16                <body name=\"link2\" pos=\"0 0.1 0.4\">
17                    <joint name=\"joint\" type=\"hinge\" axis=\"0 1 0\" pos=\"0 0 0\"/>
18                    <geom name=\"link2_geom\" type=\"capsule\" size=\"0.04\" fromto=\"0 0 0 0 0 0.4\" rgba=\"0 1 0 1\"/>
19                    
20                    <site name=\"ee_site\" pos=\"0 0 0.4\" size=\"0.01\" rgba=\"1 1 1 1\"/>
21                </body>
22            </body>
23        </body>
24    </worldbody>
25
26    <actuator>
27        <position name=\"j_ctrl\" joint=\"joint\" kp=\"45\"/>
28    </actuator>
29</mujoco>";
30
31fn main() {
32    let model = evomujoco::MjModel::from_xml_string(EXAMPLE).unwrap();
33    let mut mujoco = evomujoco::Mujoco::new(model);
34
35    let act = mujoco.get_actuator("j_ctrl".to_string());
36    let joint = mujoco.get_joint_from_name("joint".to_string());
37
38    let start = SystemTime::now();
39
40    loop {
41        act.set_ctrl(f64::sin(
42            SystemTime::now()
43                .duration_since(start)
44                .unwrap()
45                .as_secs_f64(),
46        ));
47        match joint.get_qpos() {
48            evomujoco::JointOutput::Hinge(j) => println!("{}", j.angle()),
49            _ => {}
50        }
51        mujoco.update();
52    }
53}