---
title: 'F1TENTH Gym: Autonomous Racing Simulator'
url: https://www.emergentmind.com/topics/f1tenth-gym
type: topic
---

# F1TENTH Gym: Autonomous Racing Simulator

F1TENTH Gym is a modular, OpenAI Gym-compatible simulation environment designed for research in autonomous racing, specifically tailored to the F1TENTH autonomous vehicle platform. It enables the study of high-speed perception, planning, and control algorithms in a reproducible, scalable, and simulator-agnostic manner. The environment provides interfaces for both non-ROS and ROS-based workflows, supports rigorous simulation-to-reality (Sim2Real) strategies, exposes a kinematic bicycle model for agent training, and includes evaluation protocols aligned with the requirements of competitive and academic autonomous racing research [2506.15899, 2103.08396].

## 1. Simulation Environment and API

F1TENTH Gym is distributed as “f1tenth_gym” (Python/NumPy backend, no ROS required) and “f1tenth_gym_ros” (ROS-wrapped, containerized). The API follows standard Gym conventions:
- **Observation space:** A continuous Box vector comprising LiDAR scan (N beams), ego pose $[x, y, \theta]$, velocity $v$, and optionally distance/bearing to next waypoint. Example: $\text{obs} \in \mathbb{R}^{N+4}$.
- **Action space:** Continuous vector $[a_v, \delta]$, where $a_v$ is longitudinal acceleration (throttle/brake) and $\delta$ is steering command in radians.
- **Episode logic:** Reset provides initial observation; step takes action, returns $(\text{obs}_{t+1}, r_t, \text{done}, \text{info})$. Episodes terminate on collision, off-track, reaching max steps, or lap completion.
- **ROS integration:** Topics `/scan`, `/cmd_vel`, `/odom` for sensor/command exchange; episode metrics reported via `/gym/info`.

These design choices facilitate rapid prototyping and seamless transfer from simulation to the physical F1TENTH platform [2506.15899, 2103.08396].

## 2. Vehicle Dynamics Model

The simulator is grounded in a discrete-time kinematic bicycle model:
\[
\dot x = v \cos(\theta), \quad \dot y = v \sin(\theta), \quad \dot \theta = \frac{v}{L}\tan(\delta), \quad \dot v = a_v
\]
where $(x, y)$ denote world-frame position, $\theta$ heading, $L$ wheelbase, $v$ longitudinal speed, $\delta$ steering angle, and $a_v$ longitudinal acceleration. An optional first-order lag on steering actuation is supported:
\[
\tau_\delta \dot\delta = -\delta + \delta_{\rm cmd}
\]
This abstraction aligns with widely-adopted mathematical and software conventions for both model-based and RL-based autonomous vehicle research [2506.15899, 2103.08396].

## 3. Reward Formulations

Reward design follows composite schemes standard in autonomous racing:
\[
r_t = w_p \Delta s_t - w_e |e_{ct}| - w_{\rm col} \mathbb{I}_{\rm collision} - w_{\rm off} \mathbb{I}_{\rm offtrack}
\]
with $\Delta s_t$ as forward path progress, $e_{ct}$ cross-track error, $\mathbb{I}_{\rm collision}$ and $\mathbb{I}_{\rm offtrack}$ as indicator penalties, and $w$ coefficients for weighting. Variants employ squared cross-track penalties or exponential collision penalties for sharper signal shaping:
\[
r_t = w_p \Delta s_t - w_e e_{ct}^2 - w_{\rm col}\,e^{k_{\rm col} \Delta t_{\rm col}} - w_{\rm off}|e_{ct}|
\]
Reward terms are compatible with both RL and optimal control approaches. Notably, progress-based reward is essential to avoid stationary “drifting” behaviors, while off-track/collision penalties rapidly guide agents towards viable policies [2506.15899, 2103.08396].

## 4. Simulation-to-Reality Strategies

F1TENTH Gym incorporates several Sim2Real mechanisms to enhance policy robustness and transferability:
- **Domain Randomization:** Vehicle mass, tire friction coefficient $\mu$, steering latency, sensor pose, and visual textures can be perturbed per episode.
- **Sensor Noise:** Additive Gaussian noise on LiDAR (i.i.d. $r_i' = r_i + \epsilon_i,\, \epsilon_i \sim \mathcal{N}(0, \sigma_L^2)$), odometry bias/drift, IMU noise.
- **Iterative Reality Adaptation:** Datasets of real trajectories enable domain adaptation of perception modules and policy fine-tuning after simulation pre-training.

This integration enables the F1TENTH Gym to serve as a reproducible bridge between theoretical RL/control development and deployment on physical robotic platforms [2506.15899].

## 5. Benchmarks, Evaluation Protocols, and Metrics

Evaluation adheres to methodological rigor through:
- **Metrics:** Lap time $T_{\rm lap}$, completion fraction $C$, collision count $N_{\rm col}$, minimum LiDAR range $d_{\min}$, and average cross-track error $\bar{e}_{ct}$.
- **Protocols:** Run $N \geq 10$ episodes per policy, report mean $\pm$ standard deviation for all metrics, and compare against canonical controllers (e.g., Pure Pursuit, Stanley, PID).
- **Reproducibility:** The `info` dictionary from each episode provides all primary quantitative results.

Empirical evaluations often mirror those adopted in international F1TENTH competitions and comparative RL/control studies, ensuring methodological alignment across research groups [2506.15899, 2103.08396].

## 6. Policy-Gradient RL Integration and Transfer

Policy-gradient RL serves as a typical learning paradigm in F1TENTH Gym, with neural policy architectures parameterized as Gaussians:
\[
\pi_\theta(a|s) = \mathcal{N}(a; \mu(s;\theta), \sigma^2(s;\theta)I)
\]
where input $s$ consists of $[x, y, \psi, v]$ or richer state/observation vectors. Actor–Critic, baseline subtraction, and batch-based or episodic optimization are common. Network outputs are scaled to vehicle actuation ranges; e.g., $[-1,+1] \rightarrow [-\phi_{\max},+\phi_{\max}]$ for steering [2103.08396]. 

Empirical findings indicate that:
- Actor–Critic methods outperform pure Monte Carlo PG by reducing learning variance.
- Direct zero-shot policy transfer from related domains (e.g., CartPole–F1TENTH mapping: $\delta = (\theta_p / \theta_{\max}) \phi_{\max}$) is ineffective; target-domain fine-tuning is required.
- Policy robustness is enhanced by adding small observation/action noise and enforcing early penalties on off-track infractions.

## 7. Example Usage and Practical Recommendations

A canonical usage protocol proceeds as follows:
```python
import gym, numpy as np
env = gym.make('f1tenth_gym:race-v0', map='simple_track', lidar_bins=108, max_steps=500, render_mode='human')
obs = env.reset()
for step in range(env.max_steps):
    action = np.array([0.2, 0.0], dtype=np.float32)  # Throttle, steering
    obs, reward, done, info = env.step(action)
    if done:
        break
metrics = {'lap_time': info.get('lap_time'), ...}
env.close()
```
With ROS extension, episodes are managed via launch files and metrics are retrieved from `/gym/info`. Practical advice includes clamping action outputs, using forward progress as a reward shaping term, initializing critic networks prior to policy updates, and carefully transferring policies between domains (e.g., CartPole to F1TENTH) with appropriate mapping and subsequent adaptation [2506.15899, 2103.08396].

Source: https://www.emergentmind.com/topics/f1tenth-gym