This tutorial guides you through controlling the simulation environment in AirGen. You’ll learn how to spawn objects, manipulate their position and scale, destroy objects, and modify environmental conditions like time of day.

Notebook for this example can be found: Here

Getting Started

First, let’s import the necessary libraries and initialize a connection to the AirGen simulation environment:

import airgen

# Create a general simulation client that allows environment manipulation
sim_client = airgen.VehicleClient()

The VehicleClient class provides an interface to the underlying simulation, allowing us to modify the environment even without directly controlling a specific vehicle.

Spawning Objects in the Simulation

Let’s start by placing a cube in the simulation world. We’ll position it 20 meters in front of the current vehicle:

# Get the current vehicle's position and orientation
pose = sim_client.simGetVehiclePose()
pose_obj = pose

# Position the object 20 meters in front of the vehicle
pose_obj.position.x_val -= 20

# Spawn a cube with dimensions 2x2x2 meters
sim_client.simSpawnObject(
    "Cube_New",            # Name identifier for the object
    "Cube",                # Object type
    pose_obj,              # Position and orientation
    airgen.Vector3r(2, 2, 2),  # Object scale (size)
    True,                  # Physics enabled
    False                  # Collision enabled
)

Manipulating Object Position

Once an object is spawned, we can modify its position at any time:

# Get the current position and orientation of our cube
curr_pose = sim_client.simGetObjectPose("Cube_New")
new_pose = curr_pose

# Move the cube 10 meters further in the X direction
new_pose.position.x_val = curr_pose.position.x_val + 10

# Apply the new position
sim_client.simSetObjectPose("Cube_New", new_pose)

The simGetObjectPose method retrieves the current position and orientation of any object in the simulation, while simSetObjectPose allows us to update it.

Scaling Objects

We can also modify the size of objects in the simulation:

# Get the current scale of our cube
curr_scale = sim_client.simGetObjectScale("Cube_New")
print(curr_scale)  # This will show the current dimensions

# Increase the size to 10x10x10 meters
sim_client.simSetObjectScale("Cube_New", airgen.Vector3r(10, 10, 10))

The scale is represented as a Vector3r object with x, y, and z components, allowing for non-uniform scaling if desired. This feature is useful for:

  • Creating objects of custom sizes
  • Implementing growth or shrinking effects
  • Adjusting obstacle difficulty in robotic navigation scenarios

Removing Objects from the Simulation

When an object is no longer needed, we can remove it from the simulation:

# Destroy the cube we created
sim_client.simDestroyObject("Cube_New")

The simDestroyObject method completely removes an object from the simulation environment. This can be used to:

  • Clean up temporary objects
  • Remove obstacles after they’ve served their purpose

Controlling Environmental Conditions

Beyond manipulating individual objects, AirGen allows control over environmental conditions. Let’s change the time of day:

# Change the simulation time to 10 PM
sim_client.simSetTimeOfDay(True, "2024-07-11 22:00:00")

The simSetTimeOfDay method takes two parameters:

  • is_enabled: Whether to enable custom time of day (True) or use the system time (False)
  • datetime_str: A string representing the desired date and time in “YYYY-MM-DD HH:MM:SS” format

This feature allows you to:

  • Test robotic systems under different lighting conditions
  • Simulate day/night transitions
  • Create specific time-based scenarios