|
| 1 | +import numpy as np |
| 2 | +import torch |
| 3 | +from utils import assert_allclose, assert_array_equal |
| 4 | + |
| 5 | +import genesis as gs |
| 6 | +import genesis.utils.geom as gu |
| 7 | + |
| 8 | + |
| 9 | +def expand_batch_dim(values: tuple[float, ...], n_envs: int) -> tuple[float, ...] | np.ndarray: |
| 10 | + """Helper function to expand expected values for n_envs dimension.""" |
| 11 | + if n_envs == 0: |
| 12 | + return values |
| 13 | + return np.tile(np.array(values), (n_envs,) + (1,) * len(values)) |
| 14 | + |
| 15 | + |
| 16 | +def test_imu_sensor(show_viewer, tol, n_envs): |
| 17 | + """Test if the IMU sensor returns the correct data.""" |
| 18 | + GRAVITY = -10.0 |
| 19 | + DT = 1e-2 |
| 20 | + BIAS = (0.1, 0.2, 0.3) |
| 21 | + DELAY_STEPS = 2 |
| 22 | + |
| 23 | + scene = gs.Scene( |
| 24 | + sim_options=gs.options.SimOptions( |
| 25 | + dt=DT, |
| 26 | + substeps=1, |
| 27 | + gravity=(0.0, 0.0, GRAVITY), |
| 28 | + ), |
| 29 | + profiling_options=gs.options.ProfilingOptions(show_FPS=False), |
| 30 | + show_viewer=show_viewer, |
| 31 | + ) |
| 32 | + |
| 33 | + scene.add_entity(gs.morphs.Plane()) |
| 34 | + |
| 35 | + box = scene.add_entity( |
| 36 | + morph=gs.morphs.Box( |
| 37 | + size=(0.1, 0.1, 0.1), |
| 38 | + pos=(0.0, 0.0, 0.2), |
| 39 | + ), |
| 40 | + ) |
| 41 | + |
| 42 | + imu = scene.add_sensor( |
| 43 | + gs.sensors.IMU( |
| 44 | + entity_idx=box.idx, |
| 45 | + ) |
| 46 | + ) |
| 47 | + imu_delayed = scene.add_sensor( |
| 48 | + gs.sensors.IMU( |
| 49 | + entity_idx=box.idx, |
| 50 | + delay=DT * DELAY_STEPS, |
| 51 | + ) |
| 52 | + ) |
| 53 | + imu_noisy = scene.add_sensor( |
| 54 | + gs.sensors.IMU( |
| 55 | + entity_idx=box.idx, |
| 56 | + acc_axes_skew=0.01, |
| 57 | + gyro_axes_skew=(0.02, 0.03, 0.04), |
| 58 | + acc_noise=(0.01, 0.01, 0.01), |
| 59 | + gyro_noise=(0.01, 0.01, 0.01), |
| 60 | + acc_random_walk=(0.001, 0.001, 0.001), |
| 61 | + gyro_random_walk=(0.001, 0.001, 0.001), |
| 62 | + delay=DT, |
| 63 | + jitter=DT * 0.1, |
| 64 | + interpolate=True, |
| 65 | + ) |
| 66 | + ) |
| 67 | + |
| 68 | + scene.build(n_envs=n_envs) |
| 69 | + |
| 70 | + # box is in freefall |
| 71 | + for _ in range(10): |
| 72 | + scene.step() |
| 73 | + |
| 74 | + # IMU should calculate "classical linear acceleration" using the local frame without accounting for gravity |
| 75 | + # acc_classical_lin_z = - theta_dot ** 2 - cos(theta) * g |
| 76 | + assert_allclose(imu.read().lin_acc, 0.0, tol=tol) |
| 77 | + assert_allclose(imu.read().ang_vel, 0.0, tol=tol) |
| 78 | + assert_allclose(imu_noisy.read().lin_acc, 0.0, tol=1e-1) |
| 79 | + assert_allclose(imu_noisy.read().ang_vel, 0.0, tol=1e-1) |
| 80 | + |
| 81 | + # shift COM to induce angular velocity |
| 82 | + com_shift = torch.tensor([[0.05, 0.05, 0.05]]) |
| 83 | + box.set_COM_shift(com_shift.expand((n_envs, 1, 3)) if n_envs > 0 else com_shift) |
| 84 | + |
| 85 | + # update noise and bias for accelerometer and gyroscope |
| 86 | + imu_noisy.set_noise((0.01, 0.01, 0.01, 0.02, 0.02, 0.02)) |
| 87 | + imu_noisy.set_bias((0.01, 0.01, 0.01, 0.02, 0.02, 0.02)) |
| 88 | + imu_noisy.set_jitter(0.001) |
| 89 | + |
| 90 | + for _ in range(10 - DELAY_STEPS): |
| 91 | + scene.step() |
| 92 | + |
| 93 | + true_imu_delayed_reading = imu_delayed.read_ground_truth() |
| 94 | + |
| 95 | + for _ in range(DELAY_STEPS): |
| 96 | + scene.step() |
| 97 | + |
| 98 | + assert_array_equal(imu_delayed.read().lin_acc, true_imu_delayed_reading.lin_acc) |
| 99 | + assert_array_equal(imu_delayed.read().ang_vel, true_imu_delayed_reading.ang_vel) |
| 100 | + |
| 101 | + # check that position offset affects linear acceleration |
| 102 | + imu.set_pos_offset((0.5, 0.0, 0.0)) |
| 103 | + lin_acc_no_offset = imu.read().lin_acc |
| 104 | + scene.step() |
| 105 | + lin_acc_with_offset = imu.read().lin_acc |
| 106 | + assert not np.allclose(lin_acc_no_offset, lin_acc_with_offset, atol=0.2) |
| 107 | + imu.set_pos_offset((0.0, 0.0, 0.0)) |
| 108 | + |
| 109 | + # let box collide with ground |
| 110 | + for _ in range(20): |
| 111 | + scene.step() |
| 112 | + |
| 113 | + assert_array_equal(imu.read_ground_truth().lin_acc, imu_delayed.read_ground_truth().lin_acc) |
| 114 | + assert_array_equal(imu.read_ground_truth().ang_vel, imu_delayed.read_ground_truth().ang_vel) |
| 115 | + |
| 116 | + with np.testing.assert_raises(AssertionError, msg="Angular velocity should not be zero due to COM shift"): |
| 117 | + assert_allclose(imu.read_ground_truth().ang_vel, 0.0, tol=tol) |
| 118 | + |
| 119 | + with np.testing.assert_raises(AssertionError, msg="Delayed data should not be equal to the ground truth data"): |
| 120 | + assert_array_equal(imu_delayed.read().lin_acc - imu_delayed.read_ground_truth().lin_acc, 0.0) |
| 121 | + |
| 122 | + zero_com_shift = torch.tensor([[0.0, 0.0, 0.0]]) |
| 123 | + box.set_COM_shift(zero_com_shift.expand((n_envs, 1, 3)) if n_envs > 0 else zero_com_shift) |
| 124 | + quat_tensor = torch.tensor([0.0, 0.0, 0.0, 1.0]) |
| 125 | + box.set_quat(quat_tensor.expand((n_envs, 4)) if n_envs > 0 else quat_tensor) |
| 126 | + |
| 127 | + # box is stationary on ground |
| 128 | + for _ in range(80): |
| 129 | + scene.step() |
| 130 | + |
| 131 | + assert_allclose( |
| 132 | + imu.read().lin_acc, |
| 133 | + expand_batch_dim((0.0, 0.0, -GRAVITY), n_envs), |
| 134 | + tol=5e-6, |
| 135 | + ) |
| 136 | + assert_allclose(imu.read().ang_vel, expand_batch_dim((0.0, 0.0, 0.0), n_envs), tol=1e-5) |
| 137 | + |
| 138 | + # rotate IMU 90 deg around x axis means gravity should be along -y axis |
| 139 | + imu.set_quat_offset(gu.euler_to_quat((90.0, 0.0, 0.0))) |
| 140 | + imu.set_acc_axes_skew((0.0, 1.0, 0.0)) |
| 141 | + scene.step() |
| 142 | + assert_allclose(imu.read().lin_acc, GRAVITY, tol=5e-6) |
| 143 | + imu.set_quat_offset((0.0, 0.0, 0.0, 1.0)) |
| 144 | + imu.set_acc_axes_skew((0.0, 0.0, 0.0)) |
| 145 | + |
| 146 | + scene.reset() |
| 147 | + |
| 148 | + assert_allclose(imu.read().lin_acc, 0.0, tol=gs.EPS) # biased, but cache hasn't been updated yet |
| 149 | + assert_allclose(imu_delayed.read().lin_acc, 0.0, tol=gs.EPS) |
| 150 | + assert_allclose(imu_noisy.read().ang_vel, 0.0, tol=gs.EPS) |
| 151 | + |
| 152 | + imu.set_bias(BIAS + (0.0, 0.0, 0.0)) |
| 153 | + scene.step() |
| 154 | + assert_allclose(imu.read().lin_acc, expand_batch_dim(BIAS, n_envs), tol=tol) |
| 155 | + |
| 156 | + |
| 157 | +if __name__ == "__main__": |
| 158 | + gs.init(backend=gs.cpu) |
| 159 | + # test_imu_sensor(show_viewer=False, tol=1e-4, n_envs=0) |
| 160 | + test_imu_sensor(show_viewer=False, tol=1e-4, n_envs=2) |
0 commit comments