From ced70433204d5ab10d412045921e69755dce39b9 Mon Sep 17 00:00:00 2001 From: Jialiang Xu Date: Sat, 17 Aug 2024 15:43:00 +0700 Subject: [PATCH 1/4] checking in code for car dreamer, carla right turn random env and corresponding configs --- README.md | 1 + car_dreamer/__init__.py | 7 +- car_dreamer/carla_right_turn_random_env.py | 31 +++++++ car_dreamer/configs/tasks.yaml | 101 ++++++++++----------- 4 files changed, 84 insertions(+), 56 deletions(-) create mode 100644 car_dreamer/carla_right_turn_random_env.py diff --git a/README.md b/README.md index 05a87b0..9bd8b76 100644 --- a/README.md +++ b/README.md @@ -276,6 +276,7 @@ If you find this repository useful, please cite this paper: Special thanks to the community for your valuable contributions and support in making CarDreamer better for everyone! + diff --git a/car_dreamer/__init__.py b/car_dreamer/__init__.py index 152f1ff..61bc125 100644 --- a/car_dreamer/__init__.py +++ b/car_dreamer/__init__.py @@ -15,6 +15,7 @@ from .carla_navigation_env import CarlaNavigationEnv from .carla_overtake_env import CarlaOvertakeEnv from .carla_right_turn_env import CarlaRightTurnEnv +from .carla_right_turn_random_env import CarlaRightTurnRandomEnv from .carla_roundabout_env import CarlaRoundaboutEnv from .carla_stop_sign_env import CarlaStopSignEnv from .carla_traffic_lights_env import CarlaTrafficLightsEnv @@ -39,11 +40,11 @@ def load_task_configs(task_name: str): import yaml - dir = os.path.join(os.path.dirname(__file__), "configs") - with open(os.path.join(dir, "common.yaml")) as f: + dir = os.path.dirname(__file__) + "/configs/" + with open(dir + "common.yaml", "r") as f: config = yaml.safe_load(f) config = toolkit.Config(config) - with open(os.path.join(dir, "tasks.yaml")) as f: + with open(dir + "tasks.yaml", "r") as f: task_config = yaml.safe_load(f) config = config.update(task_config[task_name]) return config diff --git a/car_dreamer/carla_right_turn_random_env.py b/car_dreamer/carla_right_turn_random_env.py new file mode 100644 index 0000000..8c6b2de --- /dev/null +++ b/car_dreamer/carla_right_turn_random_env.py @@ -0,0 +1,31 @@ +import random +import time +from collections import deque + +import carla + +from .carla_wpt_fixed_env import CarlaWptFixedEnv +from .toolkit import FixedPathPlanner, get_vehicle_pos + + +class CarlaRightTurnRandomEnv(CarlaWptFixedEnv): + """ + Vehicle passes the crossing (random turn right) and avoid collision. + + **Provided Tasks**: ``carla_right_turn_random`` + """ + + def __init__(self, config): + super().__init__(config) + + def on_reset(self) -> None: + random.seed(time.time()) + random_index = random.randint(0, len(self._config.lane_start_point) - 1) + self.ego_src = self._config.lane_start_point[random_index] + ego_transform = carla.Transform(carla.Location(*self.ego_src[:3]), carla.Rotation(yaw=self.ego_src[3])) + self.ego = self._world.spawn_actor(transform=ego_transform) + self.ego_path = self._config.ego_path[random_index] + self.use_road_waypoints = self._config.use_road_waypoints + self.ego_planner = FixedPathPlanner(vehicle=self.ego, vehicle_path=self.ego_path, use_road_waypoints=self.use_road_waypoints) + self.waypoints, self.planner_stats = self.ego_planner.run_step() + self.num_completed = self.planner_stats["num_completed"] diff --git a/car_dreamer/configs/tasks.yaml b/car_dreamer/configs/tasks.yaml index 6d3be82..4a68f5c 100644 --- a/car_dreamer/configs/tasks.yaml +++ b/car_dreamer/configs/tasks.yaml @@ -1,16 +1,8 @@ -carla_wpt: - &carla_wpt # It's a base configuration for wpt tasks, don't use it directly +carla_wpt: &carla_wpt + # It's a base configuration for wpt tasks, don't use it directly reward: desired_speed: 4 # desired speed (m/s) - scales: - { - waypoint: 2.0, - speed: 0.5, - collision: 30.0, - out_of_lane: 3.0, - time: 0.0, - destination_reached: 20.0, - } + scales: { waypoint: 2.0, speed: 0.5, collision: 30.0, out_of_lane: 3.0, time: 0.0, destination_reached: 20.0 } terminal: time_limit: 500 # maximum timesteps per episode out_lane_thres: 3 # threshold for out of lane @@ -74,34 +66,33 @@ carla_overtake: lane_end_points: - [5.8, 0.0, 0.1] swing_steer: 0.04 # The background vehicle steer for swing . - swing_amplitude: 0.2 # The y-axis amplitude of background vehicle steer. - swing_trigger_dist: 20 # The distance between ego and background vehicle that triggers swing. + swing_amplitude: 0.2 # The y-axis amplitude of background vehicle steer. + swing_trigger_dist: 20 # The distance between ego and background vehicle that triggers swing. pid_coeffs: [0.03, 0.0, 0.03] # The PID controller parameter for background vehicle lane keeping. reward: desired_speed: 5 # desired speed (m/s) reward_overtake_dist: 8 # The distance that triggers overtake reward. - early_lane_change_dist: 10 # The distance that penalizes early lane change. + early_lane_change_dist: 10 # The distance that penalizes early lane change. lane_width: 3.4 - scales: - { - waypoint: 2.0, - speed: 0.5, - stay_same_lane: 0.3, - out_of_lane: 3.0, - collision: 30.0, - time: 0.0, - exceeding: 200.0, - overtake: 200.0, - early_lane_change: 0.0, - destination_reached: 20.0, - } + scales: { + waypoint: 2.0, + speed: 0.5, + stay_same_lane: 0.3, + out_of_lane: 3.0, + collision: 30.0, + time: 0.0, + exceeding: 200.0, + overtake: 200.0, + early_lane_change: 0.0, + destination_reached: 20.0 + } terminal: out_lane_thres: 5 # threshold for out of lane time_limit: 500 # maximum timesteps per episode - left_lane_boundry: 3.7 # out of lane boundry + left_lane_boundry: 3.7 # out of lane boundry right_lane_boundry: 17.7 lane_width: 3.4 - terminal_dist: 100 # terminate tasks + terminal_dist: 100 # terminate tasks dreamerv3: encoder.cnn_keys: "birdeye_wpt" @@ -149,6 +140,30 @@ carla_right_turn_hard: env.min_flow_dist: 6 env.max_flow_dist: 8 +carla_right_turn_random: + env: + world: + town: Town03 + name: CarlaRightTurnRandomEnv-v0 + action: + discrete_steer: [-0.9, -0.3, 0.0, 0.3, 0.9] + observation.enabled: [camera, collision, birdeye_wpt] + <<: *carla_wpt + lane_start_point: [[-33.8, -135.1, 0.1, 0.0], [-3.2, -165.2, 0.1, 90], [9.3, -106.2, 0.1, -90]] + ego_path: [[[-33.8, -135.1, 0.1], [-5.0, -110.3, 0.1]], [[-3.2, -165.2, 0.1, 90], [-20.7, -142.2, 0.1, 180]], [[9.3, -106.2, 0.1, -90], [31.3, -130.7, 0.1, 0]]] + use_road_waypoints: [True, False] + + dreamerv3: + encoder.cnn_keys: "birdeye_wpt" + decoder.cnn_keys: "birdeye_wpt" + run.log_keys_video: [camera, birdeye_wpt] + + dreamerv2: + encoder.cnn_keys: "birdeye_wpt" + decoder.cnn_keys: "birdeye_wpt" + decoder.cnn_kernels: [5, 5, 5, 6, 6] + train.log_keys_video: [camera, birdeye_wpt] + carla_left_turn_simple: &carla_left_turn_simple env: world: @@ -191,15 +206,7 @@ carla_roundabout: observation.enabled: [camera, collision, birdeye_wpt] <<: *carla_wpt lane_start_point: [-52.6, 1.0, 0.1, 0.0] - ego_path: - [ - [-52.6, 1.0, 0.1], - [-23.0, 7.5, 0.1], - [-17.0, 11.7, 0.1], - [13.3, -13.2, 0.1], - [7.6, -21.8, 0.1], - [4.2, -43.2, 0.1], - ] + ego_path: [[-52.6, 1.0, 0.1], [-23.0, 7.5, 0.1], [-17.0, 11.7, 0.1], [13.3, -13.2, 0.1], [7.6, -21.8, 0.1], [4.2, -43.2, 0.1]] use_road_waypoints: [True, False, True, False, True, False] flow_spawn_point: [-14.1, -13.5, 0.1, 125.0] min_flow_dist: 10 @@ -267,22 +274,10 @@ carla_traffic_lights: time_limit: 500 # maximum timesteps per episode out_lane_thres: 3 # threshold for out of lane - lane_start_point: - [ - [-0.3, -163.4, 0.1, 90.0], - [-0.3, -163.4, 0.1, 90.0], - [-3.5, -163.8, 0.1, 90.0], - [-3.5, -163.8, 0.1, 90.0], - ] - ego_path: - [ - [[-0.3, -163.4, 0.1], [32.5, -133.8, 0.1]], - [[-0.3, -163.4, 0.1], [-1.5, -111.0, 0.1]], - [[-3.5, -163.8, 0.1], [-4.7, -110.6, 0.1]], - [[-3.5, -163.8, 0.1], [-27.0, -138.8, 0.1]], - ] + lane_start_point: [[-0.3, -163.4, 0.1, 90.0], [-0.3, -163.4, 0.1, 90.0], [-3.5, -163.8, 0.1, 90.0], [-3.5, -163.8, 0.1, 90.0]] + ego_path: [[[-0.3, -163.4, 0.1], [32.5, -133.8, 0.1]], [[-0.3, -163.4, 0.1], [-1.5, -111.0, 0.1]], [[-3.5, -163.8, 0.1], [-4.7, -110.6, 0.1]], [[-3.5, -163.8, 0.1], [-27.0, -138.8, 0.1]]] use_road_waypoints: [True, False] - traffic_locations: [-3.4, -125.1, 0.1] # Location of traffic sign + traffic_locations: [-3.4, -125.1, 0.1] # Location of traffic sign red_duration: [60, 80] green_duration: [40, 50] From 5566d65b13e870484bbdf67d0cd6b5b7d363bb9f Mon Sep 17 00:00:00 2001 From: Hanchu Zhou Date: Sat, 17 Aug 2024 10:56:02 -0700 Subject: [PATCH 2/4] Update __init__.py --- car_dreamer/__init__.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/car_dreamer/__init__.py b/car_dreamer/__init__.py index 61bc125..962d8f2 100644 --- a/car_dreamer/__init__.py +++ b/car_dreamer/__init__.py @@ -40,11 +40,11 @@ def load_task_configs(task_name: str): import yaml - dir = os.path.dirname(__file__) + "/configs/" - with open(dir + "common.yaml", "r") as f: + dir = os.path.join(os.path.dirname(__file__), "configs") + with open(os.path.join(dir, "common.yaml")) as f: config = yaml.safe_load(f) config = toolkit.Config(config) - with open(dir + "tasks.yaml", "r") as f: + with open(os.path.join(dir, "tasks.yaml")) as f: task_config = yaml.safe_load(f) config = config.update(task_config[task_name]) return config From 4e937adb1e479cabd5013d86824a5812230776f9 Mon Sep 17 00:00:00 2001 From: Hanchu Zhou Date: Sat, 17 Aug 2024 11:02:01 -0700 Subject: [PATCH 3/4] Update tasks.yaml --- car_dreamer/configs/tasks.yaml | 91 ++++++++++++++++++++++++---------- 1 file changed, 65 insertions(+), 26 deletions(-) diff --git a/car_dreamer/configs/tasks.yaml b/car_dreamer/configs/tasks.yaml index 4a68f5c..7d9eb00 100644 --- a/car_dreamer/configs/tasks.yaml +++ b/car_dreamer/configs/tasks.yaml @@ -1,8 +1,16 @@ -carla_wpt: &carla_wpt - # It's a base configuration for wpt tasks, don't use it directly +carla_wpt: + &carla_wpt # It's a base configuration for wpt tasks, don't use it directly reward: desired_speed: 4 # desired speed (m/s) - scales: { waypoint: 2.0, speed: 0.5, collision: 30.0, out_of_lane: 3.0, time: 0.0, destination_reached: 20.0 } + scales: + { + waypoint: 2.0, + speed: 0.5, + collision: 30.0, + out_of_lane: 3.0, + time: 0.0, + destination_reached: 20.0, + } terminal: time_limit: 500 # maximum timesteps per episode out_lane_thres: 3 # threshold for out of lane @@ -66,33 +74,34 @@ carla_overtake: lane_end_points: - [5.8, 0.0, 0.1] swing_steer: 0.04 # The background vehicle steer for swing . - swing_amplitude: 0.2 # The y-axis amplitude of background vehicle steer. - swing_trigger_dist: 20 # The distance between ego and background vehicle that triggers swing. + swing_amplitude: 0.2 # The y-axis amplitude of background vehicle steer. + swing_trigger_dist: 20 # The distance between ego and background vehicle that triggers swing. pid_coeffs: [0.03, 0.0, 0.03] # The PID controller parameter for background vehicle lane keeping. reward: desired_speed: 5 # desired speed (m/s) reward_overtake_dist: 8 # The distance that triggers overtake reward. - early_lane_change_dist: 10 # The distance that penalizes early lane change. + early_lane_change_dist: 10 # The distance that penalizes early lane change. lane_width: 3.4 - scales: { - waypoint: 2.0, - speed: 0.5, - stay_same_lane: 0.3, - out_of_lane: 3.0, - collision: 30.0, - time: 0.0, - exceeding: 200.0, - overtake: 200.0, - early_lane_change: 0.0, - destination_reached: 20.0 - } + scales: + { + waypoint: 2.0, + speed: 0.5, + stay_same_lane: 0.3, + out_of_lane: 3.0, + collision: 30.0, + time: 0.0, + exceeding: 200.0, + overtake: 200.0, + early_lane_change: 0.0, + destination_reached: 20.0, + } terminal: out_lane_thres: 5 # threshold for out of lane time_limit: 500 # maximum timesteps per episode - left_lane_boundry: 3.7 # out of lane boundry + left_lane_boundry: 3.7 # out of lane boundry right_lane_boundry: 17.7 lane_width: 3.4 - terminal_dist: 100 # terminate tasks + terminal_dist: 100 # terminate tasks dreamerv3: encoder.cnn_keys: "birdeye_wpt" @@ -149,8 +158,18 @@ carla_right_turn_random: discrete_steer: [-0.9, -0.3, 0.0, 0.3, 0.9] observation.enabled: [camera, collision, birdeye_wpt] <<: *carla_wpt - lane_start_point: [[-33.8, -135.1, 0.1, 0.0], [-3.2, -165.2, 0.1, 90], [9.3, -106.2, 0.1, -90]] - ego_path: [[[-33.8, -135.1, 0.1], [-5.0, -110.3, 0.1]], [[-3.2, -165.2, 0.1, 90], [-20.7, -142.2, 0.1, 180]], [[9.3, -106.2, 0.1, -90], [31.3, -130.7, 0.1, 0]]] + lane_start_point: + [ + [-33.8, -135.1, 0.1, 0.0], + [-3.2, -165.2, 0.1, 90], + [9.3, -106.2, 0.1, -90] + ] + ego_path: + [ + [[-33.8, -135.1, 0.1], [-5.0, -110.3, 0.1]], + [[-3.2, -165.2, 0.1, 90], [-20.7, -142.2, 0.1, 180]], + [[9.3, -106.2, 0.1, -90], [31.3, -130.7, 0.1, 0]] + ] use_road_waypoints: [True, False] dreamerv3: @@ -206,7 +225,15 @@ carla_roundabout: observation.enabled: [camera, collision, birdeye_wpt] <<: *carla_wpt lane_start_point: [-52.6, 1.0, 0.1, 0.0] - ego_path: [[-52.6, 1.0, 0.1], [-23.0, 7.5, 0.1], [-17.0, 11.7, 0.1], [13.3, -13.2, 0.1], [7.6, -21.8, 0.1], [4.2, -43.2, 0.1]] + ego_path: + [ + [-52.6, 1.0, 0.1], + [-23.0, 7.5, 0.1], + [-17.0, 11.7, 0.1], + [13.3, -13.2, 0.1], + [7.6, -21.8, 0.1], + [4.2, -43.2, 0.1], + ] use_road_waypoints: [True, False, True, False, True, False] flow_spawn_point: [-14.1, -13.5, 0.1, 125.0] min_flow_dist: 10 @@ -274,10 +301,22 @@ carla_traffic_lights: time_limit: 500 # maximum timesteps per episode out_lane_thres: 3 # threshold for out of lane - lane_start_point: [[-0.3, -163.4, 0.1, 90.0], [-0.3, -163.4, 0.1, 90.0], [-3.5, -163.8, 0.1, 90.0], [-3.5, -163.8, 0.1, 90.0]] - ego_path: [[[-0.3, -163.4, 0.1], [32.5, -133.8, 0.1]], [[-0.3, -163.4, 0.1], [-1.5, -111.0, 0.1]], [[-3.5, -163.8, 0.1], [-4.7, -110.6, 0.1]], [[-3.5, -163.8, 0.1], [-27.0, -138.8, 0.1]]] + lane_start_point: + [ + [-0.3, -163.4, 0.1, 90.0], + [-0.3, -163.4, 0.1, 90.0], + [-3.5, -163.8, 0.1, 90.0], + [-3.5, -163.8, 0.1, 90.0], + ] + ego_path: + [ + [[-0.3, -163.4, 0.1], [32.5, -133.8, 0.1]], + [[-0.3, -163.4, 0.1], [-1.5, -111.0, 0.1]], + [[-3.5, -163.8, 0.1], [-4.7, -110.6, 0.1]], + [[-3.5, -163.8, 0.1], [-27.0, -138.8, 0.1]], + ] use_road_waypoints: [True, False] - traffic_locations: [-3.4, -125.1, 0.1] # Location of traffic sign + traffic_locations: [-3.4, -125.1, 0.1] # Location of traffic sign red_duration: [60, 80] green_duration: [40, 50] From d10218b53f6dbe5da49ee2815ae92825d99f0461 Mon Sep 17 00:00:00 2001 From: Hanchu Zhou Date: Sat, 17 Aug 2024 11:02:49 -0700 Subject: [PATCH 4/4] Update README.md