-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMuJoCoCar.lf
More file actions
71 lines (62 loc) · 2.43 KB
/
Copy pathMuJoCoCar.lf
File metadata and controls
71 lines (62 loc) · 2.43 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
65
66
67
68
69
70
71
/** @file Reactor for the basic car demonstrator in MuJoCo. */
target Python {
keepalive: true,
files: ["../models/car.xml"]
}
import MuJoCoAdvance from "MuJoCoAdvance.lf"
/**
* @brief Model of a two-wheeled steerable vehicle.
*
* See [README.md](../README.md) for prerequisites and installation instructions.
*
* This reactor wraps the basic car demonstration model distributed with MuJoCo. It provides inputs
* to control the two actuators defined in the [XML model file](../models/car.xml) and outputs that
* report the sensor data from said model.
*
* @author Edward A. Lee
*/
reactor MuJoCoCar(model_file = {= lf.source_directory() + "/models/car.xml" =})
extends MuJoCoAdvance {
input forward
input turn
output right_force
output left_force
state forward_index = 0 # Actuator indexes.
state turn_index = 0
state right_address = 0 # Sensor addresses
state left_address = 0
reaction(startup) {=
self.forward_index = mujoco.mj_name2id(self.context.m, mujoco.mjtObj.mjOBJ_ACTUATOR, "forward")
self.turn_index = mujoco.mj_name2id(self.context.m, mujoco.mjtObj.mjOBJ_ACTUATOR, "turn")
right_index = mujoco.mj_name2id(self.context.m, mujoco.mjtObj.mjOBJ_SENSOR, "right")
left_index = mujoco.mj_name2id(self.context.m, mujoco.mjtObj.mjOBJ_SENSOR, "left")
if self.forward_index < 0 or self.turn_index < 0 or right_index < 0 or left_index < 0:
print("Mismatched model file. Missing sensors or actuators.")
lf.request_stop()
return
self.right_address = int(self.context.m.sensor_adr[right_index])
right_dim = int(self.context.m.sensor_dim[right_index])
self.left_address = int(self.context.m.sensor_adr[left_index])
left_dim = int(self.context.m.sensor_dim[left_index])
if right_dim != 1 or left_dim != 1:
print("Mismatched model file. Expected sensors of dimension 1.")
lf.request_stop()
=}
reaction(restart) {=
self.advance_simulator()
self.context.d.ctrl[self.forward_index] = 0.0
self.context.d.ctrl[self.turn_index] = 0.0
=}
reaction(forward) {=
self.advance_simulator()
self.context.d.ctrl[self.forward_index] = forward.value
=}
reaction(turn) {=
self.advance_simulator()
self.context.d.ctrl[self.turn_index] = turn.value
=}
reaction(advance) -> right_force, left_force {=
right_force.set(float(self.context.d.sensordata[self.right_address]))
left_force.set(float(self.context.d.sensordata[self.left_address]))
=}
}