Install timers by default in Python bindings
PiperOrigin-RevId: 744663306 Change-Id: Ia322ace94d1f269ff9b0275567b2e453acc94843
This commit is contained in:
committed by
Copybara-Service
parent
26c14c500e
commit
93251f07d9
@@ -1083,6 +1083,20 @@ Euler integrator, semi-implicit in velocity.
|
|||||||
):
|
):
|
||||||
mujoco.mj_forward(self.model, self.data)
|
mujoco.mj_forward(self.model, self.data)
|
||||||
|
|
||||||
|
def test_timer_installed_by_default(self):
|
||||||
|
timer_step = mujoco.mjtTimer.mjTIMER_STEP
|
||||||
|
self.assertEqual(self.data.timer[timer_step].number, 0)
|
||||||
|
self.assertEqual(self.data.timer[timer_step].duration, 0.0)
|
||||||
|
|
||||||
|
mujoco.mj_step(self.model, self.data)
|
||||||
|
self.assertEqual(self.data.timer[timer_step].number, 1)
|
||||||
|
duration_1 = self.data.timer[timer_step].duration
|
||||||
|
self.assertGreater(duration_1, 0.0)
|
||||||
|
|
||||||
|
mujoco.mj_step(self.model, self.data, 5)
|
||||||
|
self.assertEqual(self.data.timer[timer_step].number, 6)
|
||||||
|
self.assertGreater(self.data.timer[timer_step].duration, duration_1)
|
||||||
|
|
||||||
def test_mjcb_time(self):
|
def test_mjcb_time(self):
|
||||||
|
|
||||||
class CallCounter:
|
class CallCounter:
|
||||||
|
|||||||
@@ -18,6 +18,7 @@
|
|||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <array>
|
#include <array>
|
||||||
|
#include <chrono> // NOLINT(build/c++11)
|
||||||
#include <cctype>
|
#include <cctype>
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
@@ -553,6 +554,16 @@ MjDataWrapper* MjDataWrapper::FromRawPointer(raw::MjData* m) noexcept {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
// default timer callback (seconds)
|
||||||
|
mjtNum GetTime() {
|
||||||
|
using Clock = std::chrono::steady_clock;
|
||||||
|
using Seconds = std::chrono::duration<mjtNum>;
|
||||||
|
static const Clock::time_point tm_start = Clock::now();
|
||||||
|
return Seconds(Clock::now() - tm_start).count();
|
||||||
|
}
|
||||||
|
} // namespace
|
||||||
|
|
||||||
MjDataWrapper::MjWrapper(MjModelWrapper* model)
|
MjDataWrapper::MjWrapper(MjModelWrapper* model)
|
||||||
: WrapperBase(InterceptMjErrors(mj_makeData)(model->get()),
|
: WrapperBase(InterceptMjErrors(mj_makeData)(model->get()),
|
||||||
&MjDataCapsuleDestructor),
|
&MjDataCapsuleDestructor),
|
||||||
@@ -582,6 +593,14 @@ MjDataWrapper::MjWrapper(MjModelWrapper* model)
|
|||||||
throw UnexpectedError(
|
throw UnexpectedError(
|
||||||
"MjDataRawPointerMap already contains this raw mjData*");
|
"MjDataRawPointerMap already contains this raw mjData*");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// install default timer if not already installed
|
||||||
|
{
|
||||||
|
py::gil_scoped_acquire gil;
|
||||||
|
if (!mjcb_time) {
|
||||||
|
mjcb_time = GetTime;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
MjDataWrapper::MjWrapper(const MjDataWrapper& other)
|
MjDataWrapper::MjWrapper(const MjDataWrapper& other)
|
||||||
|
|||||||
Reference in New Issue
Block a user