Repository navigation
Expand file tree
/
Copy pathtest2.py
More file actions
104 lines (91 loc) · 3.41 KB
/
Copy pathtest2.py
File metadata and controls
104 lines (91 loc) · 3.41 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
102
103
104
# Required libraries
import time
import carla
import numpy as np
import cv2
# Recording GNSS data
def gnss_callback(gnss):
print("GNSS measure:\n" + str(gnss) + '\n')
print(gnss.latitude)
# Recording IMU data
def imu_callback(imu):
print(imu.accelerometer)
print("IMU measure:\n" + str(imu) + '\n')
# Displaying camera image
def show_me(image):
r = np.array(image.raw_data)
r1 = r.reshape((360, 640, 4)) # Updated resolution
r2 = r1[:, :, :3] # Extract RGB
camera_data["zs"] = r2.astype(np.uint8)
# Connecting with the CARLA client
client = carla.Client("localhost", 2000)
world = client.get_world()
blueprint_library = world.get_blueprint_library()
spawnpoints = world.get_map().get_spawn_points()
allspawn = []
ar = blueprint_library.find("vehicle.mercedes.coupe_2020")
# Spawning the ego vehicle and the spectator
while True:
transforms2 = spawnpoints[0]
if transforms2 not in allspawn:
allspawn.append(transforms2)
break
car = world.try_spawn_actor(ar, allspawn[0])
spectator = world.get_spectator()
x = carla.Transform(car.get_transform().transform(carla.Location(x=-0.4, z=1.8)), car.get_transform().rotation)
spectator.set_transform(x)
time.sleep(0.2)
# Attaching the camera to the ego vehicle
cam = blueprint_library.find("sensor.camera.rgb")
cam.set_attribute("image_size_x", "640") # Reduced resolution
cam.set_attribute("image_size_y", "360")
cam.set_attribute("fov", "110")
cam.set_attribute("sensor_tick", ".2")
transforms = carla.Transform(carla.Location(z=1.8, x=-0.2))
ego_camera = world.spawn_actor(cam, transforms, attach_to=car, attachment_type=carla.AttachmentType.Rigid)
time.sleep(0.2)
# Attaching the GNSS & the IMU to the ego vehicle
gnss = blueprint_library.find("sensor.other.gnss")
gnss.set_attribute("sensor_tick", ".2")
gnss_transform = carla.Transform(carla.Location(0, 0, 0), carla.Rotation(0, 0, 0))
ego_gnss = world.spawn_actor(gnss, gnss_transform, attach_to=car, attachment_type=carla.AttachmentType.Rigid)
time.sleep(0.2)
imu_bp = world.get_blueprint_library().find('sensor.other.imu')
imu_bp.set_attribute("sensor_tick", ".2")
imu_transform = carla.Transform(carla.Location(0, 0, 0), carla.Rotation(0, 0, 0))
ego_imu = world.spawn_actor(imu_bp, imu_transform, attach_to=car, attachment_type=carla.AttachmentType.Rigid)
time.sleep(0.2)
# Car set to autopilot
car.set_autopilot()
time.sleep(0.2)
# Setting record duration for the simulation
Record_Duration = 90
start_time = time.time()
camera_data = {"zs": np.zeros((360, 640, 3), dtype=np.uint8)} # Updated resolution
# Start listening to sensors
ego_camera.listen(lambda image: show_me(image))
ego_gnss.listen(lambda gnss: gnss_callback(gnss))
ego_imu.listen(lambda imu: imu_callback(imu))
# Running the simulation
try:
while time.time() - start_time < Record_Duration:
time.sleep(0.1) # Small delay to reduce CPU usage
except Exception as e:
print(f"Error during simulation: {e}")
finally:
# Cleanup resources
ego_camera.stop()
ego_camera.destroy()
ego_gnss.stop()
ego_gnss.destroy()
ego_imu.stop()
ego_imu.destroy()
car.destroy()
cv2.destroyAllWindows()
print("Simulation ended and resources cleaned up.")
# Display camera feed
while True:
cv2.imshow("RGB CAMERA", camera_data["zs"])
if cv2.waitKey(1) & 0xFF == ord("Q"):
break
cv2.destroyAllWindows()