-
Notifications
You must be signed in to change notification settings - Fork 13
/
test_freeway_backend.py
102 lines (78 loc) · 5.87 KB
/
test_freeway_backend.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Jul 26 12:11:46 2020
@author: shijiliu
"""
import sys
sys.path.append("..")
import carla
import time
from backend.carla_env import CARLA_ENV
from backend.section_environment import FreewayEnv
def main():
try:
# create the CARLA_ENV helper
client = carla.Client("localhost",2000)
client.set_timeout(10.0)
world = client.load_world('Town04') # use Town 04 which contains a freeway
# default the weather to be a fine day
weather = carla.WeatherParameters(
cloudiness=10.0,
precipitation=0.0,
sun_altitude_angle=90.0)
world.set_weather(weather)
spectator = world.get_spectator()
spectator.set_transform(carla.Transform(carla.Location(x=-170, y=-151, z=116.5), carla.Rotation(pitch=-33, yaw= 56.9, roll=0.0)))
# create the environment
env = CARLA_ENV(world)
time.sleep(2) # sleep for 2 seconds, wait the initialization to finish
# create a 14 section environment (support up to 7)
freewayenv = FreewayEnv(env,7)
# add ego vehicle
freewayenv.add_ego_vehicle()
freewayenv.edit_ego_vehicle(vehicle_color = '255,255,255')
# add 2 lead vehicle and 2 follow vehicle
name0 = freewayenv.add_full_path_vehicle(gap = 20.0, vehicle_type = "lead", choice = "subject")
name1 = freewayenv.add_full_path_vehicle(gap = 20.0, vehicle_type = "lead", choice = "left")
name2 = freewayenv.add_full_path_vehicle(vehicle_type = "follow", choice = "subject")
name3 = freewayenv.add_full_path_vehicle(vehicle_type = "follow", choice = "left")
name4 = freewayenv.add_full_path_vehicle(gap = 20.0, vehicle_type = "lead", choice = "subject")
name5 = freewayenv.add_full_path_vehicle(vehicle_type = "follow", choice = "subject")
# get bounding box
bb = freewayenv.get_vehicle_bounding_box(name1)
print("bb.x = %f, bb.y = %f, bb.z = %f" % (bb.x,bb.y,bb.z))
# adjust the lead and follow vehicle settings in the third section
freewayenv.edit_normal_section_setting(section_id = 3, vehicle_type = "lead", choice = "subject", vehicle_index = 0,command = "distance")
freewayenv.edit_normal_section_setting(section_id = 3, vehicle_type = "lead", choice = "left", vehicle_index = 0,command = "distance")
freewayenv.edit_normal_section_setting(section_id = 3, vehicle_type = "follow", choice = "subject", vehicle_index = 0,command = "distance")
freewayenv.edit_normal_section_setting(section_id = 3, vehicle_type = "follow", choice = "left", vehicle_index = 0,command = "distance")
freewayenv.edit_normal_section_setting(section_id = 3, vehicle_type = "lead", choice = "subject", vehicle_index = 1,command = "distance")
freewayenv.edit_normal_section_setting(section_id = 3, vehicle_type = "follow", choice = "subject", vehicle_index = 1,command = "distance")
# adjust the lead and follow vehicle settings in the fourth section
freewayenv.edit_normal_section_setting(section_id = 4, vehicle_type = "lead", choice = "subject", vehicle_index = 0,command = "speed", command_start_time = 0.0)
freewayenv.edit_normal_section_setting(section_id = 4, vehicle_type = "lead", choice = "left", vehicle_index = 0,command = "speed", command_start_time = 0.0)
freewayenv.edit_normal_section_setting(section_id = 4, vehicle_type = "follow", choice = "subject", vehicle_index = 0,command = "speed", command_start_time = 0.0)
freewayenv.edit_normal_section_setting(section_id = 4, vehicle_type = "follow", choice = "left", vehicle_index = 0,command = "speed", command_start_time = 0.0)
freewayenv.edit_normal_section_setting(section_id = 4, vehicle_type = "lead", choice = "subject", vehicle_index = 1,command = "lane")
freewayenv.edit_normal_section_setting(section_id = 4, vehicle_type = "follow", choice = "subject", vehicle_index = 1,command = "lane")
# adjust the lead and follow vehicle settings in the sixth section
freewayenv.edit_normal_section_setting(section_id = 6, vehicle_type = "lead", choice = "subject", vehicle_index = 0,command = "lane")
freewayenv.edit_normal_section_setting(section_id = 6, vehicle_type = "lead", choice = "left", vehicle_index = 0,command = "lane")
freewayenv.edit_normal_section_setting(section_id = 6, vehicle_type = "follow", choice = "subject", vehicle_index = 0,command = "lane")
freewayenv.edit_normal_section_setting(section_id = 6, vehicle_type = "follow", choice = "left", vehicle_index = 0,command = "lane")
freewayenv.edit_normal_section_setting(section_id = 6, vehicle_type = "lead", choice = "subject", vehicle_index = 1,command = "lane")
freewayenv.edit_normal_section_setting(section_id = 6, vehicle_type = "follow", choice = "subject", vehicle_index = 1,command = "lane")
# test remove vehicle
#freewayenv.remove_full_path_vehicle(name4)
# test editing vehicle settings
#freewayenv.edit_full_path_vehicle_init_setting(name0, gap = 25.0, vehicle_type = "lead", choice = "subject", vehicle_color = '0,0,0')
#freewayenv.edit_full_path_vehicle_init_setting(name1, gap = 25.0, vehicle_type = "lead", choice = "left", vehicle_color = '255,255,255')
#freewayenv.edit_full_path_vehicle_init_setting(name2, gap = 25.0, vehicle_type = "follow", choice = "subject", vehicle_color = '0,0,0')
#freewayenv.edit_full_path_vehicle_init_setting(name3, gap = 25.0, vehicle_type = "follow", choice = "left", vehicle_color = '255,255,255')
freewayenv.SectionBackend(spectator_mode = "first_person")
finally:
time.sleep(10)
env.destroy_actors()
if __name__ == '__main__':
main()