Skip to content

Commit

Permalink
fix test (metadriverse#600)
Browse files Browse the repository at this point in the history
* fix test

* doc string
  • Loading branch information
QuanyiLi authored Jan 19, 2024
1 parent c1431f5 commit e43692c
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 13 deletions.
2 changes: 1 addition & 1 deletion metadrive/component/map/scenario_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,6 @@ def get_boundary_line_vector(self, interval):
map.road_network.show_bounding_box(engine)

pos = map.get_center_point()
engine.main_camera.set_bird_view_pos(pos)
engine.main_camera.set_bird_view_pos_hpr(pos)
while True:
map.engine.step()
38 changes: 28 additions & 10 deletions metadrive/engine/core/main_camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def __init__(self, engine, camera_height: float, camera_dist: float):
self.top_down_camera_height = engine.global_config["top_down_camera_initial_z"]
self.camera_x = engine.global_config["top_down_camera_initial_x"]
self.camera_y = engine.global_config["top_down_camera_initial_y"]
self.camera_rotate = 0
self.camera_hpr = [0, 0, 0]
engine.interface.undisplay()
engine.task_manager.add(self._top_down_task, self.TOP_DOWN_TASK_NAME, extraArgs=[], appendTask=True)

Expand Down Expand Up @@ -158,11 +158,32 @@ def _callback_func(cbdata: DisplayRegionDrawCallbackData):
self.cuda_rendered_result = None

def set_bird_view_pos(self, position):
"""
Set the x,y position for the main camera
Args:
position:
Returns:
"""
self.set_bird_view_pos_hpr(position)

def set_bird_view_pos_hpr(self, position, hpr=None):
"""
Set the x,y position and heading, pitch, roll for the main camera
Args:
position:
hpr:
Returns:
"""
self.set_bird_view_pos_hpr(position)
if self.engine.task_manager.hasTaskNamed(self.TOP_DOWN_TASK_NAME):
# adjust hpr
p_pos = panda_vector(position)
self.camera_x, self.camera_y = p_pos[0], p_pos[1]
self.camera_rotate = 0
self.camera_hpr = hpr or [0, 0, 0]
self.engine.task_manager.add(self._top_down_task, self.TOP_DOWN_TASK_NAME, extraArgs=[], appendTask=True)

def reset(self):
Expand Down Expand Up @@ -344,7 +365,7 @@ def stop_track(self, bird_view_on_current_position=True):
if bird_view_on_current_position:
current_pos = self.camera.getPos()
self.camera_x, self.camera_y = current_pos[0], current_pos[1]
self.camera_rotate = 0
self.camera_hpr = [0, 0, 0]
self.engine.task_manager.add(self._top_down_task, self.TOP_DOWN_TASK_NAME, extraArgs=[], appendTask=True)

def _top_down_task(self, task):
Expand All @@ -364,13 +385,10 @@ def _top_down_task(self, task):
self.camera.setPos(self.camera_x, self.camera_y, self.top_down_camera_height)
if self.engine.global_config["show_coordinates"]:
self.engine.set_coordinates_indicator_pos([self.camera_x, self.camera_y])
self.camera.lookAt(self.camera_x, self.camera_y, 0)

if self.inputs.isSet("right_rotate"):
self.camera_rotate += 3
if self.inputs.isSet("left_rotate"):
self.camera_rotate -= 3
self.camera.setH(self.camera_rotate)
if abs(sum(self.camera_hpr)) < 0.0001:
self.camera.lookAt(self.camera_x, self.camera_y, 0)
else:
self.camera.setHpr(Vec3(*self.camera_hpr))
return task.cont

def _update_height(self, height):
Expand Down
2 changes: 1 addition & 1 deletion metadrive/envs/base_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ def reset_sensors(self):
self.main_camera.track(current_track_agent)
if bev_cam:
self.main_camera.stop_track()
self.main_camera.set_bird_view_pos(current_track_agent.position)
self.main_camera.set_bird_view_pos_hpr(current_track_agent.position)
for name, sensor in self.engine.sensors.items():
if hasattr(sensor, "track") and name != "main_camera":
sensor.track(current_track_agent.origin, [0., 0.8, 1.5], [0, 0.59681, 0])
Expand Down
3 changes: 2 additions & 1 deletion metadrive/examples/Basic_MetaDrive_Usages.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,7 @@
"\n",
"cfg=dict(image_observation=True, \n",
" show_terrain=not os.getenv('TEST_IPYNB'),\n",
" sensors={\"main_camera\": ()},\n",
" vehicle_config=dict(image_source=\"main_camera\"),\n",
" window_size=(84, 60))\n",
"\n",
Expand Down Expand Up @@ -519,7 +520,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.17"
"version": "3.7.13"
}
},
"nbformat": 4,
Expand Down

0 comments on commit e43692c

Please sign in to comment.