Skip to content

Instantly share code, notes, and snippets.

@brunojus
Created October 26, 2019 09:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brunojus/9ba02487509eb21ac8b6dc3efed10962 to your computer and use it in GitHub Desktop.
Save brunojus/9ba02487509eb21ac8b6dc3efed10962 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import glob
import os
import sys
# Importing right version of Carla client package
try:
egg_path = '/home/bruno/Downloads/CARLA_0.9.5/PythonAPI/carla/dist/carla-0.9.5-py3.5-linux-x86_64.egg'
sys.path.append(egg_path)
except IndexError:
print('Error')
import carla
import random
import time
INTERVAL = 0.3 # Arbitrary
if __name__ == '__main__':
actor_list = []
try:
# Declaring the client and setting timeout for connecting
# with the server
client = carla.Client('localhost', 2000)
client.set_timeout(2.0)
world = client.get_world() # Get the world from the server
# Almost all things in Carla world are blueprints
blueprint_library = world.get_blueprint_library()
# Get a Tesla from blueprint library
bp = random.choice(blueprint_library.filter('vehicle.tesla.*'))
# Get a random possible position to spawn (no conflicts)
# and spawn the vehicle
transform = random.choice(world.get_map().get_spawn_points())
vehicle = world.spawn_actor(bp, transform)
# Record actors for removing from the world when finished
actor_list.append(vehicle)
print('created %s' % vehicle.type_id)
# Let's get a RGB camera and set some attributes
camera_bp = blueprint_library.find('sensor.camera.rgb')
camera_transform = carla.Transform(carla.Location(x=1.5, z=2.4))
camera_bp.set_attribute('image_size_x', '800')
camera_bp.set_attribute('image_size_y', '600')
camera_bp.set_attribute('sensor_tick', str(INTERVAL))
# Spawn the camera and attach it to the vehicle
camera = world.spawn_actor(
camera_bp,
camera_transform,
attach_to=vehicle
)
actor_list.append(camera)
print('created %s' % camera.type_id)
# Let's also add a GNSS sensor
gnss_bp = blueprint_library.find('sensor.other.gnss')
gnss_transform = carla.Transform(carla.Location(x=1.5, z=2.4))
gnss = world.spawn_actor(
gnss_bp,
gnss_transform,
attach_to=vehicle
)
actor_list.append(gnss)
print('created %s' % gnss.type_id)
# Check how much "out" folders already exists
n_output = len([d for d in os.listdir() if d.startswith('out')])
# Sets the function that will be called by the camera
# This will save the images to disk at a "out" folder
camera.listen(lambda image: image.save_to_disk(
'out%02d/%06d.png' % (n_output, image.frame)
))
# Add function that will be called everytime GNSS get some data
gnss.listen(
lambda data: print(
'timestamp {:0.3f},'
' latitude {:0.3f},'
' longitude {:0.3f},'
' altitude {:0.3f}'.format(
data.timestamp, data.latitude,
data.longitude, data.altitude)
)
)
# Driving right and left some times
for _ in range(20):
vehicle.apply_control(
carla.VehicleControl(throttle=5.0, steer=0.1)
)
time.sleep(0.5)
vehicle.apply_control(
carla.VehicleControl(throttle=5.0, steer=-0.1)
)
time.sleep(0.5)
finally:
print('destroying actors')
for actor in actor_list:
actor.destroy()
print('done.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment