-
Notifications
You must be signed in to change notification settings - Fork 0
/
wrapped_carla_sim.py
executable file
·104 lines (86 loc) · 3.49 KB
/
wrapped_carla_sim.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
from __future__ import print_function
import argparse
import logging
import random
import time
import math
try:
import pygame
from pygame.locals import K_DOWN
from pygame.locals import K_LEFT
from pygame.locals import K_RIGHT
from pygame.locals import K_SPACE
from pygame.locals import K_UP
from pygame.locals import K_a
from pygame.locals import K_d
from pygame.locals import K_p
from pygame.locals import K_q
from pygame.locals import K_r
from pygame.locals import K_s
from pygame.locals import K_w
from pygame.locals import K_l
except ImportError:
raise RuntimeError('cannot import pygame, make sure pygame package is installed')
try:
import numpy as np
except ImportError:
raise RuntimeError('cannot import numpy, make sure numpy package is installed')
from carla import image_converter
from carla import sensor
from carla.client import make_carla_client, VehicleControl
from carla.planner.map import CarlaMap
from carla.settings import CarlaSettings
from carla.tcp import TCPConnectionError
from carla.util import print_over_same_line
import sys
sys.path.append('controller/')
from controller import *
def make_carla_settings(args):
"""Make a CarlaSettings object with the settings we need."""
settings = CarlaSettings()
settings.set(
SynchronousMode=True,
SendNonPlayerAgentsInfo=True,
NumberOfVehicles=0,
NumberOfPedestrians=0,
SeedVehicles = '00000',
WeatherId=1,
QualityLevel=args.quality_level)
settings.randomize_seeds()
return settings
class CarlaGame(object):
def __init__(self, carla_client, args):
self.client = carla_client
self._carla_settings = make_carla_settings(args)
self._control = VehicleControl()
self._control.throttle = 0.58
self._startID = [0,2,4,6,11,13,17,19,21,24,30,39,55,57,66,70]
self.state = State()
def _initialize_game(self):
self._on_new_episode()
def _on_new_episode(self):
scene = self.client.load_settings(self._carla_settings)
player_start = self._startID[random.randint(0,15)]
player_start = 0
print('Starting new episode...',player_start)
self.client.start_episode(player_start)
def initialize_game(self):
self._initialize_game()
def new_game(self):
self._on_new_episode()
def frame_step(self, ste, thro, bra):
measurements, sensor_data = self.client.read_data()
self._main_image = sensor_data.get('CameraRGB', None)
transform = measurements.player_measurements.transform
x,y,z = transform.location.x, transform.location.y, transform.location.z
roll,pitch,yaw = transform.rotation.roll, transform.rotation.pitch, math.radians(transform.rotation.yaw)
acceleration = measurements.player_measurements.acceleration
forward_speed = measurements.player_measurements.forward_speed
steer, brake, throttle = self._control.steer, self._control.brake, self._control.throttle
intersection_offroad = measurements.player_measurements.intersection_offroad
self.state = State(x=x, y=y, z=z, yaw=yaw, pitch=pitch, roll=roll, v=forward_speed, steer = self._control.steer, brake = self._control.brake, throttle = self._control.throttle, acc = acceleration, offroad = intersection_offroad)
self._control.steer = ste
self._control.brake = bra
self._control.throttle = thro
self.client.send_control(self._control)
return self.state