본문 바로가기
Carla 프로젝트

CARLA simulator 설치 및 사용법

by whiteTommy 2024. 1. 10.

 

앞서, Unreal Engine에서 CARLA 설치를 하였지만, 용량 문제로 다른 방법으로 Carla Simulator를 통해 차량 주행 시뮬레이션을 살펴보고자 한다. Python 코드를 바탕으로 CARLA simulator 를 통해 실행하는 방법을 알아볼 것이다.

 

https://whitehacking.tistory.com/28

 

Unreal Engine- CARLA 설치 [윈도우(Window) 버전]

Carla : 오픈소스 자율주행 시뮬레이터이다. 이는 Unreal Engine을 사용하며 가상 환경에서 LIDAR, RADAR, Camera 등 여러 가지 센서를 통해 데이터를 수집하고 실행해 볼 수 있다. Ubuntu와 다르게 Window에서

whitehacking.tistory.com

 

요구 사항

  • Anaconda Prompt
  • Carla simulator
  • python 3.7 버전

 

https://www.anaconda.com/download

 

Free Download | Anaconda

Anaconda's open-source Distribution is the easiest way to perform Python/R data science and machine learning on a single machine.

www.anaconda.com

 

https://carla.org/2023/11/10/release-0.9.15/

 

CARLA 0.9.15 Release

SimReady and NVIDIA Omniverse, new towns 13 and 15, procedural map generation tools

carla.org

https://gist.github.com/j2doll/44eceb5e991085b7dc5bd03303fd6096

 

Python 3.7 설치하기 (윈도우즈)

Python 3.7 설치하기 (윈도우즈). GitHub Gist: instantly share code, notes, and snippets.

gist.github.com

 

1. CarlaUE4 실행

 

 

 

2. Anaconda Prompt 실행

 

전에 설치했던 pythonAPI에 examples에 들어가보면 많은 python 파일이 들어있다. 이를 각각 실행할 수 있다.

 

아래 명령어 입력

cd C:\Users\admin\Downloads\CARLA_0.9.14\WindowsNoEditor\PythonAPI\examples
conda create --name carla-sim python=3.7

activate carla-sim

pip install carla
pip install pygame
pip install numpy
pip install jupyter
pip install opencv-python

 

위의 파일 중에서 dynamic_weather.py을 실행해보자.

python dynamic_weather.py

 

날씨에 대한 코드가 실행되고 있음을 알 수 잇다.

 

이 Python 코드에 대한 세부 코드 내용이다.

#!/usr/bin/env python

# Copyright (c) 2019 Computer Vision Center (CVC) at the Universitat Autonoma de
# Barcelona (UAB).
#
# This work is licensed under the terms of the MIT license.
# For a copy, see <https://opensource.org/licenses/MIT>.

"""
CARLA Dynamic Weather:

Connect to a CARLA Simulator instance and control the weather. Change Sun
position smoothly with time and generate storms occasionally.
"""

import glob
import os
import sys

try:
    sys.path.append(glob.glob('../carla/dist/carla-*%d.%d-%s.egg' % (
        sys.version_info.major,
        sys.version_info.minor,
        'win-amd64' if os.name == 'nt' else 'linux-x86_64'))[0])
except IndexError:
    pass

import carla

import argparse
import math


def clamp(value, minimum=0.0, maximum=100.0):
    return max(minimum, min(value, maximum))


class Sun(object):
    def __init__(self, azimuth, altitude):
        self.azimuth = azimuth
        self.altitude = altitude
        self._t = 0.0

    def tick(self, delta_seconds):
        self._t += 0.008 * delta_seconds
        self._t %= 2.0 * math.pi
        self.azimuth += 0.25 * delta_seconds
        self.azimuth %= 360.0
        self.altitude = (70 * math.sin(self._t)) - 20

    def __str__(self):
        return 'Sun(alt: %.2f, azm: %.2f)' % (self.altitude, self.azimuth)


