This tutorial guides you through the process of controlling vehicles in the AirGen simulation environment. You’ll learn how to initialize a car, access its state, and apply various control commands including forward movement, steering, reverse, and braking.

Notebook for this example can be found: Here

Getting Started

First, let’s initialize an AirGen car object. This is your entry point to controlling vehicles in the GRID platform.

from grid.robot.wheeled.airgen_car import AirGenCar
airgen_car_0 = AirGenCar()

This code creates an instance of a car that we can control in our simulation. The AirGenCar class provides an interface to the underlying vehicle simulation.

Basic Car Control

After initializing our car, we need to import the AirGen library and access the car’s current state. We’ll then disable automatic speed control to manually control the vehicle.

import airgen
import time

# Get current car state 
car_state = airgen_car_0.client.getCarState()

# Disable automatic speed control
airgen_car_0.client.enableCarSpeedControl(False)

Now we can create a CarControls object and set properties to control the car’s movement:

# Create control object
car_controls = airgen.CarControls()

# Set throttle (range 0-1) and steering (range -1 to 1)
car_controls.throttle = 0.5  # 50% throttle
car_controls.steering = 0.0  # Straight ahead

# Apply controls to the car
airgen_car_0.client.setCarControls(car_controls)
print("Going forward...")

# Let car drive for 3 seconds
time.sleep(3)   

# Stop the car by setting controls back to default
print("Stopping...")
airgen_car_0.client.setCarControls(airgen.CarControls())

Steering Control

Let’s explore how to steer the car while moving forward:

# Go forward + steer right
car_controls.throttle = 0.5  # 50% throttle
car_controls.steering = 1    # Full right turn (range is -1 to 1)
airgen_car_0.client.setCarControls(car_controls)
print("Going forward, steering right")

# Let car drive for 3 seconds
time.sleep(3)

# Stop the car
print("Stopping...")
airgen_car_0.client.setCarControls(airgen.CarControls())

The steering value ranges from -1 (full left) to 1 (full right), with 0 representing straight ahead.

Reverse Operation

To move the car in reverse, we need to set the gear to manual and select reverse:

# Go in reverse
car_controls.throttle = 0.5        # 50% throttle
car_controls.is_manual_gear = True # Enable manual gear selection
car_controls.manual_gear = -1      # Set to reverse gear
car_controls.steering = 0          # Straight ahead
airgen_car_0.client.setCarControls(car_controls)
print("Going reverse...")

# Let car drive for 3 seconds
time.sleep(3)

Applying Brakes

Finally, let’s see how to apply brakes to stop the car:

# Apply brakes
car_controls.is_manual_gear = False # Change back to automatic gear
car_controls.manual_gear = 0        # Reset gear selection
car_controls.throttle = 0           # No throttle
car_controls.brake = 1              # Full braking (range 0-1)
airgen_car_0.client.setCarControls(car_controls)
print("Applying brakes...")

# Let brakes work for 3 seconds
time.sleep(3.5)

# Release the brakes
car_controls.brake = 0

Car Control Parameters

The CarControls object supports several parameters:

ParameterRangeDescription
throttle0 to 1Controls acceleration (0 = no throttle, 1 = full throttle)
steering-1 to 1Controls turning (-1 = full left, 0 = straight, 1 = full right)
brake0 to 1Controls braking (0 = no brake, 1 = full brake)
handbrakebooleanApplies the handbrake when set to True
is_manual_gearbooleanEnables manual gear selection when True
manual_gearintegerGear selection (-1 = reverse, 0 = neutral, 1+ = forward gears)