From 6b19a0233e4a6ac19d75a9f131c4199970fbbc12 Mon Sep 17 00:00:00 2001 From: Zhenghao Peng Date: Thu, 5 Dec 2024 21:21:07 -0800 Subject: [PATCH] Hotfix the bugs introduced by bounding box env (#778) * revert the bug causing tire-scale code * Hot fix the bug * Allow to config "set_static", default to False to fix the bug of flickerring visualization. --- metadrive/component/vehicle/base_vehicle.py | 31 ++----------------- metadrive/envs/scenario_env.py | 11 +++++-- metadrive/manager/scenario_traffic_manager.py | 9 +++--- metadrive/policy/replay_policy.py | 9 +++++- 4 files changed, 24 insertions(+), 36 deletions(-) diff --git a/metadrive/component/vehicle/base_vehicle.py b/metadrive/component/vehicle/base_vehicle.py index 607289d69..7111c418b 100644 --- a/metadrive/component/vehicle/base_vehicle.py +++ b/metadrive/component/vehicle/base_vehicle.py @@ -145,15 +145,6 @@ def __init__( self.add_body(vehicle_chassis.getChassis()) self.system = vehicle_chassis self.chassis = self.origin - - if self.config["scale"] is not None: - w, l, h = self.config["scale"] - self.FRONT_WHEELBASE *= l - self.REAR_WHEELBASE *= l - self.LATERAL_TIRE_TO_CENTER *= w - self.TIRE_RADIUS *= h - self.CHASSIS_TO_WHEEL_AXIS *= h - self.wheels = self._create_wheel() # light experimental! @@ -635,7 +626,7 @@ def _create_vehicle_chassis(self): def _add_visualization(self): if self.render: - path, scale, offset, HPR = self.path + [path, scale, offset, HPR] = self.path should_update = (path not in BaseVehicle.model_collection) or (self.config["scale"] is not None) if should_update: @@ -659,10 +650,8 @@ def _add_visualization(self): car_model.setHpr(*HPR) car_model.setPos(offset[0], offset[1], offset[2] + extra_offset_z) BaseVehicle.model_collection[path] = car_model - else: car_model = BaseVehicle.model_collection[path] - car_model.instanceTo(self.origin) if self.config["random_color"]: material = Material() @@ -704,23 +693,7 @@ def _add_wheel(self, pos: Vec3, radius: float, front: bool, left): wheel_model = self.loader.loadModel(model_path) wheel_model.setTwoSided(self.TIRE_TWO_SIDED) wheel_model.reparentTo(wheel_np) - tire_scale = 1 * self.TIRE_MODEL_CORRECT if left else -1 * self.TIRE_MODEL_CORRECT - - if self.config['scale'] is not None: - tire_scale = ( - self.config['scale'][0] * tire_scale, self.config['scale'][1] * tire_scale, - self.config['scale'][2] * tire_scale - ) - - # A quick workaround here. - # The model position is set to height/2 in ScenarioMapManager. - # Now we set this offset to -height/2, so that the model will be placed on the ground. - # For the wheel, the bottom of it is not z=0, so we add two more terms to correct it. - extra_offset = -self.config["height"] / 2 + self.TIRE_RADIUS / self.config['scale'][ - 2] + self.CHASSIS_TO_WHEEL_AXIS / self.config['scale'][2] - wheel_model.setPos(0, 0, extra_offset) - - wheel_model.set_scale(tire_scale) + wheel_model.set_scale(1 * self.TIRE_MODEL_CORRECT if left else -1 * self.TIRE_MODEL_CORRECT) wheel = self.system.createWheel() wheel.setNode(wheel_np.node()) wheel.setChassisConnectionPointCs(pos) diff --git a/metadrive/envs/scenario_env.py b/metadrive/envs/scenario_env.py index 1ba065862..fa35f178b 100644 --- a/metadrive/envs/scenario_env.py +++ b/metadrive/envs/scenario_env.py @@ -59,6 +59,12 @@ lane_line_detector=dict(num_lasers=0, distance=50), side_detector=dict(num_lasers=12, distance=50), ), + # If set_static=True, then the agent will not "fall from the sky". This will be helpful if you want to + # capture per-frame data for the agent (for example for collecting static sensor data). + # However, the physics engine will not update the position of the agent. So in the visualization, the image will be + # very chunky as the agent will not suddenly move to the next position for each step. + # Set to False for better visualization. + set_static=False, # ===== Reward Scheme ===== # See: https://github.com/metadriverse/metadrive/issues/283 @@ -410,7 +416,8 @@ def _reset_global_seed(self, force_seed=None): # "no_traffic":True, # "start_scenario_index": 192, # "start_scenario_index": 1000, - "num_scenarios": 30, + "num_scenarios": 3, + "set_static": True, # "force_reuse_object_name": True, # "data_directory": "/home/shady/Downloads/test_processed", "horizon": 1000, @@ -424,7 +431,7 @@ def _reset_global_seed(self, force_seed=None): lane_line_detector=dict(num_lasers=12, distance=50), side_detector=dict(num_lasers=160, distance=50) ), - "data_directory": AssetLoader.file_path("nuplan", unix_style=False), + "data_directory": AssetLoader.file_path("nuscenes", unix_style=False), } ) success = [] diff --git a/metadrive/manager/scenario_traffic_manager.py b/metadrive/manager/scenario_traffic_manager.py index e43c5f831..325e1b66f 100644 --- a/metadrive/manager/scenario_traffic_manager.py +++ b/metadrive/manager/scenario_traffic_manager.py @@ -211,10 +211,11 @@ def spawn_vehicle(self, v_id, track): v_cfg["width"] = state["width"] v_cfg["length"] = state["length"] v_cfg["height"] = state["height"] - v_cfg["scale"] = ( - v_cfg["width"] / vehicle_class.DEFAULT_WIDTH, v_cfg["length"] / vehicle_class.DEFAULT_LENGTH, - v_cfg["height"] / vehicle_class.DEFAULT_HEIGHT - ) + if use_bounding_box: + v_cfg["scale"] = ( + v_cfg["width"] / vehicle_class.DEFAULT_WIDTH, v_cfg["length"] / vehicle_class.DEFAULT_LENGTH, + v_cfg["height"] / vehicle_class.DEFAULT_HEIGHT + ) if self.engine.global_config["top_down_show_real_size"]: v_cfg["top_down_length"] = track["state"]["length"][self.episode_step] diff --git a/metadrive/policy/replay_policy.py b/metadrive/policy/replay_policy.py index 28ccce036..f66b3097d 100644 --- a/metadrive/policy/replay_policy.py +++ b/metadrive/policy/replay_policy.py @@ -63,7 +63,14 @@ def act(self, *args, **kwargs): self.control_object.set_velocity(info["velocity"], in_local_frame=self._velocity_local_frame) self.control_object.set_heading_theta(info["heading"]) self.control_object.set_angular_velocity(info["angular_velocity"]) - self.control_object.set_static(True) + + # If set_static, then the agent will not "fall from the sky". + # However, the physics engine will not update the position of the agent. + # So in the visualization, the image will be very chunky as the agent will not suddenly move to the next + # position for each step. + + if self.engine.global_config.get("set_static", False): + self.control_object.set_static(True) return None # Return None action so the base vehicle will not overwrite the steering & throttle