# Tutorial 2

## Setting up the Simulation Environment

In this tutorial, we'll walk through the process of setting up the simulation environment for AVC - F1Tenth. Additionally, we'll cover how to visualize the simulation with Foxglove Studio.

### Prerequisites

- Install Foxglove Studio from <a href="https://foxglove.dev/download" target="_blank">https://foxglove.dev/download</a>, or you can use the web version at <a href="https://studio.foxglove.dev/" target="_blank">https://studio.foxglove.dev/</a>.
- You must have completed the [setup tutorial](/f1tenth/setup.md).

---

## Step 1: Build the Simulation Package

Change directories into your workspace root (`ros2_ws`) and execute the following.

```bash
colcon build
source install/local_setup.bash
```

> Running `colcon build` without `--packages-select` builds every package.

---

## Step 2: Launch the Simulation

Next, you can launch the simulation:

```bash
ros2 launch f1tenth_gym_ros gym_bridge_launch.py
```

Wait for the command to execute and for the simulation to start. You should see output in your terminal indicating that the simulation has started successfully.

!!!
In this tab, you can use `CTRL + C` at any point to stop the simulation. Use the up arrow to start it up again.
!!!

---

## Step 3: Launching the Foxglove Bridge

Once the simulation is running, the next step is to launch the Foxglove Bridge. This bridge facilitates communication between the simulation and Foxglove Studio, enabling you to visualize the simulation. Execute the following command in a split pane (`Ctrl+b, "`) to launch the Foxglove Bridge:

```bash
ros2 launch foxglove_bridge foxglove_bridge_launch.xml
```

At this point, you should have three terminals for the simulation. It should look something like this:
![](/static/uploads/tutorial-2/step4.png)

---

## Step 4: Visualizing the Simulation with Foxglove Studio

Open Foxglove Studio (either the installed version or the web version at <a href="https://studio.foxglove.dev/" target="_blank">https://studio.foxglove.dev/</a>).

Then, open a new remote connection to `ws://localhost:8765`.

Inside Foxglove Studio:

- Click on the `3D` panel (in the middle), click panel in the top left.
- Under `Topics`, enable visibility for all topics by clicking the eye icons.
  ![](/static/uploads/tutorial-2/step5.png)
- Disable the labels by going to `Transforms > Settings > Labels (Off)`.
- Under the `/scan` topic dropdown:
  - increase the radius to 15
  - change `Color by (intensity)` to `range`
  - set `Color map (gradient)` to `Rainbow`
- Click on `Custom Layers > ⋮ (three dots) > Add URDF`.
  - Select `Topic` as the source and enter `/ego_robot_description` in the topic input. The car should appear.
- Mess around with the settings to get an environment you like.

---

## Step 5: Install Ackermann Messages

In another pane (`Ctrl+b, %`) or window (`Ctrl+b, C` and `Ctrl+b, {0-9}` to switch windows), navigate to your workspace's source directory:

```bash
cd /ros2_ws/src
```

Clone the Ackermann Messages Repository:

```bash
git clone -b ros2 https://github.com/ros-drivers/ackermann_msgs.git
rm -rf ackermann_msgs/.git
```

Navigate back to your workspace root (`/ros2_ws/`) and build:

```bash
cd /ros2_ws/
colcon build
```

!!!warning
_<small>If you get a build error about something not linking, delete `build/` and try again.</small>_
!!!

Source the workspace:

```bash
source install/local_setup.bash
```

!!!secondary
**Tip:** if at any point a package can't be found after you build it, verify that your sourced the workspace!
!!!

To use AckermannDriveStamped in Foxglove Studio (virtual controller, etc), stop the Foxglove bridge from step 2, build your workspace, source it, then run Foxglove bridge again.

---

## Step 6: Making the Car Move

You can command the car to move by publishing a `AckermannDriveStamped` message to `/drive`. Here is a command (which you can run in your terminal) that will make the car move forward:

```bash
ros2 topic pub /drive ackermann_msgs/msg/AckermannDriveStamped "drive: {steering_angle: 0.5, speed: 1.0}"
```

> Feel free to mess around with the `steering_angle` and `speed`.

!!!info
A better alternative for manually controlling the car is an extension called `virtual-joystic` by Yulong. You can add it by finding `Settings` > `Extensions` > `virtual-joystick` then adding a panel for it.
!!!

---

## Step 7: Resetting the Car Position

You can reset the position of the car in the simulation by publishing a pose estimate to `/initialpose` from inside Foxglove Studio.
![](/static/uploads/tutorial-2/step8.png)

---

## Step 8: Simulation settings

Explore the F1Tenth simulation's settings such as different maps and multiple vehicles under `sim/config/sim.yaml`. Remember to `colcon build` and source the sim environment (`source install/local_setup.bash`) to apply any modifications. Restarting the Docker also applies changes.

---

And there you have it! You've successfully set up and visualized the simulation environment for AVC - F1Tenth. If you encounter any issues, don't hesitate to refer back to this guide or reach out for further assistance. Happy simulating!
