88import java .io .File ;
99
1010import org .bytedeco .javacpp .BytePointer ;
11+ import org .bytedeco .javacpp .IntPointer ;
12+ import org .bytedeco .javacpp .Pointer ;
1113import org .junit .Test ;
1214import org .mujoco .IMujocoController ;
1315import org .mujoco .MuJoCoLib ;
2123
2224public class MuJoColibTest {
2325 IMujocoController controller = (d , m ) -> {
24- // apply controls https://mujoco.readthedocs.io/en/stable/programming/simulation.html#simulation-loop
25- if ( m .nu ()==m .nv () )
26+ /**
27+ * This illustrates two concepts. First, we are checking
28+ * if the number of controls mjModel.nu equals the number
29+ * of DoFs mjModel.nv. In general, the same callback may
30+ * be used with multiple models depending on how the user
31+ * code is structured, and so it is a good idea to check
32+ * the model dimensions in the callback. Second, MuJoCo
33+ * has a library of BLAS-like functions that are very
34+ * useful; indeed a large part of the code base consists
35+ * of calling such functions internally. The mju_scl
36+ * function above scales the velocity vector mjData.qvel
37+ * by a constant feedback gain and copies the result into
38+ * the control vector mjData.ctrl.
39+ */
40+ // apply controls
41+ // https://mujoco.readthedocs.io/en/stable/programming/simulation.html#simulation-loop
42+ if (m .nu () == m .nv ())
2643 MuJoCoLib .mju_scl (d .ctrl (), d .qvel (), -0.1 , m .nv ());
2744 };
45+
2846 @ Test
2947 public void managerTest () throws InterruptedException {
3048 System .out .println ("managerTest" );
3149 String filename = "model/humanoid/humanoid.xml" ;
3250 File file = new File (filename );
33- if (!file .exists ()) {
51+ if (!file .exists ()) {
3452 fail ("File is missing from the disk" );
3553 }
3654 MuJoCoModelManager m = new MuJoCoModelManager (file );
37- mjModel_ model = m .getModel ();
38- mjData_ data = m .getData ();
3955 System .out .println ("Run ModelManager for 10 seconds" );
4056
57+
58+ for (int i = 0 ; i < m .getNumberOfJoints (); i ++) {
59+ System .out .println (i + " link = " + m .getJointName (i ));
60+ }
61+ for (int i = 0 ; i < m .getNumberOfBodys (); i ++) {
62+ System .out .println (i + " Body = " + m .getBodyName (i ));
63+ }
64+
4165 m .setController (controller );
42- while (data . time () < 10 ) {
66+ while (m . getCurrentSimulationTimeSeconds () < 10 ) {
4367 m .step ();
4468 // sleep
4569 Thread .sleep (m .getTimestepMilliSeconds ());
4670 }
4771 m .close ();
4872 }
73+
4974 @ Test
5075 public void mujocoJNILoadTest () {
5176 System .out .println ("mujocoJNILoadTest" );
@@ -54,47 +79,6 @@ public void mujocoJNILoadTest() {
5479 MuJoCoLib lib = new MuJoCoLib ();
5580
5681 System .out .println ("Starting " + MuJoCoLib .mj_versionString ().getString ());
57- int error_sz = 1000 ;
58- BytePointer error = new BytePointer (error_sz );
59- String filename = "model/humanoid/humanoid.xml" ;
60- File file = new File (filename );
61- if (!file .exists ()) {
62- fail ("File is missing from the disk" );
63- }
64- filename = file .getAbsolutePath ();
65- mjModel m = MuJoCoLib .mj_loadXML (
66- filename , null , error ,
67- error_sz );
68-
69- if (m ==null ) {
70- String message = "Model failed to load from " +filename +"\n " +error .getString ();
71- System .err .println (message );
72- fail (message );
73- }
74- System .out .println ("Humanoid model loaded " + filename );
75- mjData d = MuJoCoLib .mj_makeData (m );
76- try {
77- mjModel_ Maccessable = new mjModel_ (m );
78- try (mjData_ accessable = new mjData_ (d )) {
79- System .out .println ("Run model for 10 seconds" );
80- while (accessable .time () < 10 ) {
81- MuJoCoLib .mj_step1 (m , d );
82- // apply controls https://mujoco.readthedocs.io/en/stable/programming/simulation.html
83- controller .controlStep (accessable , Maccessable );
84- MuJoCoLib .mj_step2 (m , d );
85- double timestep = new mjOption_ (Maccessable .opt ()).timestep ()*1000 ;
86- Thread .sleep ((long ) timestep );
87-
88- }
89-
90- }
91- } catch (Exception e ) {
92- // TODO Auto-generated catch block
93- e .printStackTrace ();
94- }
95- System .out .println ("Clean up data objects" );
9682
97- MuJoCoLib .mj_deleteData (d );
98- MuJoCoLib .mj_deleteModel (m );
9983 }
10084}
0 commit comments