nablaengine 1.0.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- nablaengine/__init__.py +4 -0
- nablaengine/_fixtures/README.md +19 -0
- nablaengine/_fixtures/data/mf_s0.npz +0 -0
- nablaengine/_fixtures/mjlab/LICENSE +27 -0
- nablaengine/_fixtures/mjlab/README.md +113 -0
- nablaengine/_fixtures/mjlab/g1_meta.json +32 -0
- nablaengine/_fixtures/mjlab/g1_train.mjb +0 -0
- nablaengine/_optional.py +30 -0
- nablaengine/_paths.py +38 -0
- nablaengine/actuators/__init__.py +169 -0
- nablaengine/actuators/bam.py +306 -0
- nablaengine/actuators/bam_params/LICENSE +202 -0
- nablaengine/actuators/bam_params/xl330/m6.json +19 -0
- nablaengine/boxbox.py +259 -0
- nablaengine/ccd.py +1122 -0
- nablaengine/checks.py +376 -0
- nablaengine/cuda.py +483 -0
- nablaengine/dist_query.py +628 -0
- nablaengine/dual.py +223 -0
- nablaengine/dyn_query.py +685 -0
- nablaengine/engines.py +114 -0
- nablaengine/envs/__init__.py +24 -0
- nablaengine/envs/brax.py +165 -0
- nablaengine/envs/convert.py +771 -0
- nablaengine/envs/dmc.py +523 -0
- nablaengine/envs/env.py +233 -0
- nablaengine/envs/kin.py +371 -0
- nablaengine/envs/ops.py +134 -0
- nablaengine/envs/rewards.py +57 -0
- nablaengine/fixtures.py +243 -0
- nablaengine/forest_engine/__init__.py +0 -0
- nablaengine/forest_engine/adjoint.py +1726 -0
- nablaengine/forest_engine/adjoint_contact.py +493 -0
- nablaengine/forest_engine/bam_hooks.py +248 -0
- nablaengine/forest_engine/body_params.py +1007 -0
- nablaengine/forest_engine/capacity_diag.py +116 -0
- nablaengine/forest_engine/ccd_coop.py +250 -0
- nablaengine/forest_engine/chol_blk.py +80 -0
- nablaengine/forest_engine/contact_forces.py +142 -0
- nablaengine/forest_engine/contact_vjp.py +462 -0
- nablaengine/forest_engine/convex.py +1331 -0
- nablaengine/forest_engine/cylinder.py +73 -0
- nablaengine/forest_engine/dist_device.py +923 -0
- nablaengine/forest_engine/dyn_device.py +696 -0
- nablaengine/forest_engine/ellipsoid.py +179 -0
- nablaengine/forest_engine/elliptic.py +272 -0
- nablaengine/forest_engine/equality.py +842 -0
- nablaengine/forest_engine/floss.py +148 -0
- nablaengine/forest_engine/foot_offset.py +368 -0
- nablaengine/forest_engine/free_gyro.py +288 -0
- nablaengine/forest_engine/host.py +530 -0
- nablaengine/forest_engine/kdof64.py +127 -0
- nablaengine/forest_engine/kinematics.py +320 -0
- nablaengine/forest_engine/margin.py +167 -0
- nablaengine/forest_engine/margin_convex.py +326 -0
- nablaengine/forest_engine/midphase.py +439 -0
- nablaengine/forest_engine/model_hooks.py +913 -0
- nablaengine/forest_engine/motor_hooks.py +50 -0
- nablaengine/forest_engine/muscle.py +485 -0
- nablaengine/forest_engine/nb128.py +118 -0
- nablaengine/forest_engine/nb64.py +163 -0
- nablaengine/forest_engine/noslip.py +1124 -0
- nablaengine/forest_engine/nv128.py +1944 -0
- nablaengine/forest_engine/object_size.py +283 -0
- nablaengine/forest_engine/replay_guard.py +40 -0
- nablaengine/forest_engine/root_spread.py +126 -0
- nablaengine/forest_engine/solref.py +74 -0
- nablaengine/forest_engine/source.py +1464 -0
- nablaengine/forest_engine/sparse_pattern.py +527 -0
- nablaengine/forest_engine/spatial.py +1045 -0
- nablaengine/forest_engine/step.py +3083 -0
- nablaengine/forest_engine/surrogate.py +498 -0
- nablaengine/forest_engine/tape.py +199 -0
- nablaengine/forest_engine/tendon.py +845 -0
- nablaengine/forest_engine/terrain.py +1059 -0
- nablaengine/forest_engine/world_forces.py +166 -0
- nablaengine/forest_engine/world_params.py +230 -0
- nablaengine/g1_engine/__init__.py +0 -0
- nablaengine/g1_engine/adjoint.py +1661 -0
- nablaengine/g1_engine/adjoint_contact.py +577 -0
- nablaengine/g1_engine/bam_hooks.py +596 -0
- nablaengine/g1_engine/condim4.py +245 -0
- nablaengine/g1_engine/contact_forces.py +500 -0
- nablaengine/g1_engine/cylccd.py +1158 -0
- nablaengine/g1_engine/cylinder.py +216 -0
- nablaengine/g1_engine/elliptic.py +565 -0
- nablaengine/g1_engine/exact_replay.py +61 -0
- nablaengine/g1_engine/host.py +358 -0
- nablaengine/g1_engine/kinematics.py +320 -0
- nablaengine/g1_engine/mesh.py +305 -0
- nablaengine/g1_engine/mesh_tables.py +79 -0
- nablaengine/g1_engine/model_hooks.py +919 -0
- nablaengine/g1_engine/motor_hooks.py +70 -0
- nablaengine/g1_engine/replay_guard.py +55 -0
- nablaengine/g1_engine/source.py +1107 -0
- nablaengine/g1_engine/sphbox.py +93 -0
- nablaengine/g1_engine/step.py +3716 -0
- nablaengine/g1_engine/tape.py +173 -0
- nablaengine/g1_engine/world_params.py +447 -0
- nablaengine/identity.py +401 -0
- nablaengine/jax_ffi.py +2536 -0
- nablaengine/model/__init__.py +1 -0
- nablaengine/model/capability.py +694 -0
- nablaengine/model/fields.py +317 -0
- nablaengine/model/gravcomp.py +64 -0
- nablaengine/model/invweight.py +145 -0
- nablaengine/motor.py +143 -0
- nablaengine/object_geometry.py +609 -0
- nablaengine/params.py +258 -0
- nablaengine/provenance.py +196 -0
- nablaengine/reference/__init__.py +0 -0
- nablaengine/reference/adjoint_groups.py +226 -0
- nablaengine/reference/jax64.py +690 -0
- nablaengine/reference/warp_physics.py +262 -0
- nablaengine/sensors/__init__.py +4 -0
- nablaengine/sensors/cameras.py +285 -0
- nablaengine/sensors/data/LIVOX_MID360_LICENSE +21 -0
- nablaengine/sensors/data/livox_mid360.npz +0 -0
- nablaengine/sensors/g1.py +521 -0
- nablaengine/sensors/jax_rays.py +364 -0
- nablaengine/sensors/optix_rays.py +725 -0
- nablaengine/sensors/raycore.py +630 -0
- nablaengine/sensors/raygrad.py +374 -0
- nablaengine/sensors/raygrad_cuda.py +191 -0
- nablaengine/sensors/robot_mesh.py +273 -0
- nablaengine/sensors/torch_rays.py +275 -0
- nablaengine/terrain.py +424 -0
- nablaengine/torch_ffi.py +1328 -0
- nablaengine/workdir.py +37 -0
- nablaengine-1.0.2.dist-info/METADATA +236 -0
- nablaengine-1.0.2.dist-info/RECORD +136 -0
- nablaengine-1.0.2.dist-info/WHEEL +5 -0
- nablaengine-1.0.2.dist-info/licenses/LICENSE +202 -0
- nablaengine-1.0.2.dist-info/licenses/NOTICE +23 -0
- nablaengine-1.0.2.dist-info/licenses/THIRD_PARTY_NOTICES.md +176 -0
- nablaengine-1.0.2.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
"""MuJoCo Warp physics of the G1 control step (with the analytic adjoint of MuJoCo Warp pull request 1535) as one
|
|
2
|
+
batched, differentiable JAX op."""
|
|
3
|
+
|
|
4
|
+
import dataclasses
|
|
5
|
+
|
|
6
|
+
import mujoco
|
|
7
|
+
import numpy as np
|
|
8
|
+
import warp as wp
|
|
9
|
+
|
|
10
|
+
import mujoco_warp as mjw
|
|
11
|
+
from mujoco_warp._src.types import vec5
|
|
12
|
+
|
|
13
|
+
SUBSTEPS = 4
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@wp.kernel
|
|
17
|
+
def _set_dr(friction: wp.array(dtype=float), com: wp.array2d(dtype=float), foot_pairs: wp.array(dtype=int),
|
|
18
|
+
torso: int, base_ipos: wp.vec3, pair_friction: wp.array2d(dtype=vec5),
|
|
19
|
+
body_ipos: wp.array2d(dtype=wp.vec3)):
|
|
20
|
+
w = wp.tid()
|
|
21
|
+
f = friction[w]
|
|
22
|
+
for i in range(foot_pairs.shape[0]):
|
|
23
|
+
p = foot_pairs[i]
|
|
24
|
+
v = pair_friction[w, p]
|
|
25
|
+
v[0] = f
|
|
26
|
+
v[1] = f
|
|
27
|
+
pair_friction[w, p] = v
|
|
28
|
+
body_ipos[w, torso] = base_ipos + wp.vec3(com[w, 0], com[w, 1], com[w, 2])
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
@wp.kernel(enable_backward=False)
|
|
32
|
+
def _store_warm(src: wp.array2d(dtype=float), k: int, nv: int, dst: wp.array2d(dtype=float)):
|
|
33
|
+
w, i = wp.tid()
|
|
34
|
+
dst[w, k * nv + i] = src[w, i]
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
@wp.kernel(enable_backward=False)
|
|
38
|
+
def _load_warm(src: wp.array2d(dtype=float), k: int, nv: int, dst: wp.array2d(dtype=float)):
|
|
39
|
+
w, i = wp.tid()
|
|
40
|
+
dst[w, i] = src[w, k * nv + i]
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
@wp.kernel
|
|
44
|
+
def _accum(a: wp.array2d(dtype=float), out: wp.array2d(dtype=float)):
|
|
45
|
+
w, i = wp.tid()
|
|
46
|
+
out[w, i] += a[w, i]
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
class _Sim:
|
|
50
|
+
"""MJWarp model and forward/backward buffers for one device and batch size."""
|
|
51
|
+
|
|
52
|
+
def __init__(self, mjm, nworld, foot_pairs, torso_id, nconmax, njmax):
|
|
53
|
+
mjd = mujoco.MjData(mjm)
|
|
54
|
+
mujoco.mj_forward(mjm, mjd)
|
|
55
|
+
self.m = mjw.put_model(mjm, batch_sizes={"pair_friction": nworld, "body_ipos": nworld})
|
|
56
|
+
self.m.opt.warn_overflow = 0
|
|
57
|
+
kw = dict(nworld=nworld, nconmax=nconmax, njmax=njmax)
|
|
58
|
+
self.df = mjw.put_data(mjm, mjd, **kw)
|
|
59
|
+
self.D = [mjw.put_data(mjm, mjd, **kw) for _ in range(SUBSTEPS + 1)]
|
|
60
|
+
for a in (self.D[0].qpos, self.D[0].qvel, self.D[0].ctrl):
|
|
61
|
+
a.requires_grad = True
|
|
62
|
+
self.bc = mjw.create_backward_context(self.m, self.D[0])
|
|
63
|
+
self.foot_pairs = wp.array(np.asarray(foot_pairs, np.int32), dtype=int)
|
|
64
|
+
self.torso = int(torso_id)
|
|
65
|
+
self.base_ipos = wp.vec3(*mjm.body_ipos[torso_id].astype(np.float32))
|
|
66
|
+
self.nworld = nworld
|
|
67
|
+
self.nv = int(mjm.nv)
|
|
68
|
+
self.m_ws = dataclasses.replace(self.m, opt=dataclasses.replace(self.m.opt, iterations=1))
|
|
69
|
+
|
|
70
|
+
def _dr(self, friction, com):
|
|
71
|
+
wp.launch(_set_dr, dim=self.nworld,
|
|
72
|
+
inputs=[friction, com, self.foot_pairs, self.torso, self.base_ipos],
|
|
73
|
+
outputs=[self.m.pair_friction, self.m.body_ipos])
|
|
74
|
+
|
|
75
|
+
def forward(self, qpos, qvel, warm, ctrl, friction, com, qpos_out, qvel_out, warm_out):
|
|
76
|
+
d = self.df
|
|
77
|
+
self._dr(friction, com)
|
|
78
|
+
wp.copy(d.qpos, qpos)
|
|
79
|
+
wp.copy(d.qvel, qvel)
|
|
80
|
+
wp.copy(d.qacc_warmstart, warm)
|
|
81
|
+
wp.copy(d.ctrl, ctrl)
|
|
82
|
+
for _ in range(SUBSTEPS):
|
|
83
|
+
mjw.step(self.m, d)
|
|
84
|
+
wp.copy(qpos_out, d.qpos)
|
|
85
|
+
wp.copy(qvel_out, d.qvel)
|
|
86
|
+
wp.copy(warm_out, d.qacc_warmstart)
|
|
87
|
+
|
|
88
|
+
def forward_res(self, qpos, qvel, warm, ctrl, friction, com, qpos_out, qvel_out, warm_out, warm_seq):
|
|
89
|
+
"""forward(), also writing each substep's converged acceleration into warm_seq (nworld, SUBSTEPS * nv)."""
|
|
90
|
+
d = self.df
|
|
91
|
+
self._dr(friction, com)
|
|
92
|
+
wp.copy(d.qpos, qpos)
|
|
93
|
+
wp.copy(d.qvel, qvel)
|
|
94
|
+
wp.copy(d.qacc_warmstart, warm)
|
|
95
|
+
wp.copy(d.ctrl, ctrl)
|
|
96
|
+
for k in range(SUBSTEPS):
|
|
97
|
+
mjw.step(self.m, d)
|
|
98
|
+
wp.launch(_store_warm, dim=(self.nworld, self.nv), inputs=[d.qacc_warmstart, k, self.nv], outputs=[warm_seq])
|
|
99
|
+
wp.copy(qpos_out, d.qpos)
|
|
100
|
+
wp.copy(qvel_out, d.qvel)
|
|
101
|
+
wp.copy(warm_out, d.qacc_warmstart)
|
|
102
|
+
|
|
103
|
+
def backward_ws(self, qpos, qvel, warm, ctrl, friction, com, warm_seq, g_qpos, g_qvel, gq_out, gv_out, gu_out):
|
|
104
|
+
"""backward() whose recompute starts every substep's solver at the forward's converged acceleration and runs
|
|
105
|
+
one iteration (the adjoint is taken at the converged solution, so the gradients are the same)."""
|
|
106
|
+
D = self.D
|
|
107
|
+
self._dr(friction, com)
|
|
108
|
+
wp.copy(D[0].qpos, qpos)
|
|
109
|
+
wp.copy(D[0].qvel, qvel)
|
|
110
|
+
wp.copy(D[0].qacc_warmstart, warm)
|
|
111
|
+
wp.copy(D[0].ctrl, ctrl)
|
|
112
|
+
tape = wp.Tape()
|
|
113
|
+
with mjw.backward_context(self.bc):
|
|
114
|
+
for k in range(SUBSTEPS):
|
|
115
|
+
wp.launch(_load_warm, dim=(self.nworld, self.nv), inputs=[warm_seq, k, self.nv],
|
|
116
|
+
outputs=[D[k].qacc_warmstart])
|
|
117
|
+
with tape:
|
|
118
|
+
mjw.step(self.m_ws, D[k], D[k + 1])
|
|
119
|
+
tape.zero()
|
|
120
|
+
wp.copy(D[-1].qpos.grad, g_qpos)
|
|
121
|
+
wp.copy(D[-1].qvel.grad, g_qvel)
|
|
122
|
+
tape.backward()
|
|
123
|
+
wp.copy(gq_out, D[0].qpos.grad)
|
|
124
|
+
wp.copy(gv_out, D[0].qvel.grad)
|
|
125
|
+
gu_out.zero_()
|
|
126
|
+
for k in range(SUBSTEPS):
|
|
127
|
+
wp.launch(_accum, dim=gu_out.shape, inputs=[D[k].ctrl.grad], outputs=[gu_out])
|
|
128
|
+
|
|
129
|
+
def backward(self, qpos, qvel, warm, ctrl, friction, com, g_qpos, g_qvel, gq_out, gv_out, gu_out):
|
|
130
|
+
D = self.D
|
|
131
|
+
self._dr(friction, com)
|
|
132
|
+
wp.copy(D[0].qpos, qpos)
|
|
133
|
+
wp.copy(D[0].qvel, qvel)
|
|
134
|
+
wp.copy(D[0].qacc_warmstart, warm)
|
|
135
|
+
wp.copy(D[0].ctrl, ctrl)
|
|
136
|
+
tape = wp.Tape()
|
|
137
|
+
with mjw.backward_context(self.bc):
|
|
138
|
+
with tape:
|
|
139
|
+
for k in range(SUBSTEPS):
|
|
140
|
+
mjw.step(self.m, D[k], D[k + 1])
|
|
141
|
+
tape.zero()
|
|
142
|
+
wp.copy(D[-1].qpos.grad, g_qpos)
|
|
143
|
+
wp.copy(D[-1].qvel.grad, g_qvel)
|
|
144
|
+
tape.backward()
|
|
145
|
+
wp.copy(gq_out, D[0].qpos.grad)
|
|
146
|
+
wp.copy(gv_out, D[0].qvel.grad)
|
|
147
|
+
gu_out.zero_()
|
|
148
|
+
for k in range(SUBSTEPS):
|
|
149
|
+
wp.launch(_accum, dim=gu_out.shape, inputs=[D[k].ctrl.grad], outputs=[gu_out])
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
def mjx_capacity(mjm):
|
|
153
|
+
from mujoco import mjx
|
|
154
|
+
|
|
155
|
+
"""(contacts, constraint rows) per world that MJX pads this model to."""
|
|
156
|
+
d = mjx.make_data(mjm)
|
|
157
|
+
return int(d._impl.contact.dist.shape[0]), int(d._impl.efc_J.shape[0])
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
class WarpPhysics:
|
|
161
|
+
def __init__(self, mjm, nworld, foot_pairs, torso_id, graph_mode="warp_staged", capacity_factor=2.0,
|
|
162
|
+
nconmax=None, njmax=None, *, warm_recompute=False):
|
|
163
|
+
"""nconmax / njmax: contacts and constraint rows per world (default: capacity_factor x MJX's padding);
|
|
164
|
+
warm_recompute: the forward keeps each substep's converged acceleration as a residual and the backward's
|
|
165
|
+
recompute starts its solver there with one iteration (_Sim.backward_ws)."""
|
|
166
|
+
import jax
|
|
167
|
+
import jax.numpy as jnp
|
|
168
|
+
|
|
169
|
+
ncon, nefc = mjx_capacity(mjm)
|
|
170
|
+
self.nconmax, self.njmax = int(np.ceil(capacity_factor * ncon)), int(np.ceil(capacity_factor * nefc))
|
|
171
|
+
self.nconmax, self.njmax = int(nconmax or self.nconmax), int(njmax or self.njmax)
|
|
172
|
+
wp.config.quiet = True
|
|
173
|
+
mjw.enable_grad()
|
|
174
|
+
dev = wp.get_device("cuda:0")
|
|
175
|
+
with wp.ScopedDevice(dev):
|
|
176
|
+
self.sim = _Sim(mjm, nworld, foot_pairs, torso_id, self.nconmax, self.njmax)
|
|
177
|
+
self._warmup(warm_recompute)
|
|
178
|
+
wp.synchronize_device(dev)
|
|
179
|
+
sim = self.sim
|
|
180
|
+
|
|
181
|
+
def fwd_cb(qpos: wp.array2d(dtype=float), qvel: wp.array2d(dtype=float), warm: wp.array2d(dtype=float),
|
|
182
|
+
ctrl: wp.array2d(dtype=float), friction: wp.array(dtype=float), com: wp.array2d(dtype=float),
|
|
183
|
+
qpos_out: wp.array2d(dtype=float), qvel_out: wp.array2d(dtype=float),
|
|
184
|
+
warm_out: wp.array2d(dtype=float)):
|
|
185
|
+
sim.forward(qpos, qvel, warm, ctrl, friction, com, qpos_out, qvel_out, warm_out)
|
|
186
|
+
|
|
187
|
+
def bwd_cb(qpos: wp.array2d(dtype=float), qvel: wp.array2d(dtype=float), warm: wp.array2d(dtype=float),
|
|
188
|
+
ctrl: wp.array2d(dtype=float), friction: wp.array(dtype=float), com: wp.array2d(dtype=float),
|
|
189
|
+
g_qpos: wp.array2d(dtype=float), g_qvel: wp.array2d(dtype=float),
|
|
190
|
+
gq_out: wp.array2d(dtype=float), gv_out: wp.array2d(dtype=float),
|
|
191
|
+
gu_out: wp.array2d(dtype=float)):
|
|
192
|
+
sim.backward(qpos, qvel, warm, ctrl, friction, com, g_qpos, g_qvel, gq_out, gv_out, gu_out)
|
|
193
|
+
|
|
194
|
+
gm = {"none": wp.JaxCallableGraphMode.NONE, "warp": wp.JaxCallableGraphMode.WARP,
|
|
195
|
+
"warp_staged": wp.JaxCallableGraphMode.WARP_STAGED}[graph_mode]
|
|
196
|
+
fwd_call = wp.jax_callable(fwd_cb, num_outputs=3, graph_mode=gm)
|
|
197
|
+
bwd_call = wp.jax_callable(bwd_cb, num_outputs=3, graph_mode=gm)
|
|
198
|
+
|
|
199
|
+
def fwd_res_cb(qpos: wp.array2d(dtype=float), qvel: wp.array2d(dtype=float), warm: wp.array2d(dtype=float),
|
|
200
|
+
ctrl: wp.array2d(dtype=float), friction: wp.array(dtype=float), com: wp.array2d(dtype=float),
|
|
201
|
+
qpos_out: wp.array2d(dtype=float), qvel_out: wp.array2d(dtype=float),
|
|
202
|
+
warm_out: wp.array2d(dtype=float), warm_seq: wp.array2d(dtype=float)):
|
|
203
|
+
sim.forward_res(qpos, qvel, warm, ctrl, friction, com, qpos_out, qvel_out, warm_out, warm_seq)
|
|
204
|
+
|
|
205
|
+
def bwd_ws_cb(qpos: wp.array2d(dtype=float), qvel: wp.array2d(dtype=float), warm: wp.array2d(dtype=float),
|
|
206
|
+
ctrl: wp.array2d(dtype=float), friction: wp.array(dtype=float), com: wp.array2d(dtype=float),
|
|
207
|
+
warm_seq: wp.array2d(dtype=float), g_qpos: wp.array2d(dtype=float),
|
|
208
|
+
g_qvel: wp.array2d(dtype=float), gq_out: wp.array2d(dtype=float),
|
|
209
|
+
gv_out: wp.array2d(dtype=float), gu_out: wp.array2d(dtype=float)):
|
|
210
|
+
sim.backward_ws(qpos, qvel, warm, ctrl, friction, com, warm_seq, g_qpos, g_qvel, gq_out, gv_out, gu_out)
|
|
211
|
+
|
|
212
|
+
fwd_res_call = wp.jax_callable(fwd_res_cb, num_outputs=4, graph_mode=gm)
|
|
213
|
+
bwd_ws_call = wp.jax_callable(bwd_ws_cb, num_outputs=3, graph_mode=gm)
|
|
214
|
+
nseq = SUBSTEPS * int(mjm.nv)
|
|
215
|
+
|
|
216
|
+
@jax.custom_vjp
|
|
217
|
+
def step(qpos, qvel, warm, ctrl, friction, com):
|
|
218
|
+
dims = {"qpos_out": qpos.shape, "qvel_out": qvel.shape, "warm_out": warm.shape}
|
|
219
|
+
return tuple(fwd_call(qpos, qvel, warm, ctrl, friction, com, output_dims=dims))
|
|
220
|
+
|
|
221
|
+
def step_fwd(qpos, qvel, warm, ctrl, friction, com):
|
|
222
|
+
if warm_recompute:
|
|
223
|
+
dims = {"qpos_out": qpos.shape, "qvel_out": qvel.shape, "warm_out": warm.shape,
|
|
224
|
+
"warm_seq": (qpos.shape[0], nseq)}
|
|
225
|
+
qo, vo, wo, ws = fwd_res_call(qpos, qvel, warm, ctrl, friction, com, output_dims=dims)
|
|
226
|
+
return (qo, vo, wo), (qpos, qvel, warm, ctrl, friction, com, ws)
|
|
227
|
+
return step(qpos, qvel, warm, ctrl, friction, com), (qpos, qvel, warm, ctrl, friction, com, None)
|
|
228
|
+
|
|
229
|
+
def step_bwd(res, cot):
|
|
230
|
+
qpos, qvel, warm, ctrl, friction, com, ws = res
|
|
231
|
+
gq, gv, _ = cot
|
|
232
|
+
dims = {"gq_out": qpos.shape, "gv_out": qvel.shape, "gu_out": ctrl.shape}
|
|
233
|
+
if warm_recompute:
|
|
234
|
+
dq, dv, du = bwd_ws_call(qpos, qvel, warm, ctrl, friction, com, ws, gq, gv, output_dims=dims)
|
|
235
|
+
else:
|
|
236
|
+
dq, dv, du = bwd_call(qpos, qvel, warm, ctrl, friction, com, gq, gv, output_dims=dims)
|
|
237
|
+
return dq, dv, jnp.zeros_like(warm), du, jnp.zeros_like(friction), jnp.zeros_like(com)
|
|
238
|
+
|
|
239
|
+
step.defvjp(step_fwd, step_bwd)
|
|
240
|
+
self.step = step
|
|
241
|
+
|
|
242
|
+
def _warmup(self, warm_recompute=False):
|
|
243
|
+
s, nw = self.sim, self.sim.nworld
|
|
244
|
+
z = lambda a: wp.zeros_like(a, requires_grad=False)
|
|
245
|
+
fr = wp.full(nw, 0.75, dtype=float)
|
|
246
|
+
com = wp.zeros((nw, 3), dtype=float)
|
|
247
|
+
s.forward(s.df.qpos, s.df.qvel, s.df.qacc_warmstart, s.df.ctrl, fr, com,
|
|
248
|
+
z(s.df.qpos), z(s.df.qvel), z(s.df.qvel))
|
|
249
|
+
s.backward(s.df.qpos, s.df.qvel, s.df.qacc_warmstart, s.df.ctrl, fr, com,
|
|
250
|
+
z(s.df.qpos), z(s.df.qvel), z(s.df.qpos), z(s.df.qvel), z(s.df.ctrl))
|
|
251
|
+
if warm_recompute:
|
|
252
|
+
ws = wp.zeros((nw, SUBSTEPS * s.nv), dtype=float)
|
|
253
|
+
s.forward_res(s.df.qpos, s.df.qvel, s.df.qacc_warmstart, s.df.ctrl, fr, com,
|
|
254
|
+
z(s.df.qpos), z(s.df.qvel), z(s.df.qvel), ws)
|
|
255
|
+
s.backward_ws(s.df.qpos, s.df.qvel, s.df.qacc_warmstart, s.df.ctrl, fr, com, ws,
|
|
256
|
+
z(s.df.qpos), z(s.df.qvel), z(s.df.qpos), z(s.df.qvel), z(s.df.ctrl))
|
|
257
|
+
|
|
258
|
+
def stats(self):
|
|
259
|
+
"""High-water marks of the last forward call (host sync; call outside jit)."""
|
|
260
|
+
d = self.sim.df
|
|
261
|
+
return {"nacon": int(d.nacon.numpy()[0]), "naconmax": self.nconmax * self.sim.nworld,
|
|
262
|
+
"nefc_max": int(d.nefc.numpy().max()), "njmax": self.njmax}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
"""Perception sensors on the engine's scenes (docs/sensors.md): batched GPU ray casting
|
|
2
|
+
against the terrain (boxes, heightfields, an optional floor plane), per-world objects (boxes) and the robot's visual
|
|
3
|
+
meshes (posed per world from the body poses), with the hit geometry's id; the depth camera (the G1's RealSense D435i),
|
|
4
|
+
LiDAR (Livox MID-360) and height-scan patterns on top of it. Independent of the physics kernels (its own library)."""
|
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
"""Sensor definitions: mount (body, position, roll-pitch-yaw in the body frame, as a URDF fixed joint), ray pattern in
|
|
2
|
+
the sensor frame (x forward, y left, z up: the URDF link convention), range limits. The G1's sensors from Unitree's
|
|
3
|
+
URDF (unitree_ros robots/g1_description/g1_29dof_rev_1_0.urdf: d435_joint, mid360_joint on torso_link; the mjlab G1's
|
|
4
|
+
torso_link frame is the URDF's, checked 2026-11-04). Field of view and ranges after the vendors' datasheets (to be
|
|
5
|
+
checked against them: the requirements note marks them as from memory)."""
|
|
6
|
+
from dataclasses import dataclass, replace
|
|
7
|
+
|
|
8
|
+
import numpy as np
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def rpy_matrix(r, p, y):
|
|
12
|
+
"""URDF rpy: R = Rz(y) Ry(p) Rx(r)."""
|
|
13
|
+
cr, sr, cp, sp, cy, sy = np.cos(r), np.sin(r), np.cos(p), np.sin(p), np.cos(y), np.sin(y)
|
|
14
|
+
return np.array([[cy * cp, cy * sp * sr - sy * cr, cy * sp * cr + sy * sr],
|
|
15
|
+
[sy * cp, sy * sp * sr + cy * cr, sy * sp * cr - cy * sr],
|
|
16
|
+
[-sp, cp * sr, cp * cr]])
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def quat_matrix(q):
|
|
20
|
+
w, x, y, z = q
|
|
21
|
+
return np.array([[1 - 2 * (y * y + z * z), 2 * (x * y - z * w), 2 * (x * z + y * w)],
|
|
22
|
+
[2 * (x * y + z * w), 1 - 2 * (x * x + z * z), 2 * (y * z - x * w)],
|
|
23
|
+
[2 * (x * z - y * w), 2 * (y * z + x * w), 1 - 2 * (x * x + y * y)]])
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
INTRINSICS_NOISE_MAX = 0.2
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def pixel_dirs(xp, width, height, fx, fy, cx, cy):
|
|
30
|
+
"""Unit ray directions (..., H W, 3) in the sensor frame (x forward, y left, z up) of a pinhole camera with focal
|
|
31
|
+
lengths fx, fy and principal point cx, cy in pixels (u right from the image's left edge, v down from its top edge;
|
|
32
|
+
pixel centres at + 0.5); fx ... cy scalars or arrays (...) broadcast together (xp: numpy or jax.numpy)."""
|
|
33
|
+
u, v = np.meshgrid(np.arange(width) + 0.5, np.arange(height) + 0.5, indexing="xy")
|
|
34
|
+
u, v = xp.asarray(u.reshape(-1)), xp.asarray(v.reshape(-1))
|
|
35
|
+
fx, fy, cx, cy = (xp.asarray(a)[..., None] for a in (fx, fy, cx, cy))
|
|
36
|
+
y, z = -(u - cx) / fx, -(v - cy) / fy
|
|
37
|
+
d = xp.stack([xp.ones_like(y + z), y + 0 * z, z + 0 * y], -1)
|
|
38
|
+
return d / xp.linalg.norm(d, axis=-1, keepdims=True)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
@dataclass(frozen=True)
|
|
42
|
+
class DepthCamera:
|
|
43
|
+
"""A pinhole depth camera: width x height pixels over hfov x vfov degrees (pixel centres), z-depth along the optical
|
|
44
|
+
axis (sensor x), valid in [min_depth, max_depth]. fx, fy, cx, cy (pixels, optional): the intrinsics in place of
|
|
45
|
+
hfov / vfov and the centred principal point (each one given replaces its default: fx = width / 2 / tan(hfov / 2),
|
|
46
|
+
cx = width / 2, ...; all None, the default, is the field-of-view camera, bitwise as before). quat (optional, w x y
|
|
47
|
+
z): the mount's rotation in the body frame in place of rpy (e.g. from_mjcf's)."""
|
|
48
|
+
body: str
|
|
49
|
+
pos: tuple
|
|
50
|
+
rpy: tuple
|
|
51
|
+
width: int = 87
|
|
52
|
+
height: int = 58
|
|
53
|
+
hfov: float = 87.0
|
|
54
|
+
vfov: float = 58.0
|
|
55
|
+
min_depth: float = 0.28
|
|
56
|
+
max_depth: float = 10.0
|
|
57
|
+
fx: float = None
|
|
58
|
+
fy: float = None
|
|
59
|
+
cx: float = None
|
|
60
|
+
cy: float = None
|
|
61
|
+
quat: tuple = None
|
|
62
|
+
|
|
63
|
+
def with_resolution(self, width, height):
|
|
64
|
+
"""The camera at width x height pixels (explicit fx, fy, cx, cy are scaled with the image)."""
|
|
65
|
+
sx, sy = int(width) / self.width, int(height) / self.height
|
|
66
|
+
sc = lambda a, s: None if a is None else float(a) * s
|
|
67
|
+
return replace(self, width=int(width), height=int(height), fx=sc(self.fx, sx), fy=sc(self.fy, sy),
|
|
68
|
+
cx=sc(self.cx, sx), cy=sc(self.cy, sy))
|
|
69
|
+
|
|
70
|
+
def with_intrinsics(self, fx=None, fy=None, cx=None, cy=None):
|
|
71
|
+
"""The camera with these intrinsics in pixels (None: keep the current value or default)."""
|
|
72
|
+
return replace(self, fx=self.fx if fx is None else float(fx), fy=self.fy if fy is None else float(fy),
|
|
73
|
+
cx=self.cx if cx is None else float(cx), cy=self.cy if cy is None else float(cy))
|
|
74
|
+
|
|
75
|
+
def intrinsics(self):
|
|
76
|
+
"""(fx, fy, cx, cy) in pixels (Python floats)."""
|
|
77
|
+
fx = self.width / 2 / np.tan(np.radians(self.hfov) / 2) if self.fx is None else self.fx
|
|
78
|
+
fy = self.height / 2 / np.tan(np.radians(self.vfov) / 2) if self.fy is None else self.fy
|
|
79
|
+
return (float(fx), float(fy), float(self.width / 2 if self.cx is None else self.cx),
|
|
80
|
+
float(self.height / 2 if self.cy is None else self.cy))
|
|
81
|
+
|
|
82
|
+
def explicit(self):
|
|
83
|
+
"""True where any of fx, fy, cx, cy is given (rays() then uses pixel_dirs)."""
|
|
84
|
+
return any(a is not None for a in (self.fx, self.fy, self.cx, self.cy))
|
|
85
|
+
|
|
86
|
+
def noisy_intrinsics(self, noise):
|
|
87
|
+
"""(fx, fy, cx, cy) per world (B,) each from noise (B, 4) = (relative change of fx, of fy, shift of cx as a
|
|
88
|
+
fraction of the width, of cy as a fraction of the height), each clipped to +-INTRINSICS_NOISE_MAX (JAX)."""
|
|
89
|
+
import jax.numpy as jnp
|
|
90
|
+
fx, fy, cx, cy = self.intrinsics()
|
|
91
|
+
e = jnp.clip(jnp.asarray(noise, jnp.float32), -INTRINSICS_NOISE_MAX, INTRINSICS_NOISE_MAX)
|
|
92
|
+
return fx * (1 + e[:, 0]), fy * (1 + e[:, 1]), cx + e[:, 2] * self.width, cy + e[:, 3] * self.height
|
|
93
|
+
|
|
94
|
+
def noisy_cone_cos(self):
|
|
95
|
+
"""The smallest optical-axis cosine of any ray of the camera under any intrinsics noise within
|
|
96
|
+
INTRINSICS_NOISE_MAX (the corners of the image at the extremes)."""
|
|
97
|
+
fx, fy, cx, cy = self.intrinsics()
|
|
98
|
+
a = INTRINSICS_NOISE_MAX
|
|
99
|
+
worst = 1.0
|
|
100
|
+
for sx in (1 - a, 1 + a):
|
|
101
|
+
for sy in (1 - a, 1 + a):
|
|
102
|
+
for dcx in (-a, a):
|
|
103
|
+
for dcy in (-a, a):
|
|
104
|
+
d = pixel_dirs(np, self.width, self.height, fx * sx, fy * sy, cx + dcx * self.width,
|
|
105
|
+
cy + dcy * self.height)
|
|
106
|
+
worst = min(worst, float(d[:, 0].min()))
|
|
107
|
+
return worst
|
|
108
|
+
|
|
109
|
+
def rays(self):
|
|
110
|
+
"""(offsets (N, 3) zeros, unit directions (N, 3)) in the sensor frame, row-major pixels (v down, u right)."""
|
|
111
|
+
if self.explicit():
|
|
112
|
+
d = pixel_dirs(np, self.width, self.height, *(np.float64(x) for x in self.intrinsics()))
|
|
113
|
+
return np.zeros_like(d, dtype=np.float32), d.astype(np.float32)
|
|
114
|
+
fx = self.width / 2 / np.tan(np.radians(self.hfov) / 2)
|
|
115
|
+
fy = self.height / 2 / np.tan(np.radians(self.vfov) / 2)
|
|
116
|
+
u, v = np.meshgrid(np.arange(self.width) + 0.5, np.arange(self.height) + 0.5, indexing="xy")
|
|
117
|
+
d = np.stack([np.ones_like(u), -(u - self.width / 2) / fx, -(v - self.height / 2) / fy], -1).reshape(-1, 3)
|
|
118
|
+
d /= np.linalg.norm(d, axis=1, keepdims=True)
|
|
119
|
+
return np.zeros_like(d, dtype=np.float32), d.astype(np.float32)
|
|
120
|
+
|
|
121
|
+
def mount(self):
|
|
122
|
+
q = getattr(self, "quat", None)
|
|
123
|
+
return np.asarray(self.pos, np.float64), (rpy_matrix(*self.rpy) if q is None
|
|
124
|
+
else quat_matrix(np.asarray(q, np.float64) / np.linalg.norm(q)))
|
|
125
|
+
|
|
126
|
+
def pose(self, body_pos, body_quat):
|
|
127
|
+
"""World position and rotation (3, 3) of the sensor for a body pose (NumPy, one world)."""
|
|
128
|
+
mp, mR = self.mount()
|
|
129
|
+
R = quat_matrix(body_quat)
|
|
130
|
+
return body_pos + R @ mp, R @ mR
|
|
131
|
+
|
|
132
|
+
def depth(self, dist, dirs):
|
|
133
|
+
"""z-depth (..., N) from ray distances along unit dirs (sensor frame): dist * dir_x; invalid (miss, nearer than
|
|
134
|
+
min_depth, beyond max_depth) as 0, the RealSense convention."""
|
|
135
|
+
z = dist * dirs[..., 0]
|
|
136
|
+
ok = (dist >= 0) & (z >= self.min_depth) & (z <= self.max_depth)
|
|
137
|
+
return z * ok
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
MJ_CAM_TO_SENSOR = np.array([[0.0, -1.0, 0.0], [0.0, 0.0, 1.0], [-1.0, 0.0, 0.0]])
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
def from_mjcf(model, camera, width, height, min_depth=0.0, max_depth=20.0):
|
|
144
|
+
"""A DepthCamera from an MJCF <camera> of model (a name or id; a fixed camera: mode "fixed"): its body, position
|
|
145
|
+
and orientation (MuJoCo's convention: looking along the camera's -z, y up, x right), at width x height pixels.
|
|
146
|
+
Intrinsics as MuJoCo's renderer: fovy is the vertical field of view, square pixels (fx = fy = height / 2 /
|
|
147
|
+
tan(fovy / 2)), the principal point at the image centre; a camera with sensorsize / focal / principal: fx =
|
|
148
|
+
focal_x / sensorsize_x width, fy = focal_y / sensorsize_y height, cx = width / 2 - principal_x / sensorsize_x
|
|
149
|
+
width, cy = height / 2 - principal_y / sensorsize_y height (pixels from the image's left / top edge; both forms
|
|
150
|
+
checked against MuJoCo's renderer's segmentation)."""
|
|
151
|
+
import mujoco
|
|
152
|
+
c = int(camera) if not isinstance(camera, str) else mujoco.mj_name2id(model, mujoco.mjtObj.mjOBJ_CAMERA, camera)
|
|
153
|
+
if not 0 <= c < model.ncam:
|
|
154
|
+
raise ValueError(f"from_mjcf: no camera {camera!r}")
|
|
155
|
+
if int(model.cam_mode[c]) != int(mujoco.mjtCamLight.mjCAMLIGHT_FIXED):
|
|
156
|
+
raise ValueError(f"from_mjcf: camera {camera!r} is not mode='fixed' (tracking cameras are not supported)")
|
|
157
|
+
b = int(model.cam_bodyid[c])
|
|
158
|
+
if b == 0:
|
|
159
|
+
raise ValueError(f"from_mjcf: camera {camera!r} is on the world body (a body-mounted camera is needed)")
|
|
160
|
+
R = quat_matrix(np.asarray(model.cam_quat[c], np.float64)) @ MJ_CAM_TO_SENSOR
|
|
161
|
+
q = np.zeros(4)
|
|
162
|
+
mujoco.mju_mat2Quat(q, R.ravel())
|
|
163
|
+
ss = np.asarray(model.cam_sensorsize[c], np.float64)
|
|
164
|
+
if ss[0] > 0 and ss[1] > 0:
|
|
165
|
+
f = np.asarray(model.cam_intrinsic[c], np.float64)
|
|
166
|
+
fx, fy = f[0] / ss[0] * width, f[1] / ss[1] * height
|
|
167
|
+
cx, cy = width / 2 - f[2] / ss[0] * width, height / 2 - f[3] / ss[1] * height
|
|
168
|
+
else:
|
|
169
|
+
fy = height / 2 / np.tan(np.radians(float(model.cam_fovy[c])) / 2)
|
|
170
|
+
fx, cx, cy = fy, width / 2, height / 2
|
|
171
|
+
hfov = float(np.degrees(2 * np.arctan(width / 2 / fx)))
|
|
172
|
+
vfov = float(np.degrees(2 * np.arctan(height / 2 / fy)))
|
|
173
|
+
return DepthCamera(body=model.body(b).name, pos=tuple(float(x) for x in model.cam_pos[c]), rpy=(0.0, 0.0, 0.0),
|
|
174
|
+
width=int(width), height=int(height), hfov=hfov, vfov=vfov, min_depth=float(min_depth),
|
|
175
|
+
max_depth=float(max_depth), fx=float(fx), fy=float(fy), cx=float(cx), cy=float(cy),
|
|
176
|
+
quat=tuple(float(x) for x in q))
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
D435_STREAMS = {
|
|
180
|
+
"1280x720": (87.0, 58.0, 0.280), "848x480": (87.0, 58.0, 0.195), "640x480": (75.0, 62.0, 0.175),
|
|
181
|
+
"640x360": (87.0, 58.0, 0.150), "480x270": (87.0, 58.0, 0.120), "424x240": (87.0, 58.0, 0.105),
|
|
182
|
+
}
|
|
183
|
+
D435I_DEPTH = DepthCamera(body="torso_link", pos=(0.0576235, 0.01753, 0.42987), rpy=(0.0, 0.8307767239493009, 0.0))
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
D435I_COLOR = DepthCamera(body="torso_link", pos=tuple(np.asarray(D435I_DEPTH.pos) + rpy_matrix(*D435I_DEPTH.rpy)
|
|
187
|
+
@ np.array([0.0, 0.015, 0.0])),
|
|
188
|
+
rpy=D435I_DEPTH.rpy, width=64, height=36, hfov=69.0, vfov=42.0, min_depth=0.0, max_depth=20.0)
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
def d435i_depth(stream="1280x720", width=None, height=None):
|
|
192
|
+
"""D435I_DEPTH with the field of view and minimum depth of the real camera's depth stream (D435_STREAMS; the
|
|
193
|
+
minimum depth shrinks with the stream's resolution), rendered at width x height rays (default: the default
|
|
194
|
+
DepthCamera's)."""
|
|
195
|
+
if stream not in D435_STREAMS:
|
|
196
|
+
raise ValueError(f"d435i_depth: stream {stream!r}: one of {sorted(D435_STREAMS)}")
|
|
197
|
+
hfov, vfov, zmin = D435_STREAMS[stream]
|
|
198
|
+
cam = replace(D435I_DEPTH, hfov=hfov, vfov=vfov, min_depth=zmin)
|
|
199
|
+
return cam.with_resolution(width, height) if width is not None else cam
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
# Livox's recorded MID-360 scan (Livox-SDK/livox_laser_simulation scan_mode/mid360.csv, MIT license: data/
|
|
203
|
+
# LIVOX_MID360_LICENSE): 800000 consecutive firing directions (azimuth, zenith in degrees, sensor frame) at the
|
|
204
|
+
# sensor's 200000 points per second, i.e. 4 s of its non-repetitive pattern
|
|
205
|
+
MID360_PATTERN = "livox_mid360.npz"
|
|
206
|
+
MID360_POINT_RATE = 200000
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
def livox_pattern(name=MID360_PATTERN):
|
|
210
|
+
"""(azimuth, zenith) in radians of a packaged Livox scan pattern."""
|
|
211
|
+
from pathlib import Path
|
|
212
|
+
z = np.load(Path(__file__).parent / "data" / name)
|
|
213
|
+
return np.radians(z["azimuth_deg"].astype(np.float64)), np.radians(z["zenith_deg"].astype(np.float64))
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
@dataclass(frozen=True)
|
|
217
|
+
class Lidar:
|
|
218
|
+
"""A LiDAR's rays per frame (rate_hz frames per second), returns within [min_range, max_range]. pattern "mid360":
|
|
219
|
+
Livox's recorded MID-360 scan (livox_pattern): frame f is the f-th 1 / rate_hz of firing directions (20000 at 10 Hz,
|
|
220
|
+
covering azimuth 0 .. 360 deg and elevation -7 .. 52 deg, different every frame; frames() gives the 4 s / (1 /
|
|
221
|
+
rate_hz) = 40 frames, each thinned evenly to n rays); pattern "spiral": one fixed frame, n rays spread evenly over
|
|
222
|
+
azimuth and elevation vmin .. vmax deg (a golden-angle spiral, uniform in solid angle)."""
|
|
223
|
+
body: str
|
|
224
|
+
pos: tuple
|
|
225
|
+
rpy: tuple
|
|
226
|
+
n: int = 4096
|
|
227
|
+
vmin: float = -7.0
|
|
228
|
+
vmax: float = 52.0
|
|
229
|
+
min_range: float = 0.1
|
|
230
|
+
max_range: float = 40.0
|
|
231
|
+
rate_hz: float = 10.0
|
|
232
|
+
pattern: str = "spiral"
|
|
233
|
+
|
|
234
|
+
def with_rays(self, n):
|
|
235
|
+
return replace(self, n=int(n))
|
|
236
|
+
|
|
237
|
+
def frames(self):
|
|
238
|
+
"""Unit ray directions (F, n, 3) in the sensor frame, one row per frame (F = 1 for "spiral")."""
|
|
239
|
+
if self.pattern == "spiral":
|
|
240
|
+
k = np.arange(self.n) + 0.5
|
|
241
|
+
s0, s1 = np.sin(np.radians(self.vmin)), np.sin(np.radians(self.vmax))
|
|
242
|
+
el = np.arcsin(s0 + (s1 - s0) * k / self.n)
|
|
243
|
+
az = (k * np.pi * (3 - np.sqrt(5))) % (2 * np.pi)
|
|
244
|
+
return np.stack([np.cos(el) * np.cos(az), np.cos(el) * np.sin(az), np.sin(el)], -1)[None].astype(np.float32)
|
|
245
|
+
if self.pattern != "mid360":
|
|
246
|
+
raise ValueError(f"Lidar pattern {self.pattern!r}: 'mid360' or 'spiral'")
|
|
247
|
+
az, ze = livox_pattern()
|
|
248
|
+
per = int(round(MID360_POINT_RATE / self.rate_hz))
|
|
249
|
+
if not 0 < self.n <= per:
|
|
250
|
+
raise ValueError(f"Lidar: {self.n} rays per frame, the MID-360 fires {per} per frame at {self.rate_hz} Hz")
|
|
251
|
+
F = len(az) // per
|
|
252
|
+
idx = (np.arange(F)[:, None] * per + (np.arange(self.n) * per) // self.n)
|
|
253
|
+
a, z = az[idx], ze[idx]
|
|
254
|
+
return np.stack([np.sin(z) * np.cos(a), np.sin(z) * np.sin(a), np.cos(z)], -1).astype(np.float32)
|
|
255
|
+
|
|
256
|
+
def rays(self):
|
|
257
|
+
"""(offsets (n, 3) zeros, unit directions (n, 3)) of frame 0."""
|
|
258
|
+
d = self.frames()[0]
|
|
259
|
+
return np.zeros_like(d), d
|
|
260
|
+
|
|
261
|
+
mount = DepthCamera.mount
|
|
262
|
+
pose = DepthCamera.pose
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
@dataclass(frozen=True)
|
|
266
|
+
class HeightScan:
|
|
267
|
+
"""mjlab's RayCastSensor height scan: a grid of size (x, y) metres at resolution, rays straight down from offset_z
|
|
268
|
+
above the body frame, yaw-aligned (mjlab GridPatternCfg, ray_alignment "yaw"); the output is the body height above
|
|
269
|
+
each hit (pos_z - hit_z), as mjlab's height_scan observation."""
|
|
270
|
+
body: str = "pelvis"
|
|
271
|
+
size: tuple = (1.6, 1.0)
|
|
272
|
+
resolution: float = 0.1
|
|
273
|
+
offset_z: float = 0.5
|
|
274
|
+
max_distance: float = 5.0
|
|
275
|
+
|
|
276
|
+
def rays(self):
|
|
277
|
+
x = np.arange(-self.size[0] / 2, self.size[0] / 2 + self.resolution * 0.5, self.resolution)
|
|
278
|
+
y = np.arange(-self.size[1] / 2, self.size[1] / 2 + self.resolution * 0.5, self.resolution)
|
|
279
|
+
gx, gy = np.meshgrid(x, y, indexing="xy")
|
|
280
|
+
off = np.stack([gx.ravel(), gy.ravel(), np.full(gx.size, self.offset_z)], -1).astype(np.float32)
|
|
281
|
+
return off, np.tile(np.float32([0, 0, -1]), (len(off), 1))
|
|
282
|
+
|
|
283
|
+
|
|
284
|
+
MID360 = Lidar(body="torso_link", pos=(0.0002835, 0.00003, 0.428434), rpy=(np.pi, 0.05112069379091391, 0.0),
|
|
285
|
+
pattern="mid360")
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021 livox
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
Binary file
|