A dip in Carla II: Control your car and your world! | by Parthan Manisekaran | May 2023

In my previous post, we installed Carla, the Carla Python API and ran our first demo: WASD your own Car around. I hope you rode until ̶y̶o̶u̶r̶ ̶c̶a̶r̶ ̶r̶a̶n̶ ̶o̶u̶t̶ ̶o̶f̶ ̶f̶u̶e̶l ̶ you run out of energy!

I hope that was you in Carla or at least in the real world!

Before we start with this tutorial, I want to let you know that I edited the previous post to make sure we’re working with it. Carla v0.9.14, as it is the latest stable version and has more features than the previous v0.9.13. I apologize for the confusion. From now on, we will work with the last version: v0.9.14

While driving, if you looked closely at the terminal or the pygame window, you could see that there were many options to play with, such as the Time, Manual/automatic pilot fashion, Sensors and of course, changing the ego-vehicle itself. To use Carla’s full capacity, we must know these properties, both from Carla’s world and from your ego-vehicle itself.

In this article, I will walk through some of the properties of the world, NPCs (non-playable characters), and ego-vehicle (Reference/Your Vehicle). At the end, you should be able to program the previous demo with your own choice of properties. So let’s ride!

SpectatorView

When we run ./CarlaUE4.sh on the terminal, the Carla server/simulator is up and running. You can move around using the WASDQE keys themselves. This visualization allows you to see the different properties of the simulation such as weather, map and also the different vehicles through the maps.

When we perform our program, we will ensure that the viewer focuses on the vehicle of our ego, so that it is easier to know where we are starting from. If you want change the viewer’s positionall you have to do is give the contact details of spectators(position and orientation). If you can’t find the right contact details, you can always ATTACH the viewer has a vehicle or any object in the world and move the viewer in relation to it. (I will do this in the Vehicle Generation section)

Credits: Carla Docs

Customer Setup

To work with Carla using the Python API, we need to connect the Python client to the Carla server. We connect to ‘localhost’ with the default port 2000, which means we are running both the Carla server and the Python client on the same system. You can change the localhost to another system via the IP address. It helps you run the server on a powerful system and use your personal computer to control the environment through the Python API.

import carla
import random
import pygame # We will be using this for manual control

# server running on our system with default port 2000

client = carla.Client('localhost',2000)

Carl Cards

Carla provides you with 8 maps that can be used to test different driving scenarios. These maps offer a rich variety of driving scenarios, from navigating Michigan roundabouts and lefts to managing multiple lanes in the same direction. You can view them here.

# I am choosing the map with a michigan left. 
# Do check out the other maps as well

world = client.load_world('Town06')

City 06, with only static actors

Meteorological properties

In order to make the simulations as close to reality, an important property is to keep the environment as realistic. To help this we use the weather property which allows us to control the weather sun orientation, cloudiness, wind, fog etc These properties allow you to test your algorithms like Object detection And follow up, detection path And depth estimate under various conditions. When I was working on the apps above, the simulation was pretty close to the real world even in low light and rainy conditions, which is super awesome!

The code snippet below consists of a few properties that you can play around with. Some properties like fog_density, aptitude affect the RGB camera sensor, just like real life. You can explore more of these weather properties here.

# I have changed the weather to a clear noon.

weather = carla.WeatherParameters(
cloudiness=0.0,
precipitation=0.0,
sun_altitude_angle=10.0,
sun_azimuth_angle = 90.0,
precipitation_deposits = 0.0,
wind_intensity = 0.0,
fog_density = 0.0,
wetness = 0.0,
)
world.set_weather(weather)

City 06, now with a clear noon condition
A clear noon can be a great condition to test your lane detection algorithms!

Appearance of vehicles

Carla offers a Blueprint library that contains all the assets (actors) that can be generated in the world. The buildings you see around, streetlights, pedestrians, and automobiles are all part of the Blueprint Library. We will generate our ego-vehicle from here. If you want to build your own world, you can also spawn buildings and other players.

Similar to the coordinates of the spectator, we can give the coordinates to make our vehicle appear. Or we could use the safe spawn points that allow us to spawn at any safe place. What we’re going to do is spawn our vehicle at a safe spawn point and position the viewer so that we can see our vehicle.

We can choose a wide variety of automobiles to spawn, including buses, bicycles, police cars, ambulances, and passenger vehicles. You can view the vehicle catalog here.

# First let's get the blueprint library and the spawn points for our world.
# Depending on your Carla version and the map chosen, you get different actors
# and spawn points respectively

bp_lib = world.get_blueprint_library()
spawn_points = world.get_map().get_spawn_points()

