maxspahn / gym_envs_urdf

URDF environments for gym
https://maxspahn.github.io/gym_envs_urdf/
GNU General Public License v3.0
43 stars 14 forks source link

Obstacles fail the `check_observation` method #200

Closed behradkhadem closed 1 year ago

behradkhadem commented 1 year ago

For the code below, because of the obstacle that I've added I get an error inside the check_observation method inside UrdfEnv file.

from urdfenvs.robots.generic_urdf import GenericUrdfReacher
from urdfenvs.sensors.full_sensor import FullSensor
from urdfenvs.urdf_common import UrdfEnv
from mpscenes.obstacles.box_obstacle import BoxObstacle

import numpy as np
import os

movable_obstacle_dict = {
'type': 'box',
'geometry': {
    'position' : [0.5, 0.5, 0.0],
    'width': 0.04,
    'height': 0.04,
    'length': 0.1,
},
'movable': True,
'high': {
        'position' : [5.0, 5.0, 1.0],
    'width': 0.35,
    'height': 0.35,
    'length': 0.35,
},
'low': {
    'position' : [0.0, 0.0, 0.5],
    'width': 0.2,
    'height': 0.2,
    'length': 0.2,
}
}
movable_obstacle = BoxObstacle(name="movable_box", content_dict=movable_obstacle_dict)

robots = [
    GenericUrdfReacher(urdf="panda_with_gripper.urdf", mode="vel"),
]

env = UrdfEnv(render=True, robots=robots)

sensor = FullSensor(['position'], ['position', 'size'], variance=0.0)
env.add_sensor(sensor, [0])
env.set_spaces()
env.add_obstacle(movable_obstacle)
ob, *_ = env.reset()
print(f"Initial observation : {ob}")

Error is not clear enough. It just says obstacles but when I clear the check_observation method and replace it with pass things work fine. Is there something that I do wrong or there is an issue with the package?

Traceback (most recent call last):
  File "/home/behradx/projects/RL-Robotic-Arm/max.py", line 46, in <module>
    ob, *_ = env.reset()
  File "/home/behradx/projects/gym_envs_urdf/urdfenvs/urdf_common/urdf_env.py", line 595, in reset
    return self._get_ob(), self._info
  File "/home/behradx/projects/gym_envs_urdf/urdfenvs/urdf_common/urdf_env.py", line 273, in _get_ob
    check_observation(self.observation_space, observation)
  File "/home/behradx/projects/gym_envs_urdf/urdfenvs/urdf_common/urdf_env.py", line 72, in check_observation
    check_observation(obs[key], value)
  File "/home/behradx/projects/gym_envs_urdf/urdfenvs/urdf_common/urdf_env.py", line 72, in check_observation
    check_observation(obs[key], value)
  File "/home/behradx/projects/gym_envs_urdf/urdfenvs/urdf_common/urdf_env.py", line 72, in check_observation
    check_observation(obs[key], value)
  File "/home/behradx/anaconda3/envs/SB3/lib/python3.9/site-packages/gymnasium/spaces/dict.py", line 181, in __getitem__
    return self.spaces[key]
KeyError: 'obstacles'
behradkhadem commented 1 year ago

Found the issue. I should've put env.set_spaces() at the end like this:

sensor = FullSensor(['position'], ['position', 'size'], variance=0.0)
env.add_sensor(sensor, [0])
env.add_obstacle(movable_obstacle)
env.set_spaces()
ob, *_ = env.reset()
print(f"Initial observation : {ob}")