Add a test for lengthrange computation, with and without threading.

PiperOrigin-RevId: 650997405
Change-Id: I45c3725dacb6ccf17977953a9ffed717c927b786
This commit is contained in:
Yuval Tassa
2024-07-10 06:38:25 -07:00
committed by Copybara-Service
parent 17f6b12930
commit 7e97caaafd
2 changed files with 85 additions and 0 deletions
+44
View File
@@ -0,0 +1,44 @@
<mujoco>
<compiler>
<lengthrange mode="all"/>
</compiler>
<worldbody>
<light pos="0 0 10"/>
<site name="a" size=".02"/>
<replicate offset="0 .1 0" count="17">
<body pos="1 0 1">
<joint axis="0 -1 0" range="0 90"/>
<geom type="capsule" size=".02" fromto="0 0 0 0 0 -1"/>
<site name="b" pos="0 0 -1"/>
</body>
</replicate>
</worldbody>
<tendon>
<spatial name="b">
<site site="a"/>
<site site="b"/>
</spatial>
</tendon>
<actuator>
<position name="b00" tendon="b00" ctrlrange="0 3" kp="100" dampratio="1"/>
<position name="b01" tendon="b01" ctrlrange="0 3" kp="100" dampratio="1"/>
<position name="b02" tendon="b02" ctrlrange="0 3" kp="100" dampratio="1"/>
<position name="b03" tendon="b03" ctrlrange="0 3" kp="100" dampratio="1"/>
<position name="b04" tendon="b04" ctrlrange="0 3" kp="100" dampratio="1"/>
<position name="b05" tendon="b05" ctrlrange="0 3" kp="100" dampratio="1"/>
<position name="b06" tendon="b06" ctrlrange="0 3" kp="100" dampratio="1"/>
<position name="b07" tendon="b07" ctrlrange="0 3" kp="100" dampratio="1"/>
<position name="b08" tendon="b08" ctrlrange="0 3" kp="100" dampratio="1"/>
<position name="b09" tendon="b09" ctrlrange="0 3" kp="100" dampratio="1"/>
<position name="b10" tendon="b10" ctrlrange="0 3" kp="100" dampratio="1"/>
<position name="b11" tendon="b11" ctrlrange="0 3" kp="100" dampratio="1"/>
<position name="b12" tendon="b12" ctrlrange="0 3" kp="100" dampratio="1"/>
<position name="b13" tendon="b13" ctrlrange="0 3" kp="100" dampratio="1"/>
<position name="b14" tendon="b14" ctrlrange="0 3" kp="100" dampratio="1"/>
<position name="b15" tendon="b15" ctrlrange="0 3" kp="100" dampratio="1"/>
<position name="b16" tendon="b16" ctrlrange="0 3" kp="100" dampratio="1"/>
</actuator>
</mujoco>
+41
View File
@@ -15,8 +15,10 @@
// Tests for user/user_model.cc.
#include <array>
#include <cmath>
#include <cstddef>
#include <string>
#include <vector>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
@@ -404,5 +406,44 @@ TEST_F(DiscardVisualTest, DiscardVisualEquivalent) {
mj_deleteData(d2);
}
// ------------- test lengthrange ----------------------------------------------
using LengthRangeTest = MujocoTest;
TEST_F(LengthRangeTest, LengthRangeThreading) {
char error[1024];
size_t error_sz = 1024;
std::string field = "";
static const char* const kLengthrangePath =
"user/testdata/lengthrange.xml";
const std::string xml_path1 = GetTestDataFilePath(kLengthrangePath);
mjSpec* spec = mj_parseXML(xml_path1.c_str(), 0, error, error_sz);
EXPECT_THAT(spec, NotNull()) << error;
mjModel* model1 = mj_compile(spec, 0);
EXPECT_THAT(model1, NotNull()) << error;
// model is such that the lengthrange for first actuator is [1, sqrt(5)]
EXPECT_THAT(model1->actuator_lengthrange[0], DoubleNear(1.0, 1e-3));
EXPECT_THAT(model1->actuator_lengthrange[1],
DoubleNear(std::sqrt(5.0), 1e-3));
// recompile without threads
ASSERT_EQ(spec->usethread, 1);
spec->usethread = 0;
mjModel* model2 = mj_compile(spec, 0);
EXPECT_THAT(model2, NotNull()) << error;
// expect threaded and unthreaded models to be identical
EXPECT_LE(CompareModel(model1, model2, field), 0)
<< "Threaded and unthreaded lengthrange models are different!\n"
<< "Different field: " << field << '\n';
mj_deleteModel(model1);
mj_deleteModel(model2);
mj_deleteSpec(spec);
}
} // namespace
} // namespace mujoco