Skip to content

Commit 43c42b5

Browse files
committed
test the model by running it
1 parent 0d07b38 commit 43c42b5

File tree

1 file changed

+79
-25
lines changed

1 file changed

+79
-25
lines changed

src/test/java/mujoco/java/XMLtest.java

Lines changed: 79 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static org.junit.Assert.*;
44

55
import java.io.File;
6+
import java.io.IOException;
67
import java.math.BigDecimal;
78
import java.util.Arrays;
89
import java.util.Iterator;
@@ -13,19 +14,22 @@
1314

1415
import org.junit.Test;
1516
import org.mujoco.xml.BodyarchType;
17+
import org.mujoco.MuJoCoModelManager;
1618
import org.mujoco.MuJoCoXML;
1719
import org.mujoco.xml.Mujoco;
1820
import org.mujoco.xml.Mujoco.Builder;
21+
import org.mujoco.xml.attributetypes.BuiltinType;
1922
import org.mujoco.xml.attributetypes.CameramodeType;
2023
import org.mujoco.xml.attributetypes.GeomtypeType;
24+
import org.mujoco.xml.attributetypes.JointtypeType;
2125
import org.mujoco.xml.body.LightType;
2226

2327
import com.kscs.util.jaxb.BoundList;
2428

2529
public class XMLtest {
2630

2731
@Test
28-
public void marshal() throws JAXBException {
32+
public void marshal() throws JAXBException, IOException, InterruptedException {
2933

3034

3135
Builder<Void> builder = Mujoco.builder();
@@ -38,34 +42,66 @@ public void marshal() throws JAXBException {
3842
;
3943
builder.addStatistic()
4044
.withCenter("0 0 0.7");
45+
Mujoco.Asset.Builder<?> asset = builder.addAsset();
46+
asset.addTexture()
47+
.withName("grid")
48+
.withType("2d")
49+
.withBuiltin(BuiltinType.CHECKER)
50+
.withRgb1(".1 .2 .3")
51+
.withRgb2(".2 .3 .4")
52+
.withWidth(512)
53+
.withHeight(512)
54+
;
55+
asset.addMaterial()
56+
.withName("grid")
57+
.withTexture("grid")
58+
.withTexrepeat("1 1")
59+
.withTexuniform(true)
60+
.withReflectance(BigDecimal.valueOf(0.2))
61+
;
62+
// builder.addKeyframe()
63+
// .addKey()
64+
// .withName("squat")
65+
// .withQpos("0 0 0.596 0.988015 0 0.154359 0 0 0.4 0 -0.25 -0.5 -2.5 -2.65 -0.8 0.56 -0.25 -0.5 -2.5 -2.65 -0.8 0.56 0 0 0 0 0 0")
66+
//
67+
// ;
68+
builder.addTendon()
69+
.addFixed()
70+
.addJoint()
71+
.withJoint("torso")
72+
.withCoef(BigDecimal.valueOf(-0.5))
4173

42-
builder.addKeyframe()
43-
.addKey()
44-
.withName("squat")
45-
.withQpos("0 0 0.596 0.988015 0 0.154359 0 0 0.4 0 -0.25 -0.5 -2.5 -2.65 -0.8 0.56 -0.25 -0.5 -2.5 -2.65 -0.8 0.56 0 0 0 0 0 0")
46-
4774
;
48-
builder.addTendon().addFixed().addJoint();
4975
builder.addContact().addExclude()
50-
.withBody1("waist_lower")
51-
.withBody2("thigh_right")
76+
.withBody1("torso")
77+
.withBody2("head")
5278
;
5379
Mujoco.Default.Builder<?> addDefault = builder.addDefault();
5480
addDefault
5581
.addMotor()
5682
.withCtrllimited(true)
5783
.withCtrlrange("-1 1")
5884
;
59-
addDefault.addDefault()
85+
org.mujoco.xml.DefaultType.Builder<?> addDefault2 = addDefault.addDefault();
86+
addDefault2
6087
.withClazz("body")
6188
.addDefault()
6289
.withClazz("thigh")
6390
.addGeom()
6491
.withSize("0.06")
6592

6693
;
94+
addDefault2.addGeom()
95+
.withType(GeomtypeType.CAPSULE)
96+
.withCondim(1)
97+
.withGroup(1)
98+
.withMaterial("grid")
99+
.withFriction("0.7")
100+
.withSolimp("0.9 0.99 0.003")
101+
.withSolref("0.015 1")
102+
;
67103
Mujoco.Actuator.Builder<?> addActuator = builder.addActuator();
68-
addActuator.addMuscle();
104+
//addActuator.addMuscle();
69105
addActuator.addMotor()
70106
.withName("torso")
71107
.withGear("100")
@@ -97,40 +133,58 @@ public void marshal() throws JAXBException {
97133
.withPos("0 0 1.282")
98134
.addGeom()
99135
.withName("torso")
136+
.withSize("0.07")
137+
.withFromto("0 -.07 0 0 .07 0")
100138

101139
;
140+
102141
topbody.addLight()
103142
.withName("top")
104143
.withMode(CameramodeType.TRACKCOM)
105144
.withPos("0 0 2")
106145
;
107146
topbody.addCamera();
108147
topbody.addFreejoint().withName("root");
109-
148+
topbody.addInertial()
149+
.withMass(BigDecimal.valueOf(0.01))
150+
.withPos(".1 .1 .1")
151+
;
110152
//topbody.addBody();
111153
// In the future if this test breaks after generating code, see the solution in
112154
// https://github.com/CommonWealthRobotics/mujoco-java/pull/6/commits/e8d65eeec490bb935111a99f5d049991735864c0
113-
topbody.addBody()
114-
.withName("Head")
155+
BodyarchType.Builder<?> head = topbody.addBody();
156+
head
157+
.withName("head")
115158
.withPos("0 0 2.2")
116-
.addBody()
117-
.withName("mowhawk")
118-
.withPos("0 0 1.2")
119159
;
120-
topbody.addBody().withName("arm").withPos("0 1.1 0");
121-
122-
//topbody.addBody(topbody.ge);
123-
//org.mujoco.xml.BodyarchType.Builder<?> lightBuilder = topbody.addLight(element );
124-
//lightBuilder.withName("spotlight");
125-
//lightBuilder.withPos("0 -6 4");
126-
160+
head.addInertial()
161+
.withMass(BigDecimal.valueOf(0.01))
162+
.withPos(".1 .1 .1")
163+
;
164+
head.addJoint(JointtypeType.HINGE)
165+
.withName("torso")
166+
.withAxis("2 1 1")
167+
;
168+
// topbody.addBody().withName("arm").withPos("0 1.1 0");
127169

128170
Mujoco m =builder.build();
129171

130172
String marshaled = MuJoCoXML.marshal(m);
131173

132174
System.out.println(marshaled);
133-
175+
Mujoco m2 = MuJoCoXML.unmarshal(marshaled);
176+
if(m2==null)
177+
fail("unmarshal failed");
178+
MuJoCoModelManager mRuntime = new MuJoCoModelManager(marshaled);
179+
while (mRuntime.getCurrentSimulationTimeSeconds() < 1) {
180+
mRuntime.step();
181+
// sleep
182+
Thread.sleep(mRuntime.getTimestepMilliSeconds());
183+
for(String s:mRuntime.getBodyNames()) {
184+
//System.out.println("Body "+s+" pose "+mRuntime.getBodyPose(s));
185+
}
186+
}
187+
mRuntime.close();
134188
}
135189

136190
@Test

0 commit comments

Comments
 (0)