-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* traffic course * traffic course * traffic course * traffic course * traffic course * course * course * course * course
- Loading branch information
1 parent
a6939fa
commit fb238c4
Showing
42 changed files
with
8,526 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# 路口微观3D 动画演示运动场景spectator.py | ||
|
||
#### **介绍** | ||
|
||
此脚本连接到CARLA仿真环境,并定期切换湖工商附近的四个预设路口视角。通过不断改变观众(Spectator)的视角,用户可以观察到不同路口的交通状况。 | ||
|
||
#### **环境要求** | ||
|
||
- Python 3.6 及以上版本 | ||
|
||
- CARLA 仿真环境 | ||
|
||
- 安装必要的Python库: | ||
|
||
``` | ||
pip install carla | ||
``` | ||
|
||
#### **运行** | ||
|
||
此示例支持设置 **spectator** 视角来观察交通路口运动情形。 | ||
|
||
以下示例通过预定义 **spectator** 对象视角的位置 **Location** 和旋转角度 **pitch** 来进行视角的更换 | ||
|
||
``` | ||
client = carla.Client('localhost', 2000) | ||
client.set_timeout(10) | ||
world = client.get_world() | ||
spectator_transform = [ | ||
carla.Transform(carla.Location(x=-323, y=-15.7, z=108), carla.Rotation(pitch=-90)), | ||
carla.Transform(carla.Location(x=-359.6, y=391.3, z=94), carla.Rotation(pitch=-90)), | ||
carla.Transform(carla.Location(x=426.2, y=-33.4, z=91.6), carla.Rotation(pitch=-90)), | ||
carla.Transform(carla.Location(x=375.1, y=-558.4, z=91.6), carla.Rotation(pitch=-90)) | ||
] | ||
spectator = world.get_spectator() | ||
spectator.set_transform(spectator_transform[0]) | ||
``` | ||
|
||
##### 定期切换视角 | ||
|
||
脚本将连接到CARLA服务器,并每5秒切换一次预设的观众视角。 | ||
|
||
``` | ||
start_time = time.time() | ||
state = 0 | ||
while True: | ||
elapsed_time = time.time() - start_time | ||
if elapsed_time >= 5: | ||
spectator.set_transform(spectator_transform[state]) | ||
state += 1 | ||
if state > 3: | ||
state = 0 | ||
start_time = time.time() | ||
``` | ||
|
||
##### 运行结果: | ||
|
||
![](../img/traffic_course_img/1.gif) | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
# 配置多视角摄像头camera.py | ||
|
||
#### **介绍** | ||
|
||
此脚本连接到CARLA仿真环境,在车辆的左右两侧设置相机,并使用Pygame显示相机捕获的实时图像。系统采用多进程架构,确保左右相机图像分别在不同窗口中实时显示。 | ||
|
||
#### **环境要求** | ||
|
||
- Python 3.6 及以上版本 | ||
- CARLA 仿真环境 | ||
- 安装必要的Python库: | ||
|
||
``` | ||
pip install carla pygame numpy opencv-python | ||
``` | ||
|
||
#### **脚本结构** | ||
|
||
##### 初始化 | ||
|
||
``` | ||
def __init__(self): | ||
self.client = None | ||
self.world = None | ||
self.camera_left = None | ||
self.camera_right = None | ||
self.car = None | ||
self.image_left = None | ||
self.image_right = None | ||
self.capture = True | ||
``` | ||
|
||
初始化CARLA客户端、世界、车辆和相机变量。 | ||
|
||
##### 相机设置 | ||
|
||
```python | ||
def setup_camera(self): | ||
#设置相机的变换位置并附加到车辆上 | ||
#左相机 | ||
left_camera_transform = carla.Transform(carla.Location(x=-1.6, y=-1.5, z=1.7)) | ||
self.camera_left = self.world.spawn_actor(self.camera_bp(), left_camera_transform, attach_to=self.car) | ||
#右相机 | ||
right_camera_transform = carla.Transform(carla.Location(x=-1.6, y=1.5, z=1.7)) | ||
self.camera_right = self.world.spawn_actor(self.camera_bp(), right_camera_transform, attach_to=self.car) | ||
#监听左右相机图像 | ||
weak_self = weakref.ref(self) | ||
self.camera_left.listen(lambda image: weak_self().set_image(weak_self, 'left', image)) | ||
self.camera_right.listen(lambda image: weak_self().set_image(weak_self, 'right', image)) | ||
``` | ||
|
||
在车辆的左右两侧生成相机,并设置监听器捕获相机图像。 | ||
|
||
##### 图像处理 | ||
|
||
``` | ||
@staticmethod | ||
def set_image(weak_self, side, img): | ||
self = weak_self() | ||
if side == 'left': | ||
self.image_left = img | ||
elif side == 'right': | ||
self.image_right = img | ||
def get_image_array(self, side): | ||
if side == 'left' and self.image_left is not None: | ||
array = np.frombuffer(self.image_left.raw_data, dtype=np.dtype("uint8")) | ||
array = array.reshape((VIEW_HEIGHT, VIEW_WIDTH, 4)) | ||
array = array[:, :, :3] # Drop alpha channel | ||
array = array[:, :, ::-1] # Convert from BGRA to RGB | ||
return array | ||
elif side == 'right' and self.image_right is not None: | ||
array = np.frombuffer(self.image_right.raw_data, dtype=np.dtype("uint8")) | ||
array = array.reshape((VIEW_HEIGHT, VIEW_WIDTH, 4)) | ||
array = array[:, :, :3] # Drop alpha channel | ||
array = array[:, :, ::-1] # Convert from BGRA to RGB | ||
return array | ||
else: | ||
return None | ||
``` | ||
|
||
将相机捕获的图像从BGRA格式转换为RGB格式,并去除Alpha通道。 | ||
|
||
##### 渲染函数 | ||
|
||
``` | ||
def render(queue, side, x, y): | ||
pygame.init() | ||
os.environ['SDL_VIDEO_WINDOW_POS'] = f"{x},{y}" | ||
display = pygame.display.set_mode((VIEW_WIDTH, VIEW_HEIGHT), pygame.HWSURFACE | pygame.DOUBLEBUF) | ||
pygame.display.set_caption(f'{side.capitalize()} Camera') | ||
clock = pygame.time.Clock() | ||
try: | ||
while True: | ||
array = queue.get() | ||
if array is None: | ||
break | ||
surface = pygame.surfarray.make_surface(array.swapaxes(0, 1)) | ||
display.blit(surface, (0, 0)) | ||
pygame.display.flip() | ||
for event in pygame.event.get(): | ||
if event.type == pygame.QUIT: | ||
return | ||
clock.tick_busy_loop(30) | ||
finally: | ||
pygame.quit() | ||
``` | ||
|
||
使用Pygame显示左右相机捕获的图像。 | ||
|
||
##### 多线程 | ||
|
||
为了在CARLA仿真环境中创建两个窗口来显示车辆的左右视角,我们需要利用Pygame和多进程来实现。 | ||
|
||
``` | ||
left_process = multiprocessing.Process(target=render, args=(left_queue, 'left', 0, 100)) | ||
right_process = multiprocessing.Process(target=render, args=(right_queue, 'right', VIEW_WIDTH + 10, 100)) | ||
``` | ||
|
||
![](../img/traffic_course_img/2.gif) | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
# 自定义车辆运行轨迹 | ||
|
||
车辆运动支持定义车辆运动起点和目标位置,为每辆车指定随机速度值,使其沿轨迹移动时不会发生碰撞或模拟碰撞,看到车辆沿指定轨迹行驶以到达其目标位置。 | ||
|
||
本示例设计了一个**automatic_control_revised.py**脚本进行实现 | ||
|
||
![](../img/traffic_course_img/4.gif) | ||
|
||
**注意** 需要将**agents**文件夹位置添加到代码环境变量中,或者复制该文件夹使其与本示例脚本同级。 | ||
|
||
|
||
|
||
自定义一些生成位置的起始点坐标 | ||
|
||
```python | ||
diming = { | ||
"北门": {"x": 224.2, "y": 1.7, "z": 1}, | ||
"西门": {"x": -146, "y": -161, "z": 10}, | ||
"楚枫桥": {"x": 1.8, "y": -15.9, "z": 30}, | ||
"楚枫轩": {"x": 14.8, "y": 64.7, "z": 20}, | ||
"日新楼": {"x": 58, "y": -75, "z": 30}, | ||
"体育馆": {"x": 160, "y": -265, "z": 40}, | ||
"湘江楼": {"x": -2.4, "y": -100, "z": 30}, | ||
"科技楼": {"x": -97.4, "y": -217, "z": 15}, | ||
} | ||
``` | ||
|
||
定义命令行参数 | ||
|
||
```python | ||
argparser = argparse.ArgumentParser( | ||
description=__doc__) | ||
argparser.add_argument( | ||
'--s', | ||
default="北门", | ||
type=str) | ||
argparser.add_argument( | ||
'--e', | ||
default="西门", | ||
type=str) | ||
args = argparser.parse_args() | ||
|
||
``` | ||
|
||
获取仿真时间对象及设置 | ||
|
||
```python | ||
client = carla.Client('localhost', 2000) | ||
client.set_timeout(10) | ||
world = client.get_world() | ||
origin_settings = world.get_settings() | ||
|
||
# 设置 | ||
settings = world.get_settings() | ||
settings.synchronous_mode = True | ||
settings.fixed_delta_seconds = 0.05 | ||
world.apply_settings(settings) | ||
``` | ||
|
||
从车辆蓝图中自定义车辆对象,并获取车辆起点坐标 | ||
|
||
```python | ||
# 生成车辆和代理 | ||
blueprint_library = world.get_blueprint_library() | ||
# 将diming字典中的每个键值对转换为一个carla.Location对象 | ||
# 获取生成点 | ||
locations = {key: carla.Location(**value) for key, value in diming.items()} | ||
spawn_location = carla.Location(locations[args.s]) | ||
spawn_rotation = carla.Rotation(yaw=180) | ||
spawn_point = carla.Transform(spawn_location, spawn_rotation) #车辆起点 | ||
# 生成车辆 | ||
ego_vehicle_bp = blueprint_library.find('vehicle.audi.tt') | ||
ego_vehicle_bp.set_attribute('color', '100, 250, 250') | ||
vehicle = world.spawn_actor(ego_vehicle_bp, spawn_point) | ||
#推进仿真环境时间 | ||
world.tick() | ||
``` | ||
|
||
生成终点,给起点和终点生成一个车辆运行轨迹 | ||
|
||
```python | ||
# 创建代理 | ||
agent = BehaviorAgent(vehicle, behavior='normal') | ||
destination = carla.Location(locations[args.e]) | ||
start = vehicle.get_location() | ||
# 生成轨迹 | ||
agent.set_destination(destination) | ||
``` | ||
|
||
给目标车辆设置多个相机传感器。这些相机可以提供不同的视角,用于观察和记录车辆在仿真环境中的行为。 | ||
|
||
```python | ||
# 查找相机蓝图 | ||
camera_bp = world.get_blueprint_library().find('sensor.camera.rgb') | ||
|
||
# 设置Camera的附加类型Camera跟随车辆 | ||
Atment_SpringArmGhost = carla.AttachmentType.SpringArmGhost | ||
Atment_Rigid = carla.AttachmentType.Rigid | ||
# 设置相对车辆的安装位置,配置上帝视图(Camera无法实现上帝视图,画面会抖动) | ||
Vehicle_transform_list = [ | ||
(carla.Location(z=35), carla.Rotation(pitch=-90)) | ||
] | ||
# 设置camera的安装位置,配置后往前视图以及前后左右视图 | ||
Camera_transform_list = [ | ||
(carla.Transform(carla.Location(x=-8, y=0, z=5), | ||
carla.Rotation(pitch=15, yaw=0, roll=0)), Atment_SpringArmGhost) | ||
] | ||
|
||
# 拼接两个transform_list | ||
spectator_transform_list = Vehicle_transform_list + Camera_transform_list | ||
|
||
# 上帝视图坐标系以及所有camera对象填入spectator_obj_list; | ||
for spectator_transform_index in spectator_transform_list: | ||
|
||
# spectator_transform_list第0个元素为上帝视图坐标系 | ||
if spectator_transform_list.index(spectator_transform_index) == 0: | ||
spectator_obj_list.append(spectator_transform_index) | ||
|
||
# spectator_transform_list其余元素为Camera安装参数,下面生成Camera对象 | ||
else: | ||
camera = world.spawn_actor(camera_bp, spectator_transform_index[0], | ||
attach_to=vehicle, attachment_type=spectator_transform_index[1]) | ||
spectator_obj_list.append(camera) | ||
``` | ||
|
||
更新代理、切换视角、处理代理局部规划器中的路径点。 | ||
|
||
```python | ||
# 设置Vehicle_transform_list[0]为初始视图(上帝视图); | ||
spectator_obj = Vehicle_transform_list[0] | ||
c = 1 | ||
|
||
while True: | ||
agent._update_information() | ||
|
||
world.tick() | ||
|
||
if len(agent._local_planner._waypoints_queue) < 1: | ||
time.sleep(2) | ||
print('======== Success=============') | ||
break | ||
|
||
# top view | ||
spectator = world.get_spectator() | ||
transform = vehicle.get_transform() | ||
if c: | ||
# 上一个spectator的索引号; | ||
last_spectator_obj_index = spectator_obj_list.index(spectator_obj) | ||
# 计算下一个spectator的索引,如果列表索引超限则重新拿第0个spectator; | ||
spectator_obj_index = last_spectator_obj_index + 1 if len( | ||
spectator_obj_list) - last_spectator_obj_index - 1 > 0 else 0 | ||
spectator_obj = spectator_obj_list[spectator_obj_index] | ||
time.sleep(0.2) | ||
|
||
# 更新视图 | ||
if spectator_obj_list.index(spectator_obj) == 0: | ||
# 设置上帝视图 | ||
Vehicle_transform = carla.Transform(vehicle.get_transform().location + spectator_obj_list[0][0], | ||
spectator_obj_list[0][1]) | ||
world.get_spectator().set_transform(Vehicle_transform) | ||
else: | ||
# 设置其他Camera视图 | ||
world.get_spectator().set_transform(spectator_obj.get_transform()) | ||
c = 0 | ||
|
||
# speed_limit = vehicle.get_speed_limit() | ||
# agent.get_local_planner().set_speed(50) | ||
control = agent.run_step(debug=True) | ||
vehicle.apply_control(control) | ||
``` |
Oops, something went wrong.