-
Notifications
You must be signed in to change notification settings - Fork 0
/
carla_world_practice.py
198 lines (117 loc) · 4.57 KB
/
carla_world_practice.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#!/usr/bin/env python
# coding: utf-8
# In[1]:
# import carla
# print("import carla success")
# import sys
# print(sys.version)
# In[2]:
# import carla
# import random
# # ./CarlaUE4.sh -quality-level=Low
# # ./CarlaUE4.sh -quality-level=epic
# # ./CarlaUE4.sh -RenderOffScreen
# # Connect to the client and retrieve the world object
# client = carla.Client('localhost', 2000)
# world = client.get_world()
# # Setting synchronous mode
# # By default, CARLA runs in asynchronous mode.
# # Synchronous mode must be enabled before loading or reloading the world:
# # Differing timestamps can arise if the world is not in synchronous mode
# # from the very beginning. This can generate small differences in physics
# # simulation and in the life cycle of objects such as traffics lights.
# settings = world.get_settings()
# settings.synchronous_mode = True # Enables synchronous mode
# settings.fixed_delta_seconds = 0.02 # (default is 0.05)
# world.apply_settings(settings)
# # set no_rendering_mode
# settings = world.get_settings()
# settings.no_rendering_mode = True
# world.apply_settings(settings)
# # settings.no_rendering_mode = False
# # world.apply_settings(settings)
# #imoprt map
# client.load_world('Town01')
# # get blueprint
# level = world.get_map()
# weather = world.get_weather()
# blueprint_library = world.get_blueprint_library()
# step = 0
# while step <= 10:
# world.tick()
# print(step)
# step += 1
# # carla.terminate() ???
# In[3]:
import carla
import random
# Connect to the client and retrieve the world object
client = carla.Client('localhost', 2000)
world = client.get_world()
settings = world.get_settings()
# set rendering mode
# settings.no_rendering_mode = True
settings.synchronous_mode = True # Enables synchronous mode
settings.fixed_delta_seconds = 0.05 # (default is 0.05)
world.apply_settings(settings)
# load map
client.load_world('Town01')
# In[4]:
# get blueprint
level = world.get_map()
weather = world.get_weather()
blueprint_library = world.get_blueprint_library()
# Get the blueprint library and filter for the vehicle blueprints
vehicle_blueprints = world.get_blueprint_library().filter('*vehicle*')
# Get the map's spawn points
spawn_points = world.get_map().get_spawn_points()
# In[5]:
# Spawn 50 vehicles randomly distributed throughout the map
# for each spawn point, we choose a random vehicle from the blueprint library
# for i in range(0,10):
# world.try_spawn_actor(random.choice(vehicle_blueprints), random.choice(spawn_points))
# In[6]:
# print(spawn_points)
ego_vehicle_spawn_point = carla.Transform(carla.Location(x=151.119736, y=198.759842, z=0.299438), carla.Rotation(pitch=0.000000, yaw=0.000000, roll=0.000000))
# ego_vehicle = world.spawn_actor(random.choice(vehicle_blueprints.filter('tesla')), ego_vehicle_spawn_point)
# ego_vehicle = world.spawn_actor(blueprint_library.find('vehicle.tesla.model3'), ego_vehicle_spawn_point)
ego_vehicle = world.spawn_actor(blueprint_library.find('vehicle.ford.mustang'), ego_vehicle_spawn_point)
# In[7]:
# # Create a transform to place the camera on top of the vehicle
# camera_init_trans = carla.Transform(carla.Location(z=1.5))
# # We create the camera through a blueprint that defines its properties
# camera_bp = world.get_blueprint_library().find('sensor.camera.rgb')
# # We spawn the camera and attach it to our ego vehicle
# camera = world.spawn_actor(camera_bp, camera_init_trans, attach_to=ego_vehicle)
# # Start camera with PyGame callback
# camera.listen(lambda image: image.save_to_disk('out/%06d.png' % image.frame))
# for vehicle in world.get_actors().filter('*vehicle*'):
# if vehicle != ego_vehicle:
# vehicle.set_autopilot(True)
# Transform(Location(x=151.119736, y=198.759842, z=0.299438), Rotation(pitch=0.000000, yaw=0.000000, roll=0.000000))
spectator = world.get_spectator()
while True:
transform = ego_vehicle.get_transform()
spectator.set_transform(carla.Transform(transform.location + carla.Location(x=-50, y=0,z=20), carla.Rotation(pitch=0)))
# print(transform)
# ego_vehicle.set_autopilot(True)
ego_vehicle.apply_control(carla.VehicleControl(throttle=0.5, steer=0.1))
world.tick()
# In[ ]:
# In[ ]:
# town = 'Town0'
# num = 1
# for i in range(5):
# mapname = town+str(num+i)
# print(mapname)
# client.load_world(mapname)
# # get blueprint
# level = world.get_map()
# weather = world.get_weather()
# blueprint_library = world.get_blueprint_library()
# step = 0
# while step <= 10:
# world.tick()
# print(step)
# step += 1
# carla.terminate()