Skip to content

Commit b996485

Browse files
committed
Preserve sensor timing compatibility in the release backport
1 parent ed02a3e commit b996485

14 files changed

Lines changed: 852 additions & 130 deletions

File tree

‎source/isaaclab/changelog.d/sensor-update-period-float-drift.changed.rst‎

Lines changed: 0 additions & 6 deletions
This file was deleted.

‎source/isaaclab/changelog.d/sensor-update-period-float-drift.fixed.rst‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,7 @@
44
with ``update_period`` set to a multiple of the physics time step skipped refreshes (for example, the height
55
scanner of the locomotion tasks from 16 s into an episode, and sensors with long update periods at small
66
time steps within the first second). The time since the last refresh is now accumulated in float64.
7+
Existing timestamp buffers and Warp kernel signatures were preserved. Custom sensors that allocate or
8+
replace their own timestamp buffers automatically acquire elapsed-time tracking on first use.
9+
Built-in sensors use the new ``*_elapsed_kernel`` entry points; direct callers of the existing kernels
10+
continue to work with their original timestamp-based timing behavior.

‎source/isaaclab/changelog.d/sensor-update-period-float-drift.major‎

Whitespace-only changes.

‎source/isaaclab/isaaclab/sensors/kernels.py‎

Lines changed: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,76 @@
88

