Enable activation clamping when using implicit integrator.

- Introduced private function `mj_advance()` as single point of state & time advancement.

PiperOrigin-RevId: 456525969
Change-Id: Iae17217e305c7baf07cb5ecd1e1b84b936421097
This commit is contained in:
Yuval Tassa
2022-06-22 08:55:06 -07:00
committed by Copybara-Service
parent 2a26bf1ba5
commit 03c2011463
3 changed files with 73 additions and 70 deletions
+29 -8
View File
@@ -38,11 +38,17 @@ static const char* const kDampedActuatorsPath =
using ::testing::Pointwise;
using ::testing::DoubleNear;
using ::testing::Ne;
using ForwardTest = MujocoTest;
// --------------------------- activation limits -------------------------------
TEST_F(ForwardTest, ActLimited) {
struct ActLimitedTestCase {
std::string test_name;
mjtIntegrator integrator;
};
using ParametrizedForwardTest = ::testing::TestWithParam<ActLimitedTestCase>;
TEST_P(ParametrizedForwardTest, ActLimited) {
static constexpr char xml[] = R"(
<mujoco>
<option timestep="0.01"/>
@@ -63,15 +69,17 @@ TEST_F(ForwardTest, ActLimited) {
mjModel* model = LoadModelFromString(xml);
mjData* data = mj_makeData(model);
model->opt.integrator = GetParam().integrator;
data->ctrl[0] = 1.0;
// integrating up from 0, we will hit the clamp after 99 steps
for (int i=0; i<200; i++) {
mj_step(model, data);
// always greater than lower bound
ASSERT_GT(data->act[0], -1);
EXPECT_GT(data->act[0], -1);
// after 99 steps we hit the upper bound
if (i < 99) ASSERT_LT(data->act[0], 1);
if (i >= 99) ASSERT_EQ(data->act[0], 1);
if (i < 99) EXPECT_LT(data->act[0], 1);
if (i >= 99) EXPECT_EQ(data->act[0], 1);
}
data->ctrl[0] = -1.0;
@@ -79,18 +87,31 @@ TEST_F(ForwardTest, ActLimited) {
for (int i=0; i<300; i++) {
mj_step(model, data);
// always smaller than upper bound
ASSERT_LT(data->act[0], model->actuator_actrange[1]);
EXPECT_LT(data->act[0], model->actuator_actrange[1]);
// after 199 steps we hit the lower bound
if (i < 199) ASSERT_GT(data->act[0], model->actuator_actrange[0]);
if (i >= 199) ASSERT_EQ(data->act[0], model->actuator_actrange[0]);
if (i < 199) EXPECT_GT(data->act[0], model->actuator_actrange[0]);
if (i >= 199) EXPECT_EQ(data->act[0], model->actuator_actrange[0]);
}
mj_deleteData(data);
mj_deleteModel(model);
}
INSTANTIATE_TEST_SUITE_P(
ParametrizedForwardTest, ParametrizedForwardTest,
testing::ValuesIn<ActLimitedTestCase>({
{"Euler", mjINT_EULER},
{"Implicit", mjINT_IMPLICIT},
{"RK4", mjINT_RK4},
}),
[](const testing::TestParamInfo<ParametrizedForwardTest::ParamType>& info) {
return info.param.test_name;
});
// --------------------------- damping actuator --------------------------------
using ForwardTest = MujocoTest;
TEST_F(ForwardTest, DamperDampens) {
static constexpr char xml[] = R"(
<mujoco>
+1 -1
View File
@@ -113,10 +113,10 @@
<motor tendon="fixed" gear="100"/>
<motor tendon="spatial" gear="10"/>
<motor joint="hipy_2" gear="100"/>
<position joint="hipz_0" kp="100"/>
<position joint="hipz_1" kp="100"/>
<position joint="hipz_2" kp="100"/>
<velocity joint="wheel_2" kv="1"/>
<intvelocity joint="hipz_0" kp="100" actrange="-1 1"/>
<general site="wheel_0" gear="0 0 0 0 10 0" dyntype="filter" dynprm="1"/>
<general joint="wheel_1" biastype="affine" dyntype="integrator" dynprm="1" biasprm="0 -1"/>
</actuator>