Preserve time when recompiling a model.
Fixes #1890. PiperOrigin-RevId: 662084324 Change-Id: I6aed40761cee5044e63426935d102123db295b25
This commit is contained in:
committed by
Copybara-Service
parent
d3a976e10d
commit
648fd56ed1
@@ -91,13 +91,16 @@ mjModel* mj_compile(mjSpec* s, const mjVFS* vfs) {
|
|||||||
// recompile spec into existing model and data while preserving the state
|
// recompile spec into existing model and data while preserving the state
|
||||||
void mj_recompile(mjSpec* s, const mjVFS* vfs, mjModel* m, mjData* d) {
|
void mj_recompile(mjSpec* s, const mjVFS* vfs, mjModel* m, mjData* d) {
|
||||||
mjCModel* modelC = static_cast<mjCModel*>(s->element);
|
mjCModel* modelC = static_cast<mjCModel*>(s->element);
|
||||||
|
mjtNum time = 0;
|
||||||
if (d) {
|
if (d) {
|
||||||
|
time = d->time;
|
||||||
modelC->SaveState(d->qpos, d->qvel, d->act);
|
modelC->SaveState(d->qpos, d->qvel, d->act);
|
||||||
}
|
}
|
||||||
modelC->Compile(vfs, &m);
|
modelC->Compile(vfs, &m);
|
||||||
if (d) {
|
if (d) {
|
||||||
modelC->MakeData(m, &d);
|
modelC->MakeData(m, &d);
|
||||||
modelC->RestoreState(d->qpos, d->qvel, d->act);
|
modelC->RestoreState(d->qpos, d->qvel, d->act);
|
||||||
|
d->time = time;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -851,6 +851,7 @@ TEST_F(MujocoTest, PreserveState) {
|
|||||||
// step models
|
// step models
|
||||||
mj_step(model, data);
|
mj_step(model, data);
|
||||||
mj_step(m_expected, d_expected);
|
mj_step(m_expected, d_expected);
|
||||||
|
EXPECT_THAT(data->time, model->opt.timestep);
|
||||||
|
|
||||||
// detach subtree
|
// detach subtree
|
||||||
mjsBody* body = mjs_findBody(spec, "detachable");
|
mjsBody* body = mjs_findBody(spec, "detachable");
|
||||||
@@ -877,6 +878,7 @@ TEST_F(MujocoTest, PreserveState) {
|
|||||||
// compile new model
|
// compile new model
|
||||||
mj_recompile(spec, 0, model, data);
|
mj_recompile(spec, 0, model, data);
|
||||||
EXPECT_THAT(model, NotNull());
|
EXPECT_THAT(model, NotNull());
|
||||||
|
EXPECT_THAT(data->time, model->opt.timestep);
|
||||||
|
|
||||||
// compare qpos
|
// compare qpos
|
||||||
EXPECT_EQ(model->nq, m_expected->nq);
|
EXPECT_EQ(model->nq, m_expected->nq);
|
||||||
|
|||||||
Reference in New Issue
Block a user