Skip to content

Commit

Permalink
Adding a helper class to make loading in Groovy easier
Browse files Browse the repository at this point in the history
  • Loading branch information
madhephaestus committed Dec 22, 2023
1 parent 60f5119 commit ebacbf7
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 19 deletions.
72 changes: 72 additions & 0 deletions src/main/java/org/mujoco/MuJoCoModelManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package org.mujoco;

import java.io.File;

import org.mujoco.MuJoCoLib.mjData;
import org.mujoco.MuJoCoLib.mjData_;
import org.mujoco.MuJoCoLib.mjModel;
import org.mujoco.MuJoCoLib.mjModel_;
import org.mujoco.MuJoCoLib.mjVFS;

public class MuJoCoModelManager {
MuJoCoLib lib = new MuJoCoLib();

private mjModel m;
private mjData d;
private mjModel_ maccessable;
private mjData_ daccessable;

public MuJoCoModelManager(File config){
byte[] error = new byte[1000];
int error_sz = 0;
m = MuJoCoLib.mj_loadXML(
config.getAbsolutePath(),(mjVFS) null, error,
error_sz);
if(m==null)
throw new RuntimeException("Model File Failed to load "+new String(error));
System.out.println("Humanoid model loaded " + m);
d = MuJoCoLib.mj_makeData(m);
setModel(new mjModel_(m));
setData(new mjData_(d));
}

public void close() {
MuJoCoLib.mj_deleteData(d);
MuJoCoLib.mj_deleteModel(m);
}

/**
* @return the maccessable
*/
public mjModel_ getModel() {
return maccessable;
}

/**
* @param maccessable the maccessable to set
*/
private void setModel(mjModel_ maccessable) {
this.maccessable = maccessable;
}

/**
* @return the daccessable
*/
public mjData_ getData() {
return daccessable;
}

/**
* @param daccessable the daccessable to set
*/
public void setData(mjData_ daccessable) {
this.daccessable = daccessable;
}

public void stepOne() {
MuJoCoLib.mj_step1(m, d);
}
public void stepTwo() {
MuJoCoLib.mj_step2(m, d);
}
}
34 changes: 15 additions & 19 deletions src/test/java/mujoco/java/MuJoColibTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
import org.mujoco.MuJoCoLib.mjModel;
import org.mujoco.MuJoCoLib.mjModel_;
import org.mujoco.MuJoCoLib.mjVFS;
import org.mujoco.MuJoCoModelManager;

import static org.junit.Assert.*;

import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand All @@ -25,34 +27,28 @@ public class MuJoColibTest {
public void mujocoJNILoadTest() {
System.out.println(System.getProperty("org.bytedeco.javacpp.logger.debug"));
System.setProperty("org.bytedeco.javacpp.logger.debug", "true");
MuJoCoLib lib = new MuJoCoLib();

System.out.println("Starting " + MuJoCoLib.mj_versionString().getString());
byte[] error = new byte[100];
int error_sz = 0;
mjModel m = MuJoCoLib.mj_loadXML(
"/home/hephaestus/git/mujoco-java/src/main/resources/mujoco/java/humanoid/humanoid.xml", null, error,
error_sz);
System.out.println("Humanoid model loaded " + m);
mjData d = MuJoCoLib.mj_makeData(m);

MuJoCoModelManager manager = new MuJoCoModelManager(
new File("/home/hephaestus/git/mujoco-java/src/main/resources/mujoco/java/humanoid/humanoid.xml"));

try {
mjModel_ Maccessable = new mjModel_(m);
try (mjData_ accessable = new mjData_(d)) {
System.out.println("Run model for 10 seconds");
while (accessable.time() < 10) {
MuJoCoLib.mj_step(m, d);
Thread.sleep(1);

}
mjModel_ Maccessable = manager.getModel();
mjData_ accessable = manager.getData();
System.out.println("Run model for 10 seconds");
while (accessable.time() < 10) {
manager.stepOne();
manager.stepTwo();
Thread.sleep(1);

}

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Clean up data objects");

MuJoCoLib.mj_deleteData(d);
MuJoCoLib.mj_deleteModel(m);
manager.close();
}
}

0 comments on commit ebacbf7

Please sign in to comment.