-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMuJoCoCarDemo.lf
77 lines (69 loc) · 2.22 KB
/
MuJoCoCarDemo.lf
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
65
66
67
68
69
70
71
72
73
74
75
76
77
/**
* @file Basic car driving program for MuJoCo.
*
* This program uses the [MuJoCoCar](lib/MuJoCoCar.lf) reactor to build a simple interactive
* simulation. The arrow keys can be used to drive the car. The _backspace_ or _delete_ key will
* reset the simulation to its initial conditions, and the `q` or `Q` key will quit the simulation.
*
* See [README.md](../README.md) for prerequisites and installation instructions.
*
* @author Edward A. Lee
*/
target C {
keepalive: true // Because of physical action.
}
import MuJoCoCar from "lib/MuJoCoCar.lf"
main reactor(
period: time = 33333333 ns,
speed_sensitivity: double = 0.05,
turn_sensitivity: double = 0.01) {
timer t(0, period)
state speed: double = 0
state turn: double = 0
m = new MuJoCoCar()
reaction(startup) {=
lf_print("*** Backspace to reset.");
lf_print("*** Type q to quit.\n");
=}
reaction(t) -> m.advance {=
lf_set(m.advance, true);
=}
reaction(m.key) -> m.restart, m.forward, m.turn {=
// If backspace: reset simulation
// If q or Q: quit
if (m.key->value.act==GLFW_PRESS) {
if (m.key->value.key==GLFW_KEY_BACKSPACE) {
lf_set(m.restart, true);
self->speed = 0.0;
self->turn = 0.0;
} else if (m.key->value.key==GLFW_KEY_Q) {
lf_request_stop();
} else if (m.key->value.key==GLFW_KEY_UP) {
self->speed += self->speed_sensitivity;
lf_set(m.forward, self->speed);
} else if (m.key->value.key==GLFW_KEY_DOWN) {
self->speed -= self->speed_sensitivity;
lf_set(m.forward, self->speed);
} else if (m.key->value.key==GLFW_KEY_RIGHT) {
self->turn -= self->turn_sensitivity;
lf_set(m.turn, self->turn);
} else if (m.key->value.key==GLFW_KEY_LEFT) {
self->turn += self->turn_sensitivity;
lf_set(m.turn, self->turn);
}
}
=}
reaction(m.right_force, m.left_force) {=
if (m.left_force->is_present) {
printf("\r<--- %f", m.left_force->value);
}
if (m.right_force->is_present) {
printf("\t %f --->", m.right_force->value);
}
// Flush the output buffer to ensure the line updates immediately
fflush(stdout);
=}
reaction(shutdown) {=
lf_print("\nExiting.");
=}
}