Skip to main content
Skip table of contents

[예제]MSC API 및 UDP로 시뮬레이터 제어하기

본 섹션에서는 MSC API와 UDP 통신으로 MORAI Simulator를 제어하는 방법에 대해서 설명한다.

MSC API를 사용하여 MORAI Simulator를 실행 후, UDP 통신을 적용한 예제 코드를 통해 차량 주행 테스트까지 진행한다.


사전 요구 사항

UDP 통신을 적용한 MSC API를 사용하기 위하여 필요한 설치 프로그램 정보는 아래와 같다.

  • Python 3.7.5 이상

  • MORAI SIM(버전 상관 없음)

예제 진행 절차

본 섹션에서 다루는 예제를 아래의 절차대로 진행한다.

Step 1: MORAI Launcher 실행

MORAI Simulator Control API | Step-1:-MORAI-Launcher-실행 방법과 동일하다.

Step 2: 예제 코드 다운로드

아래 경로의 예제 코드를 다운로드한다.

Step 3: 예제 코드로 MORAI Simulator 실행

1] 파라미터 정보 수정

아래의 사항을 고려하여 다운로드한 예제 코드의 파라미터를 수정한다.

  • 시뮬레이터 실행과 예제 코드를 실행하는 사용자 환경이 다른 경우(IP가 다른 환경), params.txt 파일 내 관련 IP 정보를 수정해야 한다.

  • params.txt 내 id, pw, version , map, vehicle 과 같은 파라미터를 사용자 환경에 맞게 기입한다(대소문자 구분).
    예:
    receive_user_ip : 127.0.0.1
    receive_user_port : 10326
    request_dst_ip : 127.0.0.1
    request_dst_port : 10508
    user_id : id
    user_pw : pw
    version : v.R1.240407.0
    map : R_KR_PG_K-City
    vehicle : 2017_Kia_Niro(HEV)
    network_file : udp_network
    Sensor_file :
    Scenario_file :

2] Simulator Network setting

  • Network Setting은 Scenario를 저장할 때의 네트워크 정보를 적용.

  • ‘network_example’ 폴더의 udp_network.json 파일을 ‘MoraiLauncher_Win_Data > SaveFile > Network > (version)’ 경로에 복사한다.

3] 예제 코드 실행

api.py를 실행하면 parmas.txt 내 id, pw ,version, map, vehicle, network_file 정보에 맞추어 MORAI 시뮬레이터를 실행 및 주행 테스트를 진행할 수 있다.

예제 코드 설명

UDP 통신을 적용한 예제 코드에 관한 설명은 아래와 같다.

  • class launcher_start: params.txt 내 파라미터 정보를 읽어 MSC API로 시뮬레이션을 시작하는 class

PY
import time,os
from lib.define import *
from lib.read_text import *
from lib.controller import *

class launcher_start(controller):

    def __init__(self):
        self.controller = controller()

    def launcher_start(self):

        while True:
            if self.controller.update(): #data update
                
                self.controller.is_downloading() #check downloading status

                if self.controller.is_befor_login():
                    self.controller.commander(Command.LOGIN, f'{user_id}/{user_pw}')#Login명령
                        
                if self.controller.is_after_login() or self.controller.is_after_sim_quit_to_launcher():  # is_after_sim_quit_to_launcher : Simulator에서 quit 명령 후 Launcher 복귀 상태 확인
                                        
                    if not self.controller.is_version_selected():
                        self.controller.commander(Command.SELECT_VER, version)#version 선택명령
                        
                        if self.controller.is_not_find_version(): #선택한 버전이 없는 버전인지 확인.                    
                            break
                    
                    if self.controller.is_can_execute_sim():
                        self.controller.commander(Command.EXECUTE_SIM,'') #Simulator 실행 명령
                                                
                    if self.controller.is_sim_not_install():
                        self.controller.commander(Command.INSTALL_SIM,'') #Simulator 설치 명령

                if self.controller.is_sim_lobby():
                    self.controller.commander(Command.MAP_VEHICLE_SELECT, f'{map}/{vehicle}')#시뮬레이션/옵션 변경 실행 명령

                if self.controller.is_sim_playing():
                    if network_file :
                        self.controller.commander(Command.NET_SETTING, network_file) #시뮬레이터 네트워크 세팅 명령
                    if sensor_file:
                        self.controller.commander(Command.SEN_SETTING, sensor_file) #시뮬레이터 센서 세팅 명령
                    if scenario_file:
                        self.controller.commander(Command.SCEN_SETTING, f'{scenario_file}/30') #시뮬레이터 시나리오 세팅 명령
                    '''
                        Scenario Setting Option
                        0x01 : Delete All
                        0x02 : Ego Vehicle
                        0x04 : Surround Vehicle
                        0x08 : Pedestrian
                        0x10 : Obstacle
                        0x20 : Pause (scenario load 후 시뮬레이터 정지(직접 esc키 또는 play command로 해제 가능))
                        원하는 option을 or 연산 해서 사용
                        e.g) Ego vihcle  Surrond Vehicle, Pedestrain, Obstacle을 load하려면
                             0x02 | 0x04 | 0x08 | 0x10  -> 0b0010 | 0b0100 | 0b1000 | 0b0001 0000 -> 30 (0b0001 1110, 0x1E)
                    '''
                    break

            else :                
                print("[NO Simulator Control Data]",end='\r')                

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.