> ## Documentation Index
> Fetch the complete documentation index at: https://scaledfoundations-rebrand.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Hello GRID

This tutorial provides a detailed, step-by-step guide to configuring and launching your custom session on the GRID platform. In this session, you will:

* Set up a custom scenario using the neighbourhood scene.
* Configure an SUV vehicle with custom camera settings.
* Add a LiDAR sensor with tailored parameters.
* Integrate an AI model for image segmentation.

## Configuring a Custom Session

Follow these steps to launch your custom scenario:

1. **Configure the Robot**\
   For this tutorial, we will use an SUV positioned at the origin. You can adjust the vehicle type and starting position according to your needs.

   <img src="https://mintcdn.com/scaledfoundations-rebrand/Xi7Qx08aC4xdqlWX/assets/images/hello_grid/robot.png?maxW=2430&auto=format&n=Xi7Qx08aC4xdqlWX&q=85&s=684a9245c31b63dc9749f59dfccd9d9f" alt="Car Selection" width="2430" height="1168" data-path="assets/images/hello_grid/robot.png" />

2. **Name Your Session and Select the Scene**\
   Provide a descriptive name for your session and choose the neighbourhood scene from the list of available scenarios.

   <img src="https://mintcdn.com/scaledfoundations-rebrand/Xi7Qx08aC4xdqlWX/assets/images/hello_grid/scene.png?maxW=2413&auto=format&n=Xi7Qx08aC4xdqlWX&q=85&s=489b9393c1435fd36d4ded17fbb2d93c" alt="Scene Selection" width="2413" height="1156" data-path="assets/images/hello_grid/scene.png" />

3. **Configure Sensors**
   * **RGB Camera:** Set up an RGB camera with a 512x512 resolution to ensure high-quality images for AI processing.

     <img src="https://mintcdn.com/scaledfoundations-rebrand/Xi7Qx08aC4xdqlWX/assets/images/hello_grid/sensor.png?maxW=2421&auto=format&n=Xi7Qx08aC4xdqlWX&q=85&s=9997a01bf41722e8e48c93ef3130a984" alt="Camera Settings" width="2421" height="1078" data-path="assets/images/hello_grid/sensor.png" />

   * **LiDAR Sensor:** Add a LiDAR sensor with custom parameters. For more details on sensor configuration options, refer to the [Sensors](/simulation/airgen/features/sensors) tab.

     <img src="https://mintcdn.com/scaledfoundations-rebrand/Xi7Qx08aC4xdqlWX/assets/images/hello_grid/lidar.png?maxW=2416&auto=format&n=Xi7Qx08aC4xdqlWX&q=85&s=e25947cdd8ee3c82c5083aa31d48b62c" alt="Sensor Settings" width="2416" height="931" data-path="assets/images/hello_grid/lidar.png" />

4. **Select AI Models**\
   For this demonstration, we will integrate the [Grounded SAM model](/models/segmentation/gsam) to perform image segmentation tasks.

   <img src="https://mintcdn.com/scaledfoundations-rebrand/Xi7Qx08aC4xdqlWX/assets/images/hello_grid/model_selection.png?maxW=3813&auto=format&n=Xi7Qx08aC4xdqlWX&q=85&s=c480d45e23de0b7ed9aa42ec20551694" alt="Model Selection" width="3813" height="1618" data-path="assets/images/hello_grid/model_selection.png" />

When you are ready, click the **Launch** button to start the simulation.

## Get Started with the GRID Session

Once your simulation is running, you can interact with GRID using the code interface along with the Copilot, Telemetry, and Storage tabs.