# I am spawning an Audi etron here. You can check out the blueprint library
# to spawn your vehicle of choice. Also we spawn in a random safe point 79

vehicle_bp = bp_lib.find('vehicle.audi.etron')
ego_vehicle = world.try_spawn_actor(vehicle_bp, spawn_points[79])

# Let's position the spectator just behind the vehicle
# Carla.Transform has two parameters - Location and Rotation. We use this to
# to position the spectator by going 4 metres behind and 2.5 metres above the
# ego_vehicle

spectator = world.get_spectator()
transform = carla.Transform(ego_vehicle.get_transform().transform(carla.Location(x=-4,z=2.5)),ego_vehicle.get_transform().rotation)
spectator.set_transform(transform)

# If you want to position the your_actor with just the coordinates,
# you can use the below codes.
# location = carla.Location(x=0, y=0, z=30)
# rotation = carla.Rotation(roll=0, pitch=-30, yaw=180)
# transform = carla.Transform(location, rotation)
# your_actor.set_transform(transform)

Play with WASD where the spectator transforms to discover your super car!

spawn NPC

Of course, it’s just wonderful to drive in an empty city with no traffic. But when you wake up from this dream and look outside, you see pedestrians, vehicles of different capacities and abilities. The best part about Carla is that you don’t have to control every NPC (duh, it’s an NPC for a reason). The traffic manager will support each of these vehicles with an autopilot function.

For you to spawn these vehicles just check the code below

# Let's add some traffic. I am spawning 200 vehicle actors at random spawn_points
# (includes all kinds of automobiles). The method try_spawn_actor makes sure
# you can spawn it without messing with any existing actors.

for i in range(200):
vehicle_bp = random.choice(bp_lib.filter('vehicle'))
npc = world.try_spawn_actor(vehicle_bp, random.choice(spawn_points))

#Set traffic in motion
for v in world.get_actors().filter('*vehicle*'):
v.set_autopilot(True) # This makes all the vehicles function in autopilot
ego_vehicle.set_autopilot(False) # Allows you to drive the ego_vehicle manually

More vehicles! Hooray?

Although we’ve set the autopilot to true, we don’t see the cars moving yet. We need a loop to be able to see them moving. We’ll create our manual control function and pass it inside the loop.

Vehicle properties

These are usually advanced settings, but if you want to simulate the exact vehicle or have your own values ​​for different vehicle physics properties, vehicle properties are your friend. You see all vehicle properties here and wheel properties here.

I’m just going to show you a few properties that might be useful but won’t be used in the final code.

## Vehicle PHYSICS property

# Create Wheels Physics Control
front_left_wheel = carla.WheelPhysicsControl(tire_friction=2.0, damping_rate=1.5, max_steer_angle=70.0, long_stiff_value=1000)
front_right_wheel = carla.WheelPhysicsControl(tire_friction=2.0, damping_rate=1.5, max_steer_angle=70.0, long_stiff_value=1000)
rear_left_wheel = carla.WheelPhysicsControl(tire_friction=3.0, damping_rate=1.5, max_steer_angle=0.0, long_stiff_value=1000)
rear_right_wheel = carla.WheelPhysicsControl(tire_friction=3.0, damping_rate=1.5, max_steer_angle=0.0, long_stiff_value=1000) # Reducing friction increases idle throttle

wheels = [front_left_wheel, front_right_wheel, rear_left_wheel, rear_right_wheel]

# Change Vehicle Physics Control parameters of the vehicle
physics_control = vehicle.get_physics_control()
physics_control.torque_curve = [carla.Vector2D(x=0, y=400), carla.Vector2D(x=1300, y=600)]
physics_control.max_rpm = 10000
physics_control.moi = 1.0
physics_control.damping_rate_full_throttle = 0.0
physics_control.use_gear_autobox = True
physics_control.gear_switch_time = 0.5
physics_control.clutch_strength = 10
physics_control.mass = 10000
physics_control.drag_coefficient = 0.25
physics_control.steering_curve = [carla.Vector2D(x=0, y=1), carla.Vector2D(x=100, y=1), carla.Vector2D(x=300, y=1)]
physics_control.use_sweep_wheel_collision = True
physics_control.wheels = wheels

# Apply Vehicle Physics Control for the vehicle
vehicle.apply_physics_control(physics_control)

Manual control

In the last example, you used the provided manual_control code to control your car. If you don’t want to have all the bells and whistles of this code, we can isolate just the keyboard control from the code. This way you can use it as a function in your own code.

