[Research] Carla Client and Vehicle spawn

 

Carla Client 생성 및 Vehicle 제어

이번 시간에는 Carla 시뮬레이터에서 Python 코드를 사용하여 client를 생성하고 vehicle을 생성하는 방법을 단계별로 살펴보겠다. 다음의 예제를 기반으로 각 명령어의 의미와 초기 build 과정, 그리고 GlobalRoutePlanner의 활용법에 대해 자세히 알아보자.

1. Carla Client 생성 및 Vehicle 배치

Python
import carla 

# Carla client 생성
client = carla.Client('host ip', 2000) 
world = client.get_world() 

# 맵에서 vehicle의 spawn point 가져오기
spawn_points = world.get_map().get_spawn_points() 

# firetruck blueprint 가져오기
vehicle_bp = world.get_blueprint_library().filter("firetruck*") 

# 첫 번째 spawn point에 firetruck 배치
start_point = spawn_points[0] 
vehicle = world.try_spawn_actor(vehicle_bp[0], start_point) 

2. Vehicle 위치 확인 및 이동

Python
# 맵에서 vehicle의 위치 정보 가져오기
vehicle_pos = vehicle.get_transform() 
print(vehicle_pos) Transform(Location(x=-64.644844, y=24.471968, z=-0.001595), Rotation(pitch=0.000020, yaw=0.159199, roll=0.000936))

# 초기 spawn point 정보 출력 (0.6m 높이) print(start_point) Transform(Location(x=-64.644844, y=24.471010, z=0.600000), Rotation(pitch=0.000000, yaw=0.159198, roll=0.000000)) # Autopilot 모드 활성화 vehicle.set_autopilot(True) # Autopilot 모드 주행 후 vehicle 위치 정보 가져오기 (달라졌음을 확인할 수 있다) vehicle_pos = vehicle.get_transform() print(vehicle_pos) Transform(Location(x=-23.173193, y=13.119796, z=-0.003491), Rotation(pitch=-0.021877, yaw=-179.830887, roll=-0.000244))

3. 맵 정보 획득 및 Topology 분석

Python
# 맵 정보 가져오기
town_map = world.get_map() 

# 맵 타입 확인
type(town_map) carla.libcarla.Map

# 맵 정보 출력 (예: Carla/Maps/Town10HD_Opt)
print(town_map) Map(name=Carla/Maps/Town10HD_Opt)


# 도로 정보 (waypoint 쌍) 가져오기
roads = town_map.get_topology() 
print(roads) [(<carla.libcarla.Waypoint object at 0x7f55680f70f0>, <carla.libcarla.Waypoint object at 0x7f55680f7150>), ...,]

# waypoint 정보 확인 (transform 정보 포함)
print(roads[0][0]) 
Waypoint(Transform(Location(x=-27.017662, y=66.214005, z=0.000000), Rotation(pitch=0.000000, yaw=-179.926727, roll=0.000000)))

4. GlobalRoutePlanner를 활용한 경로 계획 및 시각화

Python
import sys 
sys.path.append('/opt/carla-simulator/PythonAPI/carla') 
from agents.navigation.global_route_planner import GlobalRoutePlanner 

# GlobalRoutePlanner 초기화 (sampling_resolution = 2)
sampling_resolution = 2 
grp = GlobalRoutePlanner(town_map, sampling_resolution) 

# 시작 지점 (point_a) 및 목표 지점 (point_b) 설정
point_a = carla.Location(x=-64.644844, y=24.471010, z=0.600000) 
point_b = carla.Location(x=-113.903656, y=14.422489, z=-0.003719) 

# GlobalRoutePlanner를 사용하여 경로 생성
route = grp.trace_route(point_a, point_b) 

# 생성된 경로를 시뮬레이터에 시각화
for waypoint in route: 
    world.debug.draw_string(waypoint[0].transform.location, '^'
                            draw_shadow=False, 
                            color=carla.Color(r=0, g=0, b=255), 
                            life_time=120.0, 
                            persistent_lines=True) 

5. Vehicle 제거

Python
# 시뮬레이터에서 vehicle 및 sensor 제거
for actor in world.get_actors().filter('*vehicle*'): 
    actor.destroy() 

for sensor in world.get_actors().filter('*sensor*'): 
    sensor.destroy() 

초기 Build 과정:

Carla Client를 생성하고 Vehicle을 배치하는 초기 build 과정은 다음과 같다.

  1. carla.Client를 사용하여 Carla 시뮬레이터에 연결한다.
    (여기서 host ip는 상황에 따라 localhost이거나 달라질 수 있다.(이전글 참조))
  2. client.get_world()를 사용하여 현재 시뮬레이션 world를 가져온다.
  3. world.get_map().get_spawn_points()를 사용하여 맵에서 사용 가능한 spawn point들을 가져온다.
  4. world.get_blueprint_library().filter()를 사용하여 원하는 vehicle의 blueprint를 가져온다.
  5. world.try_spawn_actor()를 사용하여 spawn point에 vehicle을 배치한다.
spawn firetruck


GlobalRoutePlanner:

GlobalRoutePlanner는 Carla 시뮬레이터에서 경로 계획을 위한 유용한 도구이다.

  1. GlobalRoutePlanner 클래스를 초기화한다. 이때 sampling_resolution 매개변수를 사용하여 경로 생성 시 샘플링 해상도를 조절할 수 있다.
  2. trace_route() 함수를 사용하여 시작 지점과 목표 지점 사이의 경로를 생성한다.
  3. 생성된 경로는 waypoint들의 리스트로 반환된다. 각 waypoint는 transform 정보를 포함하고 있어 위치, 회전 정보를 쉽게 활용할 수 있다.
    (잘 보이지 않지만 '^' 모양의 waypoint들이 생성된 것을 확인할 수 있다)


결론:

이번 포스팅에서는 Carla 시뮬레이터에서 Python 코드를 사용하여 client를 생성하고 vehicle을 생성하는 방법을 살펴보았다. 초기 build 과정과 GlobalRoutePlanner를 활용한 경로 계획 및 시각화 방법을 익힘으로써 Carla 시뮬레이터를 이용한 자율 주행 시뮬레이션 개발에 한 걸음 더 나아갈 수 있다.

추천글:

[Research] Carla 환경 설정 (server: window + client: ubuntu
(https://hyeondev.blogspot.com/2025/01/research-carla-server-window-client.html)

hyeon_B

안녕하세요! AI 기술을 이용해 더 나은 세상을 만들어 나가고 싶은 과기원생 Hyeon이라고 합니다. 저는 앞으로 인공지능 시대에는 지식을 '활용'하는 능력이 중요해질 것이라고 생각합니다. 대부분의 일들은 인공지능이 뛰어난 모습을 보이지만, 인공지능은 데이터로 부터 연관관계를 학습하기 때문에 지식들을 새로 통합해서 활용하는 능력이 부족합니다. 인공지능이 뉴턴 전에 만들어졌다면 사과가 떨어지는 이유에 대답하지 못했을 것이고, 아인슈타인 전에 만들어졌다면 중력이 어떻게 생기는지 설명하지 못했을 것입니다. 따라서 앞으로 우리는 '본질'을 탐구하고 그 본질로부터 다른 곳에 적용하며 인공지능을 현명하게 활용해야 할 것입니다. 함께 인공지능 시대를 준비합시다!

댓글 쓰기

다음 이전

POST ADS1

POST ADS 2