Skip to content

Commit

Permalink
course (#22)
Browse files Browse the repository at this point in the history
* course

* course

---------

Co-authored-by: zoudai <2184819232>
  • Loading branch information
jiandaoshou-aidehua authored Jul 7, 2024
1 parent c52f7da commit c7d311c
Show file tree
Hide file tree
Showing 11 changed files with 319 additions and 43 deletions.
139 changes: 139 additions & 0 deletions docs/course/locate_moving.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
# 车辆按地图选点进行移动

​ 此示例支持在 **pygame** 窗口对显示区域进行车辆生成并进行移动。

## **环境依赖**

- **python 3.7.9**
- **carla 0.9.15**
- **[湖工商场景](https://pan.baidu.com/s/15T1hGoWJ70tVmsTX7-zcSw?pwd=hutb )(WindowsNoEditor)**
- **[运行 locate_moving.py](../../src/course/locate_moving.py)**



## CarlaSimulator 类

​ 该类主要完成设置相机、处理相机图像、将Pygame坐标转换为CARLA世界坐标。



### 函数初始化__init__

​ 初始化函数,设置模拟器窗口的宽度和高度,连接CARLA服务器,并获取世界和蓝图库。

```
def __init__(self, width, height):
self.width = width
self.height = height
self.client = carla.Client('localhost', 2000)
self.client.set_timeout(10.0)
self.world = self.client.get_world()
self.blueprint_library = self.world.get_blueprint_library()
self.vehicle_bp = self.blueprint_library.filter('vehicle.*')[0]
self.camera = None
self.vehicles = []
self.fov = VIEW_FOV
```

### setup_camera函数

​ 设置相机,定义相机的蓝图、分辨率、视场角(FOV),并将相机生成在指定的位置和角度。

```
def setup_camera(self):
camera_bp = self.blueprint_library.find('sensor.camera.rgb')
camera_bp.set_attribute('image_size_x', str(self.width))
camera_bp.set_attribute('image_size_y', str(self.height))
camera_bp.set_attribute('fov', str(self.fov))
camera_transform = carla.Transform(carla.Location(x=0, y=0, z=50), carla.Rotation(yaw=270, pitch=-90))
self.camera = self.world.spawn_actor(camera_bp, camera_transform)
```

### process_image函数

​ 处理相机捕获的图像,将其转换为Pygame可显示的格式。

```
def process_image(self, image):
array = np.frombuffer(image.raw_data, dtype=np.uint8)
array = array.reshape((self.height, self.width, 4))
array = array[:, :, :3] # Remove alpha channel
array = array[:, :, ::-1] # Convert BGR to RGB
surface = pygame.surfarray.make_surface(array.swapaxes(0, 1))
return surface
```

### convert_pygame_to_world函数

​ 将Pygame窗口中的坐标转换为CARLA世界坐标。主要通过相机的视角和Pygame窗口的大小进行计算。

```
def convert_pygame_to_world(self, x, y):
cam_transform = self.camera.get_transform()
cam_loc = cam_transform.location
aspect_ratio = self.width / self.height
camera_fov_rad = np.radians(self.fov)
camera_fov_y = 2 * np.arctan(np.tan(camera_fov_rad / 2) / aspect_ratio)
world_units_per_pixel_x = (2 * cam_loc.z * np.tan(camera_fov_rad / 2)) / self.width
world_units_per_pixel_y = (2 * cam_loc.z * np.tan(camera_fov_y / 2)) / self.height
relative_x = (x - self.width / 2) * world_units_per_pixel_x
relative_y = (y - self.height / 2) * world_units_per_pixel_y
world_x = cam_loc.x + relative_x
world_y = cam_loc.y - relative_y
return world_x, world_y
```

### run函数

​ 运行模拟器,初始化Pygame窗口,设置相机监听器,并处理Pygame事件,如点击事件生成车辆。

```
def run(self):
pygame.init()
display = pygame.display.set_mode((self.width, self.height), pygame.HWSURFACE | pygame.DOUBLEBUF)
clock = pygame.time.Clock()
self.setup_camera()
self.camera.listen(lambda image: display.blit(self.process_image(image), (0, 0)))
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.MOUSEBUTTONDOWN:
x, y = event.pos
world_x, world_y = self.convert_pygame_to_world(x, y)
spawn_point = carla.Transform(carla.Location(x=world_x, y=-world_y, z=1), carla.Rotation())
vehicle = self.world.spawn_actor(self.vehicle_bp, spawn_point)
vehicle.set_autopilot()
self.vehicles.append(vehicle)
pygame.display.flip()
clock.tick(30)
for vehicle in self.vehicles:
vehicle.destroy()
self.camera.destroy()
pygame.quit()
```



![](../img/traffic_course_img/locate_moving.gif)





20 changes: 13 additions & 7 deletions docs/course/microscopic_Intersection_demo.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@

此脚本连接到CARLA仿真环境,并定期切换湖工商附近的四个预设路口视角。通过不断改变观众(Spectator)的视角,用户可以观察到不同路口的交通状况。

#### **环境要求**

- Python 3.6 及以上版本

- CARLA 仿真环境

- 安装必要的Python库:
- #### **环境要求**

- Python 3.6 及以上版本

- CARLA 仿真环境

- 安装必要的Python库:

- **[spectator.py](https://github.com/OpenHUTB/carla_doc/blob/master/src/course/spectator/spectator.py)**

- [**湖工商场景**](https://pan.baidu.com/s/15T1hGoWJ70tVmsTX7-zcSw?pwd=hutb )**(WindowsNoEditor)**,并运行**[generate_traffic.py](https://github.com/OpenHUTB/carla_doc/blob/master/src/examples/generate_traffic.py)**

- 安装必要的python库

```
pip install carla
Expand Down
10 changes: 6 additions & 4 deletions docs/course/multi-view_ camera.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

此脚本连接到CARLA仿真环境,在车辆的左右两侧设置相机,并使用Pygame显示相机捕获的实时图像。系统采用多进程架构,确保左右相机图像分别在不同窗口中实时显示。

#### **环境要求**
- #### **环境要求**

- Python 3.6 及以上版本
- CARLA 仿真环境
- 安装必要的Python库:
- Python 3.6 及以上版本
- CARLA 仿真环境
- 安装必要的Python库:
- [**camera.py**](https://github.com/OpenHUTB/carla_doc/blob/master/src/course/camera.py)
- [**湖工商场景**](https://pan.baidu.com/s/15T1hGoWJ70tVmsTX7-zcSw?pwd=hutb )**(WindowsNoEditor)**

```
pip install carla pygame numpy opencv-python
Expand Down
40 changes: 24 additions & 16 deletions docs/course/navigation.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

​ 车辆运动支持定义车辆运动起点和目标位置,为每辆车指定随机速度值,使其沿轨迹移动时不会发生碰撞或模拟碰撞,看到车辆沿指定轨迹行驶以到达其目标位置。

​ 本示例设计了一个**automatic_control_revised.py**脚本进行实现
​ 本示例设计了一个 [**automatic_control_revised.py**](https://github.com/OpenHUTB/carla_doc/blob/master/src/course/navigation/automatic_control_revised.py) 脚本进行实现

![](../img/traffic_course_img/4.gif)

**注意** 需要将**agents**文件夹位置添加到代码环境变量中,或者复制该文件夹使其与本示例脚本同级。
**注意** 需要将 [**agents**](https://github.com/OpenHUTB/carla_doc/tree/master/src/carla_agent) 文件夹位置添加到代码环境变量中,或者复制该文件夹使其与本示例脚本同级。

[**湖工商场景**](https://pan.baidu.com/s/15T1hGoWJ70tVmsTX7-zcSw?pwd=hutb )**(WindowsNoEditor)**



Expand All @@ -28,18 +30,20 @@ diming = {
​ 定义命令行参数

```python
argparser = argparse.ArgumentParser(
description=__doc__)
argparser.add_argument(
'--s',
default="北门",
type=str)
argparser.add_argument(
'--e',
default="西门",
type=str)
args = argparser.parse_args()

argparser = argparse.ArgumentParser(
description=__doc__)
argparser.add_argument(
'--s',
default="北门",
type=str)
argparser.add_argument(
'--e',
default="西门",
type=str)
argparser.add_argument(
'--speed',
default=5,
type=int)
```

​ 获取仿真时间对象及设置
Expand Down Expand Up @@ -76,9 +80,11 @@ vehicle = world.spawn_actor(ego_vehicle_bp, spawn_point)
world.tick()
```

​ 生成终点,给起点和终点生成一个车辆运行轨迹
​ 生成终点,给起点和终点生成一个车辆运行轨迹,定义初始速度

```python
# 设置初始速度
vehicle.set_target_velocity(carla.Vector3D(args.speed, 0, 0))
# 创建代理
agent = BehaviorAgent(vehicle, behavior='normal')
destination = carla.Location(locations[args.e])
Expand Down Expand Up @@ -167,4 +173,6 @@ while True:
# agent.get_local_planner().set_speed(50)
control = agent.run_step(debug=True)
vehicle.apply_control(control)
```
```

![](../img/traffic_course_img/5.png)
4 changes: 3 additions & 1 deletion docs/course/object_detection.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# 基于Carla仿真环境的目标检测

**object_dection.py** 脚本主要是在 **Carla** 中使用 **YOLOv3** 检测车辆。[相关链接](https://openhutb.github.io/carla_doc/algorithms/perception/)
[**object_dection.py**](https://github.com/OpenHUTB/carla_doc/blob/master/src/course/object_detection/object_detection.py) 脚本主要是在 **Carla** 中使用 **YOLOv3** 检测车辆。[相关链接](https://openhutb.github.io/carla_doc/algorithms/perception/)

[**湖工商场景**](https://pan.baidu.com/s/15T1hGoWJ70tVmsTX7-zcSw?pwd=hutb )**(WindowsNoEditor)**,并运行**[generate_traffic.py](https://github.com/OpenHUTB/carla_doc/blob/master/src/examples/generate_traffic.py)**

![](../img/traffic_course_img/3.gif)

Expand Down
4 changes: 2 additions & 2 deletions docs/course/traffic_indicators.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# 路口评价指标分析计算(HutbCarlaCity)

为支持高保正的十字路口三维建模,对路口交通流量、路口车均延误、路口饱和、排队长度四种路口真实性评价指标分析。
为支持 [**湖工商场景**](https://pan.baidu.com/s/15T1hGoWJ70tVmsTX7-zcSw?pwd=hutb ) **(WindowsNoEditor)** 高保正的十字路口三维建模,设计 **[traffic_indicators.py](https://github.com/OpenHUTB/carla_doc/blob/master/src/course/traffic%20indicators.py)** 脚本对路口交通流量、路口车均延误、路口饱和、排队长度四种路口真实性评价指标分析。

**首先需要对场景中添加车辆,运行generate_traffic.py脚本,生成足够的车辆**
**首先需要对场景中添加车辆,运行[generate_traffic.py](https://github.com/OpenHUTB/carla_doc/blob/master/src/examples/generate_traffic.py)脚本,生成足够的车辆**



Expand Down
4 changes: 2 additions & 2 deletions docs/course/trajectory_tracking.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# 轨迹跟踪 Drive.py

​ 本示例使用 **Carla** 模拟器来执行轨迹跟踪任务,主要功能包括连接CARLA服务器、设置模拟环境、生成车辆、读取和处理路径点数据、控制车辆运动、记录和保存仿真结果下面对于主要函数进行介绍。

​ 本示例使用 **Carla** 模拟器来执行轨迹跟踪任务, [**Drive.py**](https://github.com/OpenHUTB/carla_doc/blob/master/src/course/trajectory_tracking/Drive.py) 主要功能包括连接CARLA服务器、设置模拟环境、生成车辆、读取和处理路径点数据、控制车辆运动、记录和保存仿真结果下面对于主要函数进行介绍。

基于 [**湖工商场景**](https://pan.baidu.com/s/15T1hGoWJ70tVmsTX7-zcSw?pwd=hutb )**(WindowsNoEditor)**,首先需要运行[generate_traffic.py](https://github.com/OpenHUTB/carla_doc/blob/master/src/examples/generate_traffic.py)

## 定义CARLA设置

Expand Down
Binary file added docs/img/traffic_course_img/5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/img/traffic_course_img/locate_moving.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
107 changes: 107 additions & 0 deletions src/course/locate_moving.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import carla
import pygame
import numpy as np
import random

# 设置Pygame窗口大小
VIEW_WIDTH = 1920 // 2
VIEW_HEIGHT = 1080 // 2
VIEW_FOV = 90


class CarlaSimulator:
def __init__(self, width, height):
self.width = width
self.height = height
self.client = carla.Client('localhost', 2000)
self.client.set_timeout(10.0)
self.world = self.client.get_world()
self.blueprint_library = self.world.get_blueprint_library()
self.vehicle_bp = self.blueprint_library.filter('vehicle.*')[0]
self.camera = None
self.vehicles = []
self.fov = VIEW_FOV

def setup_camera(self):
camera_bp = self.blueprint_library.find('sensor.camera.rgb')
camera_bp.set_attribute('image_size_x', str(self.width))
camera_bp.set_attribute('image_size_y', str(self.height))
camera_bp.set_attribute('fov', str(self.fov))
camera_transform = carla.Transform(carla.Location(x=0, y=0, z=50), carla.Rotation(yaw=270, pitch=-90))
self.camera = self.world.spawn_actor(camera_bp, camera_transform)

def process_image(self, image):
array = np.frombuffer(image.raw_data, dtype=np.uint8)
array = array.reshape((self.height, self.width, 4))
array = array[:, :, :3] # Remove alpha channel
array = array[:, :, ::-1] # Convert BGR to RGB
surface = pygame.surfarray.make_surface(array.swapaxes(0, 1))
return surface

def convert_pygame_to_world(self, x, y):
# 获取相机位置和旋转
cam_transform = self.camera.get_transform()
cam_loc = cam_transform.location

# 计算相机的水平和垂直视角
aspect_ratio = self.width / self.height
camera_fov_rad = np.radians(self.fov)
camera_fov_y = 2 * np.arctan(np.tan(camera_fov_rad / 2) / aspect_ratio)

# 计算每个像素在实际世界中的对应距离
world_units_per_pixel_x = (2 * cam_loc.z * np.tan(camera_fov_rad / 2)) / self.width
world_units_per_pixel_y = (2 * cam_loc.z * np.tan(camera_fov_y / 2)) / self.height

# 将Pygame坐标转换为相对于图像中心的坐标
relative_x = (x - self.width / 2) * world_units_per_pixel_x
relative_y = (y - self.height / 2) * world_units_per_pixel_y

# 转换为世界坐标
world_x = cam_loc.x + relative_x
world_y = cam_loc.y - relative_y # 注意这里是减去,因为y轴方向是相反的

return world_x, world_y

def run(self):
pygame.init()
display = pygame.display.set_mode((self.width, self.height), pygame.HWSURFACE | pygame.DOUBLEBUF)
clock = pygame.time.Clock()

self.setup_camera()
self.camera.listen(lambda image: display.blit(self.process_image(image), (0, 0)))

spectator = self.world.get_spectator()

running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.MOUSEBUTTONDOWN:
x, y = event.pos
# 将Pygame坐标转换为CARLA世界坐标
world_x, world_y = self.convert_pygame_to_world(x, y)
# print(f'World coordinates: ({world_x}, {world_y})')

# self.world.debug.draw_string(carla.Location(x=world_x, y=-world_y, z=1), 'ss', life_time=1000,
# color=carla.Color(255, 0, 0))
spawn_point = carla.Transform(carla.Location(x=world_x, y=-world_y, z=1), carla.Rotation())
vehicle = self.world.spawn_actor(self.vehicle_bp, spawn_point)
vehicle.set_autopilot()
# if self.vehicles is not None:
# last_vehicle_transform = self.vehicles[-1].get_transform()
# spectator.set_transform(last_vehicle_transform)
self.vehicles.append(vehicle)

pygame.display.flip()
clock.tick(30)

for vehicle in self.vehicles:
vehicle.destroy()
self.camera.destroy()
pygame.quit()


if __name__ == '__main__':
simulator = CarlaSimulator(VIEW_WIDTH, VIEW_HEIGHT)
simulator.run()
Loading

0 comments on commit c7d311c

Please sign in to comment.