Skip to content

Commit 08edde5

Browse files
yuvaltassacopybara-github
authored andcommitted
Add test for armature, model will be used in upcoming improved documentation for armature.
PiperOrigin-RevId: 719916567 Change-Id: I7f74bd3c3864f43f5f68d458d5b128cfee1d7e6c
1 parent 2c3d0be commit 08edde5

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

test/engine/engine_forward_test.cc

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,47 @@ TEST_F(ForwardTest, DamperDampens) {
162162
mj_deleteModel(model);
163163
}
164164

165+
static const char* const kArmatureEquivalencePath =
166+
"engine/testdata/armature_equivalence.xml";
167+
168+
// test that adding joint armature is equivalent to a coupled rotating mass with
169+
// a gear ratio enforced by an equality
170+
TEST_F(ForwardTest, ArmatureEquivalence) {
171+
const std::string xml_path = GetTestDataFilePath(kArmatureEquivalencePath);
172+
char error[1000];
173+
mjModel* model = mj_loadXML(xml_path.c_str(), nullptr, error, sizeof(error));
174+
ASSERT_THAT(model, NotNull()) << error;
175+
mjData* data = mj_makeData(model);
176+
177+
// with actuators
178+
mjtNum qpos_mse = 0;
179+
int nstep = 0;
180+
while (data->time < 4) {
181+
data->ctrl[0] = data->ctrl[1] = mju_sin(2*data->time);
182+
mj_step(model, data);
183+
nstep++;
184+
mjtNum err = data->qpos[0] - data->qpos[2];
185+
qpos_mse += err * err;
186+
}
187+
EXPECT_LT(mju_sqrt(qpos_mse/nstep), 1e-3);
188+
189+
// no actuators
190+
model->opt.disableflags |= mjDSBL_ACTUATION;
191+
qpos_mse = 0;
192+
nstep = 0;
193+
mj_resetData(model, data);
194+
while (data->time < 4) {
195+
mj_step(model, data);
196+
nstep++;
197+
mjtNum err = data->qpos[0] - data->qpos[2];
198+
qpos_mse += err * err;
199+
}
200+
EXPECT_LT(mju_sqrt(qpos_mse/nstep), 1e-3);
201+
202+
mj_deleteData(data);
203+
mj_deleteModel(model);
204+
}
205+
165206
// --------------------------- implicit integrator -----------------------------
166207

167208
using ImplicitIntegratorTest = MujocoTest;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<mujoco>
2+
<worldbody>
3+
<body name="link1">
4+
<!-- inertia of link1 is 1.0 -->
5+
<joint name="link1" axis="0 -1 0"/>
6+
<geom type="capsule" size=".02" fromto="0 0 0 1 0 0" mass="0"/>
7+
<geom type="sphere" size=".1" mass="1" pos="1 0 0"/>
8+
</body>
9+
<body name="motor1">
10+
<!-- inertia of motor1 is 1/12 * m * (0.2^2 + 0.2^2) = 0.2
11+
see https://en.wikipedia.org/wiki/List_of_moments_of_inertia -->
12+
<joint name="motor1" axis="0 -1 0"/>
13+
<geom type="box" size=".1 .1 .1" mass="30" contype="0" conaffinity="0"/>
14+
</body>
15+
16+
<body name="link2" pos="1.5 0 0">
17+
<!-- armature from reflected inertia of motor1 is 0.2 * 3^2 = 1.8 -->
18+
<joint name="link2" armature="1.8" axis="0 -1 0"/>
19+
<geom type="capsule" size=".02" fromto="0 0 0 1 0 0" mass="0"/>
20+
<geom type="sphere" size=".1" mass="1" pos="1 0 0"/>
21+
</body>
22+
</worldbody>
23+
24+
<equality>
25+
<!-- gear ratio between motor1 and link1 is 3:1 -->
26+
<joint joint1="motor1" joint2="link1" polycoef="0 3"/>
27+
</equality>
28+
29+
<actuator>
30+
<position name="link1" joint="motor1" kp="1" kv=".1" ctrlrange="-5 5"/>
31+
<!-- actuators are made equivalent by setting link2 gear to link1 gear ratio -->
32+
<position name="link2" joint="link2" kp="1" kv=".1" ctrlrange="-5 5" gear="3"/>
33+
</actuator>
34+
</mujoco>

0 commit comments

Comments
 (0)