-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcarla_car_drive_AB.py
More file actions
101 lines (79 loc) · 3.11 KB
/
carla_car_drive_AB.py
File metadata and controls
101 lines (79 loc) · 3.11 KB
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
import glob
import os
import sys
import random
import time
import numpy as np
import cv2
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
IM_WIDTH = 640
IM_HEIGHT = 480
def image(image):
matrix_representational_data = np.array(image.raw_data)
reshape_of_image = matrix_representational_data.reshape(
(IM_HEIGHT, IM_WIDTH, 4))
live_feed_from_camera = reshape_of_image[:, :, :3]
cv2.imshow("", live_feed_from_camera)
cv2.waitKey(1)
return
def camera(get_blueprint_of_world):
camera_sensor = get_blueprint_of_world.find('sensor.camera.rgb')
camera_sensor.set_attribute('image_size_x', f'{IM_WIDTH}')
camera_sensor.set_attribute('image_size_y', f'{IM_HEIGHT}')
camera_sensor.set_attribute('fov', '70')
return camera_sensor
def carControl():
dropped_vehicle.apply_control(carla.VehicleControl(throttle=0.52, gear=0, steer=-1))
time.sleep(5)
dropped_vehicle.apply_control(carla.VehicleControl(throttle=0.5, gear=0))
time.sleep(6)
dropped_vehicle.apply_control(carla.VehicleControl(throttle=0.5, gear=0, steer=-0.17))
time.sleep(2)
dropped_vehicle.apply_control(carla.VehicleControl(throttle=0.5, gear=0, steer=0.14))
time.sleep(9)
dropped_vehicle.apply_control(carla.VehicleControl(throttle=0.4, gear=0, steer=-0.25))
time.sleep(1)
dropped_vehicle.apply_control(carla.VehicleControl(throttle=0.8, gear=0))
time.sleep(4)
dropped_vehicle.apply_control(carla.VehicleControl(hand_brake=True))
time.sleep(5)
return dropped_vehicle.get_location()
data = []
actor_list = []
try:
client = carla.Client('127.0.0.1', 2000)
client.set_timeout(2.0)
world = client.get_world()
get_blueprint_of_world = world.get_blueprint_library()
car_model = get_blueprint_of_world.filter('model3')[0]
spawn_point = (world.get_map().get_spawn_points()[1])
dropped_vehicle = world.spawn_actor(car_model, spawn_point)
simulator_camera_location_rotation = carla.Transform(
spawn_point.location, spawn_point.rotation)
simulator_camera_location_rotation.location += spawn_point.get_forward_vector() * 30
simulator_camera_location_rotation.rotation.yaw += 180
simulator_camera_view = world.get_spectator()
simulator_camera_view.set_transform(simulator_camera_location_rotation)
location = dropped_vehicle.get_location()
print(f"Current location of car is {location}")
camera_sensor = camera(get_blueprint_of_world)
sensor_camera_spawn_point = carla.Transform(carla.Location(x=2.5, z=0.7))
sensor = world.spawn_actor(
camera_sensor, sensor_camera_spawn_point, attach_to=dropped_vehicle)
actor_list.append(sensor)
sensor.listen(lambda camera_data: image(camera_data))
newLocation = carControl()
print(f"New location of car is {newLocation}")
actor_list.append(dropped_vehicle)
finally:
print('destroying actors')
for actor in actor_list:
actor.destroy()
print('done.')