-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCar.lf
69 lines (61 loc) · 2.01 KB
/
Car.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
/**
* Basic car driving program for Mujoco. This is the same as the MuJoCoCarDemo
* example in the mujoco-c library, which you need to install first using `lingo build`
* in this directory.
*
* See [README.md](../README.md) for prerequisites and installation instructions.
*
* @author Edward A. Lee
*/
target C {
keepalive: true, // Because of physical action in MuJoCoCar.
}
import MuJoCoCar from <mujoco-c/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;
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);
} 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->speed -= self->turn_sensitivity;
lf_set(m.turn, self->speed);
} else if (m.key->value.key==GLFW_KEY_LEFT) {
self->speed += self->turn_sensitivity;
lf_set(m.turn, self->speed);
}
}
=}
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.");
=}
}