A C++ simulation of obstacle avoidance logic for an unmanned ground vehicle (UGV). The vehicle uses three simulated distance sensors (front, left, right) to detect obstacles and navigate around them autonomously.
Built to explore autonomous navigation concepts relevant to embedded and unmanned systems development.
The simulation runs a UGV through an obstacle course over a configurable number of timesteps. Each step:
- Three distance sensors take readings (with configurable Gaussian noise)
- The navigation controller decides what to do: continue, turn left, turn right, or stop
- The vehicle state machine updates position and heading accordingly
- All events are logged to a CSV file for analysis
The decision logic picks the turn direction with the most clearance when an obstacle is detected ahead. If all sides are blocked, the vehicle stops.
src/
sensor.h / sensor.cpp - distance sensor simulation with noise
vehicle.h / vehicle.cpp - vehicle state machine (IDLE/MOVING/TURNING/STOPPED)
navigation.h / navigation.cpp - obstacle avoidance decision logic
logger.h / logger.cpp - CSV event logger
main.cpp - simulation entry point
tests/
test_sensor.cpp
test_vehicle.cpp
test_navigation.cpp
CMakeLists.txt
Requirements: CMake 3.14+, a C++17 compiler (GCC or Clang)
mkdir build && cd build
cmake ..
make
./ugv_sim # run with default 200 steps
./ugv_sim 500 # run with custom step countThe simulation prints a timestamped log to the console and saves a full CSV report to logs/simulation.csv.
Tests use GoogleTest. Install it first:
# macOS
brew install googletest
# Ubuntu/Debian
sudo apt install libgtest-devThen build and run:
mkdir build && cd build
cmake ..
make
ctest --output-on-failure=== UGV Obstacle Avoidance Simulation ===
Steps: 200 | Timestep: 100ms
t= 2800ms | TURN_RIGHT | F: 30.2cm L: 299.8cm R: 301.1cm | State=TURNING ...
t= 8000ms | TURN_LEFT | F: 19.8cm L: 24.3cm R: 300.5cm | State=TURNING ...
t= 19900ms | STOP | F: 15.1cm L: 17.9cm R: 18.2cm | State=STOPPED ...
=== Simulation Complete ===
Total steps: 200
Obstacles avoided: 3
Final position: (142.3, 891.7) cm
Writing this forced me to understand state machines properly — the vehicle cannot just "decide" to turn, it has to transition through states with defined rules. I also got a much better feel for why sensor noise matters in real systems: even 2cm of noise can change whether an obstacle is detected or not, which affects navigation decisions significantly.
The CSV logging was useful for visualizing what the vehicle actually did step-by-step, which is how I debugged the turn completion logic.
- Siegwart, Nourbakhsh, Scaramuzza — Introduction to Autonomous Mobile Robots (MIT Press)
- Fraunhofer IEM Advanced Mobility Systems research
- HC-SR04 ultrasonic sensor datasheet (for realistic noise modeling)