A PyTorch-based framework for training autonomous drifting policies for vehicles using reinforcement learning. This framework provides high-performance vectorized environments for drift control tasks and several baseline implementations.
Installation | Quick Start | Environments | Training | Technical Details | Code Structure
- High-Performance Training - GPU-accelerated parallel simulation reduces training time from hours to minutes through vectorized environment implementations
- Advanced Vehicle Dynamics - Individual Wheel Drive (IWD) model enabling independent control of each wheel's speed and torque for superior maneuverability
- Domain Randomization - Systematic randomization of vehicle dynamics and environmental parameters to bridge the simulation-to-reality gap
- Diverse Drift Tasks - Comprehensive suite of drift scenarios from basic circular paths to challenging variable-curvature tracks and figure-eight patterns
- Real-time Control - Computationally efficient implementation suitable for embedded systems and real-world deployment
- Comprehensive Analysis - Extensive tools for visualizing and analyzing drift control performance across different trajectory patterns
- NVIDIA GPU with CUDA support (Tested on NVIDIA RTX 3080, reduce parallel instances for lower-end GPUs)
- CUDA 11.8 or later
- PyTorch 2.0.1 or later
Create a conda environment using the provided environment.yml file:
conda env create -f environment.ymlThis project uses Git submodules. After cloning the repository, initialize and update the submodules:
git submodule init
git submodule updateThe following submodule is included:
- rl_games: Reinforcement learning algorithm library (forked from [Denys88/rl_games])
- Path:
rl_games - URL: https://github.com/yiwenlu66/rl_games.git
- Path:
To run a basic training experiment with default settings:
cd experiments/continuous_drift_iwd
./run.shThis will train a policy for the consecutive drifting task. Results will be saved in the data directory.
We currently support several drift control environments:
| Environment | Description | Code |
|---|---|---|
| Fixed Circle | Drift in a circular trajectory | fixed_circle_iwd.py |
| Eight-Shaped | Drift in a figure-eight pattern | eight_drift_iwd.py |
| State Tracker | Track reference states for drifting | state_tracker_iwd.py |
| Continuous Drift | Follow continuous drift trajectories | continuous_drift_iwd.py |
Training scripts are provided for each environment in the experiments directory. Key parameters can be configured through command line arguments:
python run.py train fixed_circle_iwd \
--car-preset xcar \
--device cuda:0 \
--num-parallel 100000 \
--epochs 500Common training flags:
--car-preset: Vehicle configuration (xcar, racecar, sensorcar)--device: Computing device--num-parallel: Number of parallel environments--epochs: Number of training epochs--disturbed: Enable disturbance modeling--randomize-tyre: Randomize tire parameters
The project consists of several key components:
-
Core Simulation Platform (
xcar-simulation):- The PyTorch-based simulation is implemented in
gpu_vectorized_car_env.py IWDCarDynamics: A PyTorch module for individual wheel drive vehicle dynamics that takes current states, control inputu, and model parameterspas inputs and outputs state derivativesGPUVectorizedCarEnv: A parallelized Gym environment where parameternindicates the number of parallel instances;obs,reward, anddoneare PyTorch tensors with shape(n, ?)- The
stepfunction utilizesIWDCarDynamicsand PyTorch's differential equation solver torchdiffeq to compute the next state - Base implementation provides raw state as
obs, zeroreward, andFalsefordone, which can be overridden by specific tasks
- The PyTorch-based simulation is implemented in
-
Environment Implementation (
envs):- Task-specific environments inherit from
GPUVectorizedCarEnv - Override
obsandrewardinterfaces based on task requirements
- Task-specific environments inherit from
-
Additional Components:
rl_games: Reinforcement learning algorithm library (included as a submodule)utils: Interface adaptation coderun.py: Entry point for training and testing
├── envs/ # Environment implementations
│ ├── fixed_circle_iwd.py # Fixed circle drifting
│ ├── eight_drift_iwd.py # Eight-shaped drifting
│ ├── continuous_drift_iwd.py # Continuous drifting
│ └── state_tracker_iwd.py # State tracking for drifting
├── experiments/ # Training scripts and visualizations
│ ├── fixed_circle_iwd/
│ ├── eight_drift_iwd/
│ ├── continuous_drift_iwd/
│ └── state_tracker_iwd/
├── rl_games/ # RL algorithm library (submodule)
├── utils/ # Utility functions
│ ├── generate_segment_racetrack.py
│ └── rlgame_utils.py
├── xcar-simulation/ # Core simulation components
│ ├── gpu_vectorized_car_env.py
│ ├── IWDCarDynamics.py
│ └── presets.yaml
└── run.py # Main training script
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.