class Storm(object):
    def __init__(self, precipitation):
        self._t = precipitation if precipitation > 0.0 else -50.0
        self._increasing = True
        self.clouds = 0.0
        self.rain = 0.0
        self.wetness = 0.0
        self.puddles = 0.0
        self.wind = 0.0
        self.fog = 0.0

    def tick(self, delta_seconds):
        delta = (1.3 if self._increasing else -1.3) * delta_seconds
        self._t = clamp(delta + self._t, -250.0, 100.0)
        self.clouds = clamp(self._t + 40.0, 0.0, 90.0)
        self.rain = clamp(self._t, 0.0, 80.0)
        delay = -10.0 if self._increasing else 90.0
        self.puddles = clamp(self._t + delay, 0.0, 85.0)
        self.wetness = clamp(self._t * 5, 0.0, 100.0)
        self.wind = 5.0 if self.clouds <= 20 else 90 if self.clouds >= 70 else 40
        self.fog = clamp(self._t - 10, 0.0, 30.0)
        if self._t == -250.0:
            self._increasing = True
        if self._t == 100.0:
            self._increasing = False

    def __str__(self):
        return 'Storm(clouds=%d%%, rain=%d%%, wind=%d%%)' % (self.clouds, self.rain, self.wind)


class Weather(object):
    def __init__(self, weather):
        self.weather = weather
        self._sun = Sun(weather.sun_azimuth_angle, weather.sun_altitude_angle)
        self._storm = Storm(weather.precipitation)

    def tick(self, delta_seconds):
        self._sun.tick(delta_seconds)
        self._storm.tick(delta_seconds)
        self.weather.cloudiness = self._storm.clouds
        self.weather.precipitation = self._storm.rain
        self.weather.precipitation_deposits = self._storm.puddles
        self.weather.wind_intensity = self._storm.wind
        self.weather.fog_density = self._storm.fog
        self.weather.wetness = self._storm.wetness
        self.weather.sun_azimuth_angle = self._sun.azimuth
        self.weather.sun_altitude_angle = self._sun.altitude

    def __str__(self):
        return '%s %s' % (self._sun, self._storm)


def main():
    argparser = argparse.ArgumentParser(
        description=__doc__)
    argparser.add_argument(
        '--host',
        metavar='H',
        default='127.0.0.1',
        help='IP of the host server (default: 127.0.0.1)')
    argparser.add_argument(
        '-p', '--port',
        metavar='P',
        default=2000,
        type=int,
        help='TCP port to listen to (default: 2000)')
    argparser.add_argument(
        '-s', '--speed',
        metavar='FACTOR',
        default=1.0,
        type=float,
        help='rate at which the weather changes (default: 1.0)')
    args = argparser.parse_args()

    speed_factor = args.speed
    update_freq = 0.1 / speed_factor

    client = carla.Client(args.host, args.port)
    client.set_timeout(2.0)
    world = client.get_world()

    weather = Weather(world.get_weather())

    elapsed_time = 0.0

    while True:
        timestamp = world.wait_for_tick(seconds=30.0).timestamp
        elapsed_time += timestamp.delta_seconds
        if elapsed_time > update_freq:
            weather.tick(speed_factor * elapsed_time)
            world.set_weather(weather.weather)
            sys.stdout.write('\r' + str(weather) + 12 * ' ')
            sys.stdout.flush()
            elapsed_time = 0.0


if __name__ == '__main__':

    main()

 

추가적으로, 직접 소스 코드를 작성해서 Carla Simulator를 통해 주행 데이터를 살펴보고자 하려면 앞서 언급한 examples 파일에 코드를 넣고, python [python 파일명]을 anaconda prompt를 통해 실행해볼 수 있다.

 

'Carla 프로젝트' 카테고리의 다른 글

Unreal Engine- CARLA 설치 [윈도우(Window) 버전]  (5) 2024.01.10