99
@wp.kernel
1010
def update_timestamp_kernel(
11+
is_outdated: wp.array(dtype=wp.bool),
12+
timestamp: wp.array(dtype=wp.float32),
13+
timestamp_last_update: wp.array(dtype=wp.float32),
14+
dt: wp.float32,
15+
update_period: wp.float32,
16+
):
17+
"""Updates timestamp and marks environments as outdated if update period elapsed.
18+
19+
Args:
20+
is_outdated: Boolean array indicating which envs need update.
21+
timestamp: Current timestamp per env.
22+
timestamp_last_update: Last update timestamp per env.
23+
dt: Simulation time step (scalar).
24+
update_period: Period after which sensor should be updated.
25+
"""
26+
env = wp.tid()
27+
new_timestamp = timestamp[env] + dt
28+
timestamp[env] = new_timestamp
29+
if new_timestamp - timestamp_last_update[env] + 1e-6 >= update_period:
30+
is_outdated[env] = True
31+
32+
33+
@wp.kernel
34+
def update_outdated_envs_kernel(
35+
is_outdated: wp.array(dtype=wp.bool),
36+
timestamp: wp.array(dtype=wp.float32),
37+
timestamp_last_update: wp.array(dtype=wp.float32),
38+
):
39+
"""Updates timestamp and clears outdated flag for outdated environments.
40+
41+
Args:
42+
is_outdated: Boolean array indicating which envs need update. Will be set to False.
43+
timestamp: Current timestamp per env.
44+
timestamp_last_update: Last update timestamp per env. Will be set to current timestamp.
45+
"""
46+
env = wp.tid()
47+
if is_outdated[env]:
48+
timestamp_last_update[env] = timestamp[env]
49+
is_outdated[env] = False
50+
51+
52+
@wp.kernel
53+
def reset_envs_kernel(
54+
reset_mask: wp.array(dtype=wp.bool),
55+
is_outdated: wp.array(dtype=wp.bool),
56+
timestamp: wp.array(dtype=wp.float32),
57+
timestamp_last_update: wp.array(dtype=wp.float32),
58+
):
59+
"""Resets the current and last update timestamps and marks environments as outdated for those being reset.
60+
61+
Args:
62+
reset_mask: Boolean array indicating which envs to reset.
63+
is_outdated: Boolean array indicating which envs need update. Will be set to True for reset envs.
64+
timestamp: Current timestamp per env. Will be set to 0.0 for reset envs.
65+
timestamp_last_update: Last update timestamp per env. Will be set to 0.0 for reset envs.
66+
"""
67+
env = wp.tid()
68+
if not reset_mask[env]:
69+
return
70+
71+
# Reset the timestamp for the sensors
72+
timestamp[env] = 0.0
73+
74+
timestamp_last_update[env] = 0.0
75+
# Set all reset sensors to outdated so that they are updated when data is called the next time.
76+
is_outdated[env] = True
77+
78+
79+
@wp.kernel
80+
def update_timestamp_elapsed_kernel(
1181
is_outdated: wp.array(dtype=wp.bool),
1282
timestamp: wp.array(dtype=wp.float32),
1383
elapsed_since_update: wp.array(dtype=wp.float64),
@@ -36,7 +106,7 @@ def update_timestamp_kernel(
36106

37107

38108
@wp.kernel
39-
def update_outdated_envs_kernel(
109+
def update_outdated_envs_elapsed_kernel(
40110
is_outdated: wp.array(dtype=wp.bool),
41111
timestamp: wp.array(dtype=wp.float32),
42112
timestamp_last_update: wp.array(dtype=wp.float32),
@@ -58,7 +128,7 @@ def update_outdated_envs_kernel(
58128

59129

60130
@wp.kernel
61-
def reset_envs_kernel(
131+
def reset_envs_elapsed_kernel(
62132
reset_mask: wp.array(dtype=wp.bool),
63133
is_outdated: wp.array(dtype=wp.bool),
64134
timestamp: wp.array(dtype=wp.float32),
@@ -85,3 +155,14 @@ def reset_envs_kernel(
85155
elapsed_since_update[env] = wp.float64(0.0)
86156
# Set all reset sensors to outdated so that they are updated when data is called the next time.
87157
is_outdated[env] = True
158+
159+
160+
@wp.kernel
161+
def initialize_elapsed_time_kernel(
162+
timestamp: wp.array(dtype=wp.float32),
163+
timestamp_last_update: wp.array(dtype=wp.float32),
164+
elapsed_since_update: wp.array(dtype=wp.float64),
165+
):
166+
"""Initialize elapsed time from existing sensor timestamps when adopting timing buffers."""
167+
env = wp.tid()
168+
elapsed_since_update[env] = wp.float64(timestamp[env]) - wp.float64(timestamp_last_update[env])

‎source/isaaclab/isaaclab/sensors/sensor_base.py‎

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,21 @@
2929
from ..sim.utils.queries import get_first_matching_ancestor_prim
3030
from ..sim.utils.transforms import resolve_prim_pose
3131
from ..utils import index_fill_
32-
from .kernels import reset_envs_kernel, update_outdated_envs_kernel, update_timestamp_kernel
32+
from .kernels import (
33+
initialize_elapsed_time_kernel,
34+
reset_envs_elapsed_kernel,
35+
update_outdated_envs_elapsed_kernel,
36+
update_timestamp_elapsed_kernel,
37+
)
38+
from .kernels import (
39+
reset_envs_kernel as reset_envs_kernel,
40+
)
41+
from .kernels import (
42+
update_outdated_envs_kernel as update_outdated_envs_kernel,
43+
)
44+
from .kernels import (
45+
update_timestamp_kernel as update_timestamp_kernel,
46+
)
3347

3448
if TYPE_CHECKING:
3549
from .sensor_base_cfg import SensorBaseCfg
@@ -48,6 +62,9 @@ class SensorBase(ABC):
4862
sensor is updated at every simulation step.
4963
"""
5064

65+
_elapsed_timestamp: wp.array | None = None
66+
_elapsed_timestamp_last_update: wp.array | None = None
67+
5168
def __init__(self, cfg: SensorBaseCfg):
5269
"""Initialize the sensor class.
5370
@@ -187,7 +204,7 @@ def reset(self, env_ids: Sequence[int] | None = None, env_mask: wp.array | None
187204
"""
188205
env_mask = self._resolve_indices_and_mask(env_ids, env_mask)
189206
wp.launch(
190-
reset_envs_kernel,
207+
reset_envs_elapsed_kernel,
191208
dim=self._num_envs,
192209
inputs=[
193210
env_mask,
@@ -207,7 +224,7 @@ def update(self, dt: float, force_recompute: bool = False):
207224
self._data_dirty = True
208225
# Update the timestamp for the sensors
209226
wp.launch(
210-
update_timestamp_kernel,
227+
update_timestamp_elapsed_kernel,
211228
dim=self._num_envs,
212229
inputs=[
213230
self._is_outdated,
@@ -430,14 +447,33 @@ def _clear_debug_vis_handle(self) -> None:
430447
def _create_timing_buffers(self) -> None:
431448
"""Allocates the per-environment outdated flags, timestamps and elapsed times used to schedule updates.
432449
433-
Sensors that change :attr:`_num_envs` after :meth:`_initialize_impl` must call this again, after
434-
reallocating their environment masks.
450+
Sensors can call this again after changing :attr:`_num_envs` and reallocating their environment masks.
435451
"""
436452
# timestamp and outdated flags
437453
self._is_outdated = wp.ones(self._num_envs, dtype=wp.bool, device=self._device)
438454
self._timestamp = wp.zeros(self._num_envs, dtype=wp.float32, device=self._device)
439455
self._timestamp_last_update = wp.zeros_like(self._timestamp)
440-
self._elapsed_since_update = wp.zeros(self._num_envs, dtype=wp.float64, device=self._device)
456+
self._elapsed_time_buffer = wp.zeros(self._num_envs, dtype=wp.float64, device=self._device)
457+
self._elapsed_timestamp = self._timestamp
458+
self._elapsed_timestamp_last_update = self._timestamp_last_update
459+
460+
@property
461+
def _elapsed_since_update(self) -> wp.array:
462+
"""Elapsed time since refresh, adopting timestamp buffers allocated by custom sensors."""
463+
if (
464+
self._elapsed_timestamp is not self._timestamp
465+
or self._elapsed_timestamp_last_update is not self._timestamp_last_update
466+
):
467+
self._elapsed_time_buffer = wp.empty(self._timestamp.shape, dtype=wp.float64, device=self._timestamp.device)
468+
wp.launch(
469+
initialize_elapsed_time_kernel,
470+
dim=self._num_envs,
471+
inputs=[self._timestamp, self._timestamp_last_update, self._elapsed_time_buffer],
472+
device=self._device,
473+
)
474+
self._elapsed_timestamp = self._timestamp
475+
self._elapsed_timestamp_last_update = self._timestamp_last_update
476+
return self._elapsed_time_buffer
441477

442478
def _update_outdated_buffers(self, force_recompute: bool = False) -> None:
443479
"""Fills the sensor data for the outdated sensors."""
@@ -450,7 +486,7 @@ def _mark_buffers_updated(self) -> None:
450486
"""Commit capture timestamps after the sensor's output buffers have been filled."""
451487
# update timestamps and clear outdated flags
452488
wp.launch(
453-
update_outdated_envs_kernel,
489+
update_outdated_envs_elapsed_kernel,
454490
dim=self._num_envs,
455491
inputs=[self._is_outdated, self._timestamp, self._timestamp_last_update, self._elapsed_since_update],
456492
device=self._device,
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md).
2+
# All rights reserved.
3+
#
4+
# SPDX-License-Identifier: BSD-3-Clause
5+
6+
"""Contact timing and compatibility of the backend-specific Warp kernel entry points."""
7+
8+
import numpy as np
9+
import pytest
10+
import warp as wp
11+
from isaaclab_newton.sensors.contact_sensor import contact_sensor_kernels as newton_kernels
12+
from isaaclab_ov.sensors.contact_sensor import kernels as ov_kernels
13+
from isaaclab_physx.sensors.contact_sensor import kernels as physx_kernels
14+
15+
from isaaclab.test.utils import DeviceScope, test_devices
16+
17+
pytestmark = [pytest.mark.unit, pytest.mark.kitless]
18+
19+
20+
@pytest.mark.parametrize("device", test_devices(DeviceScope.CPU))
21+
@pytest.mark.parametrize("backend", ["physx", "ovphysx", "newton"])
22+
def test_contact_kernel_entry_points_preserve_timing_and_mask(backend, device):
23+
"""Old timestamp callers and new elapsed-time callers update forces and transitions correctly."""
24+
for use_elapsed in (False, True):
25+
timestamp = wp.full(2, 8.25, dtype=wp.float32, device=device)
26+
timing = (
27+
[wp.full(2, 0.02, dtype=wp.float64, device=device)]
28+
if use_elapsed
29+
else [timestamp, wp.full(2, 8.0, dtype=wp.float32, device=device)]
30+
)
31+
mask = wp.array([True, False], dtype=wp.bool, device=device)
32+
normal = wp.array([[(0.0, 0.0, 10.0)], [(0.0, 0.0, 20.0)]], dtype=wp.vec3f, device=device)
33+
friction = wp.zeros((2, 1), dtype=wp.vec3f, device=device)
34+
normal_history = wp.zeros((2, 2, 1), dtype=wp.vec3f, device=device)
35+
total_history = wp.zeros_like(normal_history)
36+
normal_output = wp.zeros_like(normal)
37+
total_output = wp.zeros_like(normal)
38+
air = wp.full((2, 1), 0.5, dtype=wp.float32, device=device)
39+
contact = wp.zeros_like(air)
40+
last_air = wp.full_like(air, -1.0)
41+
last_contact = wp.full_like(air, -1.0)
42+
43+
if backend == "newton":
44+
kernel = (
45+
newton_kernels.update_contact_sensor_elapsed_kernel
46+
if use_elapsed
47+
else newton_kernels.update_contact_sensor_kernel
48+
)
49+
inputs = [
50+
2,
51+
0,
52+
1.0,
53+
mask,
54+
normal,
55+
None,
56+
normal,
57+
None,
58+
friction,
59+
None,
60+
*timing,
61+
total_history,
62+
None,
63+
normal_history,
64+
None,
65+
None,
66+
None,
67+
air,
68+
contact,
69+
last_air,
70+
last_contact,
71+
]
72+
else:
73+
if backend == "physx":
74+
kernel = (
75+
physx_kernels.update_net_forces_elapsed_kernel
76+
if use_elapsed
77+
else physx_kernels.update_net_forces_kernel
78+
)
79+
else:
80+
kernel = (
81+
ov_kernels.update_net_forces_ovphysx_elapsed_kernel
82+
if use_elapsed
83+
else ov_kernels.update_net_forces_ovphysx_kernel
84+
)
85+
inputs = [
86+
normal.flatten(),
87+
friction.flatten(),
88+
None,
89+
None,
90+
mask,
91+
2,
92+
0,
93+
2,
94+
1.0,
95+
*([timestamp, *timing] if use_elapsed else timing),
96+
normal_output,
97+
normal_history,
98+
None,
99+
None,
100+
air,
101+
contact,
102+
last_air,
103+
last_contact,
104+
total_output,
105+
total_history,
106+
None,
107+
None,
108+
None,
109+
None,
110+
None,
111+
None,
112+
]
113+
wp.launch(kernel, dim=(2, 1), inputs=inputs, device=device)
114+
115+
dt = 0.02 if use_elapsed else 0.25
116+
np.testing.assert_allclose(contact.numpy(), [[dt], [0.0]], atol=1e-7, rtol=0)
117+
np.testing.assert_allclose(last_air.numpy(), [[0.5 + dt], [-1.0]], atol=1e-7, rtol=0)
118+
np.testing.assert_array_equal(air.numpy(), [[0.0], [0.5]])
119+
np.testing.assert_array_equal(last_contact.numpy(), [[-1.0], [-1.0]])
120+
expected_history = np.zeros((2, 2, 1, 3), dtype=np.float32)
121+
expected_history[0, 0, 0, 2] = 10.0
122+
np.testing.assert_array_equal(normal_history.numpy(), expected_history)
123+
np.testing.assert_array_equal(total_history.numpy(), expected_history)

0 commit comments

Comments
 (0)