Skip to content

A 2D Monte Carlo Localization (MCL) simulator with a ray-casting LIDAR, written in C++17 and SFML. It demonstrates probabilistic robot localization using particles, sensor models, and resampling. Designed as a learning tool for robotics and C++ simulation projects.

License

Notifications You must be signed in to change notification settings

kacper673/Monte-Carlo-Localization

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MCL & RayCasting – 2D Robot Localization Simulator (C++/SFML)

A lightweight simulator of Monte Carlo Localization (MCL) with LIDAR-style ray casting in a 2D world.
Built in modern C++ and SFML, it’s designed for experimentation, teaching, and rapid prototyping of probabilistic robotics.

Core idea: given noisy odometry and range measurements, maintain a cloud of weighted particles to estimate robot pose in a known map.
I simulate the range sensor via GPU-friendly ray casting.


Features

  • Particle Filter (MCL) with motion + measurement update and resampling

  • Fast ray casting against line-segment obstacles (LIDAR emulation)

  • Interactive visualization in SFML: robot, particles, rays, obstacles

  • Deterministic and stochastic runs (seed control)

  • Configurable FOV, range, particle count, step sizes

  • Extensible modular code: Environment, Robot, Particle, main loop

  • Additional Python scripts to make log charts

Methodology section below screenshots and description


Animations and screenchots

Figure 1 - Robot being efectivly localized after every move. (2500 particles, 360 deg fov, 90 lidar beams, robot lidar rays viusalisation on and off)

MCL_1


Figure 2 - using rescue() funcion to apply some randomness to already existing particles (12000 particles, 360 deg fov, 90 lidar beams)

ezgif com-optimize


Figure 3 - raycasting used in robot lidar, rays detect nearest obstacle and return the distance, simulating real life lidar (2500 particles, 360 deg fov, 120 lidar beams)

MC2025-09-1720-27-13-ezgif com-optimize


Figure 4 and 5 - effective sample size over epochs charts, caunting EffN for each epoch, saving the result to csv and plotting in Matplotlib. We can notice how robot movement and manual particle resamplment influences EffN with high values depicting low accuracy and low values showing accurate results

EffN_over_epochs EffN_over_epochs_2

Figure 6 - EffN histogram, showing distribution of EffN over epochs

EffN_hist

Description

This project is a 2D simulation of the Monte Carlo Localization (MCL) algorithm, a cornerstone of probabilistic robotics, used to estimate a robot’s position in a known map.
It is written in C++17 and uses the SFML 2.6 graphics library for visualization.


Simulation Overview

  • The environment is randomly populated with rectangular obstacles.
  • A robot is placed in the environment and can be controlled interactively:
    • W / S → move forward / backward
    • A / D → rotate left / right

The robot is equipped with a configurable LIDAR sensor:

  • Parameters: field of view (fov) and number of beams (beams_num)
  • Implemented with ray casting using the AABB slab method
  • Each ray computes the minimum intersection distance to obstacles
  • Visualized as semi-transparent rays in the simulator window

To prevent continuous sampling, the LIDAR:

  • Only acquires data after the robot moves and then remains idle for at least 2 seconds
  • Saves measurement data into a CSV file for offline analysis

Monte Carlo Localization Workflow

  1. Initialization
    A set of particles is spawned randomly across the map.

  2. Prediction
    Particles update their poses according to robot motion (forward/backward steps and rotations).

    • Planned: extend with Gaussian noise for more realistic motion models.
  3. Correction (Measurement Update)
    Each particle simulates its own LIDAR scan via ray casting.
    Particle weights are computed based on the difference between simulated and actual robot measurements.

  4. Resampling
    Particles are resampled probabilistically - higher weights increase survival probability.
    Current method: multinomial resampling
    Planned: low-variance/systematic resampling for better efficiency.

  5. Recovery / Rescue

    • A fraction of particles are always randomized to avoid particle deprivation.
    • A rescue operator can reinitialize part of the colony (useful in the kidnapped robot problem).

Data Analysis

  • After each iteration, the simulator computes the Effective Sample Size (EffN): EffN = 1 / Σ ( w_j² ) where w_j are the normalized particle weights

  • EffN measures how well the particle population approximates the true posterior.

  • Logged to CSV and later processed in Python with Matplotlib to generate performance charts.



Planned Extensions

  • Gaussian motion noise for realistic odometry models.
  • Improved likelihood model (Gaussian error instead of absolute difference).
  • Low-variance/systematic resampling for better stability.
  • Config file for easy parameter tuning.

This simulator serves as both a learning tool and a playground for robotics algorithms, helping visualize how particle filters converge to the true robot pose in dynamic environments.

Methodology

image Zrzut ekranu 2025-09-17 155934

About

A 2D Monte Carlo Localization (MCL) simulator with a ray-casting LIDAR, written in C++17 and SFML. It demonstrates probabilistic robot localization using particles, sensor models, and resampling. Designed as a learning tool for robotics and C++ simulation projects.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published