Skip to content

Commit

Permalink
configure sync/async mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Qi committed Sep 4, 2023
1 parent 0cfbf73 commit 65d3c2e
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 14 deletions.
3 changes: 2 additions & 1 deletion behavior_metrics/driver_carla.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,8 @@ def main():
traffic_manager = TrafficManager(app_configuration.number_of_vehicle,
app_configuration.number_of_walker,
app_configuration.percentage_walker_running,
app_configuration.percentage_walker_crossing)
app_configuration.percentage_walker_crossing,
app_configuration.async_mode)
traffic_manager.generate_traffic()

# Launch control
Expand Down
3 changes: 2 additions & 1 deletion behavior_metrics/pilot_carla.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,8 @@ def run(self):
time.sleep((self.time_cycle - ms) / 1000.0)
self.real_time_factors.append(self.real_time_factor)
self.brain_iterations_simulated_time.append(self.ros_clock_time - start_time_ros)
self.controller.world.tick()
if not self.async_mode:
self.controller.world.tick()
self.execution_completed = True
self.kill()
logger.info('Pilot: pilot killed.')
Expand Down
4 changes: 0 additions & 4 deletions behavior_metrics/utils/controller_carla.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,6 @@ def __init__(self):
time.sleep(1) # sleep for 1 second before checking again
self.map_waypoints = self.carla_map.generate_waypoints(0.5)
self.weather = self.world.get_weather()

settings = self.world.get_settings()
settings.synchronous_mode = True
self.world.apply_settings(settings)

# GUI update
def update_frame(self, frame_id, data):
Expand Down
20 changes: 12 additions & 8 deletions behavior_metrics/utils/traffic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@
from utils.logger import logger
import random

SpawnActor = carla.command.SpawnActor
SetAutopilot = carla.command.SetAutopilot
FutureActor = carla.command.FutureActor
spawn_actor = carla.command.SpawnActor
set_autopilot = carla.command.SetAutopilot
future_actor = carla.command.FutureActor

class TrafficManager:
def __init__(self, n_vehicle, n_walker, percentage_walker_running=0.0, percentage_walker_crossing=0.0, port=8000):
def __init__(self, n_vehicle, n_walker, percentage_walker_running=0.0, percentage_walker_crossing=0.0, async_mode=False, port=8000):
self.client = carla.Client('localhost', 2000)
self.world = self.client.get_world()
if not async_mode:
settings = self.world.get_settings()
settings.synchronous_mode = True
self.world.apply_settings(settings)

traffic_manager = self.client.get_trafficmanager(port)
traffic_manager.set_synchronous_mode(True)
Expand Down Expand Up @@ -103,8 +107,8 @@ def spawn_vehicles(self, world, client, n_vehicles, traffic_manager):
blueprint.set_attribute('driver_id', driver_id)
blueprint.set_attribute('role_name', 'autopilot')

batch.append(SpawnActor(blueprint, transform)
.then(SetAutopilot(FutureActor, True, traffic_manager.get_port())))
batch.append(spawn_actor(blueprint, transform)
.then(set_autopilot(future_actor, True, traffic_manager.get_port())))

for response in client.apply_batch_sync(batch, True):
if response.error:
Expand Down Expand Up @@ -141,7 +145,7 @@ def spawn_pedestrians(self, world, client, n_pedestrians, percentagePedestriansR
else:
print("Walker has no speed")
walker_speed.append(0.0)
batch.append(SpawnActor(walker_bp, spawn_point))
batch.append(spawn_actor(walker_bp, spawn_point))
results = client.apply_batch_sync(batch, True)
walker_speed2 = []
for i in range(len(results)):
Expand All @@ -155,7 +159,7 @@ def spawn_pedestrians(self, world, client, n_pedestrians, percentagePedestriansR
batch = []
walker_controller_bp = world.get_blueprint_library().find('controller.ai.walker')
for i in range(len(walkers_list)):
batch.append(SpawnActor(walker_controller_bp, carla.Transform(), walkers_list[i]["id"]))
batch.append(spawn_actor(walker_controller_bp, carla.Transform(), walkers_list[i]["id"]))
results = client.apply_batch_sync(batch, True)
for i in range(len(results)):
if results[i].error:
Expand Down

0 comments on commit 65d3c2e

Please sign in to comment.