> ## Documentation Index
> Fetch the complete documentation index at: https://docs.generalrobotics.dev/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/bbcuyC2Oq40Jh0Ty/assets/images/hello_grid/robot.png?fit=max&auto=format&n=bbcuyC2Oq40Jh0Ty&q=85&s=59c56916ebfefaa476dc28583fa465f8" 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/bbcuyC2Oq40Jh0Ty/assets/images/hello_grid/scene.png?fit=max&auto=format&n=bbcuyC2Oq40Jh0Ty&q=85&s=8655d39369321e97dd4c1a9e0f08971a" 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/bbcuyC2Oq40Jh0Ty/assets/images/hello_grid/sensor.png?fit=max&auto=format&n=bbcuyC2Oq40Jh0Ty&q=85&s=8e8c2759de83adffee988318196e4a44" 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/bbcuyC2Oq40Jh0Ty/assets/images/hello_grid/lidar.png?fit=max&auto=format&n=bbcuyC2Oq40Jh0Ty&q=85&s=49abf87fe0ec3e8953e279fa7d7bdf79" 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/bbcuyC2Oq40Jh0Ty/assets/images/hello_grid/model_selection.png?fit=max&auto=format&n=bbcuyC2Oq40Jh0Ty&q=85&s=f6dc77300fdafc3e2594fb4fcd3afd3e" 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/bbcuyC2Oq40Jh0Ty/assets/images/hello_grid/start.png?fit=max&auto=format&n=bbcuyC2Oq40Jh0Ty&q=85&s=82e08142e990bc7ef2d74fd97dd2617a" 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/bbcuyC2Oq40Jh0Ty/assets/images/hello_grid/capture_image.png?fit=max&auto=format&n=bbcuyC2Oq40Jh0Ty&q=85&s=062d1563e74bb1cb6cd778c7beeadd91" 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 theme={null}
  road = seg_gsam_0.run(image[0][0], "road")
  ```

  <img src="https://mintcdn.com/scaledfoundations/bbcuyC2Oq40Jh0Ty/assets/images/hello_grid/seg_road.png?fit=max&auto=format&n=bbcuyC2Oq40Jh0Ty&q=85&s=0daabaad37aece0142e7c6be4cc19a74" 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 theme={null}
  image_new = client.getImages("back_center", [0])
  rr.log("Car", rr.Image(image_new[0][0]))
  ```

  <img src="https://mintcdn.com/scaledfoundations/bbcuyC2Oq40Jh0Ty/assets/images/hello_grid/back_cam.png?fit=max&auto=format&n=bbcuyC2Oq40Jh0Ty&q=85&s=3141cb2409cfe8b888f66a12ef24b841" 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 theme={null}
  car = seg_gsam_0.run(image_new[0][0], "car")
  ```

  <img src="https://mintcdn.com/scaledfoundations/bbcuyC2Oq40Jh0Ty/assets/images/hello_grid/seg_car.png?fit=max&auto=format&n=bbcuyC2Oq40Jh0Ty&q=85&s=d71cae128447692b79c1df2fcfec6e2a" 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 theme={null}
  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/bbcuyC2Oq40Jh0Ty/assets/images/hello_grid/move_120.gif?s=9b18ea8db1be2d1798d2bd061a509ea0" 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 theme={null}
  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/bbcuyC2Oq40Jh0Ty/assets/images/hello_grid/right_turn.gif?s=c7c569420109e126bf098117961dad7b" 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 theme={null}
  controls.steering = 0.0
  client.setCarControls(controls)
  client.setCarTargetSpeed(2)
  time.sleep(3)
  client.setCarTargetSpeed(0)  
  ```

  <img src="https://mintcdn.com/scaledfoundations/bbcuyC2Oq40Jh0Ty/assets/images/hello_grid/straight.gif?s=db1dcea0c0f9e47be17eca20e90d146d" 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 theme={null}
  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 theme={null}

  %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/bbcuyC2Oq40Jh0Ty/assets/images/hello_grid/storage.png?fit=max&auto=format&n=bbcuyC2Oq40Jh0Ty&q=85&s=e63b09b7a033dbc404a83d99fcb25c1e" 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/bbcuyC2Oq40Jh0Ty/assets/images/hello_grid/pcl.png?fit=max&auto=format&n=bbcuyC2Oq40Jh0Ty&q=85&s=222be09dd6e2a42e8e0fb9c698433519" alt="Visualize Point Cloud" width="434" height="398" data-path="assets/images/hello_grid/pcl.png" />
