Preserve time when recompiling a model.

Fixes #1890.

PiperOrigin-RevId: 662084324
Change-Id: I6aed40761cee5044e63426935d102123db295b25
This commit is contained in:
Alessio Quaglino
2024-08-12 07:59:53 -07:00
committed by Copybara-Service
parent d3a976e10d
commit 648fd56ed1
2 changed files with 5 additions and 0 deletions
+3
View File
@@ -91,13 +91,16 @@ mjModel* mj_compile(mjSpec* s, const mjVFS* vfs) {
// recompile spec into existing model and data while preserving the state
void mj_recompile(mjSpec* s, const mjVFS* vfs, mjModel* m, mjData* d) {
mjCModel* modelC = static_cast<mjCModel*>(s->element);
mjtNum time = 0;
if (d) {
time = d->time;
modelC->SaveState(d->qpos, d->qvel, d->act);
}
modelC->Compile(vfs, &m);
if (d) {
modelC->MakeData(m, &d);
modelC->RestoreState(d->qpos, d->qvel, d->act);
d->time = time;
}
}
+2
View File
@@ -851,6 +851,7 @@ TEST_F(MujocoTest, PreserveState) {
// step models
mj_step(model, data);
mj_step(m_expected, d_expected);
EXPECT_THAT(data->time, model->opt.timestep);
// detach subtree
mjsBody* body = mjs_findBody(spec, "detachable");
@@ -877,6 +878,7 @@ TEST_F(MujocoTest, PreserveState) {
// compile new model
mj_recompile(spec, 0, model, data);
EXPECT_THAT(model, NotNull());
EXPECT_THAT(data->time, model->opt.timestep);
// compare qpos
EXPECT_EQ(model->nq, m_expected->nq);