26
26
27
27
28
28
class SensorLoop (object ):
29
- def __init__ (self , role_name ):
29
+ def __init__ (self ):
30
30
self .start_game_time = None
31
31
self .start_system_time = None
32
32
self .sensor = None
33
- self .ego_vehicle = None
33
+ self .ego_actor = None
34
34
self .running = False
35
35
self .timestamp_last_run = 0.0
36
36
self .timeout = 20.0
37
- self .role_name = role_name
38
37
39
38
def _stop_loop (self ):
40
39
self .running = False
@@ -48,7 +47,7 @@ def _tick_sensor(self, timestamp):
48
47
ego_action = self .sensor ()
49
48
except SensorReceivedNoData as e :
50
49
raise RuntimeError (e )
51
- self .ego_vehicle .apply_control (ego_action )
50
+ self .ego_actor .apply_control (ego_action )
52
51
if self .running :
53
52
CarlaDataProvider .get_world ().tick ()
54
53
@@ -59,7 +58,7 @@ def __init__(self):
59
58
self .param_ = self .interface .get_param ()
60
59
self .world = None
61
60
self .sensor_wrapper = None
62
- self .ego_vehicle = None
61
+ self .ego_actor = None
63
62
self .prev_tick_wall_time = 0.0
64
63
65
64
# Parameter for Initializing Carla World
@@ -80,83 +79,78 @@ def load_world(self):
80
79
client .set_timeout (self .timeout )
81
80
client .load_world (self .carla_map )
82
81
self .world = client .get_world ()
83
- if self .world is not None :
84
- settings = self .world .get_settings ()
85
- settings .fixed_delta_seconds = self .fixed_delta_seconds
86
- settings .synchronous_mode = self .sync_mode
87
- self .world .apply_settings (settings )
88
- CarlaDataProvider .set_world (self .world )
89
- CarlaDataProvider .set_client (client )
90
- spawn_point = carla .Transform ()
91
- spawn_point = carla .Transform ()
92
- point_items = self .spawn_point .split ("," )
93
- randomize = False
94
- if len (point_items ) == 6 :
95
- spawn_point .location .x = float (point_items [0 ])
96
- spawn_point .location .y = float (point_items [1 ])
97
- spawn_point .location .z = (
98
- float (point_items [2 ]) + 2
99
- ) # +2 is used so the car did not stuck on the road when spawned.
100
- spawn_point .rotation .roll = float (point_items [3 ])
101
- spawn_point .rotation .pitch = float (point_items [4 ])
102
- spawn_point .rotation .yaw = float (point_items [5 ])
103
- else :
104
- randomize = True
105
- CarlaDataProvider .request_new_actor (
106
- self .vehicle_type , spawn_point , self .agent_role_name , random_location = randomize
107
- )
108
-
109
- self .sensor_wrapper = SensorWrapper (self .interface )
110
- self .ego_vehicle = CarlaDataProvider .get_actor_by_name (self .agent_role_name )
111
- self .sensor_wrapper .setup_sensors (self .ego_vehicle , False )
112
- ##########################################################################################################################################################
113
- # TRAFFIC MANAGER
114
- ##########################################################################################################################################################
115
- if self .use_traffic_manager :
116
- traffic_manager = client .get_trafficmanager ()
117
- traffic_manager .set_synchronous_mode (True )
118
- traffic_manager .set_random_device_seed (0 )
119
- random .seed (0 )
120
- spawn_points_tm = self .world .get_map ().get_spawn_points ()
121
- for i , spawn_point in enumerate (spawn_points_tm ):
122
- self .world .debug .draw_string (spawn_point .location , str (i ), life_time = 10 )
123
- models = [
124
- "dodge" ,
125
- "audi" ,
126
- "model3" ,
127
- "mini" ,
128
- "mustang" ,
129
- "lincoln" ,
130
- "prius" ,
131
- "nissan" ,
132
- "crown" ,
133
- "impala" ,
134
- ]
135
- blueprints = []
136
- for vehicle in self .world .get_blueprint_library ().filter ("*vehicle*" ):
137
- if any (model in vehicle .id for model in models ):
138
- blueprints .append (vehicle )
139
- max_vehicles = 30
140
- max_vehicles = min ([max_vehicles , len (spawn_points_tm )])
141
- vehicles = []
142
- for i , spawn_point in enumerate (random .sample (spawn_points_tm , max_vehicles )):
143
- temp = self .world .try_spawn_actor (random .choice (blueprints ), spawn_point )
144
- if temp is not None :
145
- vehicles .append (temp )
146
-
147
- for vehicle in vehicles :
148
- vehicle .set_autopilot (True )
149
-
82
+ settings = self .world .get_settings ()
83
+ settings .fixed_delta_seconds = self .fixed_delta_seconds
84
+ settings .synchronous_mode = self .sync_mode
85
+ self .world .apply_settings (settings )
86
+ CarlaDataProvider .set_world (self .world )
87
+ CarlaDataProvider .set_client (client )
88
+ spawn_point = carla .Transform ()
89
+ point_items = self .spawn_point .split ("," )
90
+ randomize = False
91
+ if len (point_items ) == 6 :
92
+ spawn_point .location .x = float (point_items [0 ])
93
+ spawn_point .location .y = float (point_items [1 ])
94
+ spawn_point .location .z = (
95
+ float (point_items [2 ]) + 2
96
+ ) # +2 is used so the car did not stuck on the road when spawned.
97
+ spawn_point .rotation .roll = float (point_items [3 ])
98
+ spawn_point .rotation .pitch = float (point_items [4 ])
99
+ spawn_point .rotation .yaw = float (point_items [5 ])
150
100
else :
151
- print ("Carla Interface Couldn't find the world, Carla is not Running" )
101
+ randomize = True
102
+ self .ego_actor = CarlaDataProvider .request_new_actor (
103
+ self .vehicle_type , spawn_point , self .agent_role_name , random_location = randomize
104
+ )
105
+ self .interface .ego_actor = self .ego_actor # TODO improve design
106
+ self .interface .physics_control = self .ego_actor .get_physics_control ()
107
+
108
+ self .sensor_wrapper = SensorWrapper (self .interface )
109
+ self .sensor_wrapper .setup_sensors (self .ego_actor , False )
110
+ ##########################################################################################################################################################
111
+ # TRAFFIC MANAGER
112
+ ##########################################################################################################################################################
113
+ if self .use_traffic_manager :
114
+ traffic_manager = client .get_trafficmanager ()
115
+ traffic_manager .set_synchronous_mode (True )
116
+ traffic_manager .set_random_device_seed (0 )
117
+ random .seed (0 )
118
+ spawn_points_tm = self .world .get_map ().get_spawn_points ()
119
+ for i , spawn_point in enumerate (spawn_points_tm ):
120
+ self .world .debug .draw_string (spawn_point .location , str (i ), life_time = 10 )
121
+ models = [
122
+ "dodge" ,
123
+ "audi" ,
124
+ "model3" ,
125
+ "mini" ,
126
+ "mustang" ,
127
+ "lincoln" ,
128
+ "prius" ,
129
+ "nissan" ,
130
+ "crown" ,
131
+ "impala" ,
132
+ ]
133
+ blueprints = []
134
+ for vehicle in self .world .get_blueprint_library ().filter ("*vehicle*" ):
135
+ if any (model in vehicle .id for model in models ):
136
+ blueprints .append (vehicle )
137
+ max_vehicles = 30
138
+ max_vehicles = min ([max_vehicles , len (spawn_points_tm )])
139
+ vehicles = []
140
+ for i , spawn_point in enumerate (random .sample (spawn_points_tm , max_vehicles )):
141
+ temp = self .world .try_spawn_actor (random .choice (blueprints ), spawn_point )
142
+ if temp is not None :
143
+ vehicles .append (temp )
144
+
145
+ for vehicle in vehicles :
146
+ vehicle .set_autopilot (True )
152
147
153
148
def run_bridge (self ):
154
- self .bridge_loop = SensorLoop (self . agent_role_name )
149
+ self .bridge_loop = SensorLoop ()
155
150
self .bridge_loop .sensor = self .sensor_wrapper
156
- self .bridge_loop .ego_vehicle = self .ego_vehicle
151
+ self .bridge_loop .ego_actor = self .ego_actor
157
152
self .bridge_loop .start_system_time = time .time ()
158
153
self .bridge_loop .start_game_time = GameTime .get_time ()
159
- self .bridge_loop .role_name = self .agent_role_name
160
154
self .bridge_loop .running = True
161
155
while self .bridge_loop .running :
162
156
timestamp = None
@@ -179,20 +173,20 @@ def _stop_loop(self, signum, frame):
179
173
def _cleanup (self ):
180
174
self .sensor_wrapper .cleanup ()
181
175
CarlaDataProvider .cleanup ()
182
- if self .ego_vehicle :
183
- self .ego_vehicle .destroy ()
184
- self .ego_vehicle = None
176
+ if self .ego_actor :
177
+ self .ego_actor .destroy ()
178
+ self .ego_actor = None
185
179
186
180
if self .interface :
181
+ self .interface .shutdown ()
187
182
self .interface = None
188
183
189
184
190
185
def main ():
191
186
carla_bridge = InitializeInterface ()
192
187
carla_bridge .load_world ()
193
- stop_bridge = signal .signal (signal .SIGINT , carla_bridge ._stop_loop )
188
+ signal .signal (signal .SIGINT , carla_bridge ._stop_loop )
194
189
carla_bridge .run_bridge ()
195
- signal .signal (signal .SIGINT , stop_bridge )
196
190
carla_bridge ._cleanup ()
197
191
198
192
0 commit comments