Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limit-aware noise adition #263

Merged
merged 4 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions urdfenvs/sensors/lidar.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,18 @@ def __init__(
+ i * (angle_limits[1] - angle_limits[0]) / self._nb_rays
for i in range(self._nb_rays)
]
self._rel_positions = np.zeros(2 * nb_rays)
self._rel_positions = np.zeros((nb_rays, 2))
self._distances = np.zeros(nb_rays)
self._sphere_ids = [
-1,
] * self._nb_rays

self._observation_limits = np.repeat(
np.array([[-ray_length], [ray_length]]),
nb_rays * 2,
axis=1,
)

def get_observation_size(self):
"""Getter for the dimension of the observation space."""
if self._raw_data:
Expand All @@ -86,6 +92,8 @@ def get_observation_space(self, obstacles: dict, goals: dict):
)
return gym.spaces.Dict({self._name: observation_space})



def sense(self, robot, obstacles: dict, goals: dict, t: float):
"""Sense the distance toward the next object with the Lidar."""
self._call_counter += 1
Expand All @@ -105,22 +113,20 @@ def sense(self, robot, obstacles: dict, goals: dict, t: float):
lidar
* np.array([np.cos(theta + yaw), np.sin(theta + yaw)])
)
noisy_rel_positions = np.random.normal(
true_rel_positions, self._variance
)

self._rel_positions[2 * i : 2 * i + 2] = noisy_rel_positions
self._distances[i] = np.linalg.norm(
self._rel_positions[2 * i : 2 * i + 2]
)
self._rel_positions[i, :] = true_rel_positions
noisy_rel_positions = self.add_noise(self._rel_positions.flatten()).reshape(self._nb_rays, 2)
self._rel_positions = noisy_rel_positions

self._distances = np.linalg.norm(self._rel_positions, axis=1)
if (
self._plotting_interval > 0
and self._call_counter % self._plotting_interval == 0
):
self.update_lidar_spheres(lidar_position)
if self._raw_data:
return self._distances
return self._rel_positions
return self._rel_positions.flatten()

def init_lidar_spheres(self, lidar_position):
"""
Expand Down
12 changes: 12 additions & 0 deletions urdfenvs/sensors/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from typing import List
from abc import abstractmethod

import numpy as np

from urdfenvs.sensors.physics_engine_interface import PhysicsEngineInterface, PybulletInterface, MujocoInterface

class Sensor():
Expand Down Expand Up @@ -38,6 +40,16 @@ def name(self):
def set_data(self, data):
self._physics_engine.set_data(data)

def add_noise(self, exact_data: np.ndarray):
"""Add noise to the exact data."""
noisy_data = np.random.normal(exact_data, self._variance)
if np.all(exact_data >= self._observation_limits[0]) and np.all(exact_data <= self._observation_limits[1]):
clipped = np.clip(noisy_data, self._observation_limits[0], self._observation_limits[1])
return clipped
else:
return noisy_data


@abstractmethod
def get_observation_size(self):
pass
Expand Down
Loading