File size: 6,491 Bytes
406662d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md).
# All rights reserved.
#
# SPDX-License-Identifier: BSD-3-Clause

"""Launch Isaac Sim Simulator first."""

from isaaclab.app import AppLauncher

# launch omniverse app
app_launcher = AppLauncher(headless=True)
simulation_app = app_launcher.app


"""Rest everything follows."""

from collections.abc import Sequence
from dataclasses import dataclass

import pytest
import torch

import isaaclab.sim as sim_utils
from isaaclab.sensors import SensorBase, SensorBaseCfg
from isaaclab.utils import configclass


@dataclass
class DummyData:
    count: torch.Tensor = None


class DummySensor(SensorBase):
    def __init__(self, cfg):
        super().__init__(cfg)
        self._data = DummyData()

    def _initialize_impl(self):
        super()._initialize_impl()
        self._data.count = torch.zeros((self._num_envs), dtype=torch.int, device=self.device)

    @property
    def data(self):
        # update sensors if needed
        self._update_outdated_buffers()
        # return the data (where `_data` is the data for the sensor)
        return self._data

    def _update_buffers_impl(self, env_ids: Sequence[int]):
        self._data.count[env_ids] += 1

    def reset(self, env_ids: Sequence[int] | None = None):
        super().reset(env_ids=env_ids)
        # Resolve sensor ids
        if env_ids is None:
            env_ids = slice(None)
        self._data.count[env_ids] = 0


@configclass
class DummySensorCfg(SensorBaseCfg):
    class_type = DummySensor

    prim_path = "/World/envs/env_.*/Cube/dummy_sensor"


def _populate_scene():
    """"""

    # Ground-plane
    cfg = sim_utils.GroundPlaneCfg()
    cfg.func("/World/defaultGroundPlane", cfg)
    # Lights
    cfg = sim_utils.SphereLightCfg()
    cfg.func("/World/Light/GreySphere", cfg, translation=(4.5, 3.5, 10.0))
    cfg.func("/World/Light/WhiteSphere", cfg, translation=(-4.5, 3.5, 10.0))

    # create prims
    for i in range(5):
        _ = sim_utils.create_prim(
            f"/World/envs/env_{i:02d}/Cube",
            "Cube",
            translation=(i * 1.0, 0.0, 0.0),
            scale=(0.25, 0.25, 0.25),
        )


@pytest.fixture
def create_dummy_sensor(request, device):
    # Create a new stage
    sim_utils.create_new_stage()

    # Simulation time-step
    dt = 0.01
    # Load kit helper
    sim_cfg = sim_utils.SimulationCfg(dt=dt, device=device)
    sim = sim_utils.SimulationContext(sim_cfg)

    # create sensor
    _populate_scene()

    sensor_cfg = DummySensorCfg()

    sim_utils.update_stage()

    yield sensor_cfg, sim, dt

    # stop simulation
    # note: cannot use self.sim.stop() since it does one render step after stopping!! This doesn't make sense :(
    sim._timeline.stop()
    # clear the stage
    sim.clear_all_callbacks()
    sim.clear_instance()


@pytest.mark.parametrize("device", ("cpu", "cuda"))
def test_sensor_init(create_dummy_sensor, device):
    """Test that the sensor initializes, steps without update, and forces update."""

    sensor_cfg, sim, dt = create_dummy_sensor
    sensor = DummySensor(cfg=sensor_cfg)

    # Play sim
    sim.step()

    sim.reset()

    assert sensor.is_initialized
    assert int(sensor.num_instances) == 5

    # test that the data is not updated
    for i in range(10):
        sim.step()
        sensor.update(dt=dt, force_recompute=True)
        expected_value = i + 1
        torch.testing.assert_close(
            sensor.data.count,
            torch.tensor(expected_value, device=device, dtype=torch.int32).repeat(sensor.num_instances),
        )
    assert sensor.data.count.shape[0] == 5

    # test that the data is not updated if sensor.data is not accessed
    for _ in range(5):
        sim.step()
        sensor.update(dt=dt, force_recompute=False)
        torch.testing.assert_close(
            sensor._data.count,
            torch.tensor(expected_value, device=device, dtype=torch.int32).repeat(sensor.num_instances),
        )


@pytest.mark.parametrize("device", ("cpu", "cuda"))
def test_sensor_update_rate(create_dummy_sensor, device):
    """Test that the update_rate configuration parameter works by checking the value of the data is old for an update
    period of 2.
    """
    sensor_cfg, sim, dt = create_dummy_sensor
    sensor_cfg.update_period = 2 * dt
    sensor = DummySensor(cfg=sensor_cfg)

    # Play sim
    sim.step()

    sim.reset()

    assert sensor.is_initialized
    assert int(sensor.num_instances) == 5
    expected_value = 1
    for i in range(10):
        sim.step()
        sensor.update(dt=dt, force_recompute=True)
        # count should he half of the number of steps
        torch.testing.assert_close(
            sensor.data.count,
            torch.tensor(expected_value, device=device, dtype=torch.int32).repeat(sensor.num_instances),
        )
        expected_value += i % 2


@pytest.mark.parametrize("device", ("cpu", "cuda"))
def test_sensor_reset(create_dummy_sensor, device):
    """Test that sensor can be reset for all or partial env ids."""
    sensor_cfg, sim, dt = create_dummy_sensor
    sensor = DummySensor(cfg=sensor_cfg)

    # Play sim
    sim.step()
    sim.reset()

    assert sensor.is_initialized
    assert int(sensor.num_instances) == 5
    for i in range(5):
        sim.step()
        sensor.update(dt=dt)
        # count should he half of the number of steps
        torch.testing.assert_close(
            sensor.data.count,
            torch.tensor(i + 1, device=device, dtype=torch.int32).repeat(sensor.num_instances),
        )

    sensor.reset()

    for j in range(5):
        sim.step()
        sensor.update(dt=dt)
        # count should he half of the number of steps
        torch.testing.assert_close(
            sensor.data.count,
            torch.tensor(j + 1, device=device, dtype=torch.int32).repeat(sensor.num_instances),
        )

    reset_ids = [2, 4]
    cont_ids = [0, 1, 3]
    sensor.reset(env_ids=reset_ids)

    for k in range(5):
        sim.step()
        sensor.update(dt=dt)
        # count should he half of the number of steps
        torch.testing.assert_close(
            sensor.data.count[reset_ids],
            torch.tensor(k + 1, device=device, dtype=torch.int32).repeat(len(reset_ids)),
        )
        torch.testing.assert_close(
            sensor.data.count[cont_ids],
            torch.tensor(k + 6, device=device, dtype=torch.int32).repeat(len(cont_ids)),
        )