From bc1490b7a8312fc4e875042e9c0bdcf294f56ddc Mon Sep 17 00:00:00 2001 From: Alessio Quaglino Date: Tue, 4 Oct 2022 07:08:21 -0700 Subject: [PATCH] Add realtime parameter to visual global. PiperOrigin-RevId: 478775714 Change-Id: Ic2d178a5c57d298d005215219ce8470392c4651b --- doc/APIreference.rst | 1 + doc/XMLreference.rst | 3 +++ doc/changelog.rst | 2 ++ include/mujoco/mjmodel.h | 1 + simulate/simulate.cc | 12 ++++++++++++ simulate/simulate.h | 2 +- src/engine/engine_io.c | 1 + src/xml/xml_native_reader.cc | 8 ++++++-- src/xml/xml_native_writer.cc | 1 + unity/Runtime/Bindings/MujocoBindings.cs | 1 + 10 files changed, 29 insertions(+), 3 deletions(-) diff --git a/doc/APIreference.rst b/doc/APIreference.rst index 9b33a285..4371055b 100644 --- a/doc/APIreference.rst +++ b/doc/APIreference.rst @@ -1237,6 +1237,7 @@ mjVisual float ipd; // inter-pupilary distance for free camera float linewidth; // line width for wireframe and ray rendering float glow; // glow coefficient for selected body + float realtime; // initial real-time factor (1: real time) int offwidth; // width of offscreen buffer int offheight; // height of offscreen buffer } global; diff --git a/doc/XMLreference.rst b/doc/XMLreference.rst index 86457ae7..a2bd8ec5 100644 --- a/doc/XMLreference.rst +++ b/doc/XMLreference.rst @@ -592,6 +592,9 @@ is effectively a miscellaneous subsection. :at:`glow`: :at-val:`real, "0.3"` The value of this attribute is added to the emission coefficient of all geoms attached to the selected body. As a result, the selected body appears to glow. +:at:`realtime`: :at-val:`real, "1"` + This value sets the initial real-time factor of the model, when loaded in `simulate`. 1: real time. Less than 1: + slower than real time. Must be greater than 0. :at:`offwidth`: :at-val:`int, "640"` This and the next attribute specify the size in pixels of the off-screen OpenGL rendering buffer. This attribute specifies the width of the buffer. The size of this buffer can also be adjusted at runtime, but it is usually more diff --git a/doc/changelog.rst b/doc/changelog.rst index 9cef90fe..efb6102e 100644 --- a/doc/changelog.rst +++ b/doc/changelog.rst @@ -50,6 +50,8 @@ General - Added :at:`assetdir` compiler option, which sets the values of both :at:`meshdir` and :at:`texturedir`. Values in the latter attributes take precedence over :at:`assetdir`. +- Added :at:`realtime` option to :ref:`visual` for starting a simulation at a slower speed. + Python bindings ^^^^^^^^^^^^^^^ diff --git a/include/mujoco/mjmodel.h b/include/mujoco/mjmodel.h index 02541969..490929c9 100644 --- a/include/mujoco/mjmodel.h +++ b/include/mujoco/mjmodel.h @@ -429,6 +429,7 @@ struct mjVisual_ { // visualization options float elevation; // initial elevation of free camera (degrees) float linewidth; // line width for wireframe and ray rendering float glow; // glow coefficient for selected body + float realtime; // initial real-time factor (1: real time) int offwidth; // width of offscreen buffer int offheight; // height of offscreen buffer } global; diff --git a/simulate/simulate.cc b/simulate/simulate.cc index 855a450d..5f13406e 100644 --- a/simulate/simulate.cc +++ b/simulate/simulate.cc @@ -1648,6 +1648,18 @@ void Simulate::loadmodel() { // clear request this->loadrequest = 0; cond_loadrequest.notify_all(); + + // set real time index + int numclicks = sizeof(this->percentRealTime) / sizeof(this->percentRealTime[0]); + float min_error = 1e6; + float desired = mju_log(100*this->m->vis.global.realtime); + for (int click=0; clickpercentRealTime[click]) - desired); + if (error < min_error) { + min_error = error; + this->realTimeIndex = click; + } + } } diff --git a/simulate/simulate.h b/simulate/simulate.h index 1c9577e9..4091ba6e 100644 --- a/simulate/simulate.h +++ b/simulate/simulate.h @@ -125,7 +125,7 @@ class MJSIMULATEAPI Simulate { char previous_filename[kMaxFilenameLength] = ""; // time synchronization - int realTimeIndex = 0; + int realTimeIndex; bool speedChanged = true; float measuredSlowdown = 1.0; // logarithmically spaced realtime slow-down coefficients (percent) diff --git a/src/engine/engine_io.c b/src/engine/engine_io.c index 46758776..b110a6bc 100644 --- a/src/engine/engine_io.c +++ b/src/engine/engine_io.c @@ -160,6 +160,7 @@ void mj_defaultVisual(mjVisual* vis) { vis->global.glow = 0.3; vis->global.offwidth = 640; vis->global.offheight = 480; + vis->global.realtime = 1.0; // rendering quality vis->quality.shadowsize = 4096; diff --git a/src/xml/xml_native_reader.cc b/src/xml/xml_native_reader.cc index a4d33b71..6171edf4 100644 --- a/src/xml/xml_native_reader.cc +++ b/src/xml/xml_native_reader.cc @@ -109,8 +109,8 @@ static const char* MJCF[nMJCF][mjXATTRNUM] = { {"visual", "*", "0"}, {"<"}, - {"global", "?", "8", "fovy", "ipd", "azimuth", "elevation", "linewidth", "glow", "offwidth", - "offheight"}, + {"global", "?", "9", "fovy", "ipd", "azimuth", "elevation", "linewidth", "glow", "offwidth", + "offheight", "realtime"}, {"quality", "?", "5", "shadowsize", "offsamples", "numslices", "numstacks", "numquads"}, {"headlight", "?", "4", "ambient", "diffuse", "specular", "active"}, @@ -2172,6 +2172,10 @@ void mjXReader::Visual(XMLElement* section) { ReadAttr(elem, "glow", 1, &vis->global.glow, text); ReadAttrInt(elem, "offwidth", &vis->global.offwidth); ReadAttrInt(elem, "offheight", &vis->global.offheight); + if (ReadAttr(elem, "realtime", 1, &vis->global.realtime, text)) + if (vis->global.realtime<=0) { + throw mjXError(elem, "realtime must be greater than 0"); + } } // quality sub-element diff --git a/src/xml/xml_native_writer.cc b/src/xml/xml_native_writer.cc index e9db89d7..265f2595 100644 --- a/src/xml/xml_native_writer.cc +++ b/src/xml/xml_native_writer.cc @@ -868,6 +868,7 @@ void mjXWriter::Visual(XMLElement* root) { WriteAttr(elem, "elevation", 1, &vis->global.elevation, &visdef.global.elevation); WriteAttr(elem, "linewidth", 1, &vis->global.linewidth, &visdef.global.linewidth); WriteAttr(elem, "glow", 1, &vis->global.glow, &visdef.global.glow); + WriteAttr(elem, "realtime", 1, &vis->global.realtime, &visdef.global.realtime); WriteAttrInt(elem, "offwidth", vis->global.offwidth, visdef.global.offwidth); WriteAttrInt(elem, "offheight", vis->global.offheight, visdef.global.offheight); if (!elem->FirstAttribute()) { diff --git a/unity/Runtime/Bindings/MujocoBindings.cs b/unity/Runtime/Bindings/MujocoBindings.cs index f3e330da..d117fc05 100644 --- a/unity/Runtime/Bindings/MujocoBindings.cs +++ b/unity/Runtime/Bindings/MujocoBindings.cs @@ -1749,6 +1749,7 @@ public unsafe struct global { public float elevation; public float linewidth; public float glow; + public float realtime; public int offwidth; public int offheight; }