<Tip>
  All of the code featured in this section is available in a ready-to-run notebook [here](https://github.com/GenRobo/GRID-playground/blob/new/notebooks/hello_grid.ipynb).
</Tip>

### Initial Setup and Control

* **Initialize the Environment:**\
  Begin by running the boilerplate code to initialize the Airgen API and load the necessary weights for the Grounded SAM model.

  <img src="https://mintcdn.com/scaledfoundations-rebrand/Xi7Qx08aC4xdqlWX/assets/images/hello_grid/start.png?maxW=3789&auto=format&n=Xi7Qx08aC4xdqlWX&q=85&s=c582a2d6402796ee9632e6c8cb55d17e" alt="Start Code" width="3789" height="1992" data-path="assets/images/hello_grid/start.png" />

* **Control the Vehicle:**\
  Use the `setCarTargetSpeed()` API ([CarClient documentation](/simulation/airgen/reference/car-client)) to move the car forward, and then capture images using the `getImages()` API. The following code block demonstrates how to position the car and capture an image:

  <img src="https://mintcdn.com/scaledfoundations-rebrand/Xi7Qx08aC4xdqlWX/assets/images/hello_grid/capture_image.png?maxW=3790&auto=format&n=Xi7Qx08aC4xdqlWX&q=85&s=2c8e57938995ee94189dd17046c3f6bb" alt="Capture Image" width="3790" height="2001" data-path="assets/images/hello_grid/capture_image.png" />

  <Note>
    We are using the "front\_center" camera in this example because the road is directly ahead. For a list of default cameras and their positions, refer to the [Cameras](/simulation/airgen/features/camera) tab.
  </Note>

### Image Segmentation with Grounded SAM

* **Segment the Road:**\
  Apply the [Grounded SAM model](/models/segmentation/gsam) to segment the road in the image you just captured. Visualize the output using the rerun module.
  We will be running the below code to segment the road:

  ```Python
  road = seg_gsam_0.run(image[0][0], "road")
  ```

  <img src="https://mintcdn.com/scaledfoundations-rebrand/Xi7Qx08aC4xdqlWX/assets/images/hello_grid/seg_road.png?maxW=1830&auto=format&n=Xi7Qx08aC4xdqlWX&q=85&s=b06abc3f930b82584f80ce7cc4165797" alt="Segment Image" width="1830" height="915" data-path="assets/images/hello_grid/seg_road.png" />

* **Alternate Camera View:**\
  Try capturing and segmenting an image from the "back\_center" camera for a different perspective.

  The following code block captures an image from the back camera:

  ```Python
  image_new = client.getImages("back_center", [0])
  rr.log("Car", rr.Image(image_new[0][0]))
  ```

  <img src="https://mintcdn.com/scaledfoundations-rebrand/Xi7Qx08aC4xdqlWX/assets/images/hello_grid/back_cam.png?maxW=1831&auto=format&n=Xi7Qx08aC4xdqlWX&q=85&s=b4c6876b3e1b311d62afd6ed8828ba4d" alt="Original Back Camera" width="1831" height="903" data-path="assets/images/hello_grid/back_cam.png" />

  Let us now segment the image captured from the back camera:

  ```Python
  car = seg_gsam_0.run(image_new[0][0], "car")
  ```

  <img src="https://mintcdn.com/scaledfoundations-rebrand/Xi7Qx08aC4xdqlWX/assets/images/hello_grid/seg_car.png?maxW=1849&auto=format&n=Xi7Qx08aC4xdqlWX&q=85&s=1fc71bd84bf8f8109841d863ff9d56de" alt="Segmented Image" width="1849" height="912" data-path="assets/images/hello_grid/seg_car.png" />

### Navigating the Scene

* **Move to a Specific Position:**\
  Advance the car to the position **x = -120 metres** using the following code block:

  ```Python
  client.setCarTargetSpeed(5)
  while True:
      if client.simGetVehiclePose().position.x_val <= -120:
          client.setCarTargetSpeed(0)
          break
  print(client.simGetVehiclePose().position)
  ```

  <img src="https://mintcdn.com/scaledfoundations-rebrand/Xi7Qx08aC4xdqlWX/assets/images/hello_grid/move_120.gif?maxW=640&auto=format&n=Xi7Qx08aC4xdqlWX&q=85&s=d24908134c6c9a8985bde46f6488e592" alt="Move to Position" width="640" height="324" data-path="assets/images/hello_grid/move_120.gif" />

* **Perform a Right Turn:**\
  To execute a right turn, set the steering to `1.0` for a sharp turn, then move the car slowly to complete the maneuver.

  ```Python
  controls = client.getCarControls()
  controls.is_manual_gear = False
  controls.steering = 1.0
  client.setCarControls(controls)
  client.setCarTargetSpeed(2.1)
  time.sleep(3)
  client.setCarTargetSpeed(0)
  ```

  <img src="https://mintcdn.com/scaledfoundations-rebrand/Xi7Qx08aC4xdqlWX/assets/images/hello_grid/right_turn.gif?maxW=640&auto=format&n=Xi7Qx08aC4xdqlWX&q=85&s=fffe86b719fa784a8dc76d18a36ac736" alt="Right Turn" width="640" height="321" data-path="assets/images/hello_grid/right_turn.gif" />

* **Return to Straight Movement:**\
  Reset the steering to `0.0` to resume straight-line motion.

  ```Python
  controls.steering = 0.0
  client.setCarControls(controls)
  client.setCarTargetSpeed(2)
  time.sleep(3)
  client.setCarTargetSpeed(0)  
  ```

  <img src="https://mintcdn.com/scaledfoundations-rebrand/Xi7Qx08aC4xdqlWX/assets/images/hello_grid/straight.gif?maxW=640&auto=format&n=Xi7Qx08aC4xdqlWX&q=85&s=18fe91c7ce1b9225c2aa6ce9da7b673e" alt="Straight Movement" width="640" height="321" data-path="assets/images/hello_grid/straight.gif" />

### Working with LiDAR Data

* **Generate a Point Cloud:**\
  Utilize the `getLidardata()` API ([LiDAR documentation](/simulation/airgen/features/sensors#lidar)) to capture LiDAR data. The code snippet below shows how to transform the raw data into a list of points with the appropriate coordinate transformations.

  ```Python
  def get_lidar_data(client):
    lidar_data = client.getLidarData()
    if len(lidar_data.point_cloud) < 3:
        print("No points received from Lidar")
    else:
        points = np.array(lidar_data.point_cloud, dtype=np.float32)
        points = np.reshape(points, (int(points.shape[0] / 3), 3))
        points_xyz = np.zeros_like(points)
        points_xyz[:, 0] = points[:, 0]
        points_xyz[:, 1] = points[:, 1]
        points_xyz[:, 2] = -points[:, 2] + 1
        print("Got LiDAR Data")
        return points_xyz
  ```

* **Visualize the Point Cloud:**\
  With Matplotlib, scatter the LiDAR point cloud data and save the plot as an image. You can locate the session's directory via the **GRID\_USER\_SESSION\_BLOB\_DIR** variable. Below is how you're storage tab would look like after you save the LiDAR data.

  ```Python

  %matplotlib inline
  import os
  from grid import GRID_USER_SESSION_BLOB_DIR
  import numpy as np
  import matplotlib.pyplot as plt
  points = get_lidar_data(client)
  fig = plt.figure()
  ax = fig.add_subplot(projection='3d')
  for point in points:
      ax.scatter(point[0], point[1], point[2])
  ax.set_xlabel('X Label')
  ax.set_ylabel('Y Label')
  ax.set_zlabel('Z Label')
  save_path = os.path.join(GRID_USER_SESSION_BLOB_DIR, "pcl.png")
  plt.savefig(save_path, bbox_inches='tight')
  print("Saved!")
  ```

  <img src="https://mintcdn.com/scaledfoundations-rebrand/Xi7Qx08aC4xdqlWX/assets/images/hello_grid/storage.png?maxW=1828&auto=format&n=Xi7Qx08aC4xdqlWX&q=85&s=ca594d916456cc584ecead86813f3889" alt="Storage Drive" width="1828" height="898" data-path="assets/images/hello_grid/storage.png" />

  The saved point cloud is as follows:

  <img src="https://mintcdn.com/scaledfoundations-rebrand/Xi7Qx08aC4xdqlWX/assets/images/hello_grid/pcl.png?maxW=434&auto=format&n=Xi7Qx08aC4xdqlWX&q=85&s=d9a56fc97641c31e2dcf389ce07bd99c" alt="Visualize Point Cloud" width="434" height="398" data-path="assets/images/hello_grid/pcl.png" />
