🗺️

Setting up Your PyBullet Simulation

Welcome to this tutorial on setting up a PyBullet simulation using ARK! This guide will walk you through the process step by step, making it easy even if you're new to simulations.


What is a Sim Node?

A sim node is the heart of your simulation. It's responsible for:

Creating Your First Sim Node

Let's start by creating a file called sim_node.py. This will be our main simulation file.

Here's the code with explanations:

# Import necessary modules
from ark.client.comm_infrastructure.base_node import main
from ark.system.simulation.simulator_node import SimulatorNode
from ark.tools.log import log

# Path to your configuration file
CONFIG_PATH = "config/global_config.yaml"

# Create your custom simulator by inheriting from SimulatorNode
class MySimulatorNode(SimulatorNode):

    def initialize_scene(self):
        """
        This function is called after objects are loaded but before simulation starts.
        Use it to set up your scene, modify parameters, or add custom objects.
        """
        pass
    
    def step(self):
        """
        This function is called at each simulation step.
        Use it for debugging or implementing custom behaviors during simulation.
        
        Examples:
        - Print debug info: log.info("Current position: ", self.robot.get_position())
        - Apply forces: self.apply_force(...)
        - Check conditions: if self.check_collision(): ...
        """
        pass

# Entry point - this runs your node with the specified configuration
if __name__ == "__main__":
    main(MySimulatorNode, CONFIG_PATH)

Understanding the Key Components

1. Configuration Path

The CONFIG_PATH points to your global configuration file (YAML format) that contains settings for your simulation world, including physics parameters, robot specifications, and more.

2. MySimulatorNode Class

This is your custom simulator class that inherits functionality from the base SimulatorNode. You'll add your specific simulation logic here.

3. initialize_scene() Method

This is called once after all objects defined in your config are loaded but before the simulation starts running. It's the perfect place to:

4. step() Method

This is called during each simulation cycle (typically many times per second). Use it to:


Simulation Configuration Setup - A Step-by-Step Guide

The global configuration file is where you define all the parameters for your simulation environment. Let's break down how to set up this file properly.

1. Basic Simulator Configuration

Create a file called global_config.yaml in your project's config directory with these essential settings:

simulator:
  name: "my_first_simulator"              # A unique name for your simulator node
  backend_type: "pybullet"                # The physics engine we're using
  node_frequency: 240                     # How often your node's step() function runs (Hz)
  config:
    connection_mode: "GUI"                # GUI for visual interface, DIRECT for headless mode
    gravity:                              # Gravity vector [x, y, z] in m/s²
      - 0
      - 0
      - -9.81                             # Standard Earth gravity
    sim_frequency: 240                    # Physics simulation steps per second

2. Understanding Each Parameter

3. Rendering Configuration for Visualization

If you're on macOS or need to save simulation frames (for debugging or creating videos), add the following rendering configuration:

simulator:
  # ... previous settings ...
  save_render: 
    save_path: "render"                   # Directory where rendered frames will be saved
    remove_existing: false                # Whether to clean the directory before starting
    render_interval: 0.1                  # Save a frame every 0.1 seconds (10 FPS)
    overwrite_file: true                  # Overwrite existing files instead of creating new ones

4. Camera Configuration

To control how your simulation is viewed and rendered, add these camera settings:

simulator:
  # ... previous settings ...
  save_render:
    # ... previous render settings ...
    extrinsics:                           # How the camera is positioned
      look_at: [0.5, 0.0, 0.5]            # Point the camera is focused on [x, y, z]
      distance: 2.0                       # Distance from look_at point (in meters)
      azimuth: 135                        # Horizontal angle (degrees) - 0 is -y, 90 is +x
      elevation: 20                       # Vertical angle (degrees) - higher values look down
    intrinsics:                           # Camera lens properties
      width: 1280                         # Output image width in pixels
      height: 720                         # Output image height in pixels
      field_of_view: 60                   # Field of view in degrees
      near_plane: 0.01                    # Closest visible distance
      far_plane: 100.0                    # Furthest visible distance

5. Configuration Best Practices

With these settings in place, your simulation environment will be properly configured and ready for adding robots and objects, which we'll cover in the next section


Running Your Simulation: What to Expect

After setting up your configuration, it's time to test your simulation by running sim_node.py. Here's what you should expect depending on your configuration mode:

If Using "GUI" Mode:

If Using "DIRECT" Mode:

Troubleshooting Common Issues:


Code can be found:

https://github.com/Robotics-Ark/franka_gym_example

Next Steps…