Brake, Throttle Lever, Steering angles are the main factors to control to move the vehicle. So we have to program in such a way that each key we press on the keyboard corresponds to the modification of one of these three factors. This is a very basic manual keyboard control code that you can create on top of it!

In the code below, keyboard commands are parsed into corresponding factors to control the vehicle:

pygame.init() #initialising

# Set up the Pygame display
size = (640, 480)
pygame.display.set_caption("CARLA Manual Control")
screen = pygame.display.set_mode(size)

# Set up the control object and loop until the user exits the script
control = carla.VehicleControl()
clock = pygame.time.Clock()
done = False

while not done:
# Get keyboard input and handle it
keys = pygame.key.get_pressed()

# The values are directly from the manual control code which we ran in the
# last post. Just implemented just the throttle, brake and steer
# You can add reverse and gear shifting features directly from that code

if keys[pygame.K_UP] or keys[pygame.K_w]:
control.throttle = min(control.throttle + 0.05, 1.0)
else:
control.throttle = 0.0

if keys[pygame.K_DOWN] or keys[pygame.K_s]:
control.brake = min(control.brake + 0.2, 1.0)
else:
control.brake = 0.0

if keys[pygame.K_LEFT] or keys[pygame.K_a]:
control.steer = max(control.steer - 0.05, -1.0)
elif keys[pygame.K_RIGHT] or keys[pygame.K_d]:
control.steer = min(control.steer + 0.05, 1.0)
else:
control.steer = 0.0

control.hand_brake = keys[pygame.K_SPACE]

# Apply the control to the ego vehicle and tick the simulation
ego_vehicle.apply_control(control)
world.tick()

# Update the display and check for the quit event
pygame.display.flip()
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True

# Sleep to ensure consistent loop timing
clock.tick(60)

THE pygame the window will be black screen for now, as we don’t have a camera screen or spectator screen attached. In future tutorials we will use OpenCV window to see the world from the point of view of the ego-vehicle

Conclude

By putting all the above codes together, we can get a fully functional environment with your manually controlled car and other NPCs running on autopilot:

import carla
import random
import pygame

client = carla.Client('localhost',2000)
world = client.load_world('Town06')
weather = carla.WeatherParameters(
cloudiness=0.0,
precipitation=0.0,
sun_altitude_angle=10.0,
sun_azimuth_angle = 70.0,
precipitation_deposits = 0.0,
wind_intensity = 0.0,
fog_density = 0.0,
wetness = 0.0,
)
world.set_weather(weather)

bp_lib = world.get_blueprint_library()
spawn_points = world.get_map().get_spawn_points()

vehicle_bp = bp_lib.find('vehicle.audi.etron')
ego_vehicle = world.try_spawn_actor(vehicle_bp, spawn_points[79])

spectator = world.get_spectator()
transform = carla.Transform(ego_vehicle.get_transform().transform(carla.Location(x=-4,z=2.5)),ego_vehicle.get_transform().rotation)
spectator.set_transform(transform)

for i in range(200):
vehicle_bp = random.choice(bp_lib.filter('vehicle'))
npc = world.try_spawn_actor(vehicle_bp, random.choice(spawn_points))

for v in world.get_actors().filter('*vehicle*'):
v.set_autopilot(True)
ego_vehicle.set_autopilot(False)

pygame.init()

size = (640, 480)
pygame.display.set_caption("CARLA Manual Control")
screen = pygame.display.set_mode(size)

control = carla.VehicleControl()
clock = pygame.time.Clock()
done = False

while not done:

keys = pygame.key.get_pressed()

if keys[pygame.K_UP] or keys[pygame.K_w]:
control.throttle = min(control.throttle + 0.05, 1.0)
else:
control.throttle = 0.0

if keys[pygame.K_DOWN] or keys[pygame.K_s]:
control.brake = min(control.brake + 0.2, 1.0)
else:
control.brake = 0.0

if keys[pygame.K_LEFT] or keys[pygame.K_a]:
control.steer = max(control.steer - 0.05, -1.0)
elif keys[pygame.K_RIGHT] or keys[pygame.K_d]:
control.steer = min(control.steer + 0.05, 1.0)
else:
control.steer = 0.0

control.hand_brake = keys[pygame.K_SPACE]

# Apply the control to the ego vehicle and tick the simulation
ego_vehicle.apply_control(control)
world.tick()

# Update the display and check for the quit event
pygame.display.flip()
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True

# Sleep to ensure consistent loop timing
clock.tick(60)

Make sure you press the Pygame window be able to control the car with the keyboard. You can either use WASD or the Directional keys for the control.

Wheeeeeeee!!! GTA Nostalgia

Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *