Skip to content

Instantly share code, notes, and snippets.

@curtkim
Created December 31, 2019 00:17
Show Gist options
  • Save curtkim/e1c5ee0675b9a44f1787418ba9ded869 to your computer and use it in GitHub Desktop.
Save curtkim/e1c5ee0675b9a44f1787418ba9ded869 to your computer and use it in GitHub Desktop.
carla
import carla
import random
import time
def main():
actor_list = []
# In this tutorial script, we are going to add a vehicle to the simulation
# and let it drive in autopilot. We will also create a camera attached to
# that vehicle, and save all the images generated by the camera to disk.
try:
# First of all, we need to create the client that will send the requests
# to the simulator. Here we'll assume the simulator is accepting
# requests in the localhost at port 2000.
client = carla.Client('localhost', 2000)
client.set_timeout(2.0)
# Once we have a client we can retrieve the world that is currently
# running.
world = client.get_world()
# The world contains the list blueprints that we can use for adding new
# actors into the simulation.
blueprint_library = world.get_blueprint_library()
# Now let's filter all the blueprints of type 'vehicle' and choose one
# at random.
bp = random.choice(blueprint_library.filter('vehicle'))
# A blueprint contains the list of attributes that define a vehicle's
# instance, we can read them and modify some of them. For instance,
# let's randomize its color.
if bp.has_attribute('color'):
color = random.choice(bp.get_attribute('color').recommended_values)
bp.set_attribute('color', color)
# Now we need to give an initial transform to the vehicle. We choose a
# random transform from the list of recommended spawn points of the map.
#transform = random.choice(world.get_map().get_spawn_points())
transform = carla.Transform(carla.Location(x=40, y=-3.2), carla.Rotation())
# So let's tell the world to spawn the vehicle.
vehicle = world.spawn_actor(bp, transform)
# It is important to note that the actors we create won't be destroyed
# unless we call their "destroy" function. If we fail to call "destroy"
# they will stay in the simulation even after we quit the Python script.
# For that reason, we are storing all the actors we create so we can
# destroy them afterwards.
actor_list.append(vehicle)
print('created %s' % vehicle.type_id)
# Let's put the vehicle to drive around.
#vehicle.set_autopilot(True)
camera_bp = blueprint_library.find('sensor.camera.depth')
camera_transform = carla.Transform(carla.Location(x=1.5, y=0, z=2.4))
camera = world.spawn_actor(camera_bp, camera_transform, attach_to=vehicle)
actor_list.append(camera)
print('created %s' % camera.type_id)
cc = carla.ColorConverter.LogarithmicDepth
def camera_callback(image):
image.save_to_disk('_out/depth_%06d.png' % image.frame, cc)
camera.listen(camera_callback)
"""
lidar_bp = world.get_blueprint_library().find('sensor.lidar.ray_cast')
lidar_bp.set_attribute('range', str(200))
lidar_bp.set_attribute('channels', str(32))
lidar_bp.set_attribute('rotation_frequency', str(10))
lidar_bp.set_attribute('points_per_second', str(360*10*10*32))
lidar_sensor = world.spawn_actor(lidar_bp, carla.Transform(carla.Location(x=1.5, y=0, z=2.4)), attach_to=vehicle)
actor_list.append(lidar_sensor)
print('created %s' % lidar_sensor.type_id)
def lidar_callback(measurement):
measurement.save_to_disk('_out/pc_%06d.ply' % measurement.frame)
lidar_sensor.listen(lidar_callback)
"""
time.sleep(10)
finally:
print('destroying actors')
actor_list.reverse()
for actor in actor_list:
print('destroy %s' % actor.type_id)
if(isinstance(actor, carla.Sensor)):
print('stop %s' % actor.type_id)
actor.stop()
actor.destroy()
print('done.')
if __name__ == '__main__':
main()
print('last sleep')
time.sleep(2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment