-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMuJoCoAuto.lf
More file actions
64 lines (53 loc) · 2.13 KB
/
MuJoCoAuto.lf
File metadata and controls
64 lines (53 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/** @file Base class for reactors using the MuJoCo framework that advances itself. */
target C
import MuJoCoBase from "MuJoCoBase.lf"
/**
* @brief MuJoCo simulation with visualization.
*
* See [README.md](../README.md) for prerequisites and installation instructions.
*
* This reactor extends the base class to advance the simulation automatically using a
* physics timer and output a `tick` event at each step. The `sim_step` parameter,
* which defaults to 1 ms, determines the time step for the physics simulation.
* In addition, it will periodically update the display with period given by the
* `frame_period` parameter, which defaults to 33 ms, using a rendering timer.
*
* @author Edward A. Lee
* @author Chadlia Jerad
*/
reactor MuJoCoAuto(frame_period: time = 33 ms, sim_step: time = 1 ms) extends MuJoCoBase {
preamble {=
#define OVERLAY_BUF_SIZE 100
=}
output tick: bool
timer rendering_timer(0, frame_period)
timer physics_timer(0, sim_step)
state sim_start: double
state wall_start: double
state text_to_overlay: char[] // Buffer for text to overlay on the display.
reaction(startup) {=
self->text_to_overlay = (char*)malloc(OVERLAY_BUF_SIZE * sizeof(char));
snprintf(self->text_to_overlay, OVERLAY_BUF_SIZE, "MuJoCo Simulation Starting...");
=}
reaction(rendering_timer) {=
update_scene(self->text_to_overlay);
=}
reaction(physics_timer) -> tick {=
lf_set(tick, true);
if (glfwWindowShouldClose(self->window)) {
lf_request_stop();
return;
}
// Real-time pacing: if the simulation is running faster than real time, sleep briefly
// to let real time catch up.
double sim_time_elapsed = self->context.d->time - self->sim_start;
double wall_time_elapsed = glfwGetTime() - self->wall_start;
interval_t delta_ns = (interval_t)(self->sim_step * 1e9);
if (sim_time_elapsed > wall_time_elapsed + delta_ns) {
// Physics is ahead of wall clock: sleep briefly
// (glfwWaitEventsTimeout caps at ~1 frame)
glfwWaitEventsTimeout(sim_time_elapsed - wall_time_elapsed - delta_ns);
}
mj_step(self->context.m, self->context.d);
